@matheusbsbs / matheus-bagliano / commits / 1a9d7e9b48
Fork 0

attr.c: read .gitattributes from index as well.

This makes .gitattributes files to be read from the index when they are not checked out to the work tree. This is in line with the way we always allowed low-level tools to operate in sparsely checked out work tree in a reasonable way. It swaps the order of new file creation and converting the blob to work tree representation; otherwise when we are in the middle of checking out .gitattributes we would notice an empty but unwritten .gitattributes file in the work tree and will ignore the copy in the index. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 14, 2007 at 01:41 UTC 1a9d7e9b484e77436edc7f5cacd39c24ec605e6d
3 files changed +150 -11
attr.c
+59 -2
@@ -336,13 +336,70 @@ static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
336 return res;
337 }
338
339 +static void *read_index_data(const char *path)
340 +{
341 + int pos, len;
342 + unsigned long sz;
343 + enum object_type type;
344 + void *data;
345 +
346 + len = strlen(path);
347 + pos = cache_name_pos(path, len);
348 + if (pos < 0) {
349 + /*
350 + * We might be in the middle of a merge, in which
351 + * case we would read stage #2 (ours).
352 + */
353 + int i;
354 + for (i = -pos - 1;
355 + (pos < 0 && i < active_nr &&
356 + !strcmp(active_cache[i]->name, path));
357 + i++)
358 + if (ce_stage(active_cache[i]) == 2)
359 + pos = i;
360 + }
361 + if (pos < 0)
362 + return NULL;
363 + data = read_sha1_file(active_cache[pos]->sha1, &type, &sz);
364 + if (!data || type != OBJ_BLOB) {
365 + free(data);
366 + return NULL;
367 + }
368 + return data;
369 +}
370 +
371 static struct attr_stack *read_attr(const char *path, int macro_ok)
372 {
373 struct attr_stack *res;
374 + char *buf, *sp;
375 + int lineno = 0;
376
377 res = read_attr_from_file(path, macro_ok);
344 - if (!res)
345 - res = xcalloc(1, sizeof(*res));
378 + if (res)
379 + return res;
380 +
381 + res = xcalloc(1, sizeof(*res));
382 +
383 + /*
384 + * There is no checked out .gitattributes file there, but
385 + * we might have it in the index. We allow operation in a
386 + * sparsely checked out work tree, so read from it.
387 + */
388 + buf = read_index_data(path);
389 + if (!buf)
390 + return res;
391 +
392 + for (sp = buf; *sp; ) {
393 + char *ep;
394 + int more;
395 + for (ep = sp; *ep && *ep != '\n'; ep++)
396 + ;
397 + more = (*ep == '\n');
398 + *ep = '\0';
399 + handle_attr_line(res, sp, path, ++lineno, macro_ok);
400 + sp = ep + more;
401 + }
402 + free(buf);
403 return res;
404 }
405
entry.c
+10 -9
@@ -112,6 +112,16 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
112 if (!new)
113 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
114 path, sha1_to_hex(ce->sha1));
115 +
116 + /*
117 + * Convert from git internal format to working tree format
118 + */
119 + buf = convert_to_working_tree(ce->name, new, &size);
120 + if (buf) {
121 + free(new);
122 + new = buf;
123 + }
124 +
125 if (to_tempfile) {
126 strcpy(path, ".merge_file_XXXXXX");
127 fd = mkstemp(path);
@@ -123,15 +133,6 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
133 path, strerror(errno));
134 }
135
126 - /*
127 - * Convert from git internal format to working tree format
128 - */
129 - buf = convert_to_working_tree(ce->name, new, &size);
130 - if (buf) {
131 - free(new);
132 - new = buf;
133 - }
134 -
136 wrote = write_in_full(fd, new, size);
137 close(fd);
138 free(new);
t/t0020-crlf.sh
+81
@@ -290,4 +290,85 @@ test_expect_success '.gitattributes says two and three are text' '
290 fi
291 '
292
293 +test_expect_success 'in-tree .gitattributes (1)' '
294 +
295 + echo "one -crlf" >>.gitattributes &&
296 + git add .gitattributes &&
297 + git commit -m "Add .gitattributes" &&
298 +
299 + rm -rf tmp one dir .gitattributes patch.file three &&
300 + git read-tree --reset -u HEAD &&
301 +
302 + if remove_cr one >/dev/null
303 + then
304 + echo "Eh? one should not have CRLF"
305 + false
306 + else
307 + : happy
308 + fi &&
309 + remove_cr three >/dev/null || {
310 + echo "Eh? three should still have CRLF"
311 + false
312 + }
313 +'
314 +
315 +test_expect_success 'in-tree .gitattributes (2)' '
316 +
317 + rm -rf tmp one dir .gitattributes patch.file three &&
318 + git read-tree --reset HEAD &&
319 + git checkout-index -f -q -u -a &&
320 +
321 + if remove_cr one >/dev/null
322 + then
323 + echo "Eh? one should not have CRLF"
324 + false
325 + else
326 + : happy
327 + fi &&
328 + remove_cr three >/dev/null || {
329 + echo "Eh? three should still have CRLF"
330 + false
331 + }
332 +'
333 +
334 +test_expect_success 'in-tree .gitattributes (3)' '
335 +
336 + rm -rf tmp one dir .gitattributes patch.file three &&
337 + git read-tree --reset HEAD &&
338 + git checkout-index -u .gitattributes &&
339 + git checkout-index -u one dir/two three &&
340 +
341 + if remove_cr one >/dev/null
342 + then
343 + echo "Eh? one should not have CRLF"
344 + false
345 + else
346 + : happy
347 + fi &&
348 + remove_cr three >/dev/null || {
349 + echo "Eh? three should still have CRLF"
350 + false
351 + }
352 +'
353 +
354 +test_expect_success 'in-tree .gitattributes (4)' '
355 +
356 + rm -rf tmp one dir .gitattributes patch.file three &&
357 + git read-tree --reset HEAD &&
358 + git checkout-index -u one dir/two three &&
359 + git checkout-index -u .gitattributes &&
360 +
361 + if remove_cr one >/dev/null
362 + then
363 + echo "Eh? one should not have CRLF"
364 + false
365 + else
366 + : happy
367 + fi &&
368 + remove_cr three >/dev/null || {
369 + echo "Eh? three should still have CRLF"
370 + false
371 + }
372 +'
373 +
374 test_done