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
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
12 rname
= # Remote branch to base this one on
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>]] ]"
33 if [ "${1:-0}" != 0 ]; then
34 printf '%s\n' "$USAGE" >&2
36 printf '%s\n' "$USAGE"
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
58 [ -n "$quiet" ] || info
"$@"
63 while [ $# -gt 0 ]; do case "$1" in
88 -m|
--message|
--message=*)
89 case "$1" in --message=*)
92 set -- --message "${x#--message=}" "$@"
95 echo "The $1 option requires an argument" >&2
101 --tm|
--tm=*|
--topmsg|
--topmsg=*)
106 set -- --tm "${x#--tm=}" "$@";;
110 set -- --topmsg "${x#--topmsg=}" "$@";;
112 if [ $# -lt 2 ]; then
113 echo "The $1 option requires an argument" >&2
120 case "$1" in --file=*)
123 set -- --file "${x#--file=}" "$@"
125 if [ $# -lt 2 ]; then
126 echo "The $1 option requires an argument" >&2
132 --tF|
--tF=*|
--topmsg-file|
--topmsg-file=*)
137 set -- --tF "${x#--tF=}" "$@";;
141 set -- --topmsg-file "${x#--topmsg-file=}" "$@";;
143 if [ $# -lt 2 ]; then
144 echo "The $1 option requires an argument" >&2
152 rname
="$1"; [ $# -eq 0 ] ||
shift
159 echo "Unknown option: $1" >&2
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 [ -n "$continue" -o $# -eq 0 ] ||
{ name
="$1"; shift; }
170 [ -n "$continue" -o -n "$name" ] ||
{ err
"no branch name given"; usage
1; }
171 [ -z "$remote" -o -n "$rname" ] || rname
="$name"
172 [ -z "$remote" -o -z "$msg$msgfile$topmsg$topmsgfile$nocommit$nodeps" ] ||
{ err
"-r may not be combined with other options"; usage
1; }
173 [ $# -eq 0 -o -z "$remote" ] ||
{ err
"deps not allowed with -r"; usage
1; }
174 [ $# -le 1 -o -z "$nodeps" ] ||
{ err
"--no-deps allows at most one <dep>"; usage
1; }
175 [ -z "$msg" -o -z "$msgfile" ] || die
"only one -F or -m option is allowed"
176 [ -z "$continue" ] || is_active || die
"no tg create is currently active"
177 [ "$msgfile" != "-" -o "$topmsgfile" != "-" ] ||
{ err
"--message-file and --topmsg-file may not both be '-'"; usage
1; }
179 ## Fast-track creating branches based on remote ones
181 if [ -n "$rname" ]; then
182 [ -n "$name" ] || die
"no branch name given"
183 ! ref_exists
"refs/heads/$name" || die
"branch '$name' already exists"
184 ! ref_exists
"refs/top-bases/$name" || die
"'top-bases/$name' already exists"
185 if [ -z "$base_remote" ]; then
186 die
"no remote location given. Either use -r remote argument or set topgit.remote"
188 has_remote
"$rname" || die
"no branch $rname in remote $base_remote"
190 if [ -n "$logrefupdates" ]; then
191 mkdir
-p "$git_dir/logs/refs/top-bases/$(dirname "$name")" 2>/dev
/null ||
:
192 { >>"$git_dir/logs/refs/top-bases/$name" ||
:; } 2>/dev
/null
194 msg
="tgcreate: $name -r $rname"
195 git update-ref
-m "$msg" "refs/top-bases/$name" "refs/remotes/$base_remote/top-bases/$rname" ""
196 git update-ref
-m "$msg" "refs/heads/$name" "refs/remotes/$base_remote/$rname" ""
197 quiet_info
"Topic branch $name based on $base_remote : $rname set up."
201 ## Auto-guess dependencies
204 if [ -z "$deps" ]; then
205 if [ -z "$name" ] && is_active
; then
206 # We are setting up the base branch now; resume merge!
207 name
="$(cat "$git_dir/tg-create
/name
")"
208 deps
="$(cat "$git_dir/tg-create
/deps
")"
209 merge
="$(cat "$git_dir/tg-create
/merge
")"
210 msg
="$(cat "$git_dir/tg-create
/msg
")"
211 topmsg
="$(cat "$git_dir/tg-create
/topmsg
")"
212 nocommit
="$(cat "$git_dir/tg-create
/nocommit
")"
213 noedit
="$(cat "$git_dir/tg-create
/noedit
")"
214 warntop
="$(cat "$git_dir/tg-create
/warntop
")"
215 quiet
="$(cat "$git_dir/tg-create
/quiet
")"
217 quiet_info
"Resuming $name setup..."
220 [ -z "$name" ] && die
"no branch name given"
221 if [ -n "$nodeps" ]; then
224 head="$(git symbolic-ref --quiet HEAD || :)"
225 deps
="${head#refs/heads/}"
226 [ "$deps" != "$head" ] || die
"refusing to auto-depend on non-branch ref (${head:-detached HEAD})"
227 quiet_info
"Automatically marking dependency on $deps"
232 # Non-remote branch set up requires a clean tree unless the single dep is the same tree as HEAD
233 [ -n "$restarted" ] ||
[ "$deps" = "HEAD" ] ||
{
235 [ -z "$nodeps" ] || prefix
=
236 [ $# -eq 1 -a "$(git rev-parse --verify "$prefix$deps^
{tree
}" --)" = "$(git rev-parse --verify HEAD^{tree} --)" ] ||
237 (ensure_clean_tree
) ||
{
238 [ $# -ne 1 ] || info
"use \`git checkout $deps\` first and then try again"
243 [ -n "$merge" -o -n "$restarted" ] || merge
="$deps "
245 if [ -n "$nodeps" ]; then
246 # there can be only one and it need only be a committish
247 git rev-parse
--quiet --verify "$deps^0" -- >/dev
/null ||
248 die
"unknown committish \"$deps\""
252 for d
in $olddeps; do
253 if [ "$d" = "HEAD" ]; then
254 sr
="$(git symbolic-ref --quiet HEAD || :)"
255 [ -n "$sr" ] || die
"cannot depend on a detached HEAD"
256 case "$sr" in refs
/heads
/*) :;; *)
257 die
"HEAD is a symref to other than refs/heads/..."
259 d
="${sr#refs/heads/}"
261 ref_exists
"refs/heads/$d" || die
"unknown branch dependency '$d'"
265 warn
"ignoring duplicate depedency $d"
268 deps
="${deps:+$deps }$d"
274 ! ref_exists
"refs/heads/$name" ||
275 die
"branch '$name' already exists"
276 ! ref_exists
"refs/top-bases/$name" ||
277 die
"'top-bases/$name' already exists"
278 [ -n "$force" ] ||
! ref_exists
"refs/tags/$name" ||
279 die
"refusing to create branch with same name as existing tag '$name' without --force"
281 # Clean up any stale stuff
282 rm -rf "$git_dir/tg-create"
287 tab
="$(printf '\t.')" && tab
="${tab%?}"
291 grep -i "^Subject[ $tab]*:" |
292 sed -n "s/^[^: $tab][^: $tab]*[ $tab]*:[ $tab]*//; s/[ $tab][ $tab]*\$//; 1p" ||
296 if [ -z "$restarted" ]; then
297 >"$git_dir/TG_EDITMSG"
298 if [ -n "$msgfile" ]; then
299 if [ "$msgfile" = "-" ]; then
300 git stripspace
>"$git_dir/TG_EDITMSG"
302 git stripspace
<"$msgfile" >"$git_dir/TG_EDITMSG"
304 elif [ -n "$msg" ]; then
305 printf '%s\n' "$msg" | git stripspace
>"$git_dir/TG_EDITMSG"
307 if [ ! -s "$git_dir/TG_EDITMSG" ]; then
308 printf '%s\n' "tg create $name" | git stripspace
>"$git_dir/TG_EDITMSG"
310 msg
="$(cat "$git_dir/TG_EDITMSG
")"
311 rm -f "$git_dir/TG_EDITMSG"
313 >"$git_dir/TG_EDITMSG"
314 if [ -n "$topmsgfile" ]; then
315 if [ "$topmsgfile" = "-" ]; then
316 git stripspace
>"$git_dir/TG_EDITMSG"
318 git stripspace
<"$topmsgfile" >"$git_dir/TG_EDITMSG"
320 elif [ -n "$topmsg" ]; then
321 printf '%s\n' "$topmsg" | git stripspace
>"$git_dir/TG_EDITMSG"
323 if [ -s "$git_dir/TG_EDITMSG" ]; then
326 author
="$(git var GIT_AUTHOR_IDENT)"
327 author_addr
="${author%> *}>"
329 echo "From: $author_addr"
330 ! header
="$(git config topgit.to)" ||
echo "To: $header"
331 ! header
="$(git config topgit.cc)" ||
echo "Cc: $header"
332 ! header
="$(git config topgit.bcc)" ||
echo "Bcc: $header"
333 ! subject_prefix
="$(git config topgit.subjectprefix)" || subject_prefix
="$subject_prefix "
334 echo "Subject: [${subject_prefix}$branchtype] $name"
336 echo "<$branchdesc description>"
338 echo "Signed-off-by: $author_addr"
339 [ "$(git config --bool format.signoff)" = true
] && echo "Signed-off-by: $author_addr"
340 } | git stripspace
>"$git_dir/TG_EDITMSG"
342 if [ -z "$noedit" ]; then
343 cat <<EOT >>"$git_dir/TG_EDITMSG"
345 # Please enter the patch message for the new TopGit branch $name.
346 # It will be stored in the .topmsg file and used to create the
347 # patch header when \`tg patch\` is run on branch $name.
348 # The "Subject:" line will appear in \`tg summary\` and \`tg info\` output.
350 # Lines starting with '#' will be ignored, and an empty patch
351 # message aborts the \`tg create\` operation entirely.
353 # tg create ${nodeps:+--no-deps }$name $deps
355 run_editor
"$git_dir/TG_EDITMSG" || \
356 die
"there was a problem with the editor '$tg_editor'"
357 git stripspace
-s <"$git_dir/TG_EDITMSG" >"$git_dir/TG_EDITMSG"+
358 mv -f "$git_dir/TG_EDITMSG"+ "$git_dir/TG_EDITMSG"
359 [ -s "$git_dir/TG_EDITMSG" ] || die
"nothing to do"
361 subj
="$(get_subject <"$git_dir/TG_EDITMSG
")"
362 if [ -z "$subj" ]; then
363 subj
="$(sed -n "s
/^
[ $tab][ $tab]*//; 1p
" <"$git_dir/TG_EDITMSG
")";
364 case "$subj" in "["*) :;; *) subj
="[$branchtype] $subj"; esac
365 printf '%s\n' "Subject: $subj" "" >"$git_dir/TG_EDITMSG"+
366 sed -n '2,$p' <"$git_dir/TG_EDITMSG" | git stripspace
>>"$git_dir/TG_EDITMSG"+
367 mv -f "$git_dir/TG_EDITMSG"+ "$git_dir/TG_EDITMSG"
370 topmsg
="$(cat "$git_dir/TG_EDITMSG
")"
371 rm -f "$git_dir/TG_EDITMSG"
375 ## Find starting commit to create the base
377 if [ -n "$merge" -a -z "$restarted" ]; then
378 # Unshift the first item from the to-merge list
379 branch
="${merge%% *}"
381 quiet_info
"Creating $name base from $branch..."
382 # We create a detached head so that we can abort this operation
384 [ -z "$nodeps" ] || prefix
=
385 git checkout
-q "$(git rev-parse --verify "$prefix$branch^
0" --)"
389 ## Merge other dependencies into the base
391 while [ -n "$merge" ]; do
392 # Unshift the first item from the to-merge list
393 branch
="${merge%% *}"
395 quiet_info
"Merging $name base with $branch..."
397 if ! git merge
-m "tgcreate: merge $branch into top-bases/$name" "$branch^0"; then
398 info
"Please commit merge resolution and call: $tgdisplay create"
399 info
"It is also safe to abort this operation using:"
400 info
"git$gitcdopt reset --hard some_branch"
401 info
"(You are on a detached HEAD now.)"
402 mkdir
-p "$git_dir/tg-create"
403 printf '%s\n' "$name" >"$git_dir/tg-create/name"
404 printf '%s\n' "$deps" >"$git_dir/tg-create/deps"
405 printf '%s\n' "$merge" >"$git_dir/tg-create/merge"
406 printf '%s\n' "$msg" >"$git_dir/tg-create/msg"
407 printf '%s\n' "$topmsg" >"$git_dir/tg-create/topmsg"
408 printf '%s\n' "$nocommit" >"$git_dir/tg-create/nocommit"
409 printf '%s\n' "$noedit" >"$git_dir/tg-create/noedit"
410 printf '%s\n' "$warntop" >"$git_dir/tg-create/warntop"
411 printf '%s\n' "$quiet" >"$git_dir/tg-create/quiet"
417 ## Set up the topic branch
419 if [ -n "$logrefupdates" ]; then
420 mkdir
-p "$git_dir/logs/refs/top-bases/$(dirname "$name")" 2>/dev
/null ||
:
421 { >>"$git_dir/logs/refs/top-bases/$name" ||
:; } 2>/dev
/null
423 git update-ref
"refs/top-bases/$name" "HEAD" ""
424 git checkout
-b "$name"
426 if [ -n "$nodeps" ]; then
427 >"$root_dir/.topdeps"
429 printf '%s\n' $deps >"$root_dir/.topdeps"
431 git add
-f "$root_dir/.topdeps"
432 printf '%s\n' "$topmsg" >"$root_dir/.topmsg"
433 git add
-f "$root_dir/.topmsg"
434 printf '%s\n' "$msg" >"$git_dir/MERGE_MSG"
436 [ -z "$warntop" ] || warn
".topmsg content was reformatted into patch header"
437 if [ -n "$nocommit" ]; then
438 quiet_info
"Topic branch $name set up."
439 if [ -n "$noedit" ]; then
440 quiet_info
"Please fill in .topmsg now and make the initial commit."
442 quiet_info
"Please make the initial commit."
444 quiet_info
"To abort:"
445 quiet_info
" git$gitcdopt rm -f .top* && git$gitcdopt checkout ${deps%% *} && $tgdisplay delete $name"
449 git commit
-m "$msg" "$root_dir/.topdeps" "$root_dir/.topmsg" || die
"git commit failed"
450 subj
="$(get_subject <"$root_dir/.topmsg
" |
451 sed "s
/^
[^
]]*]//; s
/^
[ $tab][ $tab]*//; s
/[ $tab][ $tab]*\$
//")"
452 if [ -n "$subj" ]; then
453 printf '%s\n' "$subj" ""
454 sed -e '1,/^$/d' <"$root_dir/.topmsg"
456 cat "$root_dir/.topmsg"
457 fi >"$git_dir/MERGE_MSG"
458 quiet_info
"Topic branch $name created."