tg.sh: avoid modifying a symbolic link pre-commit hook
[topgit/pro.git] / tg-create.sh
blobf87fde6daed100f764ac871e00c3a3a0493a88dc
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # Copyright (C) Petr Baudis <pasky@suse.cz> 2008
4 # Copyright (C) Kyle J. McKay <mackyle@gmail.com> 2015
5 # All rights reserved.
6 # GPLv2
8 deps= # List of dependent branches
9 restarted= # Set to 1 if we are picking up in the middle of base setup
10 merge= # List of branches to be merged; subset of $deps
11 name=
12 rname= # Remote branch to base this one on
13 remote=
14 force=
15 msg=
16 msgfile=
17 topmsg=
18 topmsgfile=
19 noedit=
20 nocommit=
21 nodeps=
22 continue=
23 topmsg=
24 warntop=
25 quiet=
26 branchtype=PATCH
27 branchdesc=patch
29 USAGE="Usage: ${tgname:-tg} [... -r remote] create [-q] [-m <msg> | -F <file>] [--topmsg <msg> | --topmsg-file <file>] [-n] [--no-commit] [--no-deps] [<name> [<dep>...|-r [<rname>]] ]"
31 usage()
33 if [ "${1:-0}" != 0 ]; then
34 printf '%s\n' "$USAGE" >&2
35 else
36 printf '%s\n' "$USAGE"
38 exit ${1:-0}
41 is_active()
43 [ -d "$git_dir/tg-create" ] || return 1
44 [ -s "$git_dir/tg-create/name" ] || return 1
45 [ -s "$git_dir/tg-create/deps" ] || return 1
46 [ -s "$git_dir/tg-create/merge" ] || return 1
47 [ -s "$git_dir/tg-create/msg" ] || return 1
48 [ -s "$git_dir/tg-create/topmsg" ] || return 1
49 [ -f "$git_dir/tg-create/nocommit" ] || return 1
50 [ -f "$git_dir/tg-create/noedit" ] || return 1
51 [ -f "$git_dir/tg-create/warntop" ] || return 1
52 [ -f "$git_dir/tg-create/quiet" ] || return 1
53 return 0
56 quiet_info()
58 [ -n "$quiet" ] || info "$@"
61 ## Parse options
63 while [ $# -gt 0 ]; do case "$1" in
64 -h|--help)
65 usage
67 --quiet|-q)
68 quiet=1
70 --force|-f)
71 force=1
73 --no-commit)
74 nocommit=1
76 -n|--no-edit)
77 noedit=1
78 nocommit=1
80 --no-deps)
81 nodeps=1
82 branchtype=BASE
83 branchdesc=base
85 --continue)
86 continue=1
88 -m|--message|--message=*)
89 case "$1" in --message=*)
90 x="$1"
91 shift
92 set -- --message "${x#--message=}" "$@"
93 esac
94 if [ $# -lt 2 ]; then
95 echo "The $1 option requires an argument" >&2
96 usage 1
98 shift
99 msg="$1"
101 --tm|--tm=*|--topmsg|--topmsg=*)
102 case "$1" in
103 --tm=*)
104 x="$1"
105 shift
106 set -- --tm "${x#--tm=}" "$@";;
107 --topmsg=*)
108 x="$1"
109 shift
110 set -- --topmsg "${x#--topmsg=}" "$@";;
111 esac
112 if [ $# -lt 2 ]; then
113 echo "The $1 option requires an argument" >&2
114 usage 1
116 shift
117 topmsg="$1"
119 -F|--file|--file=*)
120 case "$1" in --file=*)
121 x="$1"
122 shift
123 set -- --file "${x#--file=}" "$@"
124 esac
125 if [ $# -lt 2 ]; then
126 echo "The $1 option requires an argument" >&2
127 usage 1
129 shift
130 msgfile="$1"
132 --tF|--tF=*|--topmsg-file|--topmsg-file=*)
133 case "$1" in
134 --tF=*)
135 x="$1"
136 shift
137 set -- --tF "${x#--tF=}" "$@";;
138 --topmsg-file=*)
139 x="$1"
140 shift
141 set -- --topmsg-file "${x#--topmsg-file=}" "$@";;
142 esac
143 if [ $# -lt 2 ]; then
144 echo "The $1 option requires an argument" >&2
145 usage 1
147 shift
148 topmsgfile="$1"
151 remote=1
152 rname="$2"; [ $# -eq 0 ] || shift
155 shift
156 break
158 -?*)
159 echo "Unknown option: $1" >&2
160 usage 1
163 break
165 esac; shift; done
166 [ $# -gt 0 -o -z "$rname" ] || set -- "$rname"
167 [ $# -gt 0 -o -n "$remote$msg$msgfile$topmsg$topmsgfile$nocommit$nodeps" ] || continue=1
168 [ -z "$continue" -o "$#$remote$msg$msgfile$topmsg$topmsgfile$nocommit$nodeps" = "0" ] || usage 1
169 if [ -z "$continue" -a $# -gt 0 ]; then
170 name="$1"
171 shift
172 if [ -z "$remote" -a "$1" = "-r" ]; then
173 remote=1
174 shift;
175 rname="$1"
176 [ $# -eq 0 ] || shift
179 [ -n "$continue" -o -n "$name" ] || { err "no branch name given"; usage 1; }
180 [ -z "$remote" -o -n "$rname" ] || rname="$name"
181 [ -z "$remote" -o -z "$msg$msgfile$topmsg$topmsgfile$nocommit$nodeps" ] || { err "-r may not be combined with other options"; usage 1; }
182 [ $# -eq 0 -o -z "$remote" ] || { err "deps not allowed with -r"; usage 1; }
183 [ $# -le 1 -o -z "$nodeps" ] || { err "--no-deps allows at most one <dep>"; usage 1; }
184 [ -z "$msg" -o -z "$msgfile" ] || die "only one -F or -m option is allowed"
185 [ -z "$continue" ] || is_active || die "no tg create is currently active"
186 [ "$msgfile" != "-" -o "$topmsgfile" != "-" ] || { err "--message-file and --topmsg-file may not both be '-'"; usage 1; }
188 ## Fast-track creating branches based on remote ones
190 if [ -n "$rname" ]; then
191 [ -n "$name" ] || die "no branch name given"
192 ! ref_exists "refs/heads/$name" || die "branch '$name' already exists"
193 ! ref_exists "refs/$topbases/$name" || die "'$topbases/$name' already exists"
194 if [ -z "$base_remote" ]; then
195 die "no remote location given. Either use -r remote argument or set topgit.remote"
197 has_remote "$rname" || die "no branch $rname in remote $base_remote"
198 init_reflog "refs/$topases/$name"
199 msg="tgcreate: $name -r $rname"
200 git update-ref -m "$msg" "refs/$topbases/$name" "refs/remotes/$base_remote/$topbases/$rname" ""
201 git update-ref -m "$msg" "refs/heads/$name" "refs/remotes/$base_remote/$rname" ""
202 quiet_info "Topic branch $name based on $base_remote : $rname set up."
203 exit 0
206 ## Auto-guess dependencies
208 deps="$*"
209 if [ -z "$deps" ]; then
210 if [ -z "$name" ] && is_active; then
211 # We are setting up the base branch now; resume merge!
212 name="$(cat "$git_dir/tg-create/name")"
213 deps="$(cat "$git_dir/tg-create/deps")"
214 merge="$(cat "$git_dir/tg-create/merge")"
215 msg="$(cat "$git_dir/tg-create/msg")"
216 topmsg="$(cat "$git_dir/tg-create/topmsg")"
217 nocommit="$(cat "$git_dir/tg-create/nocommit")"
218 noedit="$(cat "$git_dir/tg-create/noedit")"
219 warntop="$(cat "$git_dir/tg-create/warntop")"
220 quiet="$(cat "$git_dir/tg-create/quiet")"
221 restarted=1
222 quiet_info "Resuming $name setup..."
223 else
224 # The common case
225 [ -z "$name" ] && die "no branch name given"
226 if [ -n "$nodeps" ]; then
227 deps="HEAD"
228 else
229 head="$(git symbolic-ref --quiet HEAD)" || :
230 [ -z "$head" ] || git rev-parse --verify --quiet "$head" -- ||
231 die "refusing to auto-depend on unborn branch (use --no-deps)"
232 deps="${head#refs/heads/}"
233 [ "$deps" != "$head" ] || die "refusing to auto-depend on non-branch ref (${head:-detached HEAD})"
234 quiet_info "automatically marking dependency on $deps"
239 unborn=
240 if [ -n "$nodeps" ]; then
241 # there can be only one dep and it need only be a committish
242 # however, if it's HEAD and HEAD is an unborn branch that's okay too
243 if [ "$deps" = "HEAD" ] && unborn="$(git symbolic-ref --quiet HEAD --)" && ! git rev-parse --verify --quiet HEAD -- >/dev/null; then
244 branchtype=ROOT
245 branchdesc=root
246 else
247 unborn=
248 git rev-parse --quiet --verify "$deps^0" -- >/dev/null ||
249 die "unknown committish \"$deps\""
253 # Non-remote branch set up requires a clean tree unless the single dep is the same tree as HEAD
254 [ -n "$restarted" ] || [ "$deps" = "HEAD" ] || {
255 prefix=refs/heads/
256 [ -z "$nodeps" ] || prefix=
257 [ $# -eq 1 -a "$(git rev-parse --quiet --verify "$prefix$deps^{tree}" --)" = "$(git rev-parse --quiet --verify HEAD^{tree} --)" ] ||
258 (ensure_clean_tree) || {
259 [ $# -ne 1 ] || info "use \`git checkout $deps\` first and then try again"
260 exit 1
264 [ -n "$merge" -o -n "$restarted" ] || merge="$deps "
266 if [ -z "$nodeps" ]; then
267 olddeps="$deps"
268 deps=
269 while read d && [ -n "$d" ]; do
270 if [ "$d" = "HEAD" ]; then
271 sr="$(git symbolic-ref --quiet HEAD)" || :
272 [ -z "$sr" ] || git rev-parse --verify --quiet "$sr" -- ||
273 die "refusing to depend on unborn branch (use --no-deps)"
274 [ -n "$sr" ] || die "cannot depend on a detached HEAD"
275 case "$sr" in refs/heads/*);;*)
276 die "HEAD is a symref to other than refs/heads/..."
277 esac
278 d="${sr#refs/heads/}"
279 else
280 ref_exists "refs/heads/$d" || die "unknown branch dependency '$d'"
282 case " $deps " in
283 *" $d "*)
284 warn "ignoring duplicate depedency $d"
287 deps="${deps:+$deps }$d"
289 esac
290 done <<-EOT
291 $(sed 'y/ /\n/' <<-LIST
292 $olddeps
293 LIST
296 unset olddeps
298 if test="$(git symbolic-ref --quiet "$name" --)"; then case "$test" in
299 refs/"$topbases"/*)
300 name="${test#refs/$topbases/}"
301 break;;
302 refs/heads/*)
303 name="${test#refs/heads/}"
304 break;;
305 esac; fi
306 ! ref_exists "refs/heads/$name" ||
307 die "branch '$name' already exists"
308 ! ref_exists "refs/$topbases/$name" ||
309 die "'$topbases/$name' already exists"
310 [ -n "$force" ] || ! ref_exists "refs/tags/$name" ||
311 die "refusing to create branch with same name as existing tag '$name' without --force"
313 # Clean up any stale stuff
314 rm -rf "$git_dir/tg-create"
316 # Barf now rather than later if missing ident
317 ensure_ident_available
319 # Get messages
321 tab="$(printf '\t.')" && tab="${tab%?}"
322 get_subject()
324 sed -n '1,/^$/p' |
325 grep -i "^Subject[ $tab]*:" |
326 sed -n "s/^[^: $tab][^: $tab]*[ $tab]*:[ $tab]*//; s/[ $tab][ $tab]*\$//; 1p" ||
330 if [ -z "$restarted" ]; then
331 >"$git_dir/TG_EDITMSG"
332 if [ -n "$msgfile" ]; then
333 if [ "$msgfile" = "-" ]; then
334 git stripspace >"$git_dir/TG_EDITMSG"
335 else
336 git stripspace <"$msgfile" >"$git_dir/TG_EDITMSG"
338 elif [ -n "$msg" ]; then
339 printf '%s\n' "$msg" | git stripspace >"$git_dir/TG_EDITMSG"
341 if [ ! -s "$git_dir/TG_EDITMSG" ]; then
342 printf '%s\n' "tg create $name" | git stripspace >"$git_dir/TG_EDITMSG"
344 msg="$(cat "$git_dir/TG_EDITMSG")"
345 rm -f "$git_dir/TG_EDITMSG"
347 >"$git_dir/TG_EDITMSG"
348 if [ -n "$topmsgfile" ]; then
349 if [ "$topmsgfile" = "-" ]; then
350 git stripspace >"$git_dir/TG_EDITMSG"
351 else
352 git stripspace <"$topmsgfile" >"$git_dir/TG_EDITMSG"
354 elif [ -n "$topmsg" ]; then
355 printf '%s\n' "$topmsg" | git stripspace >"$git_dir/TG_EDITMSG"
357 if [ -s "$git_dir/TG_EDITMSG" ]; then
358 noedit=1
359 else
360 author="$(git var GIT_AUTHOR_IDENT)"
361 author_addr="${author%> *}>"
363 echo "From: $author_addr"
364 ! header="$(git config topgit.to)" || echo "To: $header"
365 ! header="$(git config topgit.cc)" || echo "Cc: $header"
366 ! header="$(git config topgit.bcc)" || echo "Bcc: $header"
367 ! subject_prefix="$(git config topgit.subjectprefix)" || subject_prefix="$subject_prefix "
368 echo "Subject: [${subject_prefix}$branchtype] $name"
369 echo
370 echo "<$branchdesc description>"
371 echo
372 echo "Signed-off-by: $author_addr"
373 [ "$(git config --bool format.signoff)" = true ] && echo "Signed-off-by: $author_addr"
374 } | git stripspace >"$git_dir/TG_EDITMSG"
376 if [ -z "$noedit" ]; then
377 cat <<EOT >>"$git_dir/TG_EDITMSG"
379 # Please enter the patch message for the new TopGit branch $name.
380 # It will be stored in the .topmsg file and used to create the
381 # patch header when \`tg patch\` is run on branch $name.
382 # The "Subject:" line will appear in \`tg summary\` and \`tg info\` output.
384 # Lines starting with '#' will be ignored, and an empty patch
385 # message aborts the \`tg create\` operation entirely.
387 # tg create ${nodeps:+--no-deps }$name $deps
389 run_editor "$git_dir/TG_EDITMSG" ||
390 die "there was a problem with the editor '$tg_editor'"
391 git stripspace -s <"$git_dir/TG_EDITMSG" >"$git_dir/TG_EDITMSG"+
392 mv -f "$git_dir/TG_EDITMSG"+ "$git_dir/TG_EDITMSG"
393 [ -s "$git_dir/TG_EDITMSG" ] || die "nothing to do"
395 subj="$(get_subject <"$git_dir/TG_EDITMSG")"
396 if [ -z "$subj" ]; then
397 subj="$(sed -n "s/^[ $tab][ $tab]*//; 1p" <"$git_dir/TG_EDITMSG")";
398 case "$subj" in "["*);;*) subj="[$branchtype] $subj"; esac
399 printf '%s\n' "Subject: $subj" "" >"$git_dir/TG_EDITMSG"+
400 sed -n '2,$p' <"$git_dir/TG_EDITMSG" | git stripspace >>"$git_dir/TG_EDITMSG"+
401 mv -f "$git_dir/TG_EDITMSG"+ "$git_dir/TG_EDITMSG"
402 warntop=1
404 topmsg="$(cat "$git_dir/TG_EDITMSG")"
405 rm -f "$git_dir/TG_EDITMSG"
409 ## Find starting commit to create the base
411 if [ -n "$merge" -a -z "$restarted" ]; then
412 # Unshift the first item from the to-merge list
413 branch="${merge%% *}"
414 merge="${merge#* }"
415 # We create a detached head so that we can abort this operation
416 prefix=refs/heads/
417 [ -z "$nodeps" ] || prefix=
418 if [ -n "$unborn" ]; then
419 quiet_info "creating $name base with empty tree..."
420 else
421 quiet_info "creating $name base from $branch..."
422 git checkout -q "$(git rev-parse --verify "$prefix$branch^0" --)"
427 ## Merge other dependencies into the base
429 while [ -n "$merge" ]; do
430 # Unshift the first item from the to-merge list
431 branch="${merge%% *}"
432 merge="${merge#* }"
433 quiet_info "Merging $name base with $branch..."
435 if ! git merge $auhopt -m "tgcreate: merge $branch into $topbases/$name" "$branch^0"; then
436 info "Please commit merge resolution and call: $tgdisplay create"
437 info "It is also safe to abort this operation using:"
438 info "git$gitcdopt reset --hard some_branch"
439 info "(You are on a detached HEAD now.)"
440 mkdir -p "$git_dir/tg-create"
441 printf '%s\n' "$name" >"$git_dir/tg-create/name"
442 printf '%s\n' "$deps" >"$git_dir/tg-create/deps"
443 printf '%s\n' "$merge" >"$git_dir/tg-create/merge"
444 printf '%s\n' "$msg" >"$git_dir/tg-create/msg"
445 printf '%s\n' "$topmsg" >"$git_dir/tg-create/topmsg"
446 printf '%s\n' "$nocommit" >"$git_dir/tg-create/nocommit"
447 printf '%s\n' "$noedit" >"$git_dir/tg-create/noedit"
448 printf '%s\n' "$warntop" >"$git_dir/tg-create/warntop"
449 printf '%s\n' "$quiet" >"$git_dir/tg-create/quiet"
450 exit 2
452 done
455 ## Set up the topic branch
457 init_reflog "refs/$topbases/$name"
458 if [ -n "$unborn" ]; then
459 mttree="$(git mktree </dev/null)"
460 emsg="tg create empty $topbases/$name"
461 [ "refs/heads/$name" = "$unborn" ] || emsg="Initial empty commit"
462 mtcommit="$(git commit-tree -m "$emsg" "$mttree")" || die "git commit-tree failed"
463 git update-ref -m "tgcreate: create ${unborn#refs/heads/}" "HEAD" "$mtcommit" ""
464 [ "refs/heads/$name" = "$unborn" ] || warn "branch ${unborn#refs/heads/} created with empty commit"
465 git update-ref -m "tgcreate: set $topbases/$name" "refs/$topbases/$name" "HEAD" ""
466 [ "refs/heads/$name" = "$unborn" ] || git checkout -b "$name"
467 else
468 git update-ref -m "tgcreate: set $topbases/$name" "refs/$topbases/$name" "HEAD" ""
469 git checkout -b "$name"
472 if [ -n "$nodeps" ] || [ -z "$deps" ]; then
473 >"$root_dir/.topdeps"
474 else
475 sed 'y/ /\n/' <<-EOT >"$root_dir/.topdeps"
476 $deps
479 git add -f "$root_dir/.topdeps"
480 printf '%s\n' "$topmsg" >"$root_dir/.topmsg"
481 git add -f "$root_dir/.topmsg"
482 printf '%s\n' "$msg" >"$git_dir/MERGE_MSG"
484 [ -z "$warntop" ] || warn ".topmsg content was reformatted into patch header"
485 if [ -n "$nocommit" ]; then
486 quiet_info "Topic branch $name set up."
487 if [ -n "$noedit" ]; then
488 quiet_info "Please fill in .topmsg now and make the initial commit."
489 else
490 quiet_info "Please make the initial commit."
492 quiet_info "To abort:"
493 quiet_info " git$gitcdopt rm -f .top* && git$gitcdopt checkout ${deps%% *} && $tgdisplay delete $name"
494 exit 0
497 git commit -m "$msg" "$root_dir/.topdeps" "$root_dir/.topmsg" || die "git commit failed"
498 subj="$(get_subject <"$root_dir/.topmsg" |
499 sed "s/^[^]]*]//; s/^[ $tab][ $tab]*//; s/[ $tab][ $tab]*\$//")"
500 if [ -n "$subj" ]; then
501 printf '%s\n' "$subj" ""
502 sed -e '1,/^$/d' <"$root_dir/.topmsg"
503 else
504 cat "$root_dir/.topmsg"
505 fi >"$git_dir/MERGE_MSG"
506 quiet_info "Topic branch $name created."
507 exit 0
509 # vim:noet