2 # tg2quilt.mk - TopGit-to-quilt functionality for debian/rules files
4 # This make(1) snippet facilitates the conversion of TopGit branches to
7 # It is intended to be included from debian/rules files of TopGit-using
8 # packages after including the quilt rules, like so:
10 # include /usr/share/quilt/quilt.make
11 # -include /usr/share/topgit/tg2quilt.mk
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.
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.
22 # tg-export: exports the TopGit patches into a quilt series under
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.
30 # tg-clean: cleans the source tree, just like the debian/rules clean
31 # target, and invokes tg-rmdir
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
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
43 # tg-cleanexport: recreates the debian/patches directory from scratch, using
44 # tg-rmdir and tg-export.
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!
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.
54 # The QUILT_PATCH_DIR variable can be set before including the file to override
55 # the default debian/patches location.
57 # More information, particularly for people working on TopGit-using packages,
58 # can be found in /usr/share/doc/topgit/HOWTO-tg2quilt.gz .
60 # Copyright © 2008 martin f. krafft <madduck@debian.org> Released under terms
61 # of the the Artistic Licence 2.0.
64 ifeq ($(shell tg summary -t),)
65 # This is not a TopGit branch, so just blubber a bit.
67 tg-export tg-clean tg-forceclean tg-rmdir tg-cleanexport:
68 @echo "E: The $@ target only works from a TopGit repository." >&2
71 # We are in a TopGit branch, so let the fun begin.
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)
80 QUILT_PATCH_DIR ?= debian/patches
81 QUILT_STAMPFN ?= patch
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
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
92 # Set some tg-export-specific variables, e.g. default TG_BRANCHES to all
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)
100 ifeq ($(wildcard $(QUILT_PATCH_DIR)/series),)
101 # The series file does not exist, so we proceed normally
103 # tg-export will not work if the target dir already exists, so try to remove
104 # it by calling tg-rmdir
106 tg export -b $(subst $(__TG_SPACE),$(__TG_COMMA),$(TG_BRANCHES)) --quilt $(QUILT_PATCH_DIR)
108 # The series file already exists, so assume there is nothing to do.
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
115 ifeq ($(wildcard $(QUILT_PATCH_DIR)),)
116 # No patch directory, so nothing to do:
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:')
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; \
131 @git update-index -q --ignore-submodules --refresh
132 @if git diff-index --quiet --cached HEAD --ignore-submodules -- > /dev/null || git diff-files --quiet --ignore-submodules > /dev/null; then \
133 echo "E: there are uncommitted changes in the working directory." >&2; \
134 echo "E: please commit or revert all changes." >&2; \
137 # remove all files whose contents matches /^tg:/
139 # remove the series file
140 rm -f $(QUILT_PATCH_DIR)/series
141 # remove dpkg v3 file
142 rm -f $(QUILT_PATCH_DIR)/.dpkg-source-applied
143 # try to remove directories
144 find $(QUILT_PATCH_DIR) -depth -type d -empty -execdir rmdir {} +
145 # fail if the directory could not be removed and still exists
146 @test ! -d $(QUILT_PATCH_DIR) || { \
147 echo "E: $(QUILT_PATCH_DIR) contains non-TopGit-generated files:" >&2; \
148 find $(QUILT_PATCH_DIR) -type f -printf 'E: %P\n' >&2; \
153 # Make sure that we try to clean up the patches directory last
155 $(MAKE) --no-print-directory -f debian/rules tg-rmdir
158 test -d $(QUILT_PATCH_DIR) && rm -r $(QUILT_PATCH_DIR) || :
160 tg-cleanexport: tg-rmdir
161 $(MAKE) --no-print-directory -f debian/rules tg-export
165 .PHONY: tg-clean tg-export tg-forceclean tg-rmdir tg-cleanexport
167 # vim:ft=make:ts=8:noet
168 # -*- Makefile -*-, you silly Emacs! (shamelessly stolen from quilt)