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