contrib / rerere-train.shon commit Merge branch 'jl/status-reduce-vertical-blank' (7b09842)
   1#!/bin/sh
   2# Copyright (c) 2008, Nanako Shiraishi
   3# Prime rerere database from existing merge commits
   4
   5me=rerere-train
   6USAGE=$(cat <<-EOF
   7usage: $me [--overwrite] <rev-list-args>
   8
   9    -h, --help            show the help
  10    -o, --overwrite       overwrite any existing rerere cache
  11EOF
  12)
  13
  14SUBDIRECTORY_OK=Yes
  15
  16overwrite=0
  17
  18while test $# -gt 0
  19do
  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
  39done
  40
  41# Overwrite or help options are not valid except as first arg
  42for opt in "$@"
  43do
  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
  54done
  55
  56. "$(git --exec-path)/git-sh-setup"
  57require_work_tree
  58cd_to_toplevel
  59
  60# Remember original branch
  61branch=$(git symbolic-ref -q HEAD) ||
  62original_HEAD=$(git rev-parse --verify HEAD) || {
  63        echo >&2 "Not on any branch and no commit yet?"
  64        exit 1
  65}
  66
  67mkdir -p "$GIT_DIR/rr-cache" || exit
  68
  69git rev-list --parents "$@" |
  70while read commit parent1 other_parents
  71do
  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
  95done
  96
  97if test -z "$branch"
  98then
  99        git checkout "$original_HEAD"
 100else
 101        git checkout "${branch#refs/heads/}"
 102fi