@matheusbsbs / matheus-bagliano / commits / 3fed15f568
Fork 0

Add 'ident' conversion.

The 'ident' attribute set to path squashes "$ident:<any bytes except dollor sign>$" to "$ident$" upon checkin, and expands it to "$ident: <blob SHA-1> $" upon checkout. As we have two conversions that affect checkin/checkout paths, clarify how they interact with each other. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 21, 2007 at 19:09 UTC 3fed15f568c24ec00ef78fddc6cbb881fbbb0277
3 files changed +252 -12
Documentation/gitattributes.txt
+29 -2
@@ -78,12 +78,17 @@ are attributes-aware.
78 Checking-out and checking-in
79 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80
81 -The attribute `crlf` affects how the contents stored in the
81 +These attributes affect how the contents stored in the
82 repository are copied to the working tree files when commands
83 -such as `git checkout` and `git merge` run. It also affects how
83 +such as `git checkout` and `git merge` run. They also affect how
84 git stores the contents you prepare in the working tree in the
85 repository upon `git add` and `git commit`.
86
87 +`crlf`
88 +^^^^^^
89 +
90 +This attribute controls the line-ending convention.
91 +
92 Set::
93
94 Setting the `crlf` attribute on a path is meant to mark
@@ -129,6 +134,28 @@ converted to LF upon checkin, but there is no conversion done
134 upon checkout.
135
136
137 +`ident`
138 +^^^^^^^
139 +
140 +When the attribute `ident` is set to a path, git replaces
141 +`$ident$` in the blob object with `$ident:`, followed by
142 +40-character hexadecimal blob object name, followed by a dollar
143 +sign `$` upon checkout. Any byte sequence that begins with
144 +`$ident:` and ends with `$` in the worktree file is replaced
145 +with `$ident$` upon check-in.
146 +
147 +
148 +Interaction between checkin/checkout attributes
149 +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
150 +
151 +In the check-in codepath, the worktree file is first converted
152 +with `ident` (if specified), and then with `crlf` (again, if
153 +specified and applicable).
154 +
155 +In the check-out codepath, the blob content is first converted
156 +with `crlf`, and then `ident`.
157 +
158 +
159 Generating diff text
160 ~~~~~~~~~~~~~~~~~~~~
161
convert.c
+184 -10
@@ -1,5 +1,6 @@
1 #include "cache.h"
2 #include "attr.h"
3 +#include "run-command.h"
4
5 /*
6 * convert.c - convert a file when checking it out and checking it in.
@@ -203,10 +204,152 @@ static char *crlf_to_worktree(const char *path, const char *src, unsigned long *
204 static void setup_convert_check(struct git_attr_check *check)
205 {
206 static struct git_attr *attr_crlf;
207 + static struct git_attr *attr_ident;
208
207 - if (!attr_crlf)
209 + if (!attr_crlf) {
210 attr_crlf = git_attr("crlf", 4);
209 - check->attr = attr_crlf;
211 + attr_ident = git_attr("ident", 5);
212 + }
213 + check[0].attr = attr_crlf;
214 + check[1].attr = attr_ident;
215 +}
216 +
217 +static int count_ident(const char *cp, unsigned long size)
218 +{
219 + /*
220 + * "$ident: 0000000000000000000000000000000000000000 $" <=> "$ident$"
221 + */
222 + int cnt = 0;
223 + char ch;
224 +
225 + while (size) {
226 + ch = *cp++;
227 + size--;
228 + if (ch != '$')
229 + continue;
230 + if (size < 6)
231 + break;
232 + if (memcmp("ident", cp, 5))
233 + continue;
234 + ch = cp[5];
235 + cp += 6;
236 + size -= 6;
237 + if (ch == '$')
238 + cnt++; /* $ident$ */
239 + if (ch != ':')
240 + continue;
241 +
242 + /*
243 + * "$ident: ... "; scan up to the closing dollar sign and discard.
244 + */
245 + while (size) {
246 + ch = *cp++;
247 + size--;
248 + if (ch == '$') {
249 + cnt++;
250 + break;
251 + }
252 + }
253 + }
254 + return cnt;
255 +}
256 +
257 +static char *ident_to_git(const char *path, const char *src, unsigned long *sizep, int ident)
258 +{
259 + int cnt;
260 + unsigned long size;
261 + char *dst, *buf;
262 +
263 + if (!ident)
264 + return NULL;
265 + size = *sizep;
266 + cnt = count_ident(src, size);
267 + if (!cnt)
268 + return NULL;
269 + buf = xmalloc(size);
270 +
271 + for (dst = buf; size; size--) {
272 + char ch = *src++;
273 + *dst++ = ch;
274 + if ((ch == '$') && (6 <= size) &&
275 + !memcmp("ident:", src, 6)) {
276 + unsigned long rem = size - 6;
277 + const char *cp = src + 6;
278 + do {
279 + ch = *cp++;
280 + if (ch == '$')
281 + break;
282 + rem--;
283 + } while (rem);
284 + if (!rem)
285 + continue;
286 + memcpy(dst, "ident$", 6);
287 + dst += 6;
288 + size -= (cp - src);
289 + src = cp;
290 + }
291 + }
292 +
293 + *sizep = dst - buf;
294 + return buf;
295 +}
296 +
297 +static char *ident_to_worktree(const char *path, const char *src, unsigned long *sizep, int ident)
298 +{
299 + int cnt;
300 + unsigned long size;
301 + char *dst, *buf;
302 + unsigned char sha1[20];
303 +
304 + if (!ident)
305 + return NULL;
306 +
307 + size = *sizep;
308 + cnt = count_ident(src, size);
309 + if (!cnt)
310 + return NULL;
311 +
312 + hash_sha1_file(src, size, "blob", sha1);
313 + buf = xmalloc(size + cnt * 43);
314 +
315 + for (dst = buf; size; size--) {
316 + const char *cp;
317 + char ch = *src++;
318 + *dst++ = ch;
319 + if ((ch != '$') || (size < 6) || memcmp("ident", src, 5))
320 + continue;
321 +
322 + if (src[5] == ':') {
323 + /* discard up to but not including the closing $ */
324 + unsigned long rem = size - 6;
325 + cp = src + 6;
326 + do {
327 + ch = *cp++;
328 + if (ch == '$')
329 + break;
330 + rem--;
331 + } while (rem);
332 + if (!rem)
333 + continue;
334 + size -= (cp - src);
335 + } else if (src[5] == '$')
336 + cp = src + 5;
337 + else
338 + continue;
339 +
340 + memcpy(dst, "ident: ", 7);
341 + dst += 7;
342 + memcpy(dst, sha1_to_hex(sha1), 40);
343 + dst += 40;
344 + *dst++ = ' ';
345 + size -= (cp - src);
346 + src = cp;
347 + *dst++ = *src++;
348 + size--;
349 + }
350 +
351 + *sizep = dst - buf;
352 + return buf;
353 }
354
355 static int git_path_check_crlf(const char *path, struct git_attr_check *check)
@@ -224,26 +367,57 @@ static int git_path_check_crlf(const char *path, struct git_attr_check *check)
367 return CRLF_GUESS;
368 }
369
370 +static int git_path_check_ident(const char *path, struct git_attr_check *check)
371 +{
372 + const char *value = check->value;
373 +
374 + return !!ATTR_TRUE(value);
375 +}
376 +
377 char *convert_to_git(const char *path, const char *src, unsigned long *sizep)
378 {
229 - struct git_attr_check check[1];
379 + struct git_attr_check check[2];
380 int crlf = CRLF_GUESS;
381 + int ident = 0;
382 + char *buf, *buf2;
383
384 setup_convert_check(check);
233 - if (!git_checkattr(path, 1, check)) {
234 - crlf = git_path_check_crlf(path, check);
385 + if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
386 + crlf = git_path_check_crlf(path, check + 0);
387 + ident = git_path_check_ident(path, check + 1);
388 + }
389 +
390 + buf = crlf_to_git(path, src, sizep, crlf);
391 +
392 + buf2 = ident_to_git(path, buf ? buf : src, sizep, ident);
393 + if (buf2) {
394 + free(buf);
395 + buf = buf2;
396 }
236 - return crlf_to_git(path, src, sizep, crlf);
397 +
398 + return buf;
399 }
400
401 char *convert_to_working_tree(const char *path, const char *src, unsigned long *sizep)
402 {
241 - struct git_attr_check check[1];
403 + struct git_attr_check check[2];
404 int crlf = CRLF_GUESS;
405 + int ident = 0;
406 + char *buf, *buf2;
407
408 setup_convert_check(check);
245 - if (!git_checkattr(path, 1, check)) {
246 - crlf = git_path_check_crlf(path, check);
409 + if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
410 + crlf = git_path_check_crlf(path, check + 0);
411 + ident = git_path_check_ident(path, check + 1);
412 }
248 - return crlf_to_worktree(path, src, sizep, crlf);
413 +
414 + buf = ident_to_worktree(path, src, sizep, ident);
415 +
416 + buf2 = crlf_to_worktree(path, buf ? buf : src, sizep, crlf);
417 + if (buf2) {
418 + free(buf);
419 + buf = buf2;
420 + }
421 +
422 + return buf;
423 }
t/t0021-conversion.sh new
+39
@@ -0,0 +1,39 @@
1 +#!/bin/sh
2 +
3 +test_description='blob conversion via gitattributes'
4 +
5 +. ./test-lib.sh
6 +
7 +test_expect_success setup '
8 + {
9 + echo "*.i ident"
10 + } >.gitattributes &&
11 +
12 + {
13 + echo a b c d e f g h i j k l m
14 + echo n o p q r s t u v w x y z
15 + echo '\''$ident$'\''
16 + } >test &&
17 + cat test >test.t &&
18 + cat test >test.o &&
19 + cat test >test.i &&
20 + git add test test.t test.i &&
21 + rm -f test test.t test.i &&
22 + git checkout -- test test.t test.i
23 +'
24 +
25 +script='s/^\$ident: \([0-9a-f]*\) \$/\1/p'
26 +
27 +test_expect_success check '
28 +
29 + cmp test.o test &&
30 + cmp test.o test.t &&
31 +
32 + # ident should be stripped in the repository
33 + git diff --raw --exit-code :test :test.i &&
34 + id=$(git rev-parse --verify :test) &&
35 + embedded=$(sed -ne "$script" test.i) &&
36 + test "z$id" = "z$embedded"
37 +'
38 +
39 +test_done