git-applymbox.shon commit Fall back to three-way merge when applying a patch. (47f0b6d)
   1#!/bin/sh
   2##
   3## "dotest" is my stupid name for my patch-application script, which
   4## I never got around to renaming after I tested it. We're now on the
   5## second generation of scripts, still called "dotest".
   6##
   7## Update: Ryan Anderson finally shamed me into naming this "applymbox".
   8##
   9## You give it a mbox-format collection of emails, and it will try to
  10## apply them to the kernel using "applypatch"
  11##
  12## The patch application may fail in the middle.  In which case:
  13## (1) look at .dotest/patch and fix it up to apply
  14## (2) re-run applymbox with -c .dotest/msg-number for the current one.
  15## Pay a special attention to the commit log message if you do this and
  16## use a Signoff_file, because applypatch wants to append the sign-off
  17## message to msg-clean every time it is run.
  18
  19. git-sh-setup || die "Not a git archive"
  20
  21usage () {
  22    echo >&2 "applymbox [-u] [-k] [-q] [-m] (-c .dotest/<num> | mbox) [signoff]"
  23    exit 1
  24}
  25
  26keep_subject= query_apply= continue= utf8= resume=t
  27while case "$#" in 0) break ;; esac
  28do
  29        case "$1" in
  30        -u)     utf8=-u ;;
  31        -k)     keep_subject=-k ;;
  32        -q)     query_apply=t ;;
  33        -c)     continue="$2"; resume=f; shift ;;
  34        -m)     fallback_3way=t ;;
  35        -*)     usage ;;
  36        *)      break ;;
  37        esac
  38        shift
  39done
  40
  41case "$continue" in
  42'')
  43        rm -rf .dotest
  44        mkdir .dotest
  45        git-mailsplit "$1" .dotest || exit 1
  46        shift
  47esac
  48
  49files=$(git-diff-index --cached --name-only HEAD) || exit
  50if [ "$files" ]; then
  51   echo "Dirty index: cannot apply patches (dirty: $files)" >&2
  52   exit 1
  53fi
  54
  55case "$query_apply" in
  56t)      touch .dotest/.query_apply
  57esac
  58case "$fall_back_3way" in
  59t)      : >.dotest/.3way
  60esac
  61case "$keep_subject" in
  62-k)     : >.dotest/.keep_subject
  63esac
  64
  65signoff="$1"
  66set x .dotest/0*
  67shift
  68while case "$#" in 0) break;; esac
  69do
  70    i="$1" 
  71    case "$resume,$continue" in
  72    f,$i)       resume=t;;
  73    f,*)        shift
  74                continue;;
  75    *)
  76            git-mailinfo $keep_subject $utf8 \
  77                .dotest/msg .dotest/patch <$i >.dotest/info || exit 1
  78            git-stripspace < .dotest/msg > .dotest/msg-clean
  79            ;;
  80    esac
  81    while :; # for fixing up and retry
  82    do
  83        git-applypatch .dotest/msg-clean .dotest/patch .dotest/info "$signoff"
  84        case "$?" in
  85        0 | 2 )
  86                # 2 is a special exit code from applypatch to indicate that
  87                # the patch wasn't applied, but continue anyway 
  88                ;;
  89        *)
  90                ret=$?
  91                if test -f .dotest/.query_apply
  92                then
  93                        echo >&2 "* Patch failed."
  94                        echo >&2 "* You could fix it up in your editor and"
  95                        echo >&2 "  retry.  If you want to do so, say yes here"
  96                        echo >&2 "  AFTER fixing .dotest/patch up."
  97                        echo >&2 -n "Retry [y/N]? "
  98                        read yesno
  99                        case "$yesno" in
 100                        [Yy]*)
 101                                continue ;;
 102                        esac
 103                fi
 104                exit $ret
 105        esac
 106        break
 107    done
 108    shift
 109done
 110# return to pristine
 111rm -fr .dotest