archive: specfile syntax change: "$Format:%PLCHLDR$" instead of just "%PLCHLDR" (take 2)
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. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Sep 7, 2007 at 00:34 UTC
df4a394f91d7d107c2a57e6c1df3638517cab54f
3 files changed
+53
-8
Documentation/gitattributes.txt
+4
-1
@@ -432,7 +432,10 @@ 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].
435
+as those for the option `--pretty=format:` of gitlink:git-log[1],
436
+except that they need to be wrapped like this: `$Format:PLACEHOLDERS$`
437
+in the file. E.g. the string `$Format:%H$` will be replaced by the
438
+commit hash.
439
440
441
GIT
builtin-archive.c
+47
-5
@@ -81,14 +81,58 @@ static int run_remote_archiver(const char *remote, int argc,
81
return !!rv;
82
}
83
84
+static void *format_specfile(const struct commit *commit, const char *format,
85
+ unsigned long *sizep)
86
+{
87
+ unsigned long len = *sizep, result_len = 0;
88
+ const char *a = format;
89
+ char *result = NULL;
90
+
91
+ for (;;) {
92
+ const char *b, *c;
93
+ char *fmt, *formatted = NULL;
94
+ unsigned long a_len, fmt_len, formatted_len, allocated = 0;
95
+
96
+ b = memmem(a, len, "$Format:", 8);
97
+ if (!b || a + len < b + 9)
98
+ break;
99
+ c = memchr(b + 8, '$', len - 8);
100
+ if (!c)
101
+ break;
102
+
103
+ a_len = b - a;
104
+ fmt_len = c - b - 8;
105
+ fmt = xmalloc(fmt_len + 1);
106
+ memcpy(fmt, b + 8, fmt_len);
107
+ fmt[fmt_len] = '\0';
108
+
109
+ formatted_len = format_commit_message(commit, fmt, &formatted,
110
+ &allocated);
111
+ result = xrealloc(result, result_len + a_len + formatted_len);
112
+ memcpy(result + result_len, a, a_len);
113
+ memcpy(result + result_len + a_len, formatted, formatted_len);
114
+ result_len += a_len + formatted_len;
115
+ len -= c + 1 - a;
116
+ a = c + 1;
117
+ }
118
+
119
+ if (result && len) {
120
+ result = xrealloc(result, result_len + len);
121
+ memcpy(result + result_len, a, len);
122
+ result_len += len;
123
+ }
124
+
125
+ *sizep = result_len;
126
+
127
+ return result;
128
+}
129
+
130
static void *convert_to_archive(const char *path,
131
const void *src, unsigned long *sizep,
132
const struct commit *commit)
133
{
134
static struct git_attr *attr_specfile;
135
struct git_attr_check check[1];
90
- char *interpolated = NULL;
91
- unsigned long allocated = 0;
136
137
if (!commit)
138
return NULL;
@@ -102,9 +146,7 @@ static void *convert_to_archive(const char *path,
146
if (!ATTR_TRUE(check[0].value))
147
return NULL;
148
105
- *sizep = format_commit_message(commit, src, &interpolated, &allocated);
106
-
107
- return interpolated;
149
+ return format_specfile(commit, src, sizep);
150
}
151
152
void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
t/t5000-tar-tree.sh
+2
-2
@@ -36,7 +36,7 @@ test_expect_success \
36
echo simple textfile >a/a &&
37
mkdir a/bin &&
38
cp /bin/sh a/bin &&
39
- printf "%s" "$SPECFILEFORMAT" >a/specfile &&
39
+ printf "A\$Format:%s\$O" "$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 &&
@@ -119,7 +119,7 @@ test_expect_success \
119
120
test_expect_success \
121
'validate specfile contents' \
122
- 'git log --max-count=1 "--pretty=format:$SPECFILEFORMAT" HEAD \
122
+ 'git log --max-count=1 "--pretty=format:A${SPECFILEFORMAT}O" HEAD \
123
>f/a/specfile.expected &&
124
diff f/a/specfile.expected f/a/specfile'
125