Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 91 additions & 13 deletions bin/git-delete-branch
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)"
Copy link
Collaborator

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.

Copy link
Author

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.

Hmm I thought using cut would be more clear. So is this what you're suggestion:

So instead of:

remote="$(echo ${OPTARG} | cut -d ':' -f 1)"
remote_branch="$(echo ${OPTARG} | cut -s -d ':' -f 2)"

you want:

remote=${OPTARG%%:*}
remote_branch=${OPTARG##*:}


# 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be great if you can extract all usage... exit ... into a function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be great if you can extract all usage... exit ... into a function.

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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you tell me the reason to replace branch -D to branch -d?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you tell me the reason to replace branch -D to branch -d?

I had replaced it with -d because it warns about unmerged branches, but I could use -D.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.
Glad to see the old one back 😄

ret=$?
if [[ $ret -ne 0 ]]; then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not git branch ... || exit $?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not git branch ... || exit $?

Can do

exit $ret
fi

git push "${remote}" ":${remote_branch}"
ret=$?
if [[ $ret -ne 0 ]]; then
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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.

Thanks, will fix.

exit $ret
fi
29 changes: 19 additions & 10 deletions man/git-delete-branch.1
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "GIT\-DELETE\-BRANCH" "1" "December 2015" "" ""
.TH "GIT\-DELETE\-BRANCH" "1" "September 2016" "" ""
.
.SH "NAME"
\fBgit\-delete\-branch\fR \- Delete branches
.
.SH "SYNOPSIS"
\fBgit\-delete\-branch\fR <branchname>
\fBgit\-delete\-branch\fR [\-r <remote[:remote branch name]>] <branchname>
.
.SH "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\.
.
.P
Local deletion will fail if the branch is currently checked out\. If the local operation fails, then the remote operation will not run\.
.
.SH "OPTIONS"
<branchname>
\-r
.
.P
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\.
.
.SH "EXAMPLES"
Delete the branch "ex1", the upstream tracking information is used\.
.
.P
$ git delete\-branch ex1
.
.nf

$ git delete\-branch integration
$ git delete\-branch integration bug/1234
.P
Delete the local branch "ex2", and the remote branch "ex2\-1" at remote "upstream"\.
.
.fi
.P
$ git delete\-branch \-r upstream:ext2\-1 ex2
.
.SH "AUTHOR"
Written by Tj Holowaychuk <\fItj@vision\-media\.ca\fR>
.
.P
Extended by Ben Turrubiates <ben@turrubiat\.es>
.
.SH "REPORTING BUGS"
<\fIhttps://github\.com/tj/git\-extras/issues\fR>
.
Expand Down
36 changes: 25 additions & 11 deletions man/git-delete-branch.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 23 additions & 8 deletions man/git-delete-branch.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,44 @@ git-delete-branch(1) -- Delete branches

## SYNOPSIS

`git-delete-branch` &lt;branchname&gt;
`git-delete-branch` [-r &lt;remote[:remote branch name]&gt;] &lt;branchname&gt;

## DESCRIPTION

Deletes local and remote branch named &lt;branchname&gt;.
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

&lt;branchname&gt;
-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.
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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.

What about:

Specify a remote name and an optional remote branch name to use during the remote
branch deletion.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a remote name with an optional remote branch name is a little better. Just change a little from your idea.


## 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 &lt;<tj@vision-media.ca>&gt;

Extended by Ben Turrubiates &lt;ben@turrubiat.es&gt;

## REPORTING BUGS

&lt;<https://github.com/tj/git-extras/issues>&gt;
Expand Down