]>
Commit | Line | Data |
---|---|---|
37f1a519 LT |
1 | #!/bin/sh |
2 | ||
075dd8ee | 3 | USAGE='[(-d | -D) <branchname>] | [[-f] <branchname> [<start-point>]]' |
d025524d | 4 | LONG_USAGE='If no arguments, show available branches and mark current branch with a star. |
a3b427b9 | 5 | If one argument, create a new branch <branchname> based off of current HEAD. |
d025524d FK |
6 | If two arguments, create a new branch <branchname> based off of <start-point>.' |
7 | ||
8 | SUBDIRECTORY_OK='Yes' | |
9 | . git-sh-setup | |
a3b427b9 | 10 | |
9cc2527c | 11 | headref=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||') |
eb777612 | 12 | |
ba65af9c | 13 | delete_branch () { |
03feddd6 JH |
14 | option="$1" |
15 | shift | |
03feddd6 JH |
16 | for branch_name |
17 | do | |
18 | case ",$headref," in | |
19 | ",$branch_name,") | |
20 | die "Cannot delete the branch you are on." ;; | |
21 | ,,) | |
22 | die "What branch are you on anyway?" ;; | |
23 | esac | |
24 | branch=$(cat "$GIT_DIR/refs/heads/$branch_name") && | |
25 | branch=$(git-rev-parse --verify "$branch^0") || | |
26 | die "Seriously, what branch are you talking about?" | |
27 | case "$option" in | |
28 | -D) | |
ba65af9c JH |
29 | ;; |
30 | *) | |
03feddd6 JH |
31 | mbs=$(git-merge-base -a "$branch" HEAD | tr '\012' ' ') |
32 | case " $mbs " in | |
33 | *' '$branch' '*) | |
34 | # the merge base of branch and HEAD contains branch -- | |
01385e27 | 35 | # which means that the HEAD contains everything in both. |
03feddd6 JH |
36 | ;; |
37 | *) | |
38 | echo >&2 "The branch '$branch_name' is not a strict subset of your current HEAD. | |
01385e27 | 39 | If you are sure you want to delete it, run 'git branch -D $branch_name'." |
03feddd6 JH |
40 | exit 1 |
41 | ;; | |
42 | esac | |
ba65af9c JH |
43 | ;; |
44 | esac | |
03feddd6 JH |
45 | rm -f "$GIT_DIR/refs/heads/$branch_name" |
46 | echo "Deleted branch $branch_name." | |
47 | done | |
ba65af9c JH |
48 | exit 0 |
49 | } | |
50 | ||
fd8fc4ad EW |
51 | ls_remote_branches () { |
52 | git-rev-parse --symbolic --all | | |
53 | sed -ne 's|^refs/\(remotes/\)|\1|p' | | |
54 | sort | |
55 | } | |
56 | ||
eb777612 | 57 | force= |
ba65af9c JH |
58 | while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac |
59 | do | |
60 | case "$1" in | |
61 | -d | -D) | |
03feddd6 | 62 | delete_branch "$@" |
ba65af9c JH |
63 | exit |
64 | ;; | |
fd8fc4ad EW |
65 | -r) |
66 | ls_remote_branches | |
67 | exit | |
68 | ;; | |
eb777612 JH |
69 | -f) |
70 | force="$1" | |
71 | ;; | |
ba65af9c JH |
72 | --) |
73 | shift | |
74 | break | |
75 | ;; | |
76 | -*) | |
77 | usage | |
78 | ;; | |
79 | esac | |
80 | shift | |
81 | done | |
82 | ||
e4aec26f KV |
83 | case "$#" in |
84 | 0) | |
e4aec26f KV |
85 | git-rev-parse --symbolic --all | |
86 | sed -ne 's|^refs/heads/||p' | | |
87 | sort | | |
88 | while read ref | |
89 | do | |
90 | if test "$headref" = "$ref" | |
91 | then | |
92 | pfx='*' | |
93 | else | |
94 | pfx=' ' | |
95 | fi | |
96 | echo "$pfx $ref" | |
97 | done | |
98 | exit 0 ;; | |
99 | 1) | |
a38e7279 | 100 | head=HEAD ;; |
e4aec26f | 101 | 2) |
a38e7279 JH |
102 | head="$2^0" ;; |
103 | esac | |
e4aec26f | 104 | branchname="$1" |
a3b427b9 | 105 | |
ff84d327 | 106 | rev=$(git-rev-parse --verify "$head") || exit |
37f1a519 | 107 | |
03feddd6 JH |
108 | git-check-ref-format "heads/$branchname" || |
109 | die "we do not like '$branchname' as a branch name." | |
37f1a519 | 110 | |
eb777612 JH |
111 | if [ -e "$GIT_DIR/refs/heads/$branchname" ] |
112 | then | |
113 | if test '' = "$force" | |
114 | then | |
115 | die "$branchname already exists." | |
116 | elif test "$branchname" = "$headref" | |
117 | then | |
118 | die "cannot force-update the current branch." | |
119 | fi | |
120 | fi | |
08db81a9 | 121 | git update-ref "refs/heads/$branchname" $rev |