Merge branch 'cc/branch-test'
[git] / git-branch.sh
1 #!/bin/sh
2
3 USAGE='[-l] [(-d | -D) <branchname>] | [[-f] <branchname> [<start-point>]] | -r'
4 LONG_USAGE='If no arguments, show available branches and mark current branch with a star.
5 If one argument, create a new branch <branchname> based off of current HEAD.
6 If two arguments, create a new branch <branchname> based off of <start-point>.'
7
8 SUBDIRECTORY_OK='Yes'
9 . git-sh-setup
10
11 headref=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||')
12
13 delete_branch () {
14     option="$1"
15     shift
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)
29             ;;
30         *)
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 --
35                 # which means that the HEAD contains everything in both.
36                 ;;
37             *)
38                 echo >&2 "The branch '$branch_name' is not a strict subset of your current HEAD.
39 If you are sure you want to delete it, run 'git branch -D $branch_name'."
40                 exit 1
41                 ;;
42             esac
43             ;;
44         esac
45         rm -f "$GIT_DIR/logs/refs/heads/$branch_name"
46         rm -f "$GIT_DIR/refs/heads/$branch_name"
47         echo "Deleted branch $branch_name."
48     done
49     exit 0
50 }
51
52 ls_remote_branches () {
53     git-rev-parse --symbolic --all |
54     sed -ne 's|^refs/\(remotes/\)|\1|p' |
55     sort
56 }
57
58 force=
59 create_log=
60 while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
61 do
62         case "$1" in
63         -d | -D)
64                 delete_branch "$@"
65                 exit
66                 ;;
67         -r)
68                 ls_remote_branches
69                 exit
70                 ;;
71         -f)
72                 force="$1"
73                 ;;
74         -l)
75                 create_log="yes"
76                 ;;
77         --)
78                 shift
79                 break
80                 ;;
81         -*)
82                 usage
83                 ;;
84         esac
85         shift
86 done
87
88 case "$#" in
89 0)
90         git-rev-parse --symbolic --branches |
91         sort |
92         while read ref
93         do
94                 if test "$headref" = "$ref"
95                 then
96                         pfx='*'
97                 else
98                         pfx=' '
99                 fi
100                 echo "$pfx $ref"
101         done
102         exit 0 ;;
103 1)
104         head=HEAD ;;
105 2)
106         head="$2^0" ;;
107 esac
108 branchname="$1"
109
110 rev=$(git-rev-parse --verify "$head") || exit
111
112 git-check-ref-format "heads/$branchname" ||
113         die "we do not like '$branchname' as a branch name."
114
115 if [ -d "$GIT_DIR/refs/heads/$branchname" ]
116 then
117         for refdir in `cd "$GIT_DIR" && \
118                 find "refs/heads/$branchname" -type d | sort -r`
119         do
120                 rmdir "$GIT_DIR/$refdir" || \
121                     die "Could not delete '$refdir', there may still be a ref there."
122         done
123 fi
124
125 if [ -e "$GIT_DIR/refs/heads/$branchname" ]
126 then
127         if test '' = "$force"
128         then
129                 die "$branchname already exists."
130         elif test "$branchname" = "$headref"
131         then
132                 die "cannot force-update the current branch."
133         fi
134 fi
135 if test "$create_log" = 'yes'
136 then
137         mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$branchname")
138         touch "$GIT_DIR/logs/refs/heads/$branchname"
139 fi
140 git update-ref -m "branch: Created from $head" "refs/heads/$branchname" $rev