Sync with 2.14.6
[git] / contrib / rerere-train.sh
1 #!/bin/sh
2 # Copyright (c) 2008, Nanako Shiraishi
3 # Prime rerere database from existing merge commits
4
5 me=rerere-train
6 USAGE=$(cat <<-EOF
7 usage: $me [--overwrite] <rev-list-args>
8
9     -h, --help            show the help
10     -o, --overwrite       overwrite any existing rerere cache
11 EOF
12 )
13
14 SUBDIRECTORY_OK=Yes
15
16 overwrite=0
17
18 while test $# -gt 0
19 do
20         opt="$1"
21         case "$opt" in
22         -h|--help)
23                 echo "$USAGE"
24                 exit 0
25                 ;;
26         -o|--overwrite)
27                 overwrite=1
28                 shift
29                 break
30                 ;;
31         --)
32                 shift
33                 break
34                 ;;
35         *)
36                 break
37                 ;;
38         esac
39 done
40
41 # Overwrite or help options are not valid except as first arg
42 for opt in "$@"
43 do
44         case "$opt" in
45         -h|--help)
46                 echo "$USAGE"
47                 exit 0
48                 ;;
49         -o|--overwrite)
50                 echo "$USAGE"
51                 exit 0
52                 ;;
53         esac
54 done
55
56 . "$(git --exec-path)/git-sh-setup"
57 require_work_tree
58 cd_to_toplevel
59
60 # Remember original branch
61 branch=$(git symbolic-ref -q HEAD) ||
62 original_HEAD=$(git rev-parse --verify HEAD) || {
63         echo >&2 "Not on any branch and no commit yet?"
64         exit 1
65 }
66
67 mkdir -p "$GIT_DIR/rr-cache" || exit
68
69 git rev-list --parents "$@" |
70 while read commit parent1 other_parents
71 do
72         if test -z "$other_parents"
73         then
74                 # Skip non-merges
75                 continue
76         fi
77         git checkout -q "$parent1^0"
78         if git merge $other_parents >/dev/null 2>&1
79         then
80                 # Cleanly merges
81                 continue
82         fi
83         if test $overwrite = 1
84         then
85                 git rerere forget .
86         fi
87         if test -s "$GIT_DIR/MERGE_RR"
88         then
89                 git show -s --pretty=format:"Learning from %h %s" "$commit"
90                 git rerere
91                 git checkout -q $commit -- .
92                 git rerere
93         fi
94         git reset -q --hard
95 done
96
97 if test -z "$branch"
98 then
99         git checkout "$original_HEAD"
100 else
101         git checkout "${branch#refs/heads/}"
102 fi