summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2010-05-06 11:57:07 +0200
committerTobias Klauser <tklauser@distanz.ch>2010-05-06 11:57:07 +0200
commit863c8225412c60be62ef3ba1de4b83aa2e53d0b6 (patch)
tree7c6f2fc7a62f07c888de6c69d651f18aafb99c38
parentb674aa902311b081e8806f076c24d35cb54283f8 (diff)
Add rename-patch (from Jean Delvare)
-rw-r--r--Makefile2
-rwxr-xr-xrename-patch91
2 files changed, 92 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index c9d8608..2d16f5a 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
BINDIR = $(HOME)/bin
-TARGETS = mkheader.py isprime multi-send-key perm.py git2svn-sync svn2git-sync
+TARGETS = mkheader.py isprime multi-send-key perm.py git2svn-sync svn2git-sync rename-patch
all: $(TARGETS)
@echo "Nothing to be done"
diff --git a/rename-patch b/rename-patch
new file mode 100755
index 0000000..e557e96
--- /dev/null
+++ b/rename-patch
@@ -0,0 +1,91 @@
+#!/bin/bash
+
+# Rename patch files according to the Content-Disposition header line
+# if they do have one, or Subject if they don't.
+#
+# Copyright (C) 2005, 2006, 2008 Jean Delvare <khali@linux-fr.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details:
+# http://www.gnu.org/copyleft/gpl.html
+#
+# The latest official version of this script can be found at:
+# ftp://ftp.kernel.org/pub/linux/kernel/people/jdelvare/scripts/rename-patch
+
+# Avoid nasty locale effects
+export LC_ALL=C
+
+findname()
+{
+ local file="$1" pattern;
+
+ # Try Content-Disposition first
+ pattern='^Content-Disposition:[[:space:]]*inline[[:space:]]*;[[:space:]]*filename[[:space:]]*=';
+ if grep -q $pattern "$file"
+ then
+ sed -ne "/$pattern/{s/$pattern[[:space:]]*//p;q}" "$file"
+ return
+ fi
+
+ # Fallback to Subject
+ pattern='^Subject:';
+ if grep -q $pattern "$file"
+ then
+ sed -ne "/$pattern/{
+ s/$pattern[[:space:]]*//;
+ s/\[[^]]*\]//g;
+ s/[^a-z0-9._-]/-/ig;
+ s/--*/-/g;
+ s/^-//;
+ s/^re-//i;
+ s/[.-]*$/.patch/;
+ p;q}" "$file" | \
+ tr A-Z a-z
+ return
+ fi
+}
+
+if [ $# -eq 0 ]
+then
+ echo "Usage: rename-patch file [file...]" >&2
+ exit 1
+fi
+
+for file in "$@"
+do
+ name=$(findname "$file")
+ if echo "$file" | grep -q '/'
+ then
+ path=$(echo "$file" | sed -e 's/\/[^/]*$/\//')
+ else
+ path=''
+ fi
+
+ if [ -z "$name" ]
+ then
+ echo "No name found for $file" >&2
+ continue
+ fi
+
+ if [ "$path$name" == "$file" ]
+ then
+ # It's already OK
+ continue
+ fi
+
+ if [ -e "$path$name" ]
+ then
+ echo "Can't rename $file to $path$name which already exists" >&2
+ continue
+ fi
+
+ mv -f "$file" "$path$name"
+ echo "$file renamed to $path$name"
+done