-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update git-delete-branch #577
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,94 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Assert there is at least one branch provided | ||
test -z $1 && echo "branch required." 1>&2 && exit 1 | ||
|
||
for branch in "$@" | ||
do | ||
remote=$(git config branch.$branch.remote) | ||
test -z $remote && remote="origin" | ||
ref=$(git config branch.$branch.merge) | ||
test -z $ref && ref="refs/heads/$branch" | ||
|
||
git branch -D $branch | ||
git branch -d -r $remote/$branch | ||
git push $remote :$ref | ||
set -u | ||
|
||
usage() | ||
{ | ||
local command="git delete-branch" | ||
cat << EOF | ||
Usage: | ||
${command} [-r <remote[:remote branch name]>] <branch> | ||
${command} -h | ||
|
||
Delete local and remote branches. If the remote switch (-r) is provided, then | ||
use the given remote and remote branch. Otherwise, the remote branch is found | ||
by first trying the upstream tracking branch. If there is no tracking branch | ||
configured, then deletion will not succeed unless provided explicitly. If the | ||
remote branch and local branch do not share a name, the remote branch name can | ||
be included with the remote switch. | ||
EOF | ||
} | ||
|
||
remote= | ||
remote_branch= | ||
|
||
while getopts "r:h" opt; do | ||
case "$opt" in | ||
r) | ||
remote="$(echo ${OPTARG} | cut -d ':' -f 1)" | ||
remote_branch="$(echo ${OPTARG} | cut -s -d ':' -f 2)" | ||
|
||
# Remote must not be empty string, or anything of that sort. The | ||
# remote branch need not be provided here. | ||
if [[ -z "$remote" ]]; then | ||
echo "error: Provided empty remote name" | ||
usage | ||
exit 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be great if you can extract all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Can do |
||
fi | ||
;; | ||
h) | ||
usage | ||
exit 0 | ||
;; | ||
\?) | ||
usage | ||
exit 1 | ||
;; | ||
esac | ||
done | ||
|
||
shift $((OPTIND-1)) | ||
|
||
if [[ $# -ne 1 ]]; then | ||
echo "error: Need a single branch as a positional argument" | ||
usage | ||
exit 1 | ||
fi | ||
|
||
branch="$1" | ||
|
||
# If the -r option was not passed, then both the remote and the remote branch | ||
# have to be assumed using the tracking configuration. Need to make sure that | ||
# these values are set as both can be set/unset and both are necessary in this | ||
# case. | ||
if [[ -z "$remote" ]]; then | ||
remote="$(git config --get branch.${branch}.remote)" | ||
if [[ $? -ne 0 ]]; then | ||
echo "error: $branch not tracking a repository" | ||
usage | ||
exit 1 | ||
fi | ||
|
||
remote_branch="$(git config --get branch.${branch}.merge)" | ||
if [[ $? -ne 0 ]]; then | ||
echo "error: $branch not tracking a remote branch" | ||
usage | ||
exit 1 | ||
fi | ||
elif [[ -z "$remote_branch" ]]; then | ||
# In the event that a remote is specified but no remote branch is | ||
# specified, then assume the branch name is the same as the local branch. | ||
remote_branch="$branch" | ||
fi | ||
|
||
git branch -d "$branch" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you tell me the reason to replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I had replaced it with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion, it is import to persist the original behaviour, unless the original one is wrong or not up-to-date. |
||
ret=$? | ||
if [[ $ret -ne 0 ]]; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Can do |
||
exit $ret | ||
fi | ||
|
||
git push "${remote}" ":${remote_branch}" | ||
ret=$? | ||
if [[ $ret -ne 0 ]]; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The exit status of the final command will always be the exit status of the script. No need to check it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Thanks, will fix. |
||
exit $ret | ||
fi |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,29 +3,44 @@ git-delete-branch(1) -- Delete branches | |
|
||
## SYNOPSIS | ||
|
||
`git-delete-branch` <branchname> | ||
`git-delete-branch` [-r <remote[:remote branch name]>] <branchname> | ||
|
||
## DESCRIPTION | ||
|
||
Deletes local and remote branch named <branchname>. | ||
Note that local deletion fails if the branch is checked out. | ||
Delete local and remote branches. If the remote switch (-r) is provided, then | ||
use the given remote and remote branch. Otherwise, the remote branch is found | ||
by first trying the upstream tracking branch. If there is no tracking branch | ||
configured, then deletion will not succeed unless provided explicitly. If the | ||
remote branch and local branch do not share a name, the remote branch name can | ||
be included with the remote switch. | ||
|
||
Local deletion will fail if the branch is currently checked out. If the local | ||
operation fails, then the remote operation will not run. | ||
|
||
## OPTIONS | ||
|
||
<branchname> | ||
-r | ||
|
||
The name of the branch to delete. | ||
If multiple branches are provided, then they will all be deleted. | ||
Specify a remote name and a remote branch name to use during the remote | ||
branch deletion. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At my first glance, it seems we need to provide two arguments, one is remote name and the other is remote branch name. Maybe we could mention the format of arguments to make it clearer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
What about:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
|
||
## EXAMPLES | ||
|
||
$ git delete-branch integration | ||
$ git delete-branch integration bug/1234 | ||
Delete the branch "ex1", the upstream tracking information is used. | ||
|
||
$ git delete-branch ex1 | ||
|
||
Delete the local branch "ex2", and the remote branch "ex2-1" at remote | ||
"upstream". | ||
|
||
$ git delete-branch -r upstream:ext2-1 ex2 | ||
|
||
## AUTHOR | ||
|
||
Written by Tj Holowaychuk <<tj@vision-media.ca>> | ||
|
||
Extended by Ben Turrubiates <ben@turrubiat.es> | ||
|
||
## REPORTING BUGS | ||
|
||
<<https://github.com/tj/git-extras/issues>> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to use parameter expansion instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I thought using cut would be more clear. So is this what you're suggestion:
So instead of:
you want: