blob: 35aa411e6cfe0c0bb5e04ad86ef61f513d71f663 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#!/usr/bin/env bash
2BASE=$1; shift
3
4usage() {
5 echo "Usage: $0 NNN <file>..."
6 exit 1
7}
8
9check_number() {
10 case "$1" in
11 [0-9][0-9][0-9]) return 0;;
12 esac
13 return 1;
14}
15
16patch_header()
17{
18 awk '
19 /^(---|\*\*\*|Index:)[ \t][^ \t]|^diff -/ \
20 { exit }
21 { print }
22 '
23}
24
25strip_diffstat()
26{
27 awk '
28 /#? .* \| / \
29 { eat = eat $0 "\n"
30 next }
31 /^#? .* files? changed(, .* insertions?\(\+\))?(, .* deletions?\(-\))?/ \
32 { eat = ""
33 next }
34 { print eat $0
35 eat = "" }
36 '
37}
38
39strip_trailing_whitespace() {
40 sed -e 's:[ '$'\t'']*$::'
41}
42
43fixup_header() {
44 awk '
45 /^From / { next }
46 /^Subject: / {
47 sub("Subject: \\[[^\]]*\\]", "Subject: [PATCH]")
48 }
49 { print }
50 '
51}
52
53check_number "$BASE" || usage
54
55quilt series > /dev/null || {
56 echo "Not in quilt directory"
57 exit 2
58}
59
60get_next() {
61 NEW=$BASE
62 quilt series | while read CUR; do
63 [ -n "$CUR" ] || break
64 CUR=${CUR%%-*}
65 check_number "$CUR" || continue
66 [ "$CUR" -lt "$NEW" ] && continue
67 [ "$CUR" -ge "$(($BASE + 100))" ] && continue
68 NEW="$(($CUR + 1))"
69 echo $NEW
70 done | tail -n1
71}
72
73CUR=$(get_next)
74CUR="${CUR:-$BASE}"
75
76while [ -n "$1" ]; do
77 FILE="$1"; shift
78 NAME="$(basename $FILE)"
79 NAME="${NAME#[0-9]*-}"
80 echo -n "Processing patch $NAME: "
81
82 [ -e "$FILE" ] || {
83 echo "file $FILE not found"
84 exit 1
85 }
86
87 grep -qE "$NAME$" patches/series && {
88 echo "already applied"
89 continue
90 }
91
92 quilt new "$CUR-$NAME" || exit 1
93 patch_header < "$FILE" |
94 strip_diffstat |
95 strip_trailing_whitespace |
96 fixup_header > "patches/$CUR-$NAME"
97
98 quilt fold < "$FILE" || {
99 cp "$FILE" ./cur_patch
100 echo "patch $FILE failed to apply, copied to ./cur_patch"
101 exit 1
102 }
103
104 quilt refresh -p ab --no-index --no-timestamps
105
106 CUR="$(($CUR + 1))"
107done
108
109exit 0