Move some comments around.
[git-central.git] / scripts / gc-tattoo
1 #!/bin/sh
2
3 #
4 # Some docs
5 #
6 # A --- B --- C    stable
7 # |\         /
8 # | D ----- E      topic1
9 #  \       /
10 #   F --- G        topic2
11 #
12 # topic1 stable result
13 #  D      A      contains, merge base=A
14 #  D      C      contains, merge base=A
15 #  E      A      contains, merge base=E
16 #  E      C      contains, merge base=A
17
18 #
19 # * --- * --------- *   stable
20 # |\     \
21 # | A --- B ---- C      topic1
22 # \             /
23 #  D --- E --- F        topic2
24 #
25 # F: contains=topic1,topic2
26 # C: contains=topic1
27
28 head=$(git rev-parse HEAD)
29
30 # Watch out for the very first commit in the repo because we use head^
31 # Hopefully we can optimize this away later.
32 git rev-parse --verify --quiet "$head^" >/dev/null
33 if [ $? -ne 0 ] ; then
34         echo "0"
35         exit 0
36 fi
37
38 contains=($(git branch -r --contains $head))
39 if [ ${#contains[@]} -eq 0 ] ; then
40         echo "$head has not been pushed"
41         exit 1
42 fi
43
44 potential=" "
45 for branch in ${contains[@]} ; do
46         branch=${branch##origin/}
47         #echo "branch=$branch"
48
49         # Walk back until we hit a baserev that is not the branch_tip itself (because it was merged)
50         branch_tip=$(git rev-parse origin/$branch)
51         stable_rev=$(git rev-parse origin/stable)
52         stable_base=$(git merge-base "$branch_tip" "$stable_rev")
53
54         # echo "stable_base=$stable_base"
55         while [ "$stable_base" == "$branch_tip" ] ; do
56                 stable_rev=$(git rev-parse "${stable_rev}^")
57                 stable_base=$(git merge-base "$branch_tip" "$stable_rev")
58         done
59         # echo "stable_base=$stable_base"
60
61         git rev-list --first-parent $stable_base..$branch_tip | grep --quiet "$head"
62         if [ $? -eq 0 ] ; then
63                 if [ "$branch" == "stable" ] ; then
64                         describe=$(git describe $head 2>/dev/null)
65                         if [ $? -eq 0 ] ; then
66                                 potential="$potential $describe"
67                         else
68                                 potential="$potential stable-$head"
69                         fi
70                 else
71                         number=$(git rev-list --first-parent "$stable_base..$head" | wc -l)
72                         potential="$potential $branch-$number"
73                 fi
74         fi
75 done
76
77 potential=($potential)
78 if [ ${#potential[@]} -eq 1 ] ; then
79         echo "${potential[0]}"
80 else
81         echo "unknown"
82 fi
83