chiark / gitweb /
f83caa6701f95fb6b2a540c24e53ebb877b78a76
[topgit.git] / debian / tg2quilt.mk
1 #
2 # tg2quilt.mk - TopGit-to-quilt functionality for debian/rules files
3 #
4 # This make(1) snippet facilitates the conversion of TopGit branches to
5 # a quilt series.
6 #
7 # It is intended to be included from debian/rules files of TopGit-using
8 # packages after including the quilt rules, like so:
9 #
10 #   include /usr/share/quilt/quilt.make
11 #   -include /usr/share/topgit/tg2quilt.mk
12 #
13 # The leading dash is necessary for make not to die when the file is not
14 # installed. TopGit is not a build dependency (and does not need to be), and
15 # if the package is not installed, debian/rules can still be used normally.
16 #
17 # The snippet exports the following targets. These targets only perform the
18 # describe behaviour when invoked from a TopGit repository (`tg summary -t`
19 # returns a non-empty set); used outside, they simply output informational
20 # messages but do not cause errors.
21 #
22 #       tg-export: exports the TopGit patches into a quilt series under
23 #                  debian/patches.
24 #                    You may set TG_BRANCHES to a comma- or -space-separated
25 #                  subset of branches (but not comma-and-space-separated) to
26 #                  export (before including the file).
27 #                    If debian/patches/series already exists, the target
28 #                  will take note, blather a bit, and get out of the way.
29 #
30 #        tg-clean: cleans the source tree, just like the debian/rules clean
31 #                  target, and invokes tg-rmdir
32 #
33 #        tg-rmdir: tries to remove debian/patches, but only if there are no
34 #                  non-TopGit files under the directory, the repository has
35 #                  no uncommitted changes, and there are not quilt patches
36 #                  applied.
37 #                    The heuristic is to find files that do not contain a line
38 #                  matchines /^tg:/, minus the series file. If any such files
39 #                  are found, an error occurs. Otherwise, the directory is
40 #                  removed. This means that edits to the series file are
41 #                  likely to vanish.
42 #
43 #  tg-cleanexport: recreates the debian/patches directory from scratch, using
44 #                  tg-rmdir and tg-export.
45 #
46 #   tg-forceclean: cleans the source tree, just like the debian/rules clean
47 #                  target, and forcefully removes the debian/patches
48 #                  directory in doing so. Yes, *force*-fully. WHAM!
49 #
50 # This file also hooks into the standard debian/rules and quilt targets such
51 # that tg-export is automatically invoked before quilt gets a chance to patch
52 # or unpatch the tree.
53 #
54 # The QUILT_PATCH_DIR variable can be set before including the file to override
55 # the default debian/patches location.
56 #
57 # More information, particularly for people working on TopGit-using packages,
58 # can be found in /usr/share/doc/topgit/HOWTO-tg2quilt.gz .
59 #
60 # Copyright © 2008 martin f. krafft <madduck@debian.org> Released under terms
61 # of the the Artistic Licence 2.0.
62 #
63
64 ifeq ($(shell tg summary -t),)
65   # This is not a TopGit branch, so just blubber a bit.
66
67   tg-export tg-clean tg-forceclean tg-rmdir tg-cleanexport:
68         @echo "E: The $@ target only works from a TopGit repository." >&2
69 else
70
71 # We are in a TopGit branch, so let the fun begin.
72
73 ifdef PATCHES_DIR
74         DUMMY := $(warning W: The $$PATCHES_DIR variable is deprecated, please use $$QUILT_PATCH_DIR instead.)
75         DUMMY := $(warning W: Sleeping for 10 seconds so you can read this!)
76         DUMMY := $(shell sleep 10)
77         QUILT_PATCH_DIR := $(PATCHES_DIR)
78 endif
79
80 QUILT_PATCH_DIR ?= debian/patches
81 QUILT_STAMPFN ?= patch
82
83 # Hook tg-export into quilt's make(1) snippet such that it gets executed
84 # before quilt patches or unpatches.
85 $(QUILT_STAMPFN): tg-export
86 unpatch: __tg-temp-export
87 __tg-temp-export:
88         @echo "Exporting TopGit branches to series so that quilt can clean up..." >&2
89         $(MAKE) --no-print-directory -f debian/rules tg-export
90 .PHONY: __tg-temp-export
91
92 # Set some tg-export-specific variables, e.g. default TG_BRANCHES to all
93 # TopGit branches
94 tg-export: TG_BRANCHES ?= $(shell tg summary -t)
95         # see make manual for this trick:
96 tg-export: __TG_COMMA := ,
97 tg-export: __TG_EMPTY :=
98 tg-export: __TG_SPACE := $(__TG_EMPTY) $(__TG_EMPTY)
99
100 ifeq ($(wildcard $(QUILT_PATCH_DIR)/series),)
101   # The series file does not exist, so we proceed normally
102
103   # tg-export will not work if the target dir already exists, so try to remove
104   # it by calling tg-rmdir
105   tg-export: tg-rmdir
106         tg export -b $(subst $(__TG_SPACE),$(__TG_COMMA),$(TG_BRANCHES)) --quilt $(QUILT_PATCH_DIR)
107 else
108   # The series file already exists, so assume there is nothing to do.
109   tg-export:
110         @echo "I: TopGit patch series already exists, nothing to do." >&2
111         @echo "I: (invoke the \`tg-clean\` target to remove the series.)" >&2
112         @echo "I: (you can ignore this message during a \`tg-clean\` run.)" >&2
113 endif
114
115 ifeq ($(wildcard $(QUILT_PATCH_DIR)),)
116   # No patch directory, so nothing to do:
117   tg-rmdir:
118         @true
119
120 else
121   # There is a patch directory, let's try to clean it out:
122   tg-rmdir: __TG_FILES := $(shell find $(QUILT_PATCH_DIR) -type f -a -not -path \*/series \
123                                     | xargs grep -l '^tg:')
124   tg-rmdir:
125         QUILT_PATCHES=$(QUILT_PATCH_DIR) quilt pop -a 2>/dev/null || :
126         @if quilt applied >/dev/null 2>&1; then \
127           echo "E: there are applied quilt patches." >&2; \
128           echo "E: please unapply (pop) all patches and try again." >&2; \
129           false; \
130         fi
131         @if git status -am. >/dev/null; then \
132           echo "E: there are uncommitted changes in the working directory." >&2; \
133           echo "E: please commit or revert all changes." >&2; \
134           false; \
135         fi
136         # remove all files whose contents matches /^tg:/
137         rm -f $(__TG_FILES)
138         # remove the series file
139         rm -f $(QUILT_PATCH_DIR)/series
140         # remove dpkg v3 file
141         rm -f $(QUILT_PATCH_DIR)/.dpkg-source-applied
142         # try to remove directories
143         find $(QUILT_PATCH_DIR) -depth -type d -empty -execdir rmdir {} +
144         # fail if the directory could not be removed and still exists
145         @test ! -d $(QUILT_PATCH_DIR) || { \
146           echo "E: $(QUILT_PATCH_DIR) contains non-TopGit-generated files:" >&2; \
147           find $(QUILT_PATCH_DIR) -type f -printf 'E:   %P\n' >&2; \
148           false; \
149         }
150 endif
151
152 # Make sure that we try to clean up the patches directory last
153 tg-clean: clean
154         $(MAKE) --no-print-directory -f debian/rules tg-rmdir
155
156 tg-forceclean: clean
157         test -d $(QUILT_PATCH_DIR) && rm -r $(QUILT_PATCH_DIR) || :
158
159 tg-cleanexport: tg-rmdir
160         $(MAKE) --no-print-directory -f debian/rules tg-export
161
162 endif
163
164 .PHONY: tg-clean tg-export tg-forceclean tg-rmdir tg-cleanexport
165
166 # vim:ft=make:ts=8:noet
167 # -*- Makefile -*-, you silly Emacs! (shamelessly stolen from quilt)