blob: d1e37307617abd6144f36f93decbebd95a97fb32 [file] [log] [blame]
Linus Torvalds37f1a512005-07-11 21:30:23 -07001#!/bin/sh
2
Sean Estabrooks2eaf2732006-04-28 09:15:04 -04003USAGE='[(-d | -D) <branchname>] | [[-f] <branchname> [<start-point>]] | -r'
Fredrik Kuivinend0255242005-12-11 10:55:49 +01004LONG_USAGE='If no arguments, show available branches and mark current branch with a star.
Amos Waterlanda3b427b2005-09-07 21:13:26 -05005If one argument, create a new branch <branchname> based off of current HEAD.
Fredrik Kuivinend0255242005-12-11 10:55:49 +01006If two arguments, create a new branch <branchname> based off of <start-point>.'
7
8SUBDIRECTORY_OK='Yes'
9. git-sh-setup
Amos Waterlanda3b427b2005-09-07 21:13:26 -050010
Junio C Hamano9cc25272005-11-27 23:16:15 -080011headref=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||')
Junio C Hamanoeb777612005-11-11 11:12:50 -080012
Junio C Hamanoba65af92005-09-14 01:43:53 -070013delete_branch () {
Junio C Hamano03feddd2005-10-13 18:57:39 -070014 option="$1"
15 shift
Junio C Hamano03feddd2005-10-13 18:57:39 -070016 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)
Junio C Hamanoba65af92005-09-14 01:43:53 -070029 ;;
30 *)
Junio C Hamano03feddd2005-10-13 18:57:39 -070031 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 --
Junio C Hamano01385e22005-12-16 23:12:33 -080035 # which means that the HEAD contains everything in both.
Junio C Hamano03feddd2005-10-13 18:57:39 -070036 ;;
37 *)
38 echo >&2 "The branch '$branch_name' is not a strict subset of your current HEAD.
Junio C Hamano01385e22005-12-16 23:12:33 -080039If you are sure you want to delete it, run 'git branch -D $branch_name'."
Junio C Hamano03feddd2005-10-13 18:57:39 -070040 exit 1
41 ;;
42 esac
Junio C Hamanoba65af92005-09-14 01:43:53 -070043 ;;
44 esac
Junio C Hamano03feddd2005-10-13 18:57:39 -070045 rm -f "$GIT_DIR/refs/heads/$branch_name"
46 echo "Deleted branch $branch_name."
47 done
Junio C Hamanoba65af92005-09-14 01:43:53 -070048 exit 0
49}
50
Eric Wongfd8fc4a2006-03-02 12:23:17 -080051ls_remote_branches () {
52 git-rev-parse --symbolic --all |
53 sed -ne 's|^refs/\(remotes/\)|\1|p' |
54 sort
55}
56
Junio C Hamanoeb777612005-11-11 11:12:50 -080057force=
Junio C Hamanoba65af92005-09-14 01:43:53 -070058while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
59do
60 case "$1" in
61 -d | -D)
Junio C Hamano03feddd2005-10-13 18:57:39 -070062 delete_branch "$@"
Junio C Hamanoba65af92005-09-14 01:43:53 -070063 exit
64 ;;
Eric Wongfd8fc4a2006-03-02 12:23:17 -080065 -r)
66 ls_remote_branches
67 exit
68 ;;
Junio C Hamanoeb777612005-11-11 11:12:50 -080069 -f)
70 force="$1"
71 ;;
Junio C Hamanoba65af92005-09-14 01:43:53 -070072 --)
73 shift
74 break
75 ;;
76 -*)
77 usage
78 ;;
79 esac
80 shift
81done
82
Kalle Valoe4aec262005-08-16 20:58:10 +030083case "$#" in
840)
Seana62be772006-05-13 21:43:00 -040085 git-rev-parse --symbolic --branches |
Kalle Valoe4aec262005-08-16 20:58:10 +030086 sort |
87 while read ref
88 do
89 if test "$headref" = "$ref"
90 then
91 pfx='*'
92 else
93 pfx=' '
94 fi
95 echo "$pfx $ref"
96 done
97 exit 0 ;;
981)
Junio C Hamanoa38e7272005-07-22 19:08:47 -070099 head=HEAD ;;
Kalle Valoe4aec262005-08-16 20:58:10 +03001002)
Junio C Hamanoa38e7272005-07-22 19:08:47 -0700101 head="$2^0" ;;
102esac
Kalle Valoe4aec262005-08-16 20:58:10 +0300103branchname="$1"
Amos Waterlanda3b427b2005-09-07 21:13:26 -0500104
Junio C Hamanoff84d322005-08-24 14:31:36 -0700105rev=$(git-rev-parse --verify "$head") || exit
Linus Torvalds37f1a512005-07-11 21:30:23 -0700106
Junio C Hamano03feddd2005-10-13 18:57:39 -0700107git-check-ref-format "heads/$branchname" ||
108 die "we do not like '$branchname' as a branch name."
Linus Torvalds37f1a512005-07-11 21:30:23 -0700109
Junio C Hamanoeb777612005-11-11 11:12:50 -0800110if [ -e "$GIT_DIR/refs/heads/$branchname" ]
111then
112 if test '' = "$force"
113 then
114 die "$branchname already exists."
115 elif test "$branchname" = "$headref"
116 then
117 die "cannot force-update the current branch."
118 fi
119fi
Shawn Pearce67644a42006-05-19 05:16:18 -0400120git update-ref -m "branch: Created from $head" "refs/heads/$branchname" $rev