Code

show-ref --hash=len, --abbrev=len, and --abbrev
[git.git] / git-branch.sh
1 #!/bin/sh
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>.'
8 SUBDIRECTORY_OK='Yes'
9 . git-sh-setup
11 headref=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||')
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=$(git-show-ref --verify --hash -- "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         git update-ref -d "refs/heads/$branch_name" "$branch"
46         echo "Deleted branch $branch_name."
47     done
48     exit 0
49 }
51 ls_remote_branches () {
52     git-rev-parse --symbolic --all |
53     sed -ne 's|^refs/\(remotes/\)|\1|p' |
54     sort
55 }
57 force=
58 create_log=
59 while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
60 do
61         case "$1" in
62         -d | -D)
63                 delete_branch "$@"
64                 exit
65                 ;;
66         -r)
67                 ls_remote_branches
68                 exit
69                 ;;
70         -f)
71                 force="$1"
72                 ;;
73         -l)
74                 create_log="yes"
75                 ;;
76         --)
77                 shift
78                 break
79                 ;;
80         -*)
81                 usage
82                 ;;
83         esac
84         shift
85 done
87 case "$#" in
88 0)
89         git-rev-parse --symbolic --branches |
90         sort |
91         while read ref
92         do
93                 if test "$headref" = "$ref"
94                 then
95                         pfx='*'
96                 else
97                         pfx=' '
98                 fi
99                 echo "$pfx $ref"
100         done
101         exit 0 ;;
102 1)
103         head=HEAD ;;
104 2)
105         head="$2^0" ;;
106 esac
107 branchname="$1"
109 rev=$(git-rev-parse --verify "$head") || exit
111 git-check-ref-format "heads/$branchname" ||
112         die "we do not like '$branchname' as a branch name."
114 if [ -d "$GIT_DIR/refs/heads/$branchname" ]
115 then
116         for refdir in `cd "$GIT_DIR" && \
117                 find "refs/heads/$branchname" -type d | sort -r`
118         do
119                 rmdir "$GIT_DIR/$refdir" || \
120                     die "Could not delete '$refdir', there may still be a ref there."
121         done
122 fi
124 prev=''
125 if git-show-ref --verify --quiet -- "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         prev=`git rev-parse --verify "refs/heads/$branchname"`
135 fi
136 if test "$create_log" = 'yes'
137 then
138         mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$branchname")
139         touch "$GIT_DIR/logs/refs/heads/$branchname"
140 fi
141 git update-ref -m "branch: Created from $head" "refs/heads/$branchname" "$rev" "$prev"