scripts: add patch-tags-from-git
[opensuse:kernel-source.git] / scripts / patch-tags-from-git
1 #!/bin/bash
2 # Add suse Kernel repo headers to a patch created using git-format-patch 
3 #
4 # Brandon Philips <bphilips@suse.de>
5
6 PATH=$(dirname $0):$PATH
7
8 if [ $# -lt 1 ]; then
9         echo "usage: $0 <filename> <kernel path> <kernel name>"
10         echo "Add relevant headers for patches pulled from git"
11         echo ""
12         echo "Adds a Git-commit: tag using the From header"
13         echo ""
14         echo "Adds a Patch-mainline: tag if <kernel path> is given"
15         echo "  a useful tree name can be provided in <kernel name>"
16         echo "  otherwise basename <kernel path> will be used"
17         echo ""
18         echo "example usage"
19         echo "-------------"
20         echo "$0 NULL-pointer-fix.patch"
21         echo "$0 NULL-pointer-fix.patch ~/kernel/linux-2.6"
22         echo "$0 NULL-pointer-fix.patch ~/kernel/linux-2.6 pci 2.6"
23
24         exit 1
25 fi
26
27 FILE=$1
28 shift
29
30 if [ ! -f $FILE ]; then
31         echo "No such file $1"
32         exit 2
33 fi
34
35 commit=$(head -n1 $FILE | sed -n "s%From\s\([0-9a-f]*\)\s.*%\1%p")
36
37 if [ "z$commit" = "z" ]; then
38         echo "$FILE doesn't look like a patch from git"
39 else
40         patch-tag -A git-commit="$commit" $FILE
41 fi
42
43 if [ $# -eq 0 ]; then
44         exit 0
45 fi
46
47 DIR=$1
48 shift
49
50 commit=$(patch-tag -p git-commit $FILE | grep -i "git-commit:" | sed "s%.*:\s\(.*\)%\1%g")
51
52 if [ "z$commit" = "z" ]; then
53         echo "No git-commit tag in $FILE"
54         exit 3
55 fi
56
57 export GIT_DIR=$DIR/.git
58
59 if [ ! -d $GIT_DIR ]; then
60         echo "No such directory $GIT_DIR"
61         echo 4
62 fi
63
64 mainline=$(git describe --contains $commit 2> /dev/null)
65
66 if [ "z$mainline" = "z" ]; then
67         if git show $commit > /dev/null 2> /dev/null; then
68                 if [ $# -eq 0 ]; then
69                         merged=$(basename $DIR)
70                 else
71                         merged="$@"
72                 fi
73                 patch-tag -A Patch-mainline="Merged into $merged" $FILE
74                 exit 0
75         else
76                 echo "$commit from $FILE does not exist in $DIR"
77                 exit 5
78         fi
79 fi
80
81 patch-tag -A Patch-mainline="$mainline" $FILE