@matheusbsbs / matheus-bagliano / commits / 8460b2fcd4
Fork 0

archive: specfile support (--pretty=format: in archive files)

Add support for a new attribute, specfile. Files marked as being specfiles are expanded by git-archive when they are written to an archive. It has no effect on worktree files. The same placeholders as those for the option --pretty=format: of git-log et al. can be used. The attribute is useful for creating auto-updating specfiles. It is limited by the underlying function format_commit_message(), though. E.g. currently there is no placeholder for git-describe like output, and expanded specfiles can't contain NUL bytes. That can be fixed in format_commit_message() later and will then benefit users of git-log, too. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Sep 3, 2007 at 20:07 UTC 8460b2fcd45668d91567c36a22ea4f1b14ba133d
6 files changed +98 -3
Documentation/gitattributes.txt
+14
@@ -421,6 +421,20 @@ frotz unspecified
421 ----------------------------------------------------------------
422
423
424 +Creating an archive
425 +~~~~~~~~~~~~~~~~~~~
426 +
427 +`specfile`
428 +^^^^^^^^^^
429 +
430 +If the attribute `specfile` is set for a file then git will expand
431 +several placeholders when adding this file to an archive. The
432 +expansion depends on the availability of a commit ID, i.e. if
433 +gitlink:git-archive[1] has been given a tree instead of a commit or a
434 +tag then no replacement will be done. The placeholders are the same
435 +as those for the option `--pretty=format:` of gitlink:git-log[1].
436 +
437 +
438 GIT
439 ---
440 Part of the gitlink:git[7] suite
archive-tar.c
+4 -1
@@ -17,6 +17,7 @@ static unsigned long offset;
17 static time_t archive_time;
18 static int tar_umask = 002;
19 static int verbose;
20 +static const struct commit *commit;
21
22 /* writes out the whole block, but only if it is full */
23 static void write_if_needed(void)
@@ -285,7 +286,8 @@ static int write_tar_entry(const unsigned char *sha1,
286 buffer = NULL;
287 size = 0;
288 } else {
288 - buffer = convert_sha1_file(path.buf, sha1, mode, &type, &size);
289 + buffer = sha1_file_to_archive(path.buf, sha1, mode, &type,
290 + &size, commit);
291 if (!buffer)
292 die("cannot read %s", sha1_to_hex(sha1));
293 }
@@ -304,6 +306,7 @@ int write_tar_archive(struct archiver_args *args)
306
307 archive_time = args->time;
308 verbose = args->verbose;
309 + commit = args->commit;
310
311 if (args->commit_sha1)
312 write_global_extended_header(args->commit_sha1);
archive-zip.c
+4 -1
@@ -12,6 +12,7 @@
12 static int verbose;
13 static int zip_date;
14 static int zip_time;
15 +static const struct commit *commit;
16
17 static unsigned char *zip_dir;
18 static unsigned int zip_dir_size;
@@ -195,7 +196,8 @@ static int write_zip_entry(const unsigned char *sha1,
196 if (S_ISREG(mode) && zlib_compression_level != 0)
197 method = 8;
198 result = 0;
198 - buffer = convert_sha1_file(path, sha1, mode, &type, &size);
199 + buffer = sha1_file_to_archive(path, sha1, mode, &type, &size,
200 + commit);
201 if (!buffer)
202 die("cannot read %s", sha1_to_hex(sha1));
203 crc = crc32(crc, buffer, size);
@@ -316,6 +318,7 @@ int write_zip_archive(struct archiver_args *args)
318 zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
319 zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
320 verbose = args->verbose;
321 + commit = args->commit;
322
323 if (args->base && plen > 0 && args->base[plen - 1] == '/') {
324 char *base = xstrdup(args->base);
archive.h
+3
@@ -8,6 +8,7 @@ struct archiver_args {
8 const char *base;
9 struct tree *tree;
10 const unsigned char *commit_sha1;
11 + const struct commit *commit;
12 time_t time;
13 const char **pathspec;
14 unsigned int verbose : 1;
@@ -42,4 +43,6 @@ extern int write_tar_archive(struct archiver_args *);
43 extern int write_zip_archive(struct archiver_args *);
44 extern void *parse_extra_zip_args(int argc, const char **argv);
45
46 +extern void *sha1_file_to_archive(const char *path, const unsigned char *sha1, unsigned int mode, enum object_type *type, unsigned long *size, const struct commit *commit);
47 +
48 #endif /* ARCHIVE_H */
builtin-archive.c
+54 -1
@@ -10,6 +10,7 @@
10 #include "exec_cmd.h"
11 #include "pkt-line.h"
12 #include "sideband.h"
13 +#include "attr.h"
14
15 static const char archive_usage[] = \
16 "git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
@@ -80,6 +81,57 @@ static int run_remote_archiver(const char *remote, int argc,
81 return !!rv;
82 }
83
84 +static void *convert_to_archive(const char *path,
85 + const void *src, unsigned long *sizep,
86 + const struct commit *commit)
87 +{
88 + static struct git_attr *attr_specfile;
89 + struct git_attr_check check[1];
90 + char *interpolated = NULL;
91 + unsigned long allocated = 0;
92 +
93 + if (!commit)
94 + return NULL;
95 +
96 + if (!attr_specfile)
97 + attr_specfile = git_attr("specfile", 8);
98 +
99 + check[0].attr = attr_specfile;
100 + if (git_checkattr(path, ARRAY_SIZE(check), check))
101 + return NULL;
102 + if (!ATTR_TRUE(check[0].value))
103 + return NULL;
104 +
105 + *sizep = format_commit_message(commit, src, &interpolated, &allocated);
106 +
107 + return interpolated;
108 +}
109 +
110 +void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
111 + unsigned int mode, enum object_type *type,
112 + unsigned long *size,
113 + const struct commit *commit)
114 +{
115 + void *buffer, *converted;
116 +
117 + buffer = read_sha1_file(sha1, type, size);
118 + if (buffer && S_ISREG(mode)) {
119 + converted = convert_to_working_tree(path, buffer, size);
120 + if (converted) {
121 + free(buffer);
122 + buffer = converted;
123 + }
124 +
125 + converted = convert_to_archive(path, buffer, size, commit);
126 + if (converted) {
127 + free(buffer);
128 + buffer = converted;
129 + }
130 + }
131 +
132 + return buffer;
133 +}
134 +
135 static int init_archiver(const char *name, struct archiver *ar)
136 {
137 int rv = -1, i;
@@ -109,7 +161,7 @@ void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
161 const unsigned char *commit_sha1;
162 time_t archive_time;
163 struct tree *tree;
112 - struct commit *commit;
164 + const struct commit *commit;
165 unsigned char sha1[20];
166
167 if (get_sha1(name, sha1))
@@ -142,6 +194,7 @@ void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
194 }
195 ar_args->tree = tree;
196 ar_args->commit_sha1 = commit_sha1;
197 + ar_args->commit = commit;
198 ar_args->time = archive_time;
199 }
200
t/t5000-tar-tree.sh
+19
@@ -28,12 +28,15 @@ commit id embedding:
28 TAR=${TAR:-tar}
29 UNZIP=${UNZIP:-unzip}
30
31 +SPECFILEFORMAT=%H%n
32 +
33 test_expect_success \
34 'populate workdir' \
35 'mkdir a b c &&
36 echo simple textfile >a/a &&
37 mkdir a/bin &&
38 cp /bin/sh a/bin &&
39 + printf "%s" "$SPECFILEFORMAT" >a/specfile &&
40 ln -s a a/l1 &&
41 (p=long_path_to_a_file && cd a &&
42 for depth in 1 2 3 4 5; do mkdir $p && cd $p; done &&
@@ -104,6 +107,22 @@ test_expect_success \
107 'validate file contents with prefix' \
108 'diff -r a c/prefix/a'
109
110 +test_expect_success \
111 + 'create an archive with a specfile' \
112 + 'echo specfile specfile >a/.gitattributes &&
113 + git archive HEAD >f.tar &&
114 + rm a/.gitattributes'
115 +
116 +test_expect_success \
117 + 'extract specfile' \
118 + '(mkdir f && cd f && $TAR xf -) <f.tar'
119 +
120 +test_expect_success \
121 + 'validate specfile contents' \
122 + 'git log --max-count=1 "--pretty=format:$SPECFILEFORMAT" HEAD \
123 + >f/a/specfile.expected &&
124 + diff f/a/specfile.expected f/a/specfile'
125 +
126 test_expect_success \
127 'git archive --format=zip' \
128 'git archive --format=zip HEAD >d.zip'