chiark / gitweb /
factor out standard tg2quilt stuff to its own doc
[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, like so:
9 #
10 #   -include /usr/share/topgit/tg2quilt.mk
11 #
12 # The snippet exports the following targets. These targets only perform the
13 # describe behaviour when invoked from a TopGit repository (`tg summary -t`
14 # returns a non-empty set); used outside, they simply output informational
15 # messages but do not cause errors.
16 #
17 #       tg-export: exports the TopGit patches into a quilt series under
18 #                  debian/patches.
19 #                    You may set TG_BRANCHES to a comma- or -space-separated
20 #                  subset of branches (but not comma-and-space-separated) to
21 #                  export (before including the file).
22 #                    If debian/patches/series already exists, the target
23 #                  will take note, blather a bit, and get out of the way.
24 #
25 #        tg-clean: cleans the source tree, just like the debian/rules clean
26 #                  target, and invokes tg-rmdir
27 #
28 #        tg-rmdir: tries to remove debian/patches, but only if there are no
29 #                  non-TopGit files under the directory.
30 #                    The heuristic is to find files that do not contain a line
31 #                  matchines /^tg:/, minus the series file. If any such files
32 #                  are found, an error occurs. Otherwise, the directory is
33 #                  removed. This means that edits to the series file are
34 #                  likely to vanish.
35 #
36 #   tg-forceclean: cleans the source tree, just like the debian/rules clean
37 #                  target, and forcefully removes the debian/patches
38 #                  directory in doing so. Yes, *force*-fully. WHAM!
39 #
40 # This file also hooks into the standard debian/rules and quilt targets such
41 # that tg-export is automatically invoked before quilt gets a chance to patch
42 # or unpatch the tree.
43 #
44 # The PATCHES_DIR variable can be set before including the file to override
45 # the default debian/patches location.
46 #
47 # Copyright © 2008 martin f. krafft <madduck@debian.org>
48 # Released under terms of the the Artistic Licence 2.0.
49 #
50
51 ifeq ($(shell tg summary -t),)
52   # This is not a TopGit branch, so just blubber a bit.
53
54   tg-export tg-clean tg-forceclean tg-rmdir:
55         @echo "E: The $@ target only works from a TopGit repository." >&2
56 else
57
58 # We are in a TopGit branch, so let the fun begin.
59
60 PATCHES_DIR ?= debian/patches
61
62 # Hook tg-export into quilt's make(1) snippet such that it gets executed
63 # before quilt patches or unpatches.
64 debian/stamp-patched: tg-export
65 unpatch: __tg-temp-export
66 __tg-temp-export:
67         @echo "Exporting TopGit branches to series so that quilt can clean up..." >&2
68         $(MAKE) $(MAKEFLAGS) -f debian/rules tg-export
69
70 # Set some tg-export-specific variables, e.g. default TG_BRANCHES to all
71 # TopGit branches
72 tg-export: TG_BRANCHES ?= $(shell tg summary -t)
73         # see make manual for this trick:
74 tg-export: __TG_COMMA := ,
75 tg-export: __TG_EMPTY :=
76 tg-export: __TG_SPACE := $(__TG_EMPTY) $(__TG_EMPTY)
77
78 ifeq ($(wildcard $(PATCHES_DIR)/series),)
79   # The series file does not exist, so we proceed normally
80
81   # tg-export will not work if the target dir already exists, so try to remove
82   # it by calling tg-rmdir
83   tg-export: tg-rmdir
84         tg export -b $(subst $(__TG_SPACE),$(__TG_COMMA),$(TG_BRANCHES)) --quilt $(PATCHES_DIR)
85 else
86   # The series file already exists, so assume there is nothing to do.
87   tg-export:
88         @echo "I: TopGit patch series already exists, nothing to do." >&2
89         @echo "I: (invoke the \`tg-clean\` target to remove the series.)" >&2
90         @echo "I: (you can ignore this message during a \`tg-clean\` run.)" >&2
91 endif
92
93 ifeq ($(wildcard $(PATCHES_DIR)),)
94   # No patch directory, so nothing to do:
95   tg-rmdir:
96         @true
97
98 else
99   # There is a patch directory, let's try to clean it out:
100   tg-rmdir: __TG_FILES := $(shell find $(PATCHES_DIR) -type f -a -not -path \*/series \
101                                     | xargs grep -l '^tg:')
102   tg-rmdir:
103         # remove all files whose contents matches /^tg:/
104         test -n "$(__TG_FILES)" && rm $(__TG_FILES) || :
105         # remove the series file
106         test -f $(PATCHES_DIR)/series && rm $(PATCHES_DIR)/series || :
107         # try to remove directories
108         find $(PATCHES_DIR) -type d | tac | xargs rmdir 2>/dev/null || :
109         # fail if the directory could not be removed and still exists
110         @test ! -d $(PATCHES_DIR) || { \
111           echo "E: $(PATCHES_DIR) contains non-TopGit-generated files:" >&2; \
112           find $(PATCHES_DIR) -type f -printf 'E:   %P\n' >&2; \
113           false; \
114         }
115 endif
116
117 # Make sure that we try to clean up the patches directory last
118 tg-clean: clean
119         $(MAKE) $(MAKEFLAGS) -f debian/rules tg-rmdir
120
121 tg-forceclean: clean
122         test -d debian/patches && rm -r $(PATCHES_DIR) || :
123
124 endif
125
126 .PHONY: tg-clean tg-export tg-forceclean tg-rmdir
127
128 # vim:ft=make:ts=8:noet