Skip to content

Commit ff98b14

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Clean up local variable usage - git functions"
2 parents 5d21e0b + 50cda69 commit ff98b14

1 file changed

Lines changed: 42 additions & 43 deletions

File tree

functions-common

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ function is_ubuntu {
500500
# ``get_release_name_from_branch branch-name``
501501
function get_release_name_from_branch {
502502
local branch=$1
503+
503504
if [[ $branch =~ "stable/" ]]; then
504505
echo ${branch#*/}
505506
else
@@ -510,72 +511,73 @@ function get_release_name_from_branch {
510511
# git clone only if directory doesn't exist already. Since ``DEST`` might not
511512
# be owned by the installation user, we create the directory and change the
512513
# ownership to the proper user.
513-
# Set global RECLONE=yes to simulate a clone when dest-dir exists
514-
# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
514+
# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
515+
# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
515516
# does not exist (default is False, meaning the repo will be cloned).
516-
# Uses global ``OFFLINE``
517+
# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
517518
# git_clone remote dest-dir branch
518519
function git_clone {
519-
GIT_REMOTE=$1
520-
GIT_DEST=$2
521-
GIT_REF=$3
520+
local git_remote=$1
521+
local git_dest=$2
522+
local git_ref=$3
523+
local orig_dir=$(pwd)
524+
522525
RECLONE=$(trueorfalse False $RECLONE)
523-
local orig_dir=`pwd`
524526

525527
if [[ "$OFFLINE" = "True" ]]; then
526528
echo "Running in offline mode, clones already exist"
527529
# print out the results so we know what change was used in the logs
528-
cd $GIT_DEST
530+
cd $git_dest
529531
git show --oneline | head -1
530532
cd $orig_dir
531533
return
532534
fi
533535

534-
if echo $GIT_REF | egrep -q "^refs"; then
536+
if echo $git_ref | egrep -q "^refs"; then
535537
# If our branch name is a gerrit style refs/changes/...
536-
if [[ ! -d $GIT_DEST ]]; then
538+
if [[ ! -d $git_dest ]]; then
537539
[[ "$ERROR_ON_CLONE" = "True" ]] && \
538540
die $LINENO "Cloning not allowed in this configuration"
539-
git_timed clone $GIT_REMOTE $GIT_DEST
541+
git_timed clone $git_remote $git_dest
540542
fi
541-
cd $GIT_DEST
542-
git_timed fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
543+
cd $git_dest
544+
git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
543545
else
544546
# do a full clone only if the directory doesn't exist
545-
if [[ ! -d $GIT_DEST ]]; then
547+
if [[ ! -d $git_dest ]]; then
546548
[[ "$ERROR_ON_CLONE" = "True" ]] && \
547549
die $LINENO "Cloning not allowed in this configuration"
548-
git_timed clone $GIT_REMOTE $GIT_DEST
549-
cd $GIT_DEST
550+
git_timed clone $git_remote $git_dest
551+
cd $git_dest
550552
# This checkout syntax works for both branches and tags
551-
git checkout $GIT_REF
553+
git checkout $git_ref
552554
elif [[ "$RECLONE" = "True" ]]; then
553555
# if it does exist then simulate what clone does if asked to RECLONE
554-
cd $GIT_DEST
556+
cd $git_dest
555557
# set the url to pull from and fetch
556-
git remote set-url origin $GIT_REMOTE
558+
git remote set-url origin $git_remote
557559
git_timed fetch origin
558560
# remove the existing ignored files (like pyc) as they cause breakage
559561
# (due to the py files having older timestamps than our pyc, so python
560562
# thinks the pyc files are correct using them)
561-
find $GIT_DEST -name '*.pyc' -delete
562-
563-
# handle GIT_REF accordingly to type (tag, branch)
564-
if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
565-
git_update_tag $GIT_REF
566-
elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
567-
git_update_branch $GIT_REF
568-
elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
569-
git_update_remote_branch $GIT_REF
563+
find $git_dest -name '*.pyc' -delete
564+
565+
# handle git_ref accordingly to type (tag, branch)
566+
if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
567+
git_update_tag $git_ref
568+
elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
569+
git_update_branch $git_ref
570+
elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
571+
git_update_remote_branch $git_ref
570572
else
571-
die $LINENO "$GIT_REF is neither branch nor tag"
573+
die $LINENO "$git_ref is neither branch nor tag"
572574
fi
573575

574576
fi
575577
fi
576578

577579
# print out the results so we know what change was used in the logs
578-
cd $GIT_DEST
580+
cd $git_dest
579581
git show --oneline | head -1
580582
cd $orig_dir
581583
}
@@ -614,35 +616,32 @@ function git_timed {
614616
# git update using reference as a branch.
615617
# git_update_branch ref
616618
function git_update_branch {
619+
local git_branch=$1
617620

618-
GIT_BRANCH=$1
619-
620-
git checkout -f origin/$GIT_BRANCH
621+
git checkout -f origin/$git_branch
621622
# a local branch might not exist
622-
git branch -D $GIT_BRANCH || true
623-
git checkout -b $GIT_BRANCH
623+
git branch -D $git_branch || true
624+
git checkout -b $git_branch
624625
}
625626

626627
# git update using reference as a branch.
627628
# git_update_remote_branch ref
628629
function git_update_remote_branch {
630+
local git_branch=$1
629631

630-
GIT_BRANCH=$1
631-
632-
git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
632+
git checkout -b $git_branch -t origin/$git_branch
633633
}
634634

635635
# git update using reference as a tag. Be careful editing source at that repo
636636
# as working copy will be in a detached mode
637637
# git_update_tag ref
638638
function git_update_tag {
639+
local git_tag=$1
639640

640-
GIT_TAG=$1
641-
642-
git tag -d $GIT_TAG
641+
git tag -d $git_tag
643642
# fetching given tag only
644-
git_timed fetch origin tag $GIT_TAG
645-
git checkout -f $GIT_TAG
643+
git_timed fetch origin tag $git_tag
644+
git checkout -f $git_tag
646645
}
647646

648647

0 commit comments

Comments
 (0)