aboutsummaryrefslogtreecommitdiffstats
path: root/git-format-named-patches.sh
blob: 5ec9c69ba1eb448781ac88160c8e88b76463e29a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash

set -eu

SUBDIR=""
CLEAN=1

fail() {
    printf "%s\n" "$1" >>/dev/stderr
    exit 32
}

while getopts "cd:" opt; do
  case $opt in
    c)
      CLEAN=0
      ;;
    d)
      SUBDIR="$(readlink -f "${OPTARG}")"
      ;;
    \?)
      echo "Invalid option: -$OPTARG"
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument."
      exit 1
      ;;
  esac
done

# Shift past the last option parsed by getopts
shift $((OPTIND-1))

if [ "$#" -lt 1 ] ; then
    INTEREST="HEAD~..HEAD"
elif  [[ ! "$1" =~ '..' ]] ; then
    INTEREST="$1..HEAD"
else
    INTEREST="$1"
fi

[ $CLEAN -eq 1 ] && [ -n "$SUBDIR" ] && rm -fv "$SUBDIR/*.patch"

IDSstr="$(git log --format="%H" "$INTEREST" 2>/dev/null | tac)"
mapfile -t IDS <<< "$IDSstr"
for id in "${IDS[@]}" ; do
    PATCH="$(git format-patch -1 --stdout "${id}")"
    TRAILERS="$(echo "$PATCH" | git interpret-trailers --parse)"
    FN="$(echo "$TRAILERS" | awk '/^Patch: / {print $2}')"
    [ -z "$FN" ] && fail "Patch filename not defined"
    if [ -n "$SUBDIR" ] ; then
        FFN="$(readlink -f "$SUBDIR"/"$FN")"
    else
        FFN="$FN"
    fi
    printf "%s\n" "$PATCH" > "$FFN"
    printf "%s\n" "$FN"
done