-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-dropb
executable file
·61 lines (49 loc) · 1.87 KB
/
git-dropb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
# Copyright (c) 2019-2022 Benjamin Holt -- MIT License
if [ "$1" == "-h" -o "$1" == "--help" ]; then
cat <<USAGE
usage: git dropb [ALIAS] Deletes local branch ALIAS, ALIAS defaults to
ALLGIT_BRANCH
git dropb -h|--help Print this message and exit
notes:
If ALIAS is the current branch, dropb will try to switch to the
default branch. If no default branch is configured, dropb will try
to find the default branch of the default remote or 'origin'.
If the remote default branch has changed since this clone was made,
run 'git remote set-head REMOTE --auto'
config:
git config bettergit.defaultbranch BRANCH Set the default branch
git config bettergit.defaultremote REMOTE Set the default remote
USAGE
exit 0
fi
Bold=`tput bold`
Off=`tput sgr0`
Alias="${1:-$ALLGIT_BRANCH}"
if [ -z "$Alias" ]; then
echo "Must specify a branch directly or with allgit" >&2
exit 1
fi
DefaultBranch=`git config bettergit.defaultbranch`
# : ${DefaultBranch:=${ConfigBranch:-"master"}}
if [ -z "$DefaultBranch" ]; then
# FIXME: copied from git-rebase-default and modified, try to unify
ConfigRemote=`git config bettergit.defaultremote || echo ""`
Remote=${ConfigRemote:-"origin"} # REM: maybe use the first remote by default? `git remote | head -n1`
DefaultBranch=`git symbolic-ref refs/remotes/${Remote}/HEAD | sed -e "s:^refs/remotes/${Remote}/::"`
fi
if [ "$Alias" == "$DefaultBranch" ]; then
echo "Deleting the default branch $DefaultBranch is not generally a good idea" >&2
exit 1
fi
Branch=`git branch --list "$Alias"`
if [ -z "$Branch" ]; then
echo "$Alias: branch not found" >&2
exit 1
fi
if [ `git rev-parse --abbrev-ref HEAD` == "$Alias" ]; then
echo "${Bold}Switching to branch $DefaultBranch${Off}"
git checkout "$DefaultBranch" || exit 1
fi
git branch -d "$Alias"
###