-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtg-depend.sh
134 lines (120 loc) · 2.95 KB
/
tg-depend.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/sh
# TopGit - A different patch queue manager
# Copyright (C) Petr Baudis <[email protected]> 2008
# Copyright (C) Kyle J. McKay <[email protected]> 2015
# All rights reserved.
# GPLv2
USAGE="Usage: ${tgname:-tg} [...] depend add [--no-update | --no-commit] <name>..."
names=
usage()
{
printf '%s\n' "$USAGE" >&2
exit 1
}
## Parse options
subcmd="$1"
case "$subcmd" in
-h|--help|"")
usage;;
add)
;;
*)
die "unknown subcommand ($subcmd)";;
esac
shift
ensure_work_tree
noupdate=
nocommit=
while [ -n "$1" ]; do
arg="$1"; shift
case "$arg" in
--no-update)
noupdate=1;;
--no-commit)
nocommit=1;;
-*)
usage;;
*)
names="${names:+$names }$arg";;
esac
done
## Sanity checks
[ -n "$names" ] || die "no branch name specified"
oldnames="$names "
names=
while
name="${oldnames%% *}"
oldnames="${oldnames#* }"
[ -n "$name" ]
do
case " $names " in *" $name "*) continue; esac
git rev-parse --quiet --verify "refs/heads/$name" -- >/dev/null ||
die "invalid branch name: $name"
names="${names:+$names }$name"
done
unset oldnames
# Check that we are on a TopGit branch.
v_verify_topgit_branch current_name HEAD
check_cycle_name()
{
[ "$current_name" != "$_dep" ] ||
die "$tgname: that dependency ($newly_added) would introduce a dependency loop"
}
check_new_dep()
{
[ "$1" != "$current_name" ] ||
die "$current_name cannot depend on itself."
! grep -F -q -x -e "$1" "$root_dir/.topdeps" ||
die "$tgname: $current_name already depends on $1"
# deps can be non-tgish but we can't run recurse_deps() on them
ref_exists "refs/$topbases/$1" || return 0
no_remotes=1
newly_added="$1"
recurse_deps check_cycle_name "$newly_added"
}
## Record new dependency
depend_add()
{
[ -z "$(git status --porcelain -- :/.topdeps)" ] ||
die ".topdeps has uncommitted changes"
# We are "read-only" and cacheable until the first change
tg_read_only=1
v_create_ref_cache
for name in $names; do
check_new_dep "$name"
done
[ -n "$nocommit" ] || ensure_ident_available
newdepsfile="$(get_temp newdeps)"
! [ -e "$root_dir/.topdeps" ] || awk '{print}' <"$root_dir/.topdeps" >"$newdepsfile"
for name in $names; do
echol "$name" >>"$newdepsfile"
done
cat "$newdepsfile" >"$root_dir/.topdeps"
git add -f "$root_dir/.topdeps"
case "$names" in
*" "*)
msg=".topdeps: add dependencies: $names";;
*)
msg=".topdeps: add new dependency $name";;
esac
[ -z "$nocommit" ] || {
[ -s "$git_dir/MERGE_MSG" ] || printf '%s\n' "$msg" >"$git_dir/MERGE_MSG"
info "updated .topdeps and staged the change"
info "run \`git commit\` then \`tg update\` to complete addition"
exit 0
}
become_non_cacheable
git commit -m "$msg" "$root_dir/.topdeps"
[ -z "$noupdate" ] || {
info "be sure to run \`tg update\` at some point"
exit 0
}
(ensure_clean_tree) || {
warn "skipping needed \`tg update\` since working directory is dirty"
warn "be sure to run \`tg update\` when working directory is clean"
exit 1
}
set -- "$current_name"
. "$TG_INST_CMDDIR"/tg-update
}
depend_$subcmd