6dbe82cb3c211cbf82ba3c722d9f748064cc6960
[topgit.git] / hooks / pre-commit.sh
blob6dbe82cb3c211cbf82ba3c722d9f748064cc6960
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
7 ## Set up all the tg machinery
9 set -e
10 tg__include=1
11 tg_util() {
12 . "@bindir@"/tg
14 tg_util
17 ## Generally have fun
19 # Don't do anything on non-topgit branch
20 if head_=$(git symbolic-ref -q HEAD); then
21 case "$head_" in
22 refs/heads/*)
23 head_="${head_#refs/heads/}"
24 git rev-parse -q --verify "refs/top-bases/$head_" >/dev/null || exit 0;;
26 exit 0;;
27 esac
29 else
30 exit 0;
33 check_topfile()
35 local tree file ls_line type size
36 tree=$1
37 file=$2
39 ls_line="$(git ls-tree --long "$tree" "$file")" ||
40 die "Can't ls tree for $file"
42 [ -n "$ls_line" ] ||
43 die "$file is missing"
45 # check for type and size
46 set -- $ls_line
47 type=$2
48 size=$4
50 # check file is of type blob (file)
51 [ "x$type" = "xblob" ] ||
52 die "$file is not a file"
54 # check for positive size
55 [ "$size" -gt 0 ] ||
56 die "$file has empty size"
59 tree=$(git write-tree) ||
60 die "Can't write tree"
62 check_topfile "$tree" ".topdeps"
63 check_topfile "$tree" ".topmsg"
65 check_cycle_name()
67 [ "$head_" != "$_dep" ] ||
68 die "TopGit dependencies form a cycle: perpetrator is $_name"
71 # we only need to check newly added deps and for these if a path exists to the
72 # current HEAD
73 git diff --cached "$root_dir/.topdeps" |
74 awk '
75 BEGIN { in_hunk = 0; }
76 /^@@ / { in_hunk = 1; }
77 /^\+/ { if (in_hunk == 1) printf("%s\n", substr($0, 2)); }
78 /^[^@ +-]/ { in_hunk = 0; }
79 ' |
80 while read newly_added; do
81 ref_exists "$newly_added" ||
82 die "Invalid branch as dependent: $newly_added"
84 # check for self as dep
85 [ "$head_" != "$newly_added" ] ||
86 die "Can't have myself as dep"
88 # deps can be non-tgish but we can't run recurse_deps() on them
89 ref_exists "refs/top-bases/$newly_added" ||
90 continue
92 # recurse_deps uses dfs but takes the .topdeps from the tree,
93 # therefore no endless loop in the cycle-check
94 no_remotes=1 recurse_deps check_cycle_name "$newly_added"
95 done