summaryrefslogtreecommitdiff
path: root/scripts/patch-renumber.sh
blob: 95bae8fb782842dd0af79852ce0f15d27fca1d36 (plain)
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
#!/bin/sh
# Yes, this intends to be a true POSIX script file.
set -e

myname="$0"

# Parse the tools' paths configuration
. "paths.mk"

doUsage() {
  cat <<_EOF_
Usage: ${myname} <dir> <base> <inc> [sed_re]
    Will renumber all patches found in 'dir', starting at 'base', and with
    an increment of 'inc'.
    If 'sed_re' is given, it is interpreted as a valid sed expression, and
    it will be applied to the patch name.
    If the environment variable FAKE is set to 'y', then the command will
    only be printed, and not executed (so you can check beforehand).
    Eg.:
      patch-renumber.sh patches/gcc/4.3.1 100 10
      patch-renumber.sh patches/gcc/4.2.4 100 10 's/(all[_-])*(gcc[-_])*//;'
_EOF_
}

[ $# -lt 3 -o $# -gt 4 ] && { doUsage; exit 1; }
[ -d "${1}" ] || { doUsage; exit 1; }

dir="${1}"
cpt="${2}"
inc="${3}"
sed_re="${4}"

case "$(LC_ALL=C hg id "${dir}" 2>/dev/null)" in
    "") CMD="";;
    *)  CMD="hg";;
esac

if [ "${FAKE}" = "y" ]; then
    CMD="echo ${CMD}"
fi

for p in "${dir}"/*.patch*; do
    [ -e "${p}" ] || { echo "No such file '${p}'"; exit 1; }
    newname="$(printf "%03d-%s"                                     \
                      "${cpt}"                                      \
                      "$( basename "${p}"                           \
                          |"${sed}" -r -e 's/^[[:digit:]]+[-_]//'   \
                                       -e "${sed_re}"               \
                        )"                                          \
              )"
    [ "${p}" = "${dir}/${newname}" ] || ${CMD} mv -v "${p}" "${dir}/${newname}"
    cpt=$((cpt+inc))
done