81947337ce3735da6b4060ade472cb77095d567b
[git-central.git] / server / update-prefer-rebase
1 #!/bin/sh
2 #
3 # Watches for merges that had only local merges (e.g. should have been rebases).
4 #
5 # If they are introducing non-merge commits /and/ merge commits, it could
6 # look like one of two ways. This way:
7 #
8 # * --- B --- * --- oldrev
9 #        \             \
10 #        new --- new --- newrev
11 #
12 # They basically had an un-shared local dev branch (probably by making a
13 # merge) and instead should have done a rebase. Also, if they did:
14 #
15 # * --- B --- * --- oldrev
16 #        \             \
17 #        new --- new --- new -- newrev
18 #
19 # We should try and catch them--where the merge happened previously to
20 # them doing more work in the newrev commit.
21 #
22 # But if it looks like:
23 #
24 # * --- B --- * --- oldrev
25 #        \              \
26 #        old --- new --- newrev
27 #
28 # Then they had a pre-shared branch that cannot be rebased and so they
29 # were correct in doing a merge to tie "old" and "oldrev" together.
30 #
31 # Also, we obviously have to be okay with:
32 #
33 # * --- * --- * --- oldrev --- new --- new --- newrev
34
35 refname="$1"
36 oldrev="$2"
37 newrev="$3"
38
39 if expr "$oldrev" : '0*$' >/dev/null ; then
40         exit 0
41 fi
42
43 # Read backwards: all commits from old..new, unless they are already referenced by a branch
44 git rev-parse --not --branches | git rev-list --stdin $oldrev..$newrev | while read commit ; do
45         number_of_parents=$(git rev-list -n 1 --parents $commit | sed 's/ /\n/g' | grep -v $commit | wc -l)
46         if [[ $number_of_parents > 1 ]] ; then
47                 # Hack to not interfer with the update-stable "--no-ff" method
48                 if [[ "$refname" == "refs/heads/stable" && "$commit" == "$newrev" ]] ; then
49                         exit 0
50                 fi
51
52                 # Find the original branch point (B)
53                 parents=$(git rev-list -n 1 --parents $commit | sed 's/ /\n/g' | grep -v $commit)
54
55                 # For each baserev
56                 git merge-base --all $parents | while read baserev ; do
57                         # For each parent
58                         git rev-list -n 1 --parents $commit | sed 's/ /\n/g' | grep -v $commit | while read parent ; do
59                                 parent_is_old=$(git branch --contains $parent | wc -l)
60                                 all_commits=$(git rev-list --first-parent $baserev..$parent | wc -l)
61                                 new_commits=$(git rev-parse --not --branches | git rev-list --stdin $baserev..$parent | wc -l)
62                                 if [ $parent_is_old -gt 0 -a $all_commits -eq $new_commits ] ; then
63                                         # echo "parent=$parent"
64                                         # echo "all_commits=$all_commits"
65                                         # echo "new_commits=$new_commits"
66                                         echo "----------------------------------------------------"
67                                         echo
68                                         echo "It looks like you should rebase instead of merging $commit"
69                                         echo
70                                         echo "----------------------------------------------------"
71                                         exit 1
72                                 fi
73                         done
74                         if [ $? -ne 0 ] ; then
75                                 exit 1
76                         fi
77                 done
78                 if [ $? -ne 0 ] ; then
79                         exit 1
80                 fi
81         fi
82 done
83