X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=hooks%2Fpre-commit.sh;h=4f2f16f33cf10cbf9dc5da3eeea5ef272521d9f2;hb=8b0f1f9d215d767488542a7853320d1789838d92;hp=8ac913f6479d86af2a64e2dea1e0aff39f347cd4;hpb=1fb84a0250902fdf93bb0de335625975bd69734b;p=topgit.git diff --git a/hooks/pre-commit.sh b/hooks/pre-commit.sh index 8ac913f..4f2f16f 100644 --- a/hooks/pre-commit.sh +++ b/hooks/pre-commit.sh @@ -20,7 +20,8 @@ tg_util if head_=$(git symbolic-ref -q HEAD); then case "$head_" in refs/heads/*) - git rev-parse -q --verify "refs/top-bases${head_#refs/heads}" >/dev/null || exit 0;; + head_="${head_#refs/heads/}" + git rev-parse -q --verify "refs/top-bases/$head_" >/dev/null || exit 0;; *) exit 0;; esac @@ -61,4 +62,46 @@ tree=$(git write-tree) || check_topfile "$tree" ".topdeps" check_topfile "$tree" ".topmsg" -# TODO: Verify .topdeps for valid branch names and against cycles +check_cycle_name() +{ + [ "$head_" != "$_dep" ] || + die "TopGit dependencies form a cycle: perpetrator is $_name" +} + +# we only need to check newly added deps and for these if a path exists to the +# current HEAD +git diff --cached "$root_dir/.topdeps" | + awk ' +BEGIN { in_hunk = 0; } +/^@@ / { in_hunk = 1; } +/^\+/ { if (in_hunk == 1) printf("%s\n", substr($0, 2)); } +/^[^@ +-]/ { in_hunk = 0; } +' | + while read newly_added; do + ref_exists "$newly_added" || + die "Invalid branch as dependent: $newly_added" + + # check for self as dep + [ "$head_" != "$newly_added" ] || + die "Can't have myself as dep" + + # deps can be non-tgish but we can't run recurse_deps() on them + ref_exists "refs/top-bases/$newly_added" || + continue + + # recurse_deps uses dfs but takes the .topdeps from the tree, + # therefore no endless loop in the cycle-check + no_remotes=1 recurse_deps check_cycle_name "$newly_added" + done + +# check for repetitions of deps +depdir="$(mktemp -t -d tg-depdir.XXXXXX)" || + die "Can't check for multiple occurrences of deps" +trap "rm -rf '$depdir'" 0 +cat_file "(i):.topdeps" | + while read dep; do + [ ! -d "$depdir/$dep" ] || + die "Multiple occurrences of the same dep: $dep" + mkdir -p "$depdir/$dep" || + die "Can't check for multiple occurrences of deps" + done