Three more improvements to branch creation tools.

1) Add default revision and soft/hard lockdown options to create_tags.sh.

These options already appear in the create_branches_and_tags.sh script.

Default revision set the default revision field of the manifest,
removing the need to specify a revision for each project.

Soft lockdown is content with tags already specified in the manifest.
Hard lockdown forces the use of sha's.

2) Improved determination of the local branch.

Need to work around inconsistencies of various git versions.
Also need to deal with 'repo' leaving us in detached head state and
local updates on a detached state, in which case we try
to find the 'best fit' local branch from which we diverged.

3) Ability to ask the branch/tag push scripts to bypass gerrit.

Successful use requires the git repositories and gerrit to
to permit direct push.  No true of the current opendev repo setup,
but might be useful for mirrors.

Closes-Bug: 1924762
Signed-off-by: Scott Little <scott.little@windriver.com>
Change-Id: I2ba01e11ef27c1d10c816b5af6104afab440b3b3
This commit is contained in:
Scott Little 2021-06-03 14:38:56 -04:00
parent fa931563f8
commit 1398925730
5 changed files with 85 additions and 34 deletions

View File

@ -38,7 +38,7 @@ usage () {
echo "If lockdown is requested, all other projects get the current"
echo "HEAD's sha set as the revision."
echo "If default-revision is selected, then the manifest default revision"
wcho "will be set."
echo "will be set."
echo ""
echo "If a gitreview-default is selected, then all branched projects"
echo "with a .gitreview file will have a defaultbranch entry added"

View File

@ -23,7 +23,7 @@ CREATE_TAGS_SH_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}" )" )"
source "${CREATE_TAGS_SH_DIR}/../git-repo-utils.sh"
usage () {
echo "create_tags.sh --tag=<tag> [ --remotes=<remotes> ] [ --projects=<projects> ] [ --manifest [ --manifest-prefix <prefix> ] [ --lock-down ]]"
echo "create_tags.sh --tag=<tag> [ --remotes=<remotes> ] [ --projects=<projects> ] [ --manifest [ --manifest-prefix <prefix> ] [ --lock-down | --soft-lock-down ] [ --default-revision ]]"
echo " "
echo "Create a git tag in all listed projects, and all projects"
echo "hosted by all listed remotes. Lists are comma separated."
@ -32,9 +32,11 @@ usage () {
echo "it will specify the tag as the revision for all tagged projects."
echo "If lockdown is requested, all non-tagged projects get the current"
echo "HEAD's sha set as the revision."
echo "If default-revision is selected, then the manifest default revision"
echo "will be set."
}
TEMP=$(getopt -o h --long remotes:,projects:,tag:,manifest,manifest-prefix:,lock-down,help -n 'create_tags.sh' -- "$@")
TEMP=$(getopt -o h --long remotes:,projects:,tag:,manifest,manifest-prefix:,lock-down,hard-lock-down,soft-lock-down,default-revision,help -n 'create_tags.sh' -- "$@")
if [ $? -ne 0 ]; then
usage
exit 1
@ -44,6 +46,7 @@ eval set -- "$TEMP"
HELP=0
MANIFEST=0
LOCK_DOWN=0
SET_DEFAULT_REVISION=0
remotes=""
projects=""
tag=""
@ -54,15 +57,18 @@ repo_root_dir=""
while true ; do
case "$1" in
-h|--help) HELP=1 ; shift ;;
--remotes) remotes+=$(echo "$2 " | tr ',' ' '); shift 2;;
--projects) projects+=$(echo "$2 " | tr ',' ' '); shift 2;;
--tag) tag=$2; shift 2;;
--manifest) MANIFEST=1 ; shift ;;
--manifest-prefix) manifest_prefix=$2; shift 2;;
--lock-down) LOCK_DOWN=1 ; shift ;;
--) shift ; break ;;
*) usage; exit 1 ;;
-h|--help) HELP=1 ; shift ;;
--remotes) remotes+=$(echo "$2 " | tr ',' ' '); shift 2;;
--projects) projects+=$(echo "$2 " | tr ',' ' '); shift 2;;
--tag) tag=$2; shift 2;;
--manifest) MANIFEST=1 ; shift ;;
--manifest-prefix) manifest_prefix=$2; shift 2;;
--lock-down) LOCK_DOWN=2 ; shift ;;
--hard-lock-down) LOCK_DOWN=2 ; shift ;;
--soft-lock-down) LOCK_DOWN=1 ; shift ;;
--default-revision) SET_DEFAULT_REVISION=1 ; shift ;;
--) shift ; break ;;
*) usage; exit 1 ;;
esac
done
@ -218,7 +224,7 @@ if [ $MANIFEST -eq 1 ]; then
fi
echo "Creating manifest ${new_manifest_name}"
manifest_set_revision "${manifest}" "${new_manifest}" "refs/tags/$tag" ${LOCK_DOWN} $projects || exit 1
manifest_set_revision "${manifest}" "${new_manifest}" "refs/tags/$tag" ${LOCK_DOWN} ${SET_DEFAULT_REVISION} $projects || exit 1
echo "Committing ${new_manifest_name} in ${new_manifest_dir}"
git add ${new_manifest_name} || exit 1

View File

@ -20,7 +20,7 @@ source "${PUSH_BRANCHES_TAGS_SH_DIR}/../git-repo-utils.sh"
source "${PUSH_BRANCHES_TAGS_SH_DIR}/../url_utils.sh"
usage () {
echo "push_branches_tags.sh --branch=<branch> [--tag=<tag>] [ --remotes=<remotes> ] [ --projects=<projects> ] [ --manifest ]"
echo "push_branches_tags.sh --branch=<branch> [--tag=<tag>] [ --remotes=<remotes> ] [ --projects=<projects> ] [ --manifest ] [ --bypass-gerrit ]"
echo ""
echo "Push a pre-existing branch and tag into all listed projects, and all"
echo "projects hosted by all listed remotes. Lists are comma separated."
@ -32,7 +32,7 @@ usage () {
echo "A manifest push can also be requested.vision."
}
TEMP=$(getopt -o h --long remotes:,projects:,branch:,tag:,manifest,help -n 'push_branches_tags.sh' -- "$@")
TEMP=$(getopt -o h --long remotes:,projects:,branch:,tag:,bypass-gerrit,manifest,help -n 'push_branches_tags.sh' -- "$@")
if [ $? -ne 0 ]; then
usage
exit 1
@ -41,6 +41,7 @@ eval set -- "$TEMP"
HELP=0
MANIFEST=0
BYPASS_GERRIT=0
remotes=""
projects=""
branch=""
@ -51,6 +52,7 @@ repo_root_dir=""
while true ; do
case "$1" in
-h|--help) HELP=1 ; shift ;;
--bypass-gerrit) BYPASS_GERRIT=1 ; shift ;;
--remotes) remotes+=$(echo "$2 " | tr ',' ' '); shift 2;;
--projects) projects+=$(echo "$2 " | tr ',' ' '); shift 2;;
--branch) branch=$2; shift 2;;
@ -196,7 +198,7 @@ for subgit in $SUBGITS; do
fi
echo "Pushing branch $branch in ${subgit}"
if [ "${review_method}" == "gerrit" ]; then
if [ "${review_method}" == "gerrit" ] && [ $BYPASS_GERRIT -eq 0 ]; then
url=$(git_repo_review_url)
if [ "${review_remote}" == "" ]; then
echo_stderr "ERROR: Failed to determine review_url in ${subgit}"
@ -212,8 +214,8 @@ for subgit in $SUBGITS; do
git config --local --replace-all "branch.${branch}.merge" refs/heads/${branch} && \
git review --topic="${branch}"
else
echo "git push --tags ${remote} ${branch}"
git push --tags ${remote} ${branch}
echo "git push --tags ${review_remote} ${branch}"
git push --tags ${review_remote} ${branch}
fi
else
echo "git push --tags --set-upstream ${remote} ${branch}"
@ -276,7 +278,7 @@ if [ $MANIFEST -eq 1 ]; then
fi
echo "Pushing branch $branch in ${manifest_dir}"
if [ "${review_method}" == "gerrit" ]; then
if [ "${review_method}" == "gerrit" ] && [ $BYPASS_GERRIT -eq 0 ]; then
# Is a reviewless push possible as part of creating a new branch in gerrit?
url=$(git_review_url)
if [ "${review_remote}" == "" ]; then

View File

@ -19,7 +19,7 @@ PUSH_TAGS_SH_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}" )" )"
source "${PUSH_TAGS_SH_DIR}/../git-repo-utils.sh"
usage () {
echo "push_tags.sh --tag=<tag> [ --remotes=<remotes> ] [ --projects=<projects> ] [ --manifest [--manifest-prefix <prefix>]]"
echo "push_tags.sh --tag=<tag> [ --remotes=<remotes> ] [ --projects=<projects> ] [ --manifest [--manifest-prefix <prefix>]] [ --bypass-gerrit ]"
echo " "
echo "Push a pre-existing git tag into all listed projects, and all projects"
echo "hosted by all listed remotes. Lists are comma separated."
@ -27,7 +27,7 @@ usage () {
echo "A manifest push can also be requested."
}
TEMP=$(getopt -o h --long remotes:,projects:,tag:,manifest,manifest-prefix:,help -n 'push_tags.sh' -- "$@")
TEMP=$(getopt -o h --long remotes:,projects:,tag:,manifest,manifest-prefix:,bypass-gerrit,help -n 'push_tags.sh' -- "$@")
if [ $? -ne 0 ]; then
usage
exit 1
@ -36,6 +36,7 @@ eval set -- "$TEMP"
HELP=0
MANIFEST=0
BYPASS_GERRIT=0
remotes=""
projects=""
tag=""
@ -47,6 +48,7 @@ repo_root_dir=""
while true ; do
case "$1" in
-h|--help) HELP=1 ; shift ;;
--bypass-gerrit) BYPASS_GERRIT=1 ; shift ;;
--remotes) remotes+=$(echo "$2 " | tr ',' ' '); shift 2;;
--projects) projects+=$(echo "$2 " | tr ',' ' '); shift 2;;
--tag) tag=$2; shift 2;;
@ -145,21 +147,27 @@ for subgit in $SUBGITS; do
exit 1
fi
if [ "${review_method}" == "gerrit" ]; then
remote=$(git_repo_review_remote)
else
remote=$(git_repo_remote)
fi
remote=$(git_repo_review_remote)
if [ "${remote}" == "" ]; then
echo_stderr "ERROR: Failed to determine remote in ${subgit}"
exit 1
fi
echo "Pushing tag $tag in ${subgit}"
if [ "${review_method}" == "gerrit" ]; then
echo "git push ${remote} ${tag}"
git push ${remote} ${tag}
review_remote=$(git_repo_review_remote)
else
review_remote=${remote}
fi
if [ "${review_remote}" == "" ]; then
echo_stderr "ERROR: Failed to determine review_remote in ${subgit}"
exit 1
fi
echo "Pushing tag $tag in ${subgit}"
if [ "${review_method}" == "gerrit" ] && [ $BYPASS_GERRIT -eq 0 ]; then
echo "git push ${review_remote} ${tag}"
git push ${review_remote} ${tag}
else
echo "git push ${remote} ${tag}"
git push ${remote} ${tag}
@ -209,14 +217,20 @@ if [ $MANIFEST -eq 1 ]; then
exit 1
fi
remote=$(git_review_remote)
remote=$(git_remote)
if [ "${remote}" == "" ]; then
echo_stderr "ERROR: Failed to determine remote in ${new_manifest_dir}"
exit 1
fi
review_remote=$(git_review_remote)
if [ "${review_remote}" == "" ]; then
echo_stderr "ERROR: Failed to determine review remote in ${new_manifest_dir}"
exit 1
fi
echo "Pushing tag $tag in ${new_manifest_dir}"
if [ "${review_method}" == "gerrit" ]; then
if [ "${review_method}" == "gerrit" ] && [ $BYPASS_GERRIT -eq 0 ]; then
git review
if [ $? != 0 ] ; then
echo_stderr "ERROR: Failed to create git review from ${new_manifest_dir}"
@ -224,7 +238,7 @@ if [ $MANIFEST -eq 1 ]; then
fi
echo "When review is merged: please run ..."
echo " cd ${new_manifest_dir}"
echo " git push ${remote} ${tag}"
echo " git push ${review_remote} ${tag}"
else
git push ${remote} ${local_branch}:${remote_branch}
git push ${remote} ${tag}:${tag}

View File

@ -397,12 +397,41 @@ git_test_context () {
git_local_branch () {
local result=""
local sha=""
# Older gits don't support this
result=$(git branch --show-current 2> /dev/null)
if [ "$result" != "" ]; then
echo $result
return 0
fi
# Might not work if detached and there are local commits
result=$(git branch | grep '^[*] ' | cut -b 3- | grep -v HEAD)
if [ "$result" != "" ]; then
echo $result
return 0
fi
# Find 'nearest' local branch that we detached from and/or added commits to
sha=$(git rev-parse HEAD)
while [ $? -eq 0 ]; do
result=$(git show-ref --head | grep -e "$sha refs/heads/" | sed "s#$sha refs/heads/##" | head -n 1)
if [ "$result" != "" ]; then
echo $result
return 0
fi
sha=$(git rev-parse $sha^ 2> /dev/null)
done
# This used to work on older git versions
result=$(git name-rev --name-only HEAD)
if [ "$result" == "" ] || [ "$result" == "undefined" ]; then
return 1
fi
# handle the case where a tag is returned by looking at the parent.
# Handle the case where a tag is returned by looking at the parent.
# This weird case when a local commit is tagged and we were in
# detached head state, or on 'default' branch.
while git_is_tag $result; do