tg-delete.sh: Factor out the emptiness test to tg.sh:branch_empty()
[topgit/kirr.git] / tg.sh
blob56c5709f1d8f319e7d356d1b0fe9f0880cf98fed
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
7 ## Auxiliary functions
9 info()
11 echo "${TG_RECURSIVE}tg: $*"
14 die()
16 info "fatal: $*"
17 exit 1
20 # setup_hook NAME
21 setup_hook()
23 hook_call="\"\$(tg --hooks-path)\"/$1 \"\$@\""
24 if fgrep -q "$hook_call" "$git_dir/hooks/$1"; then
25 # Another job well done!
26 return
28 # Prepare incanation
29 if [ -x "$git_dir/hooks/$1" ]; then
30 hook_call="$hook_call"' || exit $?'
31 else
32 hook_call="exec $hook_call"
34 # Insert call into the hook
36 echo "#!/bin/sh"
37 echo "$hook_call"
38 cat "$git_dir/hooks/$1"
39 } >"$git_dir/hooks/$1+"
40 chmod a+x "$git_dir/hooks/$1+"
41 mv "$git_dir/hooks/$1+" "$git_dir/hooks/$1"
44 # setup_ours (no arguments)
45 setup_ours()
47 if [ ! -s "$git_dir/info/gitattributes" ] || ! grep -q topmsg "$git_dir/info/gitattributes"; then
49 echo -e ".topmsg\tmerge=ours"
50 echo -e ".topdeps\tmerge=ours"
51 } >>"$git_dir/info/gitattributes"
53 if ! git config merge.ours.driver >/dev/null; then
54 git config merge.ours.name '"always keep ours" merge driver'
55 git config merge.ours.driver 'touch %A'
59 # measure_branch NAME [BASE]
60 measure_branch()
62 _name="$1"; _base="$2"
63 [ -n "$_base" ] || _base="refs/top-bases/$_name"
64 # The caller should've verified $name is valid
65 _commits="$(git rev-list "$_name" ^"$_base" | wc -l)"
66 _nmcommits="$(git rev-list --no-merges "$_name" ^"$_base" | wc -l)"
67 if [ $_commits -gt 1 ]; then
68 _suffix="commits"
69 else
70 _suffix="commit"
72 echo "$_commits/$_nmcommits $_suffix"
75 # branch_contains B1 B2
76 # Whether B1 is a superset of B2.
77 branch_contains()
79 [ -z "$(git rev-list ^"$1" "$2")" ]
82 # needs_update NAME [BRANCHPATH...]
83 # This function is recursive; it outputs reverse path from NAME
84 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
85 # inner paths first. Innermost name can be ':' if the head is
86 # not in sync with the base.
87 needs_update()
90 git cat-file blob "$1:.topdeps" 2>/dev/null |
91 while read _dep; do
92 _dep_is_tgish=1
93 git rev-parse --verify "refs/top-bases/$_dep" >/dev/null 2>&1 ||
94 _dep_is_tgish=
96 # Shoo shoo, keep our environment alone!
97 [ -z "$_dep_is_tgish" ] || (needs_update "$_dep" "$@")
99 _dep_base_uptodate=1
100 if [ -n "$_dep_is_tgish" ]; then
101 branch_contains "$_dep" "refs/top-bases/$_dep" || _dep_base_uptodate=
104 if [ -z "$_dep_base_uptodate" ]; then
105 # _dep needs to be synced with its base
106 echo ": $_dep $*"
107 elif ! branch_contains "refs/top-bases/$1" "$_dep"; then
108 # Some new commits in _dep
109 echo "$_dep $*"
111 done
112 } || : # $1 is not tracked by TopGit anymore
115 # branch_empty NAME
116 branch_empty()
118 [ -z "$(git diff-tree "refs/top-bases/$1" "$1" | fgrep -v " .top")" ]
121 # switch_to_base NAME [SEED]
122 switch_to_base()
124 _base="refs/top-bases/$1"; _seed="$2"
125 # We have to do all the hard work ourselves :/
126 # This is like git checkout -b "$_base" "$_seed"
127 # (or just git checkout "$_base"),
128 # but does not create a detached HEAD.
129 git read-tree -u -m HEAD "${_seed:-$_base}"
130 [ -z "$_seed" ] || git update-ref "$_base" "$_seed"
131 git symbolic-ref HEAD "$_base"
135 ## Initial setup
137 set -e
138 git_dir="$(git rev-parse --git-dir)"
139 root_dir="$(git rev-parse --show-cdup)"; root_dir="${root_dir:-.}"
140 # make sure merging the .top* files will always behave sanely
141 setup_ours
142 setup_hook "pre-commit"
145 ## Dispatch
147 # We were sourced from another script for our utility functions;
148 # this is set by hooks.
149 [ -z "$tg__include" ] || return 0
151 cmd="$1"
152 [ -n "$cmd" ] || die "He took a duck in the face at two hundred and fifty knots"
153 shift
155 case "$cmd" in
156 help)
157 echo "TopGit - A different patch queue manager"
158 echo "Usage: tg (create|delete|info|patch|summary|update|help) ..."
159 exit 1;;
160 create|delete|info|patch|summary|update)
161 . "@cmddir@"/tg-$cmd;;
162 --hooks-path)
163 # Internal command
164 echo "@hooksdir@";;
166 echo "Unknown subcommand: $cmd" >&2
167 exit 1;;
168 esac