tg-revert.sh: convert any top-bases in TOPGIT REFS
[topgit/pro.git] / tg-depend.sh
blob5880253f817c75901f03572863c0a08f5e67bc77
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 USAGE="Usage: ${tgname:-tg} [...] depend add [--no-update | --no-commit] <name>"
10 name=
12 usage()
14 printf '%s\n' "$USAGE" >&2
15 exit 1
18 ## Parse options
20 subcmd="$1"
21 case "$subcmd" in
22 -h|--help|"")
23 usage;;
24 add)
27 die "unknown subcommand ($subcmd)";;
28 esac
29 shift
31 noupdate=
32 nocommit=
33 while [ -n "$1" ]; do
34 arg="$1"; shift
35 case "$arg" in
36 --no-update)
37 noupdate=1;;
38 --no-commit)
39 nocommit=1;;
40 -*)
41 usage;;
43 [ -z "$name" ] || die "name already specified ($name)"
44 name="$arg";;
45 esac
46 done
49 ## Sanity checks
51 [ -n "$name" ] || die "no branch name specified"
52 branchrev="$(git rev-parse --verify "refs/heads/$name" -- 2>/dev/null)" ||
53 die "invalid branch name: $name"
55 # Check that we are on a TopGit branch.
56 current_name="$(verify_topgit_branch HEAD)"
58 ## Record new dependency
59 depend_add()
61 [ "$name" = "$current_name" ] &&
62 die "$name cannot depend on itself."
64 { $tg summary --deps; echo "$current_name" "$name"; } |
65 tsort >/dev/null ||
66 die "$tgname: that dependency would introduce a dependency loop"
68 grep -F -x -e "$name" "$root_dir/.topdeps" >/dev/null &&
69 die "$tgname: $current_name already depends on $name"
71 [ -n "$nocommit" ] || ensure_ident_available
72 echol "$name" >>"$root_dir/.topdeps"
73 git add -f "$root_dir/.topdeps"
74 msg=".topdeps: add new dependency $name"
75 [ -z "$nocommit" ] || {
76 [ -s "$git_dir/MERGE_MSG" ] || printf '%s\n' "$msg" >"$git_dir/MERGE_MSG"
77 info "added new dependency $name to .topdeps and staged it"
78 info "run \`git commit\` then \`tg update\` to complete addition"
79 exit 0
81 git commit -m "$msg" "$root_dir/.topdeps"
82 [ -z "$noupdate" ] || {
83 info "be sure to run \`tg update\` at some point"
84 exit 0
86 (ensure_clean_tree) || {
87 warn "skipping needed \`tg update\` since worktree is dirty"
88 warn "be sure to run \`tg update\` when worktree is clean"
89 exit 1
91 $tg update
94 depend_$subcmd
96 # vim:noet