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
8 deps
= # List of dependent branches
9 merge
= # List of branches to be merged; subset of $deps
11 rname
= # Remote branch to base this one on
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>]] ]"
31 if [ "${1:-0}" != 0 ]; then
32 printf '%s\n' "$USAGE" >&2
34 printf '%s\n' "$USAGE"
41 [ -n "$quiet" ] || info
"$@"
46 while [ $# -gt 0 ]; do case "$1" in
68 -m|
--message|
--message=*)
69 case "$1" in --message=*)
72 set -- --message "${x#--message=}" "$@"
75 echo "The $1 option requires an argument" >&2
81 --tm|
--tm=*|
--topmsg|
--topmsg=*)
86 set -- --tm "${x#--tm=}" "$@";;
90 set -- --topmsg "${x#--topmsg=}" "$@";;
93 echo "The $1 option requires an argument" >&2
100 case "$1" in --file=*)
103 set -- --file "${x#--file=}" "$@"
105 if [ $# -lt 2 ]; then
106 echo "The $1 option requires an argument" >&2
112 --tF|
--tF=*|
--topmsg-file|
--topmsg-file=*)
117 set -- --tF "${x#--tF=}" "$@";;
121 set -- --topmsg-file "${x#--topmsg-file=}" "$@";;
123 if [ $# -lt 2 ]; then
124 echo "The $1 option requires an argument" >&2
132 rname
="$2"; [ $# -eq 0 ] ||
shift
139 echo "Unknown option: $1" >&2
146 [ $# -gt 0 -o -z "$rname" ] ||
set -- "$rname"
147 if [ $# -gt 0 ]; then
150 if [ -z "$remote" -a "$1" = "-r" ]; then
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."
186 ## Auto-guess dependencies
188 [ "$name" != "@" ] || name
="HEAD"
190 [ "$deps" != "@" ] || deps
="HEAD"
191 if [ -z "$deps" ]; then
193 [ -z "$name" ] && die
"no branch name given"
194 if [ -n "$nodeps" ]; then
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"
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
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 HEAD
222 [ "$deps" = "HEAD" ] ||
{
224 [ -z "$nodeps" ] || prefix
=
225 [ $# -eq 1 -a "$(git rev-parse --quiet --verify "$prefix$deps^
{tree
}" --)" = "$(git rev-parse --quiet --verify HEAD^{tree} --)" ] ||
226 (ensure_clean_tree
) ||
{
227 [ $# -ne 1 ] || info
"use \`git checkout $deps\` first and then try again"
232 [ -n "$merge" ] || merge
="$deps "
234 if [ -z "$nodeps" ]; then
237 while read d
&& [ -n "$d" ]; do
238 [ "$d" != "@" ] || d
="HEAD"
239 if [ "$d" = "HEAD" ]; then
240 sr
="$(git symbolic-ref --quiet HEAD)" ||
:
241 [ -z "$sr" ] || git rev-parse
--verify --quiet "$sr" -- ||
242 die
"refusing to depend on unborn branch (use --base aka --no-deps)"
243 [ -n "$sr" ] || die
"cannot depend on a detached HEAD"
244 case "$sr" in refs
/heads
/*);;*)
245 die
"HEAD is a symref to other than refs/heads/..."
247 d
="${sr#refs/heads/}"
249 ref_exists
"refs/heads/$d" || die
"unknown branch dependency '$d'"
253 warn
"ignoring duplicate depedency $d"
256 deps
="${deps:+$deps }$d"
260 $(sed 'y/ /\n/' <<-LIST
267 if test="$(git symbolic-ref --quiet "$name" --)"; then case "$test" in
269 name
="${test#refs/$topbases/}"
272 name
="${test#refs/heads/}"
275 ! ref_exists
"refs/heads/$name" ||
276 die
"branch '$name' already exists"
277 ! ref_exists
"refs/$topbases/$name" ||
278 die
"'$topbases/$name' already exists"
279 [ -n "$force" ] ||
! ref_exists
"refs/tags/$name" ||
280 die
"refusing to create branch with same name as existing tag '$name' without --force"
282 # Barf now rather than later if missing ident
283 ensure_ident_available
285 if [ -n "$merge" ] && [ -z "$unborn" ]; then
286 # make sure the checkout won't fail
287 branch
="${merge%% *}"
289 [ -z "$nodeps" ] || prefix
=
290 git rev-parse
--quiet --verify "$prefix$branch^0" >/dev
/null ||
291 die
"invalid dependency: $branch"
292 git read-tree
-n -u -m "$prefix$branch^0" ||
293 die
"git checkout \"$branch\" would fail"
298 tab
="$(printf '\t.')" && tab
="${tab%?}"
302 grep -i "^Subject[ $tab]*:" |
303 sed -n "s/^[^: $tab][^: $tab]*[ $tab]*:[ $tab]*//; s/[ $tab][ $tab]*\$//; 1p" ||
307 >"$git_dir/TG_EDITMSG"
308 if [ -n "$msgfile" ]; then
309 if [ "$msgfile" = "-" ]; then
310 git stripspace
>"$git_dir/TG_EDITMSG"
312 git stripspace
<"$msgfile" >"$git_dir/TG_EDITMSG"
314 elif [ -n "$msg" ]; then
315 printf '%s\n' "$msg" | git stripspace
>"$git_dir/TG_EDITMSG"
317 if [ ! -s "$git_dir/TG_EDITMSG" ]; then
318 printf '%s\n' "tg create $name" | git stripspace
>"$git_dir/TG_EDITMSG"
320 msg
="$(cat "$git_dir/TG_EDITMSG
")"
321 rm -f "$git_dir/TG_EDITMSG"
323 >"$git_dir/TG_EDITMSG"
324 if [ -n "$topmsgfile" ]; then
325 if [ "$topmsgfile" = "-" ]; then
326 git stripspace
>"$git_dir/TG_EDITMSG"
328 git stripspace
<"$topmsgfile" >"$git_dir/TG_EDITMSG"
330 elif [ -n "$topmsg" ]; then
331 printf '%s\n' "$topmsg" | git stripspace
>"$git_dir/TG_EDITMSG"
333 if [ -s "$git_dir/TG_EDITMSG" ]; then
336 author
="$(git var GIT_AUTHOR_IDENT)"
337 author_addr
="${author%> *}>"
339 echo "From: $author_addr"
340 ! header
="$(git config topgit.to)" ||
echo "To: $header"
341 ! header
="$(git config topgit.cc)" ||
echo "Cc: $header"
342 ! header
="$(git config topgit.bcc)" ||
echo "Bcc: $header"
343 ! subject_prefix
="$(git config topgit.subjectprefix)" || subject_prefix
="$subject_prefix "
344 echo "Subject: [${subject_prefix}$branchtype] $name"
346 echo "<$branchdesc description>"
348 echo "Signed-off-by: $author_addr"
349 [ "$(git config --bool format.signoff)" = true
] && echo "Signed-off-by: $author_addr"
350 } | git stripspace
>"$git_dir/TG_EDITMSG"
352 if [ -z "$noedit" ]; then
353 cat <<EOT >>"$git_dir/TG_EDITMSG"
355 # Please enter the patch message for the new TopGit branch $name.
356 # It will be stored in the .topmsg file and used to create the
357 # patch header when \`tg patch\` is run on branch $name.
358 # The "Subject:" line will appear in \`tg summary\` and \`tg info\` output.
360 # Lines starting with '#' will be ignored, and an empty patch
361 # message aborts the \`tg create\` operation entirely.
363 # tg create ${nodeps:+--base }$name $deps
365 run_editor
"$git_dir/TG_EDITMSG" ||
366 die
"there was a problem with the editor '$tg_editor'"
367 git stripspace
-s <"$git_dir/TG_EDITMSG" >"$git_dir/TG_EDITMSG"+
368 mv -f "$git_dir/TG_EDITMSG"+ "$git_dir/TG_EDITMSG"
369 [ -s "$git_dir/TG_EDITMSG" ] || die
"nothing to do"
371 subj
="$(get_subject <"$git_dir/TG_EDITMSG
")"
372 if [ -z "$subj" ]; then
373 subj
="$(sed -n "s
/^
[ $tab][ $tab]*//; 1p
" <"$git_dir/TG_EDITMSG
")";
374 case "$subj" in "["*);;*) subj
="[$branchtype] $subj"; esac
375 printf '%s\n' "Subject: $subj" "" >"$git_dir/TG_EDITMSG"+
376 sed -n '2,$p' <"$git_dir/TG_EDITMSG" | git stripspace
>>"$git_dir/TG_EDITMSG"+
377 mv -f "$git_dir/TG_EDITMSG"+ "$git_dir/TG_EDITMSG"
380 topmsg
="$(cat "$git_dir/TG_EDITMSG
")"
381 rm -f "$git_dir/TG_EDITMSG"
383 ## Find starting commit to create the base
385 if [ -n "$merge" ]; then
386 # Unshift the first item from the to-merge list
387 branch
="${merge%% *}"
389 # We create a detached head so that we can abort this operation
391 [ -z "$nodeps" ] || prefix
=
392 if [ -n "$unborn" ]; then
393 quiet_info
"creating $name base with empty tree..."
395 quiet_info
"creating $name base from $branch..."
396 git checkout
-q $iowopt "$(git rev-parse --verify "$prefix$branch^
0" --)"
400 ## Set up the topic branch
402 init_reflog
"refs/$topbases/$name"
403 if [ -n "$unborn" ]; then
404 mttree
="$(git mktree </dev/null)"
405 emsg
="tg create empty $topbases/$name"
406 [ "refs/heads/$name" = "$unborn" ] || emsg
="Initial empty commit"
407 mtcommit
="$(git commit-tree -m "$emsg" "$mttree")" || die
"git commit-tree failed"
408 git update-ref
-m "tgcreate: create ${unborn#refs/heads/}" "HEAD" "$mtcommit" ""
409 [ "refs/heads/$name" = "$unborn" ] || warn
"branch ${unborn#refs/heads/} created with empty commit"
410 git update-ref
-m "tgcreate: set $topbases/$name base" "refs/$topbases/$name" "HEAD" ""
411 [ "refs/heads/$name" = "$unborn" ] || git checkout
$iowopt -b "$name"
413 basetree
="$(git rev-parse --verify "HEAD^
{tree
}" --)" && [ -n "$basetree" ] || die
"HEAD disappeared"
414 baseptree
="$(pretty_tree "HEAD
" -r)" || die
"pretty_tree HEAD -r (via git mktree) failed"
415 if [ "$basetree" != "$baseptree" ]; then
416 bmsg
="tg create $topbases/$name base"
417 basecommit
="$(git commit-tree -p "HEAD
" -m "$bmsg" "$baseptree")" || die
"git commit-tree failed"
421 git update-ref
-m "tgcreate: set $topbases/$name base" "refs/$topbases/$name" "$basecommit" ""
422 git checkout
$iowopt -b "$name"
425 if [ -n "$nodeps" ] ||
[ -z "$deps" ]; then
426 >"$root_dir/.topdeps"
428 sed 'y/ /\n/' <<-EOT >"$root_dir/.topdeps"
432 git add
-f "$root_dir/.topdeps"
433 printf '%s\n' "$topmsg" >"$root_dir/.topmsg"
434 git add
-f "$root_dir/.topmsg"
435 rm -f "$git_dir/TGMERGE_MSG"
437 [ -z "$warntop" ] || warn
".topmsg content was reformatted into patch header"
438 if [ -n "$nocommit" ]; then
439 printf '%s\n' "$msg" >"$git_dir/MERGE_MSG"
440 quiet_info
"Topic branch $name set up."
441 if [ -n "$noedit" ]; then
442 quiet_info
"Please fill in .topmsg now and make the initial commit."
444 quiet_info
"Please make the initial commit."
446 quiet_info
"Remember to run $tgdisplay update afterwards."
447 quiet_info
"To abort:"
448 quiet_info
" git$gitcdopt rm -f .top* && git$gitcdopt checkout ${deps%% *} && $tgdisplay delete $name"
452 git commit
-m "$msg" "$root_dir/.topdeps" "$root_dir/.topmsg" || die
"git commit failed"
453 rawsubj
="$(get_subject <"$root_dir/.topmsg
")"
455 case "$rawsubj" in *"["[Pp
][Aa
][Tt
][Cc
][Hh
]"]"*)
457 subj
="$(sed "s
/^
[^
]]*]//; s
/^
[ $tab][ $tab]*//; s
/[ $tab][ $tab]*\$
//" <<-EOT
462 [ -z "$subj" ] ||
printf '%s\n' "$subj" ""
463 sed -e '1,/^$/d' <"$root_dir/.topmsg"
464 } >"$git_dir/MERGE_MSG"
466 quiet_info
"Topic branch $name created."
467 [ -n "$merge" ] ||
exit 0
468 ## Merge other dependencies into the base
469 quiet_info
"Running $tgname update to merge in dependencies."
470 [ -n "$nommsg" ] ||
! [ -f "$git_dir/MERGE_MSG" ] ||
mv -f "$git_dir/MERGE_MSG" "$git_dir/TGMERGE_MSG" ||
:
472 .
"$TG_INST_CMDDIR"/tg-update