tools / git-applypatchon commit Merge from gitk (b30245c)
   1#!/bin/sh
   2##
   3## applypatch takes four file arguments, and uses those to
   4## apply the unpacked patch (surprise surprise) that they
   5## represent to the current tree.
   6##
   7## The arguments are:
   8##      $1 - file with commit message
   9##      $2 - file with the actual patch
  10##      $3 - "info" file with Author, email and subject
  11##      $4 - optional file containing signoff to add
  12##
  13signoff="$4"
  14final=.dotest/final-commit
  15##
  16## If this file exists, we ask before applying
  17##
  18query_apply=.dotest/.query_apply
  19keep_subject=.dotest/.keep_subject
  20MSGFILE=$1
  21PATCHFILE=$2
  22INFO=$3
  23EDIT=${VISUAL:-$EDITOR}
  24EDIT=${EDIT:-vi}
  25
  26export GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' .dotest/info)"
  27export GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' .dotest/info)"
  28export GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' .dotest/info)"
  29export SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' .dotest/info)"
  30
  31if [ -n "$signoff" -a -f "$signoff" ]; then
  32        cat $signoff >> $MSGFILE
  33fi
  34patch_header=
  35test -f "$keep_subject" || patch_header='[PATCH] '
  36
  37(echo "$patch_header$SUBJECT" ; if [ -s $MSGFILE ]; then echo ; cat $MSGFILE; fi ) > $final
  38
  39f=0
  40[ -f $query_apply ] || f=1
  41
  42while [ $f -eq 0 ]; do
  43        echo "Commit Body is:"
  44        echo "--------------------------"
  45        cat $final
  46        echo "--------------------------"
  47        echo -n "Apply? [y]es/[n]o/[e]dit/[a]ccept all "
  48        read reply
  49        case $reply in
  50                y|Y) f=1;;
  51                n|N) exit 2;;   # special value to tell dotest to keep going
  52                e|E) $EDIT $final;;
  53                a|A) rm -f $query_apply
  54                     f=1;;
  55        esac
  56done
  57
  58echo
  59echo Applying "'$SUBJECT'"
  60echo
  61
  62git-apply --index $PATCHFILE || exit 1
  63tree=$(git-write-tree) || exit 1
  64echo Wrote tree $tree
  65commit=$(git-commit-tree $tree -p $(cat .git/HEAD) < $final) || exit 1
  66echo Committed: $commit
  67echo $commit > .git/HEAD