t1060: test mergesetup configuration and broken --git-common-dir
[topgit/pro.git] / hooks / pre-commit.sh
blob92ace80cce1384e3ce1efeae9db28576baa86ce3
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 ## Set up all the tg machinery
10 set -e
11 tg__include=1
12 tg_util() {
13 . "@bindir@"/tg
15 tg_util
18 ## Generally have fun
20 # Don't do anything on a non-TopGit branch
21 if head_=$(git symbolic-ref -q HEAD); then
22 case "$head_" in
23 refs/heads/*)
24 head_="${head_#refs/heads/}"
25 git rev-parse -q --verify "refs/$topbases/$head_" -- >/dev/null || exit 0;;
27 exit 0;;
28 esac
30 else
31 exit 0;
34 check_topfile()
36 _tree=$1
37 _file=$2
38 _zerook="$3"
40 _ls_line="$(git ls-tree --long "$_tree" "$_file")" ||
41 die "cannot ls tree for $_file"
43 [ -n "$_ls_line" ] ||
44 die "$_file is missing"
46 # check for type and size
47 set -- $_ls_line
48 _type=$2
49 _size=$4
51 # check file is of type blob (file)
52 [ "x$_type" = "xblob" ] ||
53 die "$_file is not a file (i.e. not a 'blob')"
55 # check for positive size
56 [ -n "$_zerook" -o "$_size" -gt 0 ] ||
57 die "$_file has empty size"
60 tree=$(git write-tree) ||
61 die "cannot write tree"
63 check_topfile "$tree" ".topdeps" 1
64 check_topfile "$tree" ".topmsg"
66 # Don't do anything more if neither .topdeps nor .topmsg is changing
67 changedeps=
68 changemsg=
69 mode=modify
70 headrev="$(git rev-parse --quiet --verify HEAD -- || :)"
71 tab="$(printf '\t.')" && tab="${tab%?}"
72 prefix="[A-Z][0-9]*$tab"
73 if [ -n "$headrev" ]; then
74 headtree="$headrev^{tree}"
75 else
76 headtree="$(: | git mktree)"
78 while read -r status fn; do case "$fn" in
79 ".topdeps")
80 changedeps=1
81 case "$status" in "A"*) mode=create; esac
83 ".topmsg")
84 case "$status" in "A"*) mode=create; esac
85 changemsg=1
87 esac; done <<-EOT
88 $(git diff-index --cached --name-status "$headtree" | LC_ALL=C grep -e "^$prefix\\.topdeps\$" -e "^$prefix\\.topmsg\$")
89 EOT
90 [ -n "$changedeps" -o -n "$changemsg" ] || exit 0
92 check_cycle_name()
94 [ "$head_" != "$_dep" ] ||
95 die "TopGit dependencies form a cycle: perpetrator is $_name"
98 check_topdeps()
100 # we only need to check newly added deps and for these if a path exists to the
101 # current HEAD
102 git diff --cached "$root_dir/.topdeps" |
103 awk '
104 BEGIN { in_hunk = 0; }
105 /^@@ / { in_hunk = 1; }
106 /^\+/ { if (in_hunk == 1) printf("%s\n", substr($0, 2)); }
107 /^[^@ +-]/ { in_hunk = 0; }
109 while read newly_added; do
110 ref_exists "refs/heads/$newly_added" ||
111 die "invalid branch as dependent: $newly_added"
113 # check for self as dep
114 [ "$head_" != "$newly_added" ] ||
115 die "cannot have myself as dependent"
117 # deps can be non-tgish but we can't run recurse_deps() on them
118 ref_exists "refs/$topbases/$newly_added" ||
119 continue
121 # recurse_deps uses dfs but takes the .topdeps from the tree,
122 # therefore no endless loop in the cycle-check
123 no_remotes=1 recurse_deps check_cycle_name "$newly_added"
124 done
125 test $? -eq 0
127 # check for repetitions of deps
128 depdir="$(get_temp tg-depdir -d)" ||
129 die "cannot check for multiple occurrences of dependents"
130 cat_file "$head_:.topdeps" -i |
131 while read dep; do
132 [ ! -d "$depdir/$dep" ] ||
133 die "multiple occurrences of the same dependent: $dep"
134 mkdir -p "$depdir/$dep" ||
135 die "cannot check for multiple occurrences of dependents"
136 done
137 test $? -eq 0
140 # Only check .topdeps if it's been changed otherwise the assumption is it's been checked
141 [ -z "$changedeps" ] || check_topdeps
143 # If we are not sequestering TopGit files or the commit is changing only TopGit files we're done
144 [ -z "$tgnosequester" ] || exit 0
145 [ $(( ${changedeps:-0} + ${changemsg:-0} )) -ne $(git diff-index --cached --name-only "$headtree" | wc -l) ] || exit 0
147 # Sequester the TopGit-specific file changes into their own commit and notify the user we did so
148 tg_index="$git_dir/tg-index"
149 if [ -n "$headrev" ]; then
150 GIT_INDEX_FILE="$tg_index" git read-tree "$headrev^{tree}"
151 else
152 GIT_INDEX_FILE="$tg_index" git read-tree --empty
154 prefix="100[0-9][0-9][0-9] $octet20 0$tab"
156 printf '%s\n' "0 $nullsha$tab.topdeps"
157 printf '%s\n' "0 $nullsha$tab.topmsg"
158 git ls-files --cached -s --full-name | LC_ALL=C grep -e "^$prefix\\.topdeps\$" -e "^$prefix\\.topmsg\$"
159 } | GIT_INDEX_FILE="$tg_index" git update-index --index-info
160 newtree="$(GIT_INDEX_FILE="$tg_index" git write-tree)"
161 rm -f "$tg_index"
162 files=
163 if [ -n "$changedeps" -a -n "$changemsg" ]; then
164 files=".topdeps and .topmsg"
165 elif [ -n "$changedeps" ]; then
166 files=".topdeps"
167 else
168 files=".topmsg"
170 newcommit="$(git commit-tree -m "tg: $mode $files" ${headrev:+-p $headrev} "$newtree")"
171 git update-ref -m "tg: sequester $files changes into their own preliminary commit" HEAD "$newcommit"
172 warn "sequestered $files changes into their own preliminary commit"
173 info "run the same \`git commit\` command again to commit the remaining changes" >&2
174 exit 1