]> git.ipfire.org Git - thirdparty/git.git/blame - git-branch.sh
Remove "refs" field from "struct object"
[thirdparty/git.git] / git-branch.sh
CommitLineData
37f1a519
LT
1#!/bin/sh
2
3a4b3f26 3USAGE='[-l] [(-d | -D) <branchname>] | [[-f] <branchname> [<start-point>]] | -r'
d025524d 4LONG_USAGE='If no arguments, show available branches and mark current branch with a star.
a3b427b9 5If one argument, create a new branch <branchname> based off of current HEAD.
d025524d
FK
6If two arguments, create a new branch <branchname> based off of <start-point>.'
7
8SUBDIRECTORY_OK='Yes'
9. git-sh-setup
a3b427b9 10
9cc2527c 11headref=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||')
eb777612 12
ba65af9c 13delete_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 39If 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
3a4b3f26 45 rm -f "$GIT_DIR/logs/refs/heads/$branch_name"
03feddd6
JH
46 rm -f "$GIT_DIR/refs/heads/$branch_name"
47 echo "Deleted branch $branch_name."
48 done
ba65af9c
JH
49 exit 0
50}
51
fd8fc4ad
EW
52ls_remote_branches () {
53 git-rev-parse --symbolic --all |
54 sed -ne 's|^refs/\(remotes/\)|\1|p' |
55 sort
56}
57
eb777612 58force=
3a4b3f26 59create_log=
ba65af9c
JH
60while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
61do
62 case "$1" in
63 -d | -D)
03feddd6 64 delete_branch "$@"
ba65af9c
JH
65 exit
66 ;;
fd8fc4ad
EW
67 -r)
68 ls_remote_branches
69 exit
70 ;;
eb777612
JH
71 -f)
72 force="$1"
73 ;;
3a4b3f26
SP
74 -l)
75 create_log="yes"
76 ;;
ba65af9c
JH
77 --)
78 shift
79 break
80 ;;
81 -*)
82 usage
83 ;;
84 esac
85 shift
86done
87
e4aec26f
KV
88case "$#" in
890)
a62be77f 90 git-rev-parse --symbolic --branches |
e4aec26f
KV
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 ;;
1031)
a38e7279 104 head=HEAD ;;
e4aec26f 1052)
a38e7279
JH
106 head="$2^0" ;;
107esac
e4aec26f 108branchname="$1"
a3b427b9 109
ff84d327 110rev=$(git-rev-parse --verify "$head") || exit
37f1a519 111
03feddd6
JH
112git-check-ref-format "heads/$branchname" ||
113 die "we do not like '$branchname' as a branch name."
37f1a519 114
eb777612
JH
115if [ -e "$GIT_DIR/refs/heads/$branchname" ]
116then
117 if test '' = "$force"
118 then
119 die "$branchname already exists."
120 elif test "$branchname" = "$headref"
121 then
122 die "cannot force-update the current branch."
123 fi
124fi
3a4b3f26
SP
125if test "$create_log" = 'yes'
126then
127 mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$branchname")
128 touch "$GIT_DIR/logs/refs/heads/$branchname"
129fi
67644a4d 130git update-ref -m "branch: Created from $head" "refs/heads/$branchname" $rev