summaryrefslogtreecommitdiffhomepage
path: root/gbp-pq
blob: e4de9322440be2552afa5e34c74c5bd950b4b22b (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
#
# Convert a patch-queue branch into a patch series in debian/patches and vice
# versa.
#
# assumes you have your quilt patch queue for $branch on patch-queue/$branch
#
# See: https://honk.sigxcpu.org/piki/development/debian_packages_in_git/

is_patch_queue()
{
    local branch=$1

    case $branch in
        patch-queue/*)
            return 0
            ;;
        *)
            return 1
            ;;
    esac
}

run_git()
{
    set -e
    git "$@"
    set +e
}

pq_export()
{
   local branch=$1
   local pq="patch-queue/$branch"

    if is_patch_queue $branch; then
        branch="${branch//patch-queue\/}"
        pq="patch-queue/$branch"
        echo "On \"$pq\", switching to \"$branch\"."
        run_git checkout $branch
    fi

    rm -f debian/patches/*
    PATCHES=`git format-patch -N -o debian/patches $branch...$pq`
    if [ -n "$PATCHES" ]; then
        echo "Regenerating patch queue in \"debian/patches\"."
        > debian/patches/series
        for PATCH in $PATCHES; do
	    # delete the first line (from sha1) and last two lines (git version
	    # info) of the patch file
            sed -i -e '1d' -e 'N;$!P;$!D;$d' $PATCH
            sed -i -e 's/^-- \n[0-9\.]+$//' $PATCH
            echo $PATCH | sed -e 's%debian/patches/%%' >> debian/patches/series
        done
        run_git status -- debian/patches
    else
        echo "No patches on \"$pq\" - nothing to do."
    fi
}

pq_rebase()
{
    local branch=$1
    local pq="patch-queue/$branch"

    if ! is_patch_queue $branch; then
	echo "Switching to \"$pq\""
        run_git checkout $pq
    else
        echo "Already on \"$branch\""
    fi
    run_git rebase "$branch"
}

pq_import()
{
    local branch=$1
    local pq="patch-queue/$branch"
    local patches=debian/patches/


    if is_patch_queue $branch; then
        echo "Already on a patch-queue branch \"$branch\" - doing nothing."
        return 1
    fi

    if ! git checkout -b $pq; then
        echo "Cannot create patch-queue branch \"$pq\". Try \"rebase\" instead."
	return 1
    fi

    if [ ! -r ${patches}series ]; then
        echo "Found no series file at \"$patches\". No patches to add to patch-queue branch."
        return 0
    fi

    maintainer="$(sed -n -e 's/Maintainer: \+\(.*\)/\1/p' debian/control)"
    QUILT_PATCHES=$patches run_git quiltimport --author "$maintainer"
}

pq_drop()
{
    local branch=$1
    local pq="patch-queue/$branch"

    if is_patch_queue $branch; then
        echo "On a patch-queue branch, can't drop it."
        return 1
    else
	run_git branch -D "${pq}"
	echo "Dropped ${pq}."
    fi
}

usage ()
{
    cat <<EOF
$0 [ACTION]

Options:
	export		Export the patch queue associated to the current branch
			into a quilt patch series in debian/patches/ and update the
			series file.

	import		Create a patch queue branch from quilt patches in debian/patches.

	rebase		Switch to patch queue branch associated to the current
			branch and rebase against current branch.

	drop		Drop (delete) the patch queue associated to the current branch.
EOF
}

branch=$(run_git branch --no-color | awk '/^\*/ { print $2 }')

case "$1" in
    export)
        pq_export $branch
        ;;
    import)
	pq_import $branch
	;;
    rebase)
        pq_rebase $branch
        ;;
    drop)
	pq_drop $branch
	;;
    *)
	usage
	exit 1
	;;
esac