Skip to content

Commit

Permalink
toolsets/repos: Add git-move-branch, git-get-branch-name & git-check-…
Browse files Browse the repository at this point in the history
…branch-or-commit which are a more flexible and robust git-move-branch-to-head - which is thus removed ; Rename git-branch to git-new-branch ;
  • Loading branch information
prenaux committed Jan 14, 2023
1 parent 00ff68b commit 12c34a2
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 21 deletions.
21 changes: 21 additions & 0 deletions toolsets/repos/git-check-branch-or-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
usage() {
echo "usage: ${0##*/} branch|commit_id"
echo ""
echo " 'git-get-branch-name' then check that the branch name or commit exist in the current repo."
echo ""
exit 1
}
if [ -z "$1" ]; then
usage
fi

BRANCH_NAME=`git-get-branch-name "$1"`
if [ `git rev-parse --verify "$BRANCH_NAME" 2>/dev/null` ]
then
echo "$BRANCH_NAME"
exit 0
else
echo "E/Can't find branch: '$BRANCH_NAME'"
exit 1
fi
62 changes: 62 additions & 0 deletions toolsets/repos/git-get-branch-name
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash
usage() {
echo "usage: ${0##*/} (ORIGIN:)branch"
echo ""
echo " Get or detect the name of a branch."
echo " "
echo " Detectable branch name:"
echo " main-branch|main_branch|master-branch|master_branch"
echo " Name of the main branch, usually main or master."
echo " current-branch|current_branch|."
echo " Name of the current branch."
echo ""
echo " The 'ORIGIN:' prefix is useful to specifiy an origin and expand a detected"
echo " branch name behind it. For example:"
echo " $ ${0##*/} origin:main-branch"
echo " > 'origin/main'"
echo " $ ${0##*/} upstream:current-branch"
echo " > 'upstream/my-current-work'"
echo " "
echo " Note: Use git-get-checked-branch-name to check if a branch name exists."
exit 1
}

if [ -z "$1" ]; then
usage
fi

BRANCH=$1
if [[ $BRANCH == *":"* ]]; then
ORIGIN=`echo "$BRANCH" | cut -d ":" -f 1`
BRANCH=`echo "$BRANCH" | cut -d ":" -f 2`
fi

output_and_done() {
if [ -n "$ORIGIN" ]; then
echo $ORIGIN/$1
else
echo $1
fi
exit 0
}

case "$BRANCH" in
main-branch|main_branch|master-branch|master_branch)
if [ `git rev-parse --verify main 2>/dev/null` ]
then
output_and_done main
fi
if [ `git rev-parse --verify master 2>/dev/null` ]
then
output_and_done master
fi
echo "cant_find_main_branch"
;;
current-branch|current_branch|.)
CURRENT_BRANCH=`git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
output_and_done "$CURRENT_BRANCH"
;;
*)
output_and_done "$BRANCH"
;;
esac
31 changes: 17 additions & 14 deletions toolsets/repos/git-merge-current-branch
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -e
#!/bin/bash
. "$HAM_HOME/bin/ham-bash-setenv.sh"

usage() {
Expand All @@ -14,27 +14,30 @@ if [ -z "$1" ]; then
echo "E/No branch name specified."
usage
fi
BRANCH_NAME=$1
shift

if [ -n "$2" ]; then
ORIGIN=$2
shift
else
ORIGIN=origin
BRANCH_NAME=`git-check-branch-or-commit "$1"`
if [ $? == 1 ]; then
echo "E/Can't find branch '$1'."
usage
fi

echo "I/Fetching and rebasing on '$ORIGIN/$BRANCH_NAME'"
(set -x ;
ORIGIN_BRANCH_NAME=`git-check-branch-or-commit "${2:-origin}:${BRANCH_NAME}"`
if [ $? == 1 ]; then
echo "E/Can't get origin branch name '${2:-origin}:${BRANCH_NAME}'."
usage
fi

echo "I/Fetching and rebasing on '$ORIGIN_BRANCH_NAME'"
(set -ex ;
git fetch --all ;
git-rebase $ORIGIN/$BRANCH_NAME)
git-rebase $ORIGIN_BRANCH_NAME)

echo "I/Moving '$BRANCH_NAME' to HEAD"
(set -x ;
(set -ex ;
git push $ORIGIN --force-with-lease ;
git-move-branch-to-head $BRANCH_NAME)
git-move-branch $BRANCH_NAME)

echo "I/Checking out '$BRANCH_NAME' and pushing to '$ORIGIN'"
(set -x ;
(set -ex ;
git checkout $BRANCH_NAME ;
git push $ORIGIN)
35 changes: 35 additions & 0 deletions toolsets/repos/git-move-branch
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
usage() {
echo "usage: ${0##*/} source (destination, default HEAD)"
echo ""
echo " Moves the source branch to point to the destination branch."
echo ""
exit 1
}

case "$1" in
-*)
usage
;;
esac

SRC=`git-check-branch-or-commit "$1"`
if [ $? == 1 ]; then
echo "E/Can't find source branch '$1'."
usage
fi

DEST=`git-check-branch-or-commit "${2:-HEAD}"`
if [ $? == 1 ]; then
echo "E/Can't find destination branch/commit '$2'."
usage
fi

SRC_COMMIT=`git rev-parse --verify --short=8 "$SRC"`
DEST_COMMIT=`git rev-parse --verify --short=8 "$DEST"`

echo "I/Moving '$SRC' ($SRC_COMMIT) to point to '$DEST' ($DEST_COMMIT)."
(set -ex ;
# Move the branch
git branch -f $SRC $DEST
)
4 changes: 0 additions & 4 deletions toolsets/repos/git-move-branch-to-head

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
help() {
echo "usage: git-branch branch_name"
echo "usage: git-new-branch branch_name"
echo ""
echo " Creates a new branch, makes it active, make sure there's a remote"
echo " tracking branch on origin for it and push the initial version."
Expand Down
4 changes: 2 additions & 2 deletions toolsets/repos/gt-new-branch
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
usage() {
echo "usage: ${0##*/} BASE_BRANCH_NAME"
echo ""
echo " Create a branch with 'git-branch' and then track it with 'gt branch track'. Prefix the branch name with '$USER-%y-%m-%d'."
echo " Create a branch with 'git-new-branch' and then track it with 'gt branch track'. Prefix the branch name with '$USER-%y-%m-%d'."
echo ""
exit 1
}
Expand All @@ -16,5 +16,5 @@ fi

(set -x ;
BRANCH_PREFIX=$(date +"$USER-%y-%m-%d") ;
git-branch "$BRANCH_PREFIX-$1" ;
git-new-branch "$BRANCH_PREFIX-$1" ;
ham-gt branch track "$BRANCH_PREFIX-$1")

0 comments on commit 12c34a2

Please sign in to comment.