scripts/patch-renumber.sh
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Tue Oct 27 19:34:13 2009 +0100 (2009-10-27)
changeset 1608 3d5aead9e15d
parent 1577 c774b2cc7863
child 1622 9ad2a3fd1fcc
permissions -rwxr-xr-x
scripts: add sed expression to apply when renumbering patches

Some patchsets have superfluous members in their names (eg. the ones coming
from Gentoo), so it can come in handy to pass a sed RE to strip them out of
the final patch name.
Also add a 'fake' mode, where the command will only be printed and not
executed, so we can check beforehand if the rename will be OK.
yann@756
     1
#!/bin/sh
yann@756
     2
# Yes, this intends to be a true POSIX script file.
yann@1175
     3
set -e
yann@756
     4
yann@756
     5
myname="$0"
yann@756
     6
yann@1175
     7
# Parse the tools' paths configuration
yann@1175
     8
. "paths.mk"
yann@1175
     9
yann@756
    10
doUsage() {
yann@756
    11
  cat <<_EOF_
yann@1608
    12
Usage: ${myname} <dir> <base> <inc> [sed_re]
yann@1608
    13
    Will renumber all patches found in 'dir', starting at 'base', and with
yann@1608
    14
    an increment of 'inc'.
yann@1608
    15
    If 'sed_re' is given, it is interpreted as a valid sed expression, and
yann@1608
    16
    it will be applied to the patch name.
yann@1608
    17
    If the environment variable FAKE is set to 'y', then the command will
yann@1608
    18
    only be printed, and not executed (so you can check beforehand).
yann@1608
    19
    Eg.:
yann@1608
    20
      patch-renumber.sh patches/gcc/4.3.1 100 10
yann@1608
    21
      patch-renumber.sh patches/gcc/4.2.4 100 10 's/(all[_-])*(gcc[-_])*//;'
yann@756
    22
_EOF_
yann@756
    23
}
yann@756
    24
yann@1608
    25
[ $# -lt 3 -o $# -gt 4 ] && { doUsage; exit 1; }
yann@756
    26
[ -d "${1}" ] || { doUsage; exit 1; }
yann@756
    27
yann@756
    28
dir="${1}"
yann@756
    29
cpt="${2}"
yann@756
    30
inc="${3}"
yann@1608
    31
sed_re="${4}"
yann@756
    32
yann@1577
    33
case "$(LC_ALL=C hg id "${dir}" 2>/dev/null)" in
yann@1608
    34
    "") CMD="";;
yann@1608
    35
    *)  CMD="hg";;
yann@756
    36
esac
yann@756
    37
yann@1608
    38
if [ "${FAKE}" = "y" ]; then
yann@1608
    39
    CMD="echo ${CMD}"
yann@1608
    40
fi
yann@1608
    41
yann@1608
    42
for p in "${dir}"/*.patch*; do
yann@756
    43
    [ -e "${p}" ] || { echo "No such file '${p}'"; exit 1; }
yann@1608
    44
    newname="$(printf "%03d-%s"                                     \
yann@1608
    45
                      "${cpt}"                                      \
yann@1608
    46
                      "$( basename "${p}"                           \
yann@1608
    47
                          |"${sed}" -r -e 's/^[[:digit:]]+[-_]//'   \
yann@1608
    48
                                       -e "${sed_re}"               \
yann@1608
    49
                        )"                                          \
yann@1192
    50
              )"
yann@1608
    51
    [ "${p}" = "${dir}/${newname}" ] || ${CMD} mv -v "${p}" "${dir}/${newname}"
yann@756
    52
    cpt=$((cpt+inc))
yann@756
    53
done