-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-format-patch-for-range
executable file
·89 lines (74 loc) · 2.21 KB
/
git-format-patch-for-range
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
#
# git-format-patch-for-range --- make one patch for a range of commits
# Usage: $0 <previous commit> <head commit> <dest filename>
# The range will be <previous commit>..<head commit>, i.e., head back to
# the one just after previous.
# Outputs to <dest filename>.
#
# By Christopher White <[email protected]>
# Copyright (c) 2020 D3 Engineering, LLC.
set -eEuo pipefail
set -x
old_headG=
old_branchG=
working_branchG=
doneG=
main() {
if [[ "$#" -ne 3 ]]; then
usage
return 2
fi
# Save where to get back to
old_headG="$(git status -b --porcelain=2 | awk '/branch\.oid/ { print $3}')"
old_branchG="$(git status -b --porcelain=2 | awk '/branch\.head/ { print $3}')" || true # there may not be a branch
# Make sure it's clean
local -r status="$(git status --short)"
if [[ "$status" ]]; then
echo "Working directory is not clean - aborting" 1>&2
return 1
fi
# Capture the refs and convert them to commits before switching branches.
# Otherwise, `HEAD` is different when we use it than when the user
# typed it in.
local prev="$1"
prev="$(git rev-parse "$prev")" # on a separate line for -e
local head="$2"
head="$(git rev-parse "$head")"
local -r destfn="$3"
touch "$destfn" # bail early if unwriteable
local -r now="$(date +'%F-%H%M%S')"
working_branchG="TEMP-$now"
trap cleanup EXIT
# Set up
git checkout -b "$working_branchG" "$prev"
git merge --squash "$head"
git commit --edit -m"$(git log --format='# commit %h by %aN, committed %cr%n%n%B' HEAD.."$head")"
# Make the patch
git format-patch -1 --stdout > "$destfn"
# Cleanup
doneG=1
}
switch_back() {
if [[ "$old_branchG" ]]; then
git checkout "$old_branchG"
else
git checkout "$old_headG"
fi
git branch -D "$working_branchG" || true
}
cleanup() {
if (( doneG )); then
switch_back
else
echo "Incomplete - old branch was $old_branchG $old_headG"
fi
}
usage() {
cat 1>&2 <<EOT
Usage: $0 <previous commit> <head commit> <dest filename>
The range will be <previous commit>..<head commit>, so <previous commit>
is not included. Outputs to <dest filename>.
EOT
}
main "$@"