tg-annihilate.sh: autostash and support --stash and --no-stash
[topgit/pro.git] / tg-create.sh
blob960ddbaf374135c590363570ebc2e41acf4822a8
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,2016,2017
5 # All rights reserved.
6 # GPLv2
8 deps= # List of dependent branches
9 merge= # List of branches to be merged; subset of $deps
10 name=
11 rname= # Remote branch to base this one on
12 remote=
13 force=
14 msg=
15 msgfile=
16 topmsg=
17 topmsgfile=
18 noedit=
19 nocommit=
20 nodeps=
21 topmsg=
22 warntop=
23 quiet=
24 branchtype=PATCH
25 branchdesc=patch
27 USAGE="Usage: ${tgname:-tg} [... -r remote] create [-q] [-m <msg> | -F <file>] [--topmsg <msg> | --topmsg-file <file>] [-n] [--no-commit] [--base] [<name> [<dep>...|-r [<rname>]] ]"
29 usage()
31 if [ "${1:-0}" != 0 ]; then
32 printf '%s\n' "$USAGE" >&2
33 else
34 printf '%s\n' "$USAGE"
36 exit ${1:-0}
39 quiet_info()
41 [ -n "$quiet" ] || info "$@"
44 ## Parse options
46 while [ $# -gt 0 ]; do case "$1" in
47 -h|--help)
48 usage
50 --quiet|-q)
51 quiet=1
53 --force|-f)
54 force=1
56 --no-commit)
57 nocommit=1
59 -n|--no-edit)
60 noedit=1
61 nocommit=1
63 --no-deps|--base)
64 nodeps=1
65 branchtype=BASE
66 branchdesc=base
68 -m|--message|--message=*)
69 case "$1" in --message=*)
70 x="$1"
71 shift
72 set -- --message "${x#--message=}" "$@"
73 esac
74 if [ $# -lt 2 ]; then
75 echo "The $1 option requires an argument" >&2
76 usage 1
78 shift
79 msg="$1"
81 --tm|--tm=*|--topmsg|--topmsg=*)
82 case "$1" in
83 --tm=*)
84 x="$1"
85 shift
86 set -- --tm "${x#--tm=}" "$@";;
87 --topmsg=*)
88 x="$1"
89 shift
90 set -- --topmsg "${x#--topmsg=}" "$@";;
91 esac
92 if [ $# -lt 2 ]; then
93 echo "The $1 option requires an argument" >&2
94 usage 1
96 shift
97 topmsg="$1"
99 -F|--file|--file=*)
100 case "$1" in --file=*)
101 x="$1"
102 shift
103 set -- --file "${x#--file=}" "$@"
104 esac
105 if [ $# -lt 2 ]; then
106 echo "The $1 option requires an argument" >&2
107 usage 1
109 shift
110 msgfile="$1"
112 --tF|--tF=*|--topmsg-file|--topmsg-file=*)
113 case "$1" in
114 --tF=*)
115 x="$1"
116 shift
117 set -- --tF "${x#--tF=}" "$@";;
118 --topmsg-file=*)
119 x="$1"
120 shift
121 set -- --topmsg-file "${x#--topmsg-file=}" "$@";;
122 esac
123 if [ $# -lt 2 ]; then
124 echo "The $1 option requires an argument" >&2
125 usage 1
127 shift
128 topmsgfile="$1"
131 remote=1
132 rname="$2"; [ $# -eq 0 ] || shift
135 shift
136 break
138 -?*)
139 echo "Unknown option: $1" >&2
140 usage 1
143 break
145 esac; shift; done
146 [ $# -gt 0 -o -z "$rname" ] || set -- "$rname"
147 if [ $# -gt 0 ]; then
148 name="$1"
149 shift
150 if [ -z "$remote" -a "$1" = "-r" ]; then
151 remote=1
152 shift;
153 rname="$1"
154 [ $# -eq 0 ] || shift
157 [ -n "$name" ] || { err "no branch name given"; usage 1; }
158 [ -z "$remote" -o -n "$rname" ] || rname="$name"
159 [ -z "$remote" -o -z "$msg$msgfile$topmsg$topmsgfile$nocommit$nodeps" ] || { err "-r may not be combined with other options"; usage 1; }
160 [ $# -eq 0 -o -z "$remote" ] || { err "deps not allowed with -r"; usage 1; }
161 [ $# -le 1 -o -z "$nodeps" ] || { err "--base (aka --no-deps) allows at most one <dep>"; usage 1; }
162 [ -z "$msg" -o -z "$msgfile" ] || die "only one -F or -m option is allowed"
163 [ "$msgfile" != "-" -o "$topmsgfile" != "-" ] || { err "--message-file and --topmsg-file may not both be '-'"; usage 1; }
165 ## Fast-track creating branches based on remote ones
167 if [ -n "$rname" ]; then
168 [ -n "$name" ] || die "no branch name given"
169 ! ref_exists "refs/heads/$name" || die "branch '$name' already exists"
170 ! ref_exists "refs/$topbases/$name" || die "'$topbases/$name' already exists"
171 if [ -z "$base_remote" ]; then
172 die "no remote location given. Either use -r remote argument or set topgit.remote"
174 has_remote "$rname" || die "no branch $rname in remote $base_remote"
175 init_reflog "refs/$topases/$name"
176 msg="tgcreate: $name -r $rname"
177 tbrv="$(ref_exists_rev "refs/remotes/$base_remote/${topbases#heads/}/$rname")" ||
178 tbrv="$(ref_exists_rev "refs/remotes/$base_remote/${oldbases#heads/}/$rname")" ||
179 die "no branch $rname in remote $base_remote"
180 git update-ref -m "$msg" "refs/$topbases/$name" "$tbrv" ""
181 git update-ref -m "$msg" "refs/heads/$name" "refs/remotes/$base_remote/$rname" ""
182 quiet_info "Topic branch $name based on $base_remote : $rname set up."
183 exit 0
186 ## Auto-guess dependencies
188 [ "$name" != "@" ] || name="HEAD"
189 deps="$*"
190 [ "$deps" != "@" ] || deps="HEAD"
191 if [ -z "$deps" ]; then
192 # The common case
193 [ -n "$name" ] || die "no branch name given"
194 if [ -n "$nodeps" ]; then
195 deps="HEAD"
196 else
197 head="$(git symbolic-ref --quiet HEAD)" || :
198 [ -z "$head" ] || git rev-parse --verify --quiet "$head" -- ||
199 die "refusing to auto-depend on unborn branch (use --base aka --no-deps)"
200 deps="${head#refs/heads/}"
201 [ "$deps" != "$head" ] || die "refusing to auto-depend on non-branch ref (${head:-detached HEAD})"
202 quiet_info "automatically marking dependency on $deps"
206 unborn=
207 if [ -n "$nodeps" ]; then
208 # there can be only one dep and it need only be a committish
209 # however, if it's HEAD and HEAD is an unborn branch that's okay too
210 if [ "$deps" = "HEAD" ] && unborn="$(git symbolic-ref --quiet HEAD --)" && ! git rev-parse --verify --quiet HEAD -- >/dev/null; then
211 branchtype=ROOT
212 branchdesc=root
213 else
214 unborn=
215 git rev-parse --quiet --verify "$deps^0" -- >/dev/null ||
216 die "unknown committish \"$deps\""
220 # Non-remote branch set up requires a clean tree unless the single dep is the same tree as a not unborn HEAD
221 # Also the .topdeps and .topmsg files, if they exist, may not be overwriten unless they are "clean"
223 prefix=refs/heads/
224 [ -z "$nodeps" ] || prefix=
225 ensure_cmd=ensure_clean_tree
226 if [ -n "$unborn" ]; then
227 ensure_cmd=:
228 elif [ $# -eq 1 ] && { [ "$deps" = "HEAD" ] ||
229 [ "$(git rev-parse --quiet --verify "$prefix$deps^{tree}" --)" = "$(git rev-parse --quiet --verify HEAD^{tree} --)" ]; }; then
230 ensure_cmd=:
232 ($ensure_cmd && ensure_clean_topfiles ${unborn:+-u}) || {
233 [ $# -ne 1 ] || [ "$deps" = "HEAD" ] || info "use \`git checkout $deps\` first and then try again"
234 exit 1
237 [ -n "$merge" ] || merge="$deps "
239 if [ -z "$nodeps" ]; then
240 olddeps="$deps"
241 deps=
242 while read d && [ -n "$d" ]; do
243 [ "$d" != "@" ] || d="HEAD"
244 if [ "$d" = "HEAD" ]; then
245 sr="$(git symbolic-ref --quiet HEAD)" || :
246 [ -z "$sr" ] || git rev-parse --verify --quiet "$sr" -- ||
247 die "refusing to depend on unborn branch (use --base aka --no-deps)"
248 [ -n "$sr" ] || die "cannot depend on a detached HEAD"
249 case "$sr" in refs/heads/*);;*)
250 die "HEAD is a symref to other than refs/heads/..."
251 esac
252 d="${sr#refs/heads/}"
253 else
254 ref_exists "refs/heads/$d" || die "unknown branch dependency '$d'"
256 case " $deps " in
257 *" $d "*)
258 warn "ignoring duplicate depedency $d"
261 deps="${deps:+$deps }$d"
263 esac
264 done <<-EOT
265 $(sed 'y/ /\n/' <<-LIST
266 $olddeps
267 LIST
270 unset olddeps
272 if test="$(git symbolic-ref --quiet "$name" --)"; then case "$test" in
273 refs/"$topbases"/*)
274 name="${test#refs/$topbases/}"
275 break;;
276 refs/heads/*)
277 name="${test#refs/heads/}"
278 break;;
279 esac; fi
280 ! ref_exists "refs/heads/$name" ||
281 die "branch '$name' already exists"
282 ! ref_exists "refs/$topbases/$name" ||
283 die "'$topbases/$name' already exists"
284 [ -n "$force" ] || ! ref_exists "refs/tags/$name" ||
285 die "refusing to create branch with same name as existing tag '$name' without --force"
287 # Barf now rather than later if missing ident
288 ensure_ident_available
290 if [ -n "$merge" ] && [ -z "$unborn" ]; then
291 # make sure the checkout won't fail
292 branch="${merge%% *}"
293 prefix=refs/heads/
294 [ -z "$nodeps" ] || prefix=
295 git rev-parse --quiet --verify "$prefix$branch^0" >/dev/null ||
296 die "invalid dependency: $branch"
297 git read-tree -n -u -m "$prefix$branch^0" ||
298 die "git checkout \"$branch\" would fail"
301 # Get messages
303 tab="$(printf '\t.')" && tab="${tab%?}"
304 get_subject()
306 sed -n '1,/^$/p' |
307 grep -i "^Subject[ $tab]*:" |
308 sed -n "s/^[^: $tab][^: $tab]*[ $tab]*:[ $tab]*//; s/[ $tab][ $tab]*\$//; 1p" ||
312 >"$git_dir/TG_EDITMSG"
313 if [ -n "$msgfile" ]; then
314 if [ "$msgfile" = "-" ]; then
315 git stripspace >"$git_dir/TG_EDITMSG"
316 else
317 git stripspace <"$msgfile" >"$git_dir/TG_EDITMSG"
319 elif [ -n "$msg" ]; then
320 printf '%s\n' "$msg" | git stripspace >"$git_dir/TG_EDITMSG"
322 if [ ! -s "$git_dir/TG_EDITMSG" ]; then
323 printf '%s\n' "tg create $name" | git stripspace >"$git_dir/TG_EDITMSG"
325 msg="$(cat "$git_dir/TG_EDITMSG")"
326 rm -f "$git_dir/TG_EDITMSG"
328 >"$git_dir/TG_EDITMSG"
329 if [ -n "$topmsgfile" ]; then
330 if [ "$topmsgfile" = "-" ]; then
331 git stripspace >"$git_dir/TG_EDITMSG"
332 else
333 git stripspace <"$topmsgfile" >"$git_dir/TG_EDITMSG"
335 elif [ -n "$topmsg" ]; then
336 printf '%s\n' "$topmsg" | git stripspace >"$git_dir/TG_EDITMSG"
338 if [ -s "$git_dir/TG_EDITMSG" ]; then
339 noedit=1
340 else
341 author="$(git var GIT_AUTHOR_IDENT)"
342 author_addr="${author%> *}>"
344 echo "From: $author_addr"
345 ! header="$(git config topgit.to)" || echo "To: $header"
346 ! header="$(git config topgit.cc)" || echo "Cc: $header"
347 ! header="$(git config topgit.bcc)" || echo "Bcc: $header"
348 ! subject_prefix="$(git config topgit.subjectprefix)" || subject_prefix="$subject_prefix "
349 echo "Subject: [${subject_prefix}$branchtype] $name"
350 echo
351 echo "<$branchdesc description>"
352 echo
353 echo "Signed-off-by: $author_addr"
354 [ "$(git config --bool format.signoff)" = true ] && echo "Signed-off-by: $author_addr"
355 } | git stripspace >"$git_dir/TG_EDITMSG"
357 if [ -z "$noedit" ]; then
358 cat <<EOT >>"$git_dir/TG_EDITMSG"
360 # Please enter the patch message for the new TopGit branch $name.
361 # It will be stored in the .topmsg file and used to create the
362 # patch header when \`tg patch\` is run on branch $name.
363 # The "Subject:" line will appear in \`tg summary\` and \`tg info\` output.
365 # Lines starting with '#' will be ignored, and an empty patch
366 # message aborts the \`tg create\` operation entirely.
368 # tg create ${nodeps:+--base }$name $deps
370 run_editor "$git_dir/TG_EDITMSG" ||
371 die "there was a problem with the editor '$tg_editor'"
372 git stripspace -s <"$git_dir/TG_EDITMSG" >"$git_dir/TG_EDITMSG"+
373 mv -f "$git_dir/TG_EDITMSG"+ "$git_dir/TG_EDITMSG"
374 [ -s "$git_dir/TG_EDITMSG" ] || die "nothing to do"
376 subj="$(get_subject <"$git_dir/TG_EDITMSG")"
377 if [ -z "$subj" ]; then
378 subj="$(sed -n "s/^[ $tab][ $tab]*//; 1p" <"$git_dir/TG_EDITMSG")";
379 case "$subj" in "["*);;*) subj="[$branchtype] $subj"; esac
380 printf '%s\n' "Subject: $subj" "" >"$git_dir/TG_EDITMSG"+
381 sed -n '2,$p' <"$git_dir/TG_EDITMSG" | git stripspace >>"$git_dir/TG_EDITMSG"+
382 mv -f "$git_dir/TG_EDITMSG"+ "$git_dir/TG_EDITMSG"
383 warntop=1
385 topmsg="$(cat "$git_dir/TG_EDITMSG")"
386 rm -f "$git_dir/TG_EDITMSG"
388 ## Find starting commit to create the base
390 if [ -n "$merge" ]; then
391 # Unshift the first item from the to-merge list
392 branch="${merge%% *}"
393 merge="${merge#* }"
394 # We create a detached head so that we can abort this operation
395 prefix=refs/heads/
396 [ -z "$nodeps" ] || prefix=
397 if [ -n "$unborn" ]; then
398 quiet_info "creating $name base with empty tree..."
399 else
400 quiet_info "creating $name base from $branch..."
401 git checkout -q $iowopt "$(git rev-parse --verify "$prefix$branch^0" --)"
405 ## Set up the topic branch
407 git update-index --index-info <<EOT || die "git update-index failed"
408 0 $nullsha$tab.topdeps
409 0 $nullsha$tab.topmsg
411 rm -rf "$root_dir/.topdeps" "$root_dir/.topmsg"
412 init_reflog "refs/$topbases/$name"
413 if [ -n "$unborn" ]; then
414 mttree="$(git mktree </dev/null)"
415 emsg="tg create empty $name base"
416 [ "refs/heads/$name" = "$unborn" ] || emsg="Initial empty commit"
417 mtcommit="$(git commit-tree -m "$emsg" "$mttree")" || die "git commit-tree failed"
418 git update-ref -m "tgcreate: create ${unborn#refs/heads/}" "HEAD" "$mtcommit" ""
419 [ "refs/heads/$name" = "$unborn" ] || warn "branch ${unborn#refs/heads/} created with empty commit"
420 git update-ref -m "tgcreate: set $name base" "refs/$topbases/$name" "HEAD" ""
421 [ "refs/heads/$name" = "$unborn" ] || git checkout $iowopt -b "$name"
422 else
423 basetree="$(git rev-parse --verify "HEAD^{tree}" --)" && [ -n "$basetree" ] || die "HEAD disappeared"
424 baseptree="$(pretty_tree "HEAD" -r)" || die "pretty_tree HEAD -r (via git mktree) failed"
425 if [ "$basetree" != "$baseptree" ]; then
426 bmsg="tg create $name base"
427 basecommit="$(git commit-tree -p "HEAD" -m "$bmsg" "$baseptree")" || die "git commit-tree failed"
428 else
429 basecommit="HEAD"
431 git update-ref -m "tgcreate: set $name base" "refs/$topbases/$name" "$basecommit" ""
432 [ "$basecommit" = "HEAD" ] || git update-ref -m "tgcreate: set $name base" "HEAD" "$basecommit"
433 git checkout $iowopt -b "$name"
436 if [ -n "$nodeps" ] || [ -z "$deps" ]; then
437 >"$root_dir/.topdeps"
438 else
439 sed 'y/ /\n/' <<-EOT >"$root_dir/.topdeps"
440 $deps
443 git add -f "$root_dir/.topdeps"
444 printf '%s\n' "$topmsg" >"$root_dir/.topmsg"
445 git add -f "$root_dir/.topmsg"
446 rm -f "$git_dir/TGMERGE_MSG"
448 [ -z "$warntop" ] || warn ".topmsg content was reformatted into patch header"
449 if [ -n "$nocommit" ]; then
450 printf '%s\n' "$msg" >"$git_dir/MERGE_MSG"
451 quiet_info "Topic branch $name set up."
452 if [ -n "$noedit" ]; then
453 quiet_info "Please fill in .topmsg now and make the initial commit."
454 else
455 quiet_info "Please make the initial commit."
457 quiet_info "Remember to run $tgdisplay update afterwards."
458 quiet_info "To abort:"
459 quiet_info " git$gitcdopt rm -f .top* && git$gitcdopt checkout ${deps%% *} && $tgdisplay delete $name"
460 exit 0
463 git commit -m "$msg" "$root_dir/.topdeps" "$root_dir/.topmsg" || die "git commit failed"
464 rawsubj="$(get_subject <"$root_dir/.topmsg")"
465 nommsg=1
466 case "$rawsubj" in *"["[Pp][Aa][Tt][Cc][Hh]"]"*)
467 nommsg=
468 subj="$(sed "s/^[^]]*]//; s/^[ $tab][ $tab]*//; s/[ $tab][ $tab]*\$//" <<-EOT
469 $rawsubj
473 [ -z "$subj" ] || printf '%s\n' "$subj" ""
474 sed -e '1,/^$/d' <"$root_dir/.topmsg"
475 } >"$git_dir/MERGE_MSG"
476 esac
477 quiet_info "Topic branch $name created."
478 [ -n "$merge" ] || exit 0
479 ## Merge other dependencies into the base
480 quiet_info "Running $tgname update to merge in dependencies."
481 [ -n "$nommsg" ] || ! [ -f "$git_dir/MERGE_MSG" ] || mv -f "$git_dir/MERGE_MSG" "$git_dir/TGMERGE_MSG" || :
482 set -- "$name"
483 . "$TG_INST_CMDDIR"/tg-update