chiark / gitweb /
Let tg-update take a branch parameter
[topgit.git] / tg-depend.sh
index 498eec4e3e0018c0d57c6f24ee788e7e13aaf48c..474ccda4e098a2046a2e60a52d5addad4b007f81 100644 (file)
@@ -6,21 +6,29 @@
 name=
 
 
+usage()
+{
+    echo "Usage: tg [...] depend add NAME" >&2
+    exit 1
+}
+
 ## Parse options
 
-subcmd="$1"; shift
-[ "$subcmd" = "-h" ] && {
-       echo "Usage: tg [...] depend add NAME" >&2
-       exit 1
-}
-[ "$subcmd" = "add" ] || die "unknown subcommand ($subcmd)"
+subcmd="$1"; shift || :
+case "$subcmd" in
+       -h|"")
+               usage;;
+       add)
+               ;;
+       *)
+               die "unknown subcommand ($subcmd)";;
+esac
 
 while [ -n "$1" ]; do
        arg="$1"; shift
        case "$arg" in
        -*)
-               echo "Usage: tg [...] depend add NAME" >&2
-               exit 1;;
+               usage;;
        *)
                [ -z "$name" ] || die "name already specified ($name)"
                name="$arg";;
@@ -33,13 +41,31 @@ done
 [ -n "$name" ] || die "no branch name specified"
 branchrev="$(git rev-parse --verify "$name" 2>/dev/null)" ||
        die "invalid branch name: $name"
-baserev="$(git rev-parse --verify "refs/top-bases/$name" 2>/dev/null)" ||
-       die "not a TopGit topic branch: $name"
 
+# Check that we are on a TopGit branch.
+current_name="$(git symbolic-ref HEAD | sed 's#^refs/\(heads\|top-bases\)/##')"
+current_base_rev="$(git rev-parse --short --verify "refs/top-bases/$current_name" 2>/dev/null)" ||
+       die "not a TopGit-controlled branch"
 
 ## Record new dependency
+depend_add()
+{
+       [ "$name" = "$current_name" ] &&
+               die "$name cannot depend on itself."
+
+       { $tg summary --deps; echo "$current_name" "$name"; } |
+               tsort >/dev/null ||
+               die "tg: that dependency would introduce a dependency loop"
+
+       grep -F -x -e "$name" "$root_dir/.topdeps" >/dev/null &&
+               die "tg: $current_name already depends on $name"
+
+       echo "$name" >>"$root_dir/.topdeps"
+       git add -f "$root_dir/.topdeps"
+       git commit -m"New TopGit dependency: $name"
+       $tg update
+}
+
+depend_$subcmd
 
-echo "$name" >>.topdeps
-git add .topdeps
-git commit -m"New TopGit dependency: $name"
-$tg update
+# vim:noet