git-quiltimport.shon commit pull --rebase: Avoid spurious conflicts and reapplying unnecessary patches (cf65426)
   1#!/bin/sh
   2OPTIONS_KEEPDASHDASH=
   3OPTIONS_SPEC="\
   4git quiltimport [options]
   5--
   6n,dry-run     dry run
   7author=       author name and email address for patches without any
   8patches=      path to the quilt series and patches
   9"
  10SUBDIRECTORY_ON=Yes
  11. git-sh-setup
  12
  13dry_run=""
  14quilt_author=""
  15while test $# != 0
  16do
  17        case "$1" in
  18        --author)
  19                shift
  20                quilt_author="$1"
  21                ;;
  22        -n|--dry-run)
  23                dry_run=1
  24                ;;
  25        --patches)
  26                shift
  27                QUILT_PATCHES="$1"
  28                ;;
  29        --)
  30                shift
  31                break;;
  32        *)
  33                usage
  34                ;;
  35        esac
  36        shift
  37done
  38
  39# Quilt Author
  40if [ -n "$quilt_author" ] ; then
  41        quilt_author_name=$(expr "z$quilt_author" : 'z\(.*[^ ]\) *<.*') &&
  42        quilt_author_email=$(expr "z$quilt_author" : '.*<\([^>]*\)') &&
  43        test '' != "$quilt_author_name" &&
  44        test '' != "$quilt_author_email" ||
  45        die "malformed --author parameter"
  46fi
  47
  48# Quilt patch directory
  49: ${QUILT_PATCHES:=patches}
  50if ! [ -d "$QUILT_PATCHES" ] ; then
  51        echo "The \"$QUILT_PATCHES\" directory does not exist."
  52        exit 1
  53fi
  54
  55# Temporary directories
  56tmp_dir="$GIT_DIR"/rebase-apply
  57tmp_msg="$tmp_dir/msg"
  58tmp_patch="$tmp_dir/patch"
  59tmp_info="$tmp_dir/info"
  60
  61
  62# Find the intial commit
  63commit=$(git rev-parse HEAD)
  64
  65mkdir $tmp_dir || exit 2
  66while read patch_name level garbage <&3
  67do
  68        case "$patch_name" in ''|'#'*) continue;; esac
  69        case "$level" in
  70        -p*)    ;;
  71        ''|'#'*)
  72                level=;;
  73        *)
  74                echo "unable to parse patch level, ignoring it."
  75                level=;;
  76        esac
  77        case "$garbage" in
  78        ''|'#'*);;
  79        *)
  80                echo "trailing garbage found in series file: $garbage"
  81                exit 1;;
  82        esac
  83        if ! [ -f "$QUILT_PATCHES/$patch_name" ] ; then
  84                echo "$patch_name doesn't exist. Skipping."
  85                continue
  86        fi
  87        echo $patch_name
  88        git mailinfo "$tmp_msg" "$tmp_patch" \
  89                <"$QUILT_PATCHES/$patch_name" >"$tmp_info" || exit 3
  90        test -s "$tmp_patch" || {
  91                echo "Patch is empty.  Was it split wrong?"
  92                exit 1
  93        }
  94
  95        # Parse the author information
  96        GIT_AUTHOR_NAME=$(sed -ne 's/Author: //p' "$tmp_info")
  97        GIT_AUTHOR_EMAIL=$(sed -ne 's/Email: //p' "$tmp_info")
  98        export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
  99        while test -z "$GIT_AUTHOR_EMAIL" && test -z "$GIT_AUTHOR_NAME" ; do
 100                if [ -n "$quilt_author" ] ; then
 101                        GIT_AUTHOR_NAME="$quilt_author_name";
 102                        GIT_AUTHOR_EMAIL="$quilt_author_email";
 103                elif [ -n "$dry_run" ]; then
 104                        echo "No author found in $patch_name" >&2;
 105                        GIT_AUTHOR_NAME="dry-run-not-found";
 106                        GIT_AUTHOR_EMAIL="dry-run-not-found";
 107                else
 108                        echo "No author found in $patch_name" >&2;
 109                        echo "---"
 110                        cat $tmp_msg
 111                        printf "Author: ";
 112                        read patch_author
 113
 114                        echo "$patch_author"
 115
 116                        patch_author_name=$(expr "z$patch_author" : 'z\(.*[^ ]\) *<.*') &&
 117                        patch_author_email=$(expr "z$patch_author" : '.*<\([^>]*\)') &&
 118                        test '' != "$patch_author_name" &&
 119                        test '' != "$patch_author_email" &&
 120                        GIT_AUTHOR_NAME="$patch_author_name" &&
 121                        GIT_AUTHOR_EMAIL="$patch_author_email"
 122                fi
 123        done
 124        GIT_AUTHOR_DATE=$(sed -ne 's/Date: //p' "$tmp_info")
 125        SUBJECT=$(sed -ne 's/Subject: //p' "$tmp_info")
 126        export GIT_AUTHOR_DATE SUBJECT
 127        if [ -z "$SUBJECT" ] ; then
 128                SUBJECT=$(echo $patch_name | sed -e 's/.patch$//')
 129        fi
 130
 131        if [ -z "$dry_run" ] ; then
 132                git apply --index -C1 ${level:+"$level"} "$tmp_patch" &&
 133                tree=$(git write-tree) &&
 134                commit=$( (echo "$SUBJECT"; echo; cat "$tmp_msg") | git commit-tree $tree -p $commit) &&
 135                git update-ref -m "quiltimport: $patch_name" HEAD $commit || exit 4
 136        fi
 137done 3<"$QUILT_PATCHES/series"
 138rm -rf $tmp_dir || exit 5