Skip to contentSkip to conversation

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

PATCH In review René Scharfe 5 participants 25 messages Jun 15, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 2/3 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.

---
 Documentation/gitattributes.txt |   14 ++++++++++
 archive-tar.c                   |    5 +++-
 archive-zip.c                   |    5 +++-
 archive.h                       |    3 ++
 builtin-archive.c               |   55 ++++++++++++++++++++++++++++++++++++++-
 t/t5000-tar-tree.sh             |   19 +++++++++++++
 6 files changed, 98 insertions(+), 3 deletions(-)

This should already be sufficient for the use case which Michael and Thomas described a while ago, viz. adding a commit ID file to generated archives.

Why did it take me that long to come up with such a simple patch? There was a vacation and a feature freeze in between, but above all I was only recently able to convince myself (using ugly code) that format_commit_message() can indeed be made to expand placeholders to git-describe strings..

Documentation/gitattributes.txt +14 -0
index 46f9d59..47a621b 100644
@@ -421,6 +421,20 @@ frotz	unspecified
 ----------------------------------------------------------------
 
 
+Creating an archive
+~~~~~~~~~~~~~~~~~~~
+
+`specfile`
+^^^^^^^^^^
+
+If the attribute `specfile` is set for a file then git will expand
+several placeholders when adding this file to an archive.  The
+expansion depends on the availability of a commit ID, i.e. if
+gitlink:git-archive[1] has been given a tree instead of a commit or a
+tag then no replacement will be done.  The placeholders are the same
+as those for the option `--pretty=format:` of gitlink:git-log[1].
+
+
 GIT
 ---
 Part of the gitlink:git[7] suite
archive-tar.c +4 -1
index 66fe3e3..c0d95da 100644
@@ -17,6 +17,7 @@ static unsigned long offset;
 static time_t archive_time;
 static int tar_umask = 002;
 static int verbose;
+static const struct commit *commit;
 
 /* writes out the whole block, but only if it is full */
 static void write_if_needed(void)
@@ -285,7 +286,8 @@ static int write_tar_entry(const unsigned char *sha1,
 		buffer = NULL;
 		size = 0;
 	} else {
-		buffer = convert_sha1_file(path.buf, sha1, mode, &type, &size);
+		buffer = sha1_file_to_archive(path.buf, sha1, mode, &type,
+		                              &size, commit);
 		if (!buffer)
 			die("cannot read %s", sha1_to_hex(sha1));
 	}
@@ -304,6 +306,7 @@ int write_tar_archive(struct archiver_args *args)
 
 	archive_time = args->time;
 	verbose = args->verbose;
+	commit = args->commit;
 
 	if (args->commit_sha1)
 		write_global_extended_header(args->commit_sha1);
archive-zip.c +4 -1
index 444e162..f63dff3 100644
@@ -12,6 +12,7 @@
 static int verbose;
 static int zip_date;
 static int zip_time;
+static const struct commit *commit;
 
 static unsigned char *zip_dir;
 static unsigned int zip_dir_size;
@@ -195,7 +196,8 @@ static int write_zip_entry(const unsigned char *sha1,
 		if (S_ISREG(mode) && zlib_compression_level != 0)
 			method = 8;
 		result = 0;
-		buffer = convert_sha1_file(path, sha1, mode, &type, &size);
+		buffer = sha1_file_to_archive(path, sha1, mode, &type, &size,
+		                              commit);
 		if (!buffer)
 			die("cannot read %s", sha1_to_hex(sha1));
 		crc = crc32(crc, buffer, size);
@@ -316,6 +318,7 @@ int write_zip_archive(struct archiver_args *args)
 	zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
 	zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
 	verbose = args->verbose;
+	commit = args->commit;
 
 	if (args->base && plen > 0 && args->base[plen - 1] == '/') {
 		char *base = xstrdup(args->base);
archive.h +3 -0
index 6838dc7..5791e65 100644
@@ -8,6 +8,7 @@ struct archiver_args {
 	const char *base;
 	struct tree *tree;
 	const unsigned char *commit_sha1;
+	const struct commit *commit;
 	time_t time;
 	const char **pathspec;
 	unsigned int verbose : 1;
@@ -42,4 +43,6 @@ extern int write_tar_archive(struct archiver_args *);
 extern int write_zip_archive(struct archiver_args *);
 extern void *parse_extra_zip_args(int argc, const char **argv);
 
+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);
+
 #endif	/* ARCHIVE_H */
builtin-archive.c +54 -1

79 lines not on this page · raw message

t/t5000-tar-tree.sh +19 -0
index 1a4c53a..3d5d01b 100755
@@ -28,12 +28,15 @@ commit id embedding:
 TAR=${TAR:-tar}
 UNZIP=${UNZIP:-unzip}
 
+SPECFILEFORMAT=%H%n
+
 test_expect_success \
     'populate workdir' \
     'mkdir a b c &&
      echo simple textfile >a/a &&
      mkdir a/bin &&
      cp /bin/sh a/bin &&
+     printf "%s" "$SPECFILEFORMAT" >a/specfile &&
      ln -s a a/l1 &&
      (p=long_path_to_a_file && cd a &&
       for depth in 1 2 3 4 5; do mkdir $p && cd $p; done &&
@@ -105,6 +108,22 @@ test_expect_success \
     'diff -r a c/prefix/a'
 
 test_expect_success \
+    'create an archive with a specfile' \
+    'echo specfile specfile >a/.gitattributes &&
+     git archive HEAD >f.tar &&
+     rm a/.gitattributes'
+
+test_expect_success \
+    'extract specfile' \
+    '(mkdir f && cd f && $TAR xf -) <f.tar'
+
+test_expect_success \
+     'validate specfile contents' \
+     'git log --max-count=1 "--pretty=format:$SPECFILEFORMAT" HEAD \
+      >f/a/specfile.expected &&
+      diff f/a/specfile.expected f/a/specfile'
+
+test_expect_success \
     'git archive --format=zip' \
     'git archive --format=zip HEAD >d.zip'
 
Signature · 1 line
1.5.3
#1 lore raw
Quoted 6 lines · René Scharfe

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.

Interesting. I however wonder if "specfile" is a good name for this attribute, although I admit I do not think of anything better offhand.

Quoted 5 lines

Why did it take me that long to come up with such a simple patch? There was a vacation and a feature freeze in between, but above all I was only recently able to convince myself (using ugly code) that format_commit_message() can indeed be made to expand placeholders to git-describe strings..

Thanks. Will take a look.

#2 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 6 lines · René Scharfe

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.

Quoted 4 lines

Interesting. I however wonder if "specfile" is a good name for this attribute, although I admit I do not think of anything better offhand.

"releasefile", perhaps?

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#3 lore raw

Hi,

Quoted 10 lines · Andreas Ericsson
Quoted 9 lines · Junio C Hamano
Quoted 6 lines · Ren? Scharfe

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.

Quoted 4 lines
Quoted 2 lines

Interesting. I however wonder if "specfile" is a good name for this attribute, although I admit I do not think of anything better offhand.

"releasefile", perhaps?

Maybe we should not so much name it by purpose, but by function. How about "substformat" for the attribute name, and replacing any $Format:blablub$ inside those files with something a la --pretty=format:blablub?

Ciao,
Dscho

#4 lore raw
Quoted 13 lines · Johannes Schindelin

Hi,

Quoted 9 lines · Andreas Ericsson
Quoted 8 lines · Junio C Hamano
Quoted 6 lines · René Scharfe

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.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

Interesting. I however wonder if "specfile" is a good name for this attribute, although I admit I do not think of anything better offhand.

"releasefile", perhaps?

Maybe we should not so much name it by purpose, but by function. How about "substformat" for the attribute name, and replacing any $Format:blablub$ inside those files with something a la --pretty=format:blablub?

I like the $Format:...$ notation. How about naming the attribute "template", as that's what a thus marked file is?

René

#5 lore raw
Quoted 7 lines · René Scharfe
Quoted 4 lines

Maybe we should not so much name it by purpose, but by function. How about "substformat" for the attribute name, and replacing any $Format:blablub$ inside those files with something a la --pretty=format:blablub?

I like the $Format:...$ notation. How about naming the attribute "template", as that's what a thus marked file is?

Sounds good, although I suspect "template" might confuse newbies that checkout may apply the substitution as well. How about something with "export" in it? export-subst, perhaps?

#6 lore raw

[PATCH 4/3] archive: specfile syntax change: "$Format:%PLCHLDR$" instead of just "%PLCHLDR"

As suggested by Johannes, --pretty=format: placeholders in specfiles need to be wrapped in $Format:...$ now. This syntax change restricts the expansion of placeholders and makes it easier to use with files that contain non-placeholder percent signs.

---
 Documentation/gitattributes.txt |    5 +++-
 builtin-archive.c               |   52 +++++++++++++++++++++++++++++++++++---
 t/t5000-tar-tree.sh             |    4 +-
 3 files changed, 53 insertions(+), 8 deletions(-)
Documentation/gitattributes.txt +4 -1
index 47a621b..37b3be8 100644
@@ -432,7 +432,10 @@ several placeholders when adding this file to an archive.  The
 expansion depends on the availability of a commit ID, i.e. if
 gitlink:git-archive[1] has been given a tree instead of a commit or a
 tag then no replacement will be done.  The placeholders are the same
-as those for the option `--pretty=format:` of gitlink:git-log[1].
+as those for the option `--pretty=format:` of gitlink:git-log[1],
+except that they need to be wrapped like this: `$Format:PLACEHOLDERS$`
+in the file.  E.g. the string `$Format:%H$` will be replaced by the
+commit hash.
 
 
 GIT
builtin-archive.c +47 -5

70 lines not on this page · raw message

t/t5000-tar-tree.sh +2 -2
index 3d5d01b..6e89e07 100755
@@ -36,7 +36,7 @@ test_expect_success \
      echo simple textfile >a/a &&
      mkdir a/bin &&
      cp /bin/sh a/bin &&
-     printf "%s" "$SPECFILEFORMAT" >a/specfile &&
+     printf "A\$Format:%s\$O" "$SPECFILEFORMAT" >a/specfile &&
      ln -s a a/l1 &&
      (p=long_path_to_a_file && cd a &&
       for depth in 1 2 3 4 5; do mkdir $p && cd $p; done &&
@@ -119,7 +119,7 @@ test_expect_success \
 
 test_expect_success \
      'validate specfile contents' \
-     'git log --max-count=1 "--pretty=format:$SPECFILEFORMAT" HEAD \
+     'git log --max-count=1 "--pretty=format:A${SPECFILEFORMAT}O" HEAD \
       >f/a/specfile.expected &&
       diff f/a/specfile.expected f/a/specfile'
 
Signature · 1 line
1.5.3
#7 lore raw

Re: [PATCH 4/3] archive: specfile syntax change: "$Format:%PLCHLDR$" instead of just "%PLCHLDR"

Hi,

Quoted 2 lines · René Scharfe

As suggested by Johannes, --pretty=format: placeholders in specfiles need to be wrapped in $Format:...$ now.

Thanks.

builtin-archive.c static int run_remote_archiver(const char *remote, int argc, · line 85

Quoted 10 lines
builtin-archive.c +2 -0
index faccce3..a8a0f01 100644
@@ -81,14 +81,58 @@ static int run_remote_archiver(const char *remote, int argc,
 	return !!rv;
 }
 
+static void *format_specfile(const struct commit *commit, const char *format,
+                             unsigned long *sizep)

Should this not be "char *buffer" instead of "const char *format"? Or even better: a "struct strbuf *"?

builtin-archive.c static int run_remote_archiver(const char *remote, int argc, · line 94

Quoted 9 lines
builtin-archive.c +9 -0
+{
+	unsigned long len = *sizep, result_len = 0;
+	const char *a = format;
+	char *result = NULL;
+
+	for (;;) {
+		const char *b, *c;
+		char *fmt, *formatted = NULL;
+		unsigned long a_len, fmt_len, formatted_len, allocated = 0;

Maybe initialise formatted_len, just to be on the safe side?

builtin-archive.c static int run_remote_archiver(const char *remote, int argc, · line 98

Quoted 4 lines
builtin-archive.c +4 -0
+
+		b = memchr(a, '$', len);
+		if (!b || a + len < b + 9 || memcmp(b + 1, "Format:", 7))
+			break;

Wouldn't memmem(buffer, len, "$Format:", 8) be better here?

A general comment: since you plan to output the result into a file anyway, it should be even easier to avoid realloc(), and do a print_formatted_specfile() instead of a format_specfile(), no?

Ciao,
Dscho

#8 lore raw

Re: [PATCH 4/3] archive: specfile syntax change: "$Format:%PLCHLDR$" instead of just "%PLCHLDR"

Quoted 5 lines · Johannes Schindelin

builtin-archive.c static int run_remote_archiver(const char *remote, int argc, · line 85

Quoted 2 lines
builtin-archive.c +2 -0
+static void *format_specfile(const struct commit *commit, const char *format,
+                             unsigned long *sizep)

Should this not be "char *buffer" instead of "const char *format"? Or even better: a "struct strbuf *"?

const is correct, because the data in the buffer shouldn't be (and isn't) modified.

"buffer" is a generic term; "format" on the other hand indicates the meaning of the data within the buffer.

Input and output might indeed be passed along as struct strbuf, even more so since the new API encourages its use as generic buffer (format can contain NULs).

Quoted 11 lines

builtin-archive.c static int run_remote_archiver(const char *remote, int argc, · line 94

Quoted 9 lines
builtin-archive.c +9 -0
+{
+	unsigned long len = *sizep, result_len = 0;
+	const char *a = format;
+	char *result = NULL;
+
+	for (;;) {
+		const char *b, *c;
+		char *fmt, *formatted = NULL;
+		unsigned long a_len, fmt_len, formatted_len, allocated = 0;

Maybe initialise formatted_len, just to be on the safe side?

I wish I could use C99 syntax and move its declaration down to its first use, then it would be obvious that formatted_len is never used without initialization.

Quoted 6 lines

builtin-archive.c static int run_remote_archiver(const char *remote, int argc, · line 98

Quoted 4 lines
builtin-archive.c +4 -0
+
+		b = memchr(a, '$', len);
+		if (!b || a + len < b + 9 || memcmp(b + 1, "Format:", 7))
+			break;

Wouldn't memmem(buffer, len, "$Format:", 8) be better here?

Oh, that's a nice GNU extension, didn't know it before. We might import it to compat etc., but I think that's better left for a follow-up patch.

Quoted 3 lines

A general comment: since you plan to output the result into a file anyway, it should be even easier to avoid realloc(), and do a print_formatted_specfile() instead of a format_specfile(), no?

Hmm, not sure what you mean. At least archive-tar needs the expanded contents in a buffer (not immediately written to stdout) because it tries to mimic a real tar and always writes in blocks of 10k and therefore needs to buffer the output.

Thanks!
René

#9 lore raw

Re: [PATCH 4/3] archive: specfile syntax change: "$Format:%PLCHLDR$" instead of just "%PLCHLDR"

Quoted 9 lines · René Scharfe
Quoted 5 lines · Johannes Schindelin

builtin-archive.c static int run_remote_archiver(const char *remote, int argc, · line 98

Quoted 4 lines
builtin-archive.c +4 -0
+
+		b = memchr(a, '$', len);
+		if (!b || a + len < b + 9 || memcmp(b + 1, "Format:", 7))
+			break;

Wouldn't memmem(buffer, len, "$Format:", 8) be better here?

Oh, that's a nice GNU extension, didn't know it before. We might import it to compat etc., but I think that's better left for a follow-up patch.

Just noticed: if the memcmp() above finds a difference, the code should *not* break out of the loop. Ahem. Perhaps I should first add memmem() after all...

René

#10 lore raw

Re: [PATCH 4/3] archive: specfile syntax change: "$Format:%PLCHLDR$" instead of just "%PLCHLDR"

Quoted 3 lines · René Scharfe

Just noticed: if the memcmp() above finds a difference, the code should *not* break out of the loop. Ahem. Perhaps I should first add memmem() after all...

That sounds like a sensible thing to do.

Will drop this copy and use updated 4/3 and 3.5/3; thanks.

#11 lore raw

Re: [PATCH 4/3] archive: specfile syntax change: "$Format:%PLCHLDR$" instead of just "%PLCHLDR"

In reply to René Scharfe

Hi,

Quoted 9 lines · René Scharfe
Quoted 6 lines · Johannes Schindelin

builtin-archive.c static int run_remote_archiver(const char *remote, int argc, · line 98

Quoted 4 lines
builtin-archive.c +4 -0
+
+		b = memchr(a, '$', len);
+		if (!b || a + len < b + 9 || memcmp(b + 1, "Format:", 7))
+			break;

Wouldn't memmem(buffer, len, "$Format:", 8) be better here?

Oh, that's a nice GNU extension, didn't know it before.

Oh sorry, I didn't even realise that this is a GNU extension...

Quoted 8 lines
Quoted 3 lines

A general comment: since you plan to output the result into a file anyway, it should be even easier to avoid realloc(), and do a print_formatted_specfile() instead of a format_specfile(), no?

Hmm, not sure what you mean. At least archive-tar needs the expanded contents in a buffer (not immediately written to stdout) because it tries to mimic a real tar and always writes in blocks of 10k and therefore needs to buffer the output.

Yeah, I missed that. Thanks for explaining it to me!

Ciao,
Dscho

#12 lore raw

PATCH 1/1 add memmem()

In reply to René Scharfe

memmem() is a nice GNU extension for searching a length limited string in another one.

This compat version is based on the version found in glibc 2.2 (GPL 2); I only removed the optimization of checking the first char by hand, and generally tried to keep the code simple. We can add it back if memcmp shows up high in a profile, but for now I prefer to keep it (almost trivially) simple.

Since I don't really know which platforms beside those with a glibc have their own memmem(), I used a heuristic: if NO_STRCASESTR is set, then NO_MEMMEM is set, too.

---
 Makefile          |   11 +++++++++++
 compat/memmem.c   |   29 +++++++++++++++++++++++++++++
 git-compat-util.h |    6 ++++++
 3 files changed, 46 insertions(+), 0 deletions(-)
 create mode 100644 compat/memmem.c
Makefile +11 -0
index 51af531..bae073f 100644
@@ -28,6 +28,8 @@ all::
 #
 # Define NO_STRCASESTR if you don't have strcasestr.
 #
+# Define NO_MEMMEM if you don't have memmem.
+#
 # Define NO_STRLCPY if you don't have strlcpy.
 #
 # Define NO_STRTOUMAX if you don't have strtoumax in the C library.
@@ -402,6 +404,7 @@ ifeq ($(uname_S),SunOS)
 	NEEDS_NSL = YesPlease
 	SHELL_PATH = /bin/bash
 	NO_STRCASESTR = YesPlease
+	NO_MEMMEM = YesPlease
 	NO_HSTRERROR = YesPlease
 	ifeq ($(uname_R),5.8)
 		NEEDS_LIBICONV = YesPlease
@@ -424,6 +427,7 @@ ifeq ($(uname_O),Cygwin)
 	NO_D_TYPE_IN_DIRENT = YesPlease
 	NO_D_INO_IN_DIRENT = YesPlease
 	NO_STRCASESTR = YesPlease
+	NO_MEMMEM = YesPlease
 	NO_SYMLINK_HEAD = YesPlease
 	NEEDS_LIBICONV = YesPlease
 	NO_FAST_WORKING_DIRECTORY = UnfortunatelyYes
@@ -442,6 +446,7 @@ ifeq ($(uname_S),FreeBSD)
 endif
 ifeq ($(uname_S),OpenBSD)
 	NO_STRCASESTR = YesPlease
+	NO_MEMMEM = YesPlease
 	NEEDS_LIBICONV = YesPlease
 	BASIC_CFLAGS += -I/usr/local/include
 	BASIC_LDFLAGS += -L/usr/local/lib
@@ -456,6 +461,7 @@ ifeq ($(uname_S),NetBSD)
 endif
 ifeq ($(uname_S),AIX)
 	NO_STRCASESTR=YesPlease
+	NO_MEMMEM = YesPlease
 	NO_STRLCPY = YesPlease
 	NEEDS_LIBICONV=YesPlease
 endif
@@ -467,6 +473,7 @@ ifeq ($(uname_S),IRIX64)
 	NO_IPV6=YesPlease
 	NO_SETENV=YesPlease
 	NO_STRCASESTR=YesPlease
+	NO_MEMMEM = YesPlease
 	NO_STRLCPY = YesPlease
 	NO_SOCKADDR_STORAGE=YesPlease
 	SHELL_PATH=/usr/gnu/bin/bash
@@ -661,6 +668,10 @@ ifdef NO_HSTRERROR
 	COMPAT_CFLAGS += -DNO_HSTRERROR
 	COMPAT_OBJS += compat/hstrerror.o
 endif
+ifdef NO_MEMMEM
+	COMPAT_CFLAGS += -DNO_MEMMEM
+	COMPAT_OBJS += compat/memmem.o
+endif
 
 ifeq ($(TCLTK_PATH),)
 NO_TCLTK=NoThanks
compat/memmem.c new file +29 -0
new file mode 100644
index 0000000..cd0d877
@@ -0,0 +1,29 @@
+#include "../git-compat-util.h"
+
+void *gitmemmem(const void *haystack, size_t haystack_len,
+                const void *needle, size_t needle_len)
+{
+	const char *begin = haystack;
+	const char *last_possible = begin + haystack_len - needle_len;
+
+	/*
+	 * The first occurrence of the empty string is deemed to occur at
+	 * the beginning of the string.
+	 */
+	if (needle_len == 0)
+		return (void *)begin;
+
+	/*
+	 * Sanity check, otherwise the loop might search through the whole
+	 * memory.
+	 */
+	if (haystack_len < needle_len)
+		return NULL;
+
+	for (; begin <= last_possible; begin++) {
+		if (!memcmp(begin, needle, needle_len))
+			return (void *)begin;
+	}
+
+	return NULL;
+}
git-compat-util.h +6 -0
index ca0a597..1bfbdeb 100644
@@ -172,6 +172,12 @@ extern uintmax_t gitstrtoumax(const char *, char **, int);
 extern const char *githstrerror(int herror);
 #endif
 
+#ifdef NO_MEMMEM
+#define memmem gitmemmem
+void *gitmemmem(const void *haystack, size_t haystacklen,
+                const void *needle, size_t needlelen);
+#endif
+
 extern void release_pack_memory(size_t, int);
 
 static inline char* xstrdup(const char *str)
Signature · 1 line
1.5.3
#13 lore raw

[PATCH 4/3] archive: specfile syntax change: "$Format:%PLCHLDR$" instead of just "%PLCHLDR" (take 2)

In reply to René Scharfe

As suggested by Johannes, --pretty=format: placeholders in specfiles need to be wrapped in $Format:...$ now. This syntax change restricts the expansion of placeholders and makes it easier to use with files that contain non-placeholder percent signs.

---
This replacement patch fixes a bug in the "$Format:" search logic. It now uses memmem() and thus depends on patch 3.5 which introduces this function. Patch 5 still applies unchanged.

 Documentation/gitattributes.txt |    5 +++-
 builtin-archive.c               |   52 +++++++++++++++++++++++++++++++++++---
 t/t5000-tar-tree.sh             |    4 +-
 3 files changed, 53 insertions(+), 8 deletions(-)
Documentation/gitattributes.txt +4 -1
index 47a621b..37b3be8 100644
@@ -432,7 +432,10 @@ several placeholders when adding this file to an archive.  The
 expansion depends on the availability of a commit ID, i.e. if
 gitlink:git-archive[1] has been given a tree instead of a commit or a
 tag then no replacement will be done.  The placeholders are the same
-as those for the option `--pretty=format:` of gitlink:git-log[1].
+as those for the option `--pretty=format:` of gitlink:git-log[1],
+except that they need to be wrapped like this: `$Format:PLACEHOLDERS$`
+in the file.  E.g. the string `$Format:%H$` will be replaced by the
+commit hash.
 
 
 GIT
builtin-archive.c +47 -5

70 lines not on this page · raw message

t/t5000-tar-tree.sh +2 -2
index 3d5d01b..6e89e07 100755
@@ -36,7 +36,7 @@ test_expect_success \
      echo simple textfile >a/a &&
      mkdir a/bin &&
      cp /bin/sh a/bin &&
-     printf "%s" "$SPECFILEFORMAT" >a/specfile &&
+     printf "A\$Format:%s\$O" "$SPECFILEFORMAT" >a/specfile &&
      ln -s a a/l1 &&
      (p=long_path_to_a_file && cd a &&
       for depth in 1 2 3 4 5; do mkdir $p && cd $p; done &&
@@ -119,7 +119,7 @@ test_expect_success \
 
 test_expect_success \
      'validate specfile contents' \
-     'git log --max-count=1 "--pretty=format:$SPECFILEFORMAT" HEAD \
+     'git log --max-count=1 "--pretty=format:A${SPECFILEFORMAT}O" HEAD \
       >f/a/specfile.expected &&
       diff f/a/specfile.expected f/a/specfile'
 
Signature · 1 line
1.5.3
#14 lore raw

[PATCH 5/3] archive: rename attribute specfile to export-subst

In reply to Junio C Hamano
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · René Scharfe
Quoted 4 lines

Maybe we should not so much name it by purpose, but by function. How about "substformat" for the attribute name, and replacing any $Format:blablub$ inside those files with something a la --pretty=format:blablub?

Quoted 6 lines
Quoted 2 lines

I like the $Format:...$ notation. How about naming the attribute "template", as that's what a thus marked file is?

Sounds good, although I suspect "template" might confuse newbies that checkout may apply the substitution as well. How about something with "export" in it? export-subst, perhaps?

Well, including "export" in the name makes sense, yes. I can't come up with a better name, let's take this.

--- snip! ---
As suggested by Junio and Johannes, change the name of the former attribute specfile to export-subst to indicate its function rather than purpose and to make clear that it is not applied to working tree files.

---

 Documentation/gitattributes.txt |    6 +++---
 builtin-archive.c               |   14 +++++++-------
 t/t5000-tar-tree.sh             |   18 +++++++++---------
 3 files changed, 19 insertions(+), 19 deletions(-)
Documentation/gitattributes.txt +3 -3
index 37b3be8..d0e951e 100644
@@ -424,10 +424,10 @@ frotz	unspecified
 Creating an archive
 ~~~~~~~~~~~~~~~~~~~
 
-`specfile`
-^^^^^^^^^^
+`export-subst`
+^^^^^^^^^^^^^^
 
-If the attribute `specfile` is set for a file then git will expand
+If the attribute `export-subst` is set for a file then git will expand
 several placeholders when adding this file to an archive.  The
 expansion depends on the availability of a commit ID, i.e. if
 gitlink:git-archive[1] has been given a tree instead of a commit or a
builtin-archive.c +7 -7
index a8a0f01..af14837 100644
@@ -81,8 +81,8 @@ static int run_remote_archiver(const char *remote, int argc,
 	return !!rv;
 }
 
-static void *format_specfile(const struct commit *commit, const char *format,
-                             unsigned long *sizep)
+static void *format_subst(const struct commit *commit, const char *format,
+                          unsigned long *sizep)
 {
 	unsigned long len = *sizep, result_len = 0;
 	const char *a = format;
@@ -131,22 +131,22 @@ static void *convert_to_archive(const char *path,
                                 const void *src, unsigned long *sizep,
                                 const struct commit *commit)
 {
-	static struct git_attr *attr_specfile;
+	static struct git_attr *attr_export_subst;
 	struct git_attr_check check[1];
 
 	if (!commit)
 		return NULL;
 
-        if (!attr_specfile)
-                attr_specfile = git_attr("specfile", 8);
+        if (!attr_export_subst)
+                attr_export_subst = git_attr("export-subst", 12);
 
-	check[0].attr = attr_specfile;
+	check[0].attr = attr_export_subst;
 	if (git_checkattr(path, ARRAY_SIZE(check), check))
 		return NULL;
 	if (!ATTR_TRUE(check[0].value))
 		return NULL;
 
-	return format_specfile(commit, src, sizep);
+	return format_subst(commit, src, sizep);
 }
 
 void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
t/t5000-tar-tree.sh +9 -9
index 6e89e07..42e28ab 100755
@@ -28,7 +28,7 @@ commit id embedding:
 TAR=${TAR:-tar}
 UNZIP=${UNZIP:-unzip}
 
-SPECFILEFORMAT=%H%n
+SUBSTFORMAT=%H%n
 
 test_expect_success \
     'populate workdir' \
@@ -36,7 +36,7 @@ test_expect_success \
      echo simple textfile >a/a &&
      mkdir a/bin &&
      cp /bin/sh a/bin &&
-     printf "A\$Format:%s\$O" "$SPECFILEFORMAT" >a/specfile &&
+     printf "A\$Format:%s\$O" "$SUBSTFORMAT" >a/substfile &&
      ln -s a a/l1 &&
      (p=long_path_to_a_file && cd a &&
       for depth in 1 2 3 4 5; do mkdir $p && cd $p; done &&
@@ -108,20 +108,20 @@ test_expect_success \
     'diff -r a c/prefix/a'
 
 test_expect_success \
-    'create an archive with a specfile' \
-    'echo specfile specfile >a/.gitattributes &&
+    'create an archive with a substfile' \
+    'echo substfile export-subst >a/.gitattributes &&
      git archive HEAD >f.tar &&
      rm a/.gitattributes'
 
 test_expect_success \
-    'extract specfile' \
+    'extract substfile' \
     '(mkdir f && cd f && $TAR xf -) <f.tar'
 
 test_expect_success \
-     'validate specfile contents' \
-     'git log --max-count=1 "--pretty=format:A${SPECFILEFORMAT}O" HEAD \
-      >f/a/specfile.expected &&
-      diff f/a/specfile.expected f/a/specfile'
+     'validate substfile contents' \
+     'git log --max-count=1 "--pretty=format:A${SUBSTFORMAT}O" HEAD \
+      >f/a/substfile.expected &&
+      diff f/a/substfile.expected f/a/substfile'
 
 test_expect_success \
     'git archive --format=zip' \
#15 lore raw

Re: [PATCH 5/3] archive: rename attribute specfile to export-subst

Hi,

Quoted 6 lines · René Scharfe

As suggested by Junio and Johannes, change the name of the former attribute specfile to export-subst to indicate its function rather than purpose and to make clear that it is not applied to working tree files.

ACK!

(Even if I did not really suggest "export-subst", which I like very much...)

The bigger question is now if these two patches should be folded back into your original patch series, or stand alone as commits of their own...

Ciao,
Dscho

#16 lore raw

Re: [PATCH 5/3] archive: rename attribute specfile to export-subst

Quoted 3 lines · Johannes Schindelin

The bigger question is now if these two patches should be folded back into your original patch series, or stand alone as commits of their own...

Yes, indeed. I submitted them as add-on patches because I noticed the first three are already in next, but I sure can make a fresh three-part series based on master. Junio?

Thanks,
René

#17 lore raw

Re: [PATCH 5/3] archive: rename attribute specfile to export-subst

In reply to Johannes Schindelin
Quoted 2 lines · Johannes Schindelin

The bigger question is now if these two patches should be folded back into your original patch series, or stand alone as commits of their own...

That is no brainer, as there is a simple and hard rule that any topic already in 'next' are not to be rewound ever. Follow-up patches are the right thing to do in this case.

#18 lore raw

Re: [PATCH 5/3] archive: rename attribute specfile to export-subst

Hi,

Quoted 9 lines · Junio C Hamano
Quoted 3 lines · Johannes Schindelin

The bigger question is now if these two patches should be folded back into your original patch series, or stand alone as commits of their own...

That is no brainer, as there is a simple and hard rule that any topic already in 'next' are not to be rewound ever. Follow-up patches are the right thing to do in this case.

Right. At the time I suggested it, I was not aware that the patches were in next already.

Sorry,
Dscho

#19 lore raw
In reply to René Scharfe

Hi,

Quoted 17 lines · Ren? Scharfe
Quoted 15 lines · Johannes Schindelin
Quoted 12 lines · Andreas Ericsson
Quoted 10 lines · Junio C Hamano
Quoted 6 lines · Ren? Scharfe

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.

Interesting. I however wonder if "specfile" is a good name for this attribute, although I admit I do not think of anything better offhand.

"releasefile", perhaps?

Quoted 7 lines
Quoted 4 lines

Maybe we should not so much name it by purpose, but by function. How about "substformat" for the attribute name, and replacing any $Format:blablub$ inside those files with something a la --pretty=format:blablub?

I like the $Format:...$ notation. How about naming the attribute "template", as that's what a thus marked file is?

Unless somebody comes up with an even better name, yes (I like "template" better than "substformat" or "releasefile", but I still think that "template" is not descriptive enough).

Ciao,
Dscho

#20 lore raw
In reply to Junio C Hamano
Quoted 7 lines · Junio C Hamano
Quoted 5 lines

Why did it take me that long to come up with such a simple patch? There was a vacation and a feature freeze in between, but above all I was only recently able to convince myself (using ugly code) that format_commit_message() can indeed be made to expand placeholders to git-describe strings..

Thanks. Will take a look.

Here's that ugly code, by the way. It adds two placeholders, %d for description and %D for description depth. Shortcomings of this code:
it adds three members to struct commit, it unconditionally computes the description when format_commit_message() -- even if the format string doesn't contain %d and %D, the patch is not nicely split up. But it convinced me that describe *can* indeed be librarified. :-)

René

 Makefile           |    1 +
 builtin-describe.c |  219 +++------------------------------------------------
 cache.h            |    5 +
 commit.c           |   31 ++++++++
 commit.h           |    5 +
 describe.c         |  170 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 225 insertions(+), 206 deletions(-)
Makefile +1 -0
index 51af531..7ec95f8 100644
@@ -296,6 +296,7 @@ DIFF_OBJS = \
 LIB_OBJS = \
 	blob.o commit.o connect.o csum-file.o cache-tree.o base85.o \
 	date.o diff-delta.o entry.o exec_cmd.o ident.o \
+	describe.o \
 	interpolate.o \
 	lockfile.o \
 	patch-ids.o \
builtin-describe.c +13 -206

250 lines not on this page · raw message

cache.h +5 -0
index 70abbd5..07ee149 100644
@@ -600,4 +600,9 @@ extern int diff_auto_refresh_index;
 /* match-trees.c */
 void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int);
 
+/* describe.c */
+struct commit;
+extern void load_commit_names(int min_prio);
+extern char *describe_commit(struct commit *cmit, int max_candidates, int *depthp);
+
 #endif /* CACHE_H */
commit.c +31 -0
index dc5a064..d2343f2 100644
@@ -455,6 +455,19 @@ void clear_commit_marks(struct commit *commit, unsigned int mark)
 	}
 }
 
+void clear_commit_name_flags(struct commit *commit)
+{
+	struct commit_list *parents;
+
+	commit->name_flags = 0;
+
+	for (parents = commit->parents; parents; parents = parents->next) {
+		/* Have we already cleared this? */
+		if (parents->item->name_flags)
+			clear_commit_name_flags(parents->item);
+	}
+}
+
 /*
  * Generic support for pretty-printing the header
  */
@@ -819,6 +832,8 @@ static long format_commit_message(const struct commit *commit,
 		{ "%Cblue" },	/* blue */
 		{ "%Creset" },	/* reset color */
 		{ "%n" },	/* newline */
+		{ "%d" },	/* description */
+		{ "%D" },	/* description depth */
 		{ "%m" },	/* left/right/bottom */
 	};
 	enum interp_index {
@@ -837,6 +852,7 @@ static long format_commit_message(const struct commit *commit,
 		IBODY,
 		IRED, IGREEN, IBLUE, IRESET_COLOR,
 		INEWLINE,
+		IDESC, IDESC_DEPTH,
 		ILEFT_RIGHT,
 	};
 	struct commit_list *p;
@@ -920,6 +936,21 @@ static long format_commit_message(const struct commit *commit,
 		if (!table[i].value)
 			interp_set_entry(table, i, "<unknown>");
 
+	{
+	struct commit *cmit = (struct commit *)commit;
+	char *name;
+	char tmp[20];
+	int depth;
+	load_commit_names(2);
+	name = describe_commit(cmit, 10, &depth);
+	if (!name)
+		name = "";
+	sprintf(tmp, "%d", depth);
+	interp_set_entry(table, IDESC, name);
+	interp_set_entry(table, IDESC_DEPTH, tmp);
+	clear_commit_name_flags(cmit);
+	}
+
 	do {
 		char *buf = *buf_p;
 		unsigned long space = *space_p;
commit.h +5 -0
index 467872e..69b6d67 100644
@@ -17,6 +17,9 @@ struct commit {
 	struct commit_list *parents;
 	struct tree *tree;
 	char *buffer;
+	char *name;
+	unsigned int name_flags;
+	char name_prio;
 };
 
 extern int save_commit_buffer;
@@ -72,6 +75,8 @@ struct commit *pop_most_recent_commit(struct commit_list **list,
 struct commit *pop_commit(struct commit_list **stack);
 
 void clear_commit_marks(struct commit *commit, unsigned int mark);
+void clear_commit_name_flags(struct commit *commit);
+
 
 /*
  * Performs an in-place topological sort of list supplied.
describe.c new file +170 -0

170 lines not on this page · raw message

#21 lore raw
Quoted 6 lines · René Scharfe

Here's that ugly code, by the way. It adds two placeholders, %d for description and %D for description depth. Shortcomings of this code:
it adds three members to struct commit, it unconditionally computes the description when format_commit_message() -- even if the format string doesn't contain %d and %D, the patch is not nicely split up. But it convinced me that describe *can* indeed be librarified. :-)

Yeah, unconditional computation for %d/%D is really a killer. Otherwise the change itself does not necessarily look too ugly, though.

#22 lore raw
In reply to René Scharfe

Hi,

Quoted 5 lines · Ren? Scharfe

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.

I almost like this approach. Would it not be a little more useful if you could mark the placeholders with something like "$Format: xyz %c$"? Because then we could just shut up all those complainers that want to insert some revision specific information into the files, without affecting formats for printf().

Of course, the idea to keep the worktree unaffected is brilliant.

Ciao,
Dscho

#23 lore raw
Quoted 8 lines · Johannes Schindelin
Quoted 5 lines · Ren? Scharfe

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.

Quoted 7 lines

I almost like this approach. Would it not be a little more useful if you could mark the placeholders with something like "$Format: xyz %c$"? Because then we could just shut up all those complainers that want to insert some revision specific information into the files, without affecting formats for printf().

Of course, the idea to keep the worktree unaffected is brilliant.

I think a bit more layering would be helpful: when using git-svn, one would want to have things like $Id$ and $Date$ expanded, so maybe attribute specs like

somefile: expandmarkers="$Date: %aD$ $Id: ....$"

would be nice having. In the case of git-svn, I would expect them to be generated from git-svn from the respective svn properties, so that the user is not bothered with figuring out the awful $Id$ and whatever strings.

Signature · 1 line
David Kastrup, Kriemhildstr. 15, 44793 Bochum
#24 lore raw
Quoted 10 lines · David Kastrup

I think a bit more layering would be helpful: when using git-svn, one would want to have things like $Id$ and $Date$ expanded, so maybe attribute specs like

somefile: expandmarkers="$Date: %aD$ $Id: ....$"

would be nice having. In the case of git-svn, I would expect them to be generated from git-svn from the respective svn properties, so that the user is not bothered with figuring out the awful $Id$ and whatever strings.

Eek! I'd rather shove this into a new layer below specfile/template expansion, which would do something like that:

s/\$Date:.*\$/$Format:$$Date: %aD$/

This way A) the complexity and ugliness affects only the users of these fields, while $Format:...$ users are not adversely affected, and B) we can first implement $Format:...$ handling and add the rest later.

In order to expand $Id$, git-archive would need to run some kind of callback, right? :-/ Let's first see if such a thing is really needed.

René