Cleans up the git-related functions in functions-common
Change-Id: I5f1851c0473e92c61b1e8af60e7ef32c3019f719
... | ... |
@@ -500,6 +500,7 @@ function is_ubuntu { |
500 | 500 |
# ``get_release_name_from_branch branch-name`` |
501 | 501 |
function get_release_name_from_branch { |
502 | 502 |
local branch=$1 |
503 |
+ |
|
503 | 504 |
if [[ $branch =~ "stable/" ]]; then |
504 | 505 |
echo ${branch#*/} |
505 | 506 |
else |
... | ... |
@@ -510,72 +511,73 @@ function get_release_name_from_branch { |
510 | 510 |
# git clone only if directory doesn't exist already. Since ``DEST`` might not |
511 | 511 |
# be owned by the installation user, we create the directory and change the |
512 | 512 |
# 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 |
|
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 |
|
515 | 515 |
# does not exist (default is False, meaning the repo will be cloned). |
516 |
-# Uses global ``OFFLINE`` |
|
516 |
+# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE`` |
|
517 | 517 |
# git_clone remote dest-dir branch |
518 | 518 |
function git_clone { |
519 |
- GIT_REMOTE=$1 |
|
520 |
- GIT_DEST=$2 |
|
521 |
- GIT_REF=$3 |
|
519 |
+ local git_remote=$1 |
|
520 |
+ local git_dest=$2 |
|
521 |
+ local git_ref=$3 |
|
522 |
+ local orig_dir=$(pwd) |
|
523 |
+ |
|
522 | 524 |
RECLONE=$(trueorfalse False $RECLONE) |
523 |
- local orig_dir=`pwd` |
|
524 | 525 |
|
525 | 526 |
if [[ "$OFFLINE" = "True" ]]; then |
526 | 527 |
echo "Running in offline mode, clones already exist" |
527 | 528 |
# print out the results so we know what change was used in the logs |
528 |
- cd $GIT_DEST |
|
529 |
+ cd $git_dest |
|
529 | 530 |
git show --oneline | head -1 |
530 | 531 |
cd $orig_dir |
531 | 532 |
return |
532 | 533 |
fi |
533 | 534 |
|
534 |
- if echo $GIT_REF | egrep -q "^refs"; then |
|
535 |
+ if echo $git_ref | egrep -q "^refs"; then |
|
535 | 536 |
# If our branch name is a gerrit style refs/changes/... |
536 |
- if [[ ! -d $GIT_DEST ]]; then |
|
537 |
+ if [[ ! -d $git_dest ]]; then |
|
537 | 538 |
[[ "$ERROR_ON_CLONE" = "True" ]] && \ |
538 | 539 |
die $LINENO "Cloning not allowed in this configuration" |
539 |
- git_timed clone $GIT_REMOTE $GIT_DEST |
|
540 |
+ git_timed clone $git_remote $git_dest |
|
540 | 541 |
fi |
541 |
- cd $GIT_DEST |
|
542 |
- git_timed fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD |
|
542 |
+ cd $git_dest |
|
543 |
+ git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD |
|
543 | 544 |
else |
544 | 545 |
# do a full clone only if the directory doesn't exist |
545 |
- if [[ ! -d $GIT_DEST ]]; then |
|
546 |
+ if [[ ! -d $git_dest ]]; then |
|
546 | 547 |
[[ "$ERROR_ON_CLONE" = "True" ]] && \ |
547 | 548 |
die $LINENO "Cloning not allowed in this configuration" |
548 |
- git_timed clone $GIT_REMOTE $GIT_DEST |
|
549 |
- cd $GIT_DEST |
|
549 |
+ git_timed clone $git_remote $git_dest |
|
550 |
+ cd $git_dest |
|
550 | 551 |
# This checkout syntax works for both branches and tags |
551 |
- git checkout $GIT_REF |
|
552 |
+ git checkout $git_ref |
|
552 | 553 |
elif [[ "$RECLONE" = "True" ]]; then |
553 | 554 |
# if it does exist then simulate what clone does if asked to RECLONE |
554 |
- cd $GIT_DEST |
|
555 |
+ cd $git_dest |
|
555 | 556 |
# set the url to pull from and fetch |
556 |
- git remote set-url origin $GIT_REMOTE |
|
557 |
+ git remote set-url origin $git_remote |
|
557 | 558 |
git_timed fetch origin |
558 | 559 |
# remove the existing ignored files (like pyc) as they cause breakage |
559 | 560 |
# (due to the py files having older timestamps than our pyc, so python |
560 | 561 |
# 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 |
|
562 |
+ find $git_dest -name '*.pyc' -delete |
|
563 |
+ |
|
564 |
+ # handle git_ref accordingly to type (tag, branch) |
|
565 |
+ if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then |
|
566 |
+ git_update_tag $git_ref |
|
567 |
+ elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then |
|
568 |
+ git_update_branch $git_ref |
|
569 |
+ elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then |
|
570 |
+ git_update_remote_branch $git_ref |
|
570 | 571 |
else |
571 |
- die $LINENO "$GIT_REF is neither branch nor tag" |
|
572 |
+ die $LINENO "$git_ref is neither branch nor tag" |
|
572 | 573 |
fi |
573 | 574 |
|
574 | 575 |
fi |
575 | 576 |
fi |
576 | 577 |
|
577 | 578 |
# print out the results so we know what change was used in the logs |
578 |
- cd $GIT_DEST |
|
579 |
+ cd $git_dest |
|
579 | 580 |
git show --oneline | head -1 |
580 | 581 |
cd $orig_dir |
581 | 582 |
} |
... | ... |
@@ -614,35 +616,32 @@ function git_timed { |
614 | 614 |
# git update using reference as a branch. |
615 | 615 |
# git_update_branch ref |
616 | 616 |
function git_update_branch { |
617 |
+ local git_branch=$1 |
|
617 | 618 |
|
618 |
- GIT_BRANCH=$1 |
|
619 |
- |
|
620 |
- git checkout -f origin/$GIT_BRANCH |
|
619 |
+ git checkout -f origin/$git_branch |
|
621 | 620 |
# a local branch might not exist |
622 |
- git branch -D $GIT_BRANCH || true |
|
623 |
- git checkout -b $GIT_BRANCH |
|
621 |
+ git branch -D $git_branch || true |
|
622 |
+ git checkout -b $git_branch |
|
624 | 623 |
} |
625 | 624 |
|
626 | 625 |
# git update using reference as a branch. |
627 | 626 |
# git_update_remote_branch ref |
628 | 627 |
function git_update_remote_branch { |
628 |
+ local git_branch=$1 |
|
629 | 629 |
|
630 |
- GIT_BRANCH=$1 |
|
631 |
- |
|
632 |
- git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH |
|
630 |
+ git checkout -b $git_branch -t origin/$git_branch |
|
633 | 631 |
} |
634 | 632 |
|
635 | 633 |
# git update using reference as a tag. Be careful editing source at that repo |
636 | 634 |
# as working copy will be in a detached mode |
637 | 635 |
# git_update_tag ref |
638 | 636 |
function git_update_tag { |
637 |
+ local git_tag=$1 |
|
639 | 638 |
|
640 |
- GIT_TAG=$1 |
|
641 |
- |
|
642 |
- git tag -d $GIT_TAG |
|
639 |
+ git tag -d $git_tag |
|
643 | 640 |
# fetching given tag only |
644 |
- git_timed fetch origin tag $GIT_TAG |
|
645 |
- git checkout -f $GIT_TAG |
|
641 |
+ git_timed fetch origin tag $git_tag |
|
642 |
+ git checkout -f $git_tag |
|
646 | 643 |
} |
647 | 644 |
|
648 | 645 |
|