From e1f15d4d7a695282be091b874094a346a7c94bfd Mon Sep 17 00:00:00 2001 From: John Barbosa Date: Mon, 30 Jan 2023 21:47:06 -0500 Subject: [PATCH 1/7] git parent-branch && git update-parent-branch custom commands where added --- .gitignore | 1 + bin/git-parent-branch | 3 +++ bin/git-update-parent-branch | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100755 bin/git-parent-branch create mode 100755 bin/git-update-parent-branch diff --git a/.gitignore b/.gitignore index 20fe18d..64c4322 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /tmp +tmp Gemfile.lock diff --git a/bin/git-parent-branch b/bin/git-parent-branch new file mode 100755 index 0000000..7a8cd88 --- /dev/null +++ b/bin/git-parent-branch @@ -0,0 +1,3 @@ +#!/bin/bash +# Detect the base or parent remote branch (best guess) +git log --pretty=format:'%d' --abbrev-commit --decorate-refs-exclude=refs/tags | grep origin | head -2 | tail -1 | sed '1s/tag:[^,]*, //g' | sed -nr '1s/.*\(([^,]+),?.*/\1/p' | cut -d ')' -f 1 | cut -d '(' -f 1 diff --git a/bin/git-update-parent-branch b/bin/git-update-parent-branch new file mode 100755 index 0000000..8cf2354 --- /dev/null +++ b/bin/git-update-parent-branch @@ -0,0 +1,22 @@ +#!/bin/bash +# Updates the base or parent branch +CURRENT_BRANCH=$(git current-branch) +PARENT_BRANCH=$(git parent-branch) +COMMIT_LIST=$(git rev-list $CURRENT_BRANCH...$PARENT_BRANCH) + +git checkout -b $CURRENT_BRANCH-temp +git fetch + +git checkout $PARENT_BRANCH +git branch -D $CURRENT_BRANCH +git checkout -b $CURRENT_BRANCH + +set -- junk $COMMIT_LIST +shift +for commit_id; do + git cherry-pick "$commit_id" +done + +git push --set-upstream origin -f $CURRENT_BRANCH + +git log --oneline --color --decorate --graph --all From b14dfa9c2d4d9c254b9a699cc07f5714ee6f48b3 Mon Sep 17 00:00:00 2001 From: John Barbosa Date: Mon, 30 Jan 2023 22:10:54 -0500 Subject: [PATCH 2/7] SC2086: Double quote to prevent globbing and word splitting. --- bin/git-update-parent-branch | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bin/git-update-parent-branch b/bin/git-update-parent-branch index 8cf2354..fc02aca 100755 --- a/bin/git-update-parent-branch +++ b/bin/git-update-parent-branch @@ -2,21 +2,21 @@ # Updates the base or parent branch CURRENT_BRANCH=$(git current-branch) PARENT_BRANCH=$(git parent-branch) -COMMIT_LIST=$(git rev-list $CURRENT_BRANCH...$PARENT_BRANCH) +COMMIT_LIST=$(git rev-list "$CURRENT_BRANCH"..."$PARENT_BRANCH") -git checkout -b $CURRENT_BRANCH-temp +git checkout -b "$CURRENT_BRANCH-temp" git fetch -git checkout $PARENT_BRANCH -git branch -D $CURRENT_BRANCH -git checkout -b $CURRENT_BRANCH +git checkout "$PARENT_BRANCH" +git branch -D "$CURRENT_BRANCH" +git checkout -b "$CURRENT_BRANCH" -set -- junk $COMMIT_LIST +set -- junk "$COMMIT_LIST" shift for commit_id; do git cherry-pick "$commit_id" done -git push --set-upstream origin -f $CURRENT_BRANCH +git push --set-upstream origin -f "$CURRENT_BRANCH" git log --oneline --color --decorate --graph --all From e119024492b05ca511911ace07fdbc0c0c4a7659 Mon Sep 17 00:00:00 2001 From: John Barbosa Date: Tue, 31 Jan 2023 06:11:28 -0500 Subject: [PATCH 3/7] Support for Parent Branch specification --- bin/git-update-parent-branch | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/bin/git-update-parent-branch b/bin/git-update-parent-branch index fc02aca..7b72b1a 100755 --- a/bin/git-update-parent-branch +++ b/bin/git-update-parent-branch @@ -1,8 +1,17 @@ #!/bin/bash # Updates the base or parent branch CURRENT_BRANCH=$(git current-branch) -PARENT_BRANCH=$(git parent-branch) -COMMIT_LIST=$(git rev-list "$CURRENT_BRANCH"..."$PARENT_BRANCH") +PARENT_BRANCH="" +COMMIT_LIST="" + +if [ "$1" == "" ]; then + PARENT_BRANCH="$1" + COMMON_ANCESTOR=$(git merge-base "$CURRENT_BRANCH" "$PARENT_BRANCH") + COMMIT_LIST=$(git rev-list "$CURRENT_BRANCH"..."$COMMON_ANCESTOR") +else + PARENT_BRANCH=$(git parent-branch) + COMMIT_LIST=$(git rev-list "$CURRENT_BRANCH"..."$PARENT_BRANCH") +fi git checkout -b "$CURRENT_BRANCH-temp" git fetch From 9eb81757b3112be64ccd0dfca36409723bfe49b8 Mon Sep 17 00:00:00 2001 From: John Barbosa Date: Tue, 31 Jan 2023 19:43:30 -0500 Subject: [PATCH 4/7] Add --single-commit opt to the update-parent-branch --- bin/git-branch-override | 16 +++++++++ bin/git-remove-temp-branch | 17 ++++++++++ bin/git-update-parent-branch | 65 +++++++++++++++++++++++++++--------- 3 files changed, 82 insertions(+), 16 deletions(-) create mode 100755 bin/git-branch-override create mode 100755 bin/git-remove-temp-branch diff --git a/bin/git-branch-override b/bin/git-branch-override new file mode 100755 index 0000000..ed4bf7b --- /dev/null +++ b/bin/git-branch-override @@ -0,0 +1,16 @@ +#!/bin/bash +# Removes a branch specified as temp of another +if [ "$1" == "" ]; then + echo -e "ERROR: Must pass branch name." 1>&2 + exit 1 +fi + +if [ "$2" == "" ]; then + echo -e "ERROR: Must pass the new branch name." 1>&2 + exit 1 +fi + +git checkout "$1" +git checkout -b "$2" +git branch -D "$1" +git logra diff --git a/bin/git-remove-temp-branch b/bin/git-remove-temp-branch new file mode 100755 index 0000000..0de1c60 --- /dev/null +++ b/bin/git-remove-temp-branch @@ -0,0 +1,17 @@ +#!/bin/bash +# Removes a branch specified as temp of another +if [ "$1" == "" ]; then + echo -e "ERROR: Must pass branch name." 1>&2 + exit 1 +fi + +if [ "$2" == "" ]; then + echo -e "ERROR: Must specify a temp branch name." 1>&2 + exit 1 +fi + +git checkout "$2" +git branch -D "$1" +git checkout -b "$1" +git branch -D "$2" +git logra diff --git a/bin/git-update-parent-branch b/bin/git-update-parent-branch index 7b72b1a..a859b27 100755 --- a/bin/git-update-parent-branch +++ b/bin/git-update-parent-branch @@ -1,31 +1,64 @@ #!/bin/bash # Updates the base or parent branch -CURRENT_BRANCH=$(git current-branch) +CURRENT_BRANCH="$(git current-branch)" PARENT_BRANCH="" -COMMIT_LIST="" +COMMIT_MODE="multi-commit" +COMMITS="" -if [ "$1" == "" ]; then - PARENT_BRANCH="$1" - COMMON_ANCESTOR=$(git merge-base "$CURRENT_BRANCH" "$PARENT_BRANCH") - COMMIT_LIST=$(git rev-list "$CURRENT_BRANCH"..."$COMMON_ANCESTOR") +while getopts ":-:" opt; do + case ${opt} in + -) + case ${OPTARG} in + "single-commit"*) COMMIT_MODE="single-commit";; + esac + esac +done + +if [ "$COMMIT_MODE" == "single-commit" ]; then + if [ "$2" == "" ]; then + PARENT_BRANCH="$(git parent-branch)" + else + PARENT_BRANCH="$2" + fi + COMMITS="$(git rev-parse "$CURRENT_BRANCH")" else - PARENT_BRANCH=$(git parent-branch) - COMMIT_LIST=$(git rev-list "$CURRENT_BRANCH"..."$PARENT_BRANCH") + if [ "$1" == "" ]; then + PARENT_BRANCH="$(git parent-branch)" + COMMITS="$(git rev-list "$CURRENT_BRANCH"..."$PARENT_BRANCH")" + else + PARENT_BRANCH="$1" + COMMON_ANCESTOR="$(git merge-base "$CURRENT_BRANCH" "$PARENT_BRANCH")" + COMMITS="$(git rev-list "$CURRENT_BRANCH"..."$COMMON_ANCESTOR")" + fi fi +echo "CURRENT BRANCH -> $CURRENT_BRANCH" +echo "PARENT BRANCH -> $PARENT_BRANCH" +echo "COMMIT LIST -> $COMMITS" +echo "----" + git checkout -b "$CURRENT_BRANCH-temp" git fetch -git checkout "$PARENT_BRANCH" -git branch -D "$CURRENT_BRANCH" -git checkout -b "$CURRENT_BRANCH" +git checkout "$PARENT_BRANCH" > /dev/null 2>&1 +git branch -D "$CURRENT_BRANCH" > /dev/null 2>&1 +git checkout -b "$CURRENT_BRANCH" > /dev/null 2>&1 -set -- junk "$COMMIT_LIST" -shift -for commit_id; do - git cherry-pick "$commit_id" -done +if [ "$COMMIT_MODE" == "single-commit" ]; then + echo "----" + echo "Cherry Picking -> $COMMITS" + git cherry-pick "$COMMITS" +else + COMMIT_ARRAY=($COMMIT_LIST) + for commit_id in "${COMMIT_ARRAY[@]}" + do + echo "----" + echo "Cherry Picking -> $commit_id" + git cherry-pick "$commit_id" + done +fi +echo "----" git push --set-upstream origin -f "$CURRENT_BRANCH" git log --oneline --color --decorate --graph --all From ebe1af3dddb4465b89959c2b15165ee68d91f450 Mon Sep 17 00:00:00 2001 From: John Barbosa Date: Tue, 31 Jan 2023 19:59:41 -0500 Subject: [PATCH 5/7] Reminders for common errors management --- bin/git-update-parent-branch | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bin/git-update-parent-branch b/bin/git-update-parent-branch index a859b27..b31bc3f 100755 --- a/bin/git-update-parent-branch +++ b/bin/git-update-parent-branch @@ -31,12 +31,14 @@ else COMMITS="$(git rev-list "$CURRENT_BRANCH"..."$COMMON_ANCESTOR")" fi fi +# TODO - What if we don't have commits? echo "CURRENT BRANCH -> $CURRENT_BRANCH" echo "PARENT BRANCH -> $PARENT_BRANCH" echo "COMMIT LIST -> $COMMITS" echo "----" +# TODO - What if a checkout fails? git checkout -b "$CURRENT_BRANCH-temp" git fetch @@ -44,6 +46,7 @@ git checkout "$PARENT_BRANCH" > /dev/null 2>&1 git branch -D "$CURRENT_BRANCH" > /dev/null 2>&1 git checkout -b "$CURRENT_BRANCH" > /dev/null 2>&1 +# TODO - What if a cherry-pick fails? if [ "$COMMIT_MODE" == "single-commit" ]; then echo "----" echo "Cherry Picking -> $COMMITS" From ac0341a4b272ed4f37cd2c633c9063e65a3c0331 Mon Sep 17 00:00:00 2001 From: John Barbosa Date: Fri, 10 Feb 2023 16:01:49 -0500 Subject: [PATCH 6/7] Stop parameter for multi-commit mode when the base branch was already updated I left it out not working sorry! --- bin/git-update-parent-branch | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/bin/git-update-parent-branch b/bin/git-update-parent-branch index b31bc3f..4947151 100755 --- a/bin/git-update-parent-branch +++ b/bin/git-update-parent-branch @@ -3,6 +3,7 @@ CURRENT_BRANCH="$(git current-branch)" PARENT_BRANCH="" COMMIT_MODE="multi-commit" +UNTIL_COMMIT="" COMMITS="" while getopts ":-:" opt; do @@ -30,6 +31,10 @@ else COMMON_ANCESTOR="$(git merge-base "$CURRENT_BRANCH" "$PARENT_BRANCH")" COMMITS="$(git rev-list "$CURRENT_BRANCH"..."$COMMON_ANCESTOR")" fi + + if [ "$2" != "" ]; then + UNTIL_COMMIT="$2" + fi fi # TODO - What if we don't have commits? @@ -52,12 +57,29 @@ if [ "$COMMIT_MODE" == "single-commit" ]; then echo "Cherry Picking -> $COMMITS" git cherry-pick "$COMMITS" else - COMMIT_ARRAY=($COMMIT_LIST) - for commit_id in "${COMMIT_ARRAY[@]}" - do + echo 'Multi Commit Mode Enabled!' + COMMIT_ARRAY=($COMMITS) + COMMITS_TO_CHERRY=() + + if [ "$UNTIL_COMMIT" != "" ]; then + for commit_id in "${COMMIT_ARRAY[@]}" + do + if [ "$UNTIL_COMMIT" == "$commit_id" ]; then + break + fi + COMMITS_TO_CHERRY+=("$commit_id") + done + fi + + size=${#COMMITS_TO_CHERRY[@]} + for (( i=size-1; i>=0; i-- )); do echo "----" - echo "Cherry Picking -> $commit_id" - git cherry-pick "$commit_id" + if [ "$UNTIL_COMMIT" == "${COMMITS_TO_CHERRY[i]}" ]; then + echo "NOT Cherry Picking! -> ${COMMITS_TO_CHERRY[i]}" + else + echo "Cherry Picking -> ${COMMITS_TO_CHERRY[i]}" + git cherry-pick "${COMMITS_TO_CHERRY[i]}" + fi done fi From 72c8c2ac6418489fd40e81e1b6a6d2cc5cd4bdd6 Mon Sep 17 00:00:00 2001 From: John Barbosa Date: Fri, 10 Feb 2023 17:22:33 -0500 Subject: [PATCH 7/7] Support for small commit specification --- bin/git-update-parent-branch | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/git-update-parent-branch b/bin/git-update-parent-branch index 4947151..3daad7e 100755 --- a/bin/git-update-parent-branch +++ b/bin/git-update-parent-branch @@ -64,7 +64,7 @@ else if [ "$UNTIL_COMMIT" != "" ]; then for commit_id in "${COMMIT_ARRAY[@]}" do - if [ "$UNTIL_COMMIT" == "$commit_id" ]; then + if [[ "$commit_id" =~ "$UNTIL_COMMIT" ]]; then break fi COMMITS_TO_CHERRY+=("$commit_id") @@ -74,7 +74,7 @@ else size=${#COMMITS_TO_CHERRY[@]} for (( i=size-1; i>=0; i-- )); do echo "----" - if [ "$UNTIL_COMMIT" == "${COMMITS_TO_CHERRY[i]}" ]; then + if [[ "${COMMITS_TO_CHERRY[i]}" =~ "$UNTIL_COMMIT" ]]; then echo "NOT Cherry Picking! -> ${COMMITS_TO_CHERRY[i]}" else echo "Cherry Picking -> ${COMMITS_TO_CHERRY[i]}"