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