am: reload .gitattributes after patching it

When applying multiple patches with git am, or when rebasing using the am backend, it's possible that one of our patches has updated a gitattributes file. Currently, we cache this information, so if a file in a subsequent patch has attributes applied, the file will be written out with the attributes in place as of the time we started the rebase or am operation, not with the attributes applied by the previous patch. This problem does not occur when using the -m or -i flags to rebase. To ensure we write the correct data into the working tree, expire the cache after each patch that touches a path ending in ".gitattributes". Since we load these attributes in multiple separate files, we must expire them accordingly. Verify that both the am and rebase code paths work correctly, including the conflict marker size with am -3. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Sep 2, 2019 at 22:39 UTC 2c65d90f7579a0e2a6460eebce44795587e87043
7 files changed +141 -5
apply.c
+11
@@ -4663,6 +4663,7 @@ static int apply_patch(struct apply_state *state,
4663 struct patch *list = NULL, **listp = &list;
4664 int skipped_patch = 0;
4665 int res = 0;
4666 + int flush_attributes = 0;
4667
4668 state->patch_input_file = filename;
4669 if (read_patch_file(&buf, fd) < 0)
@@ -4690,6 +4691,14 @@ static int apply_patch(struct apply_state *state,
4691 patch_stats(state, patch);
4692 *listp = patch;
4693 listp = &patch->next;
4694 +
4695 + if ((patch->new_name &&
4696 + ends_with_path_components(patch->new_name,
4697 + GITATTRIBUTES_FILE)) ||
4698 + (patch->old_name &&
4699 + ends_with_path_components(patch->old_name,
4700 + GITATTRIBUTES_FILE)))
4701 + flush_attributes = 1;
4702 }
4703 else {
4704 if (state->apply_verbosity > verbosity_normal)
@@ -4766,6 +4775,8 @@ static int apply_patch(struct apply_state *state,
4775 if (state->summary && state->apply_verbosity > verbosity_silent)
4776 summary_patch_list(list);
4777
4778 + if (flush_attributes)
4779 + reset_parsed_attributes();
4780 end:
4781 free_patch_list(list);
4782 strbuf_release(&buf);
convert.c
+20 -1
@@ -8,6 +8,7 @@
8 #include "pkt-line.h"
9 #include "sub-process.h"
10 #include "utf8.h"
11 +#include "ll-merge.h"
12
13 /*
14 * convert.c - convert a file when checking it out and checking it in.
@@ -1293,10 +1294,11 @@ struct conv_attrs {
1294 const char *working_tree_encoding; /* Supported encoding or default encoding if NULL */
1295 };
1296
1297 +static struct attr_check *check;
1298 +
1299 static void convert_attrs(const struct index_state *istate,
1300 struct conv_attrs *ca, const char *path)
1301 {
1299 - static struct attr_check *check;
1302 struct attr_check_item *ccheck = NULL;
1303
1304 if (!check) {
@@ -1339,6 +1341,23 @@ static void convert_attrs(const struct index_state *istate,
1341 ca->crlf_action = CRLF_AUTO_INPUT;
1342 }
1343
1344 +void reset_parsed_attributes(void)
1345 +{
1346 + struct convert_driver *drv, *next;
1347 +
1348 + attr_check_free(check);
1349 + check = NULL;
1350 + reset_merge_attributes();
1351 +
1352 + for (drv = user_convert; drv; drv = next) {
1353 + next = drv->next;
1354 + free((void *)drv->name);
1355 + free(drv);
1356 + }
1357 + user_convert = NULL;
1358 + user_convert_tail = NULL;
1359 +}
1360 +
1361 int would_convert_to_git_filter_fd(const struct index_state *istate, const char *path)
1362 {
1363 struct conv_attrs ca;
convert.h
+6
@@ -94,6 +94,12 @@ void convert_to_git_filter_fd(const struct index_state *istate,
94 int would_convert_to_git_filter_fd(const struct index_state *istate,
95 const char *path);
96
97 +/*
98 + * Reset the internal list of attributes used by convert_to_git and
99 + * convert_to_working_tree.
100 + */
101 +void reset_parsed_attributes(void);
102 +
103 /*****************************************************************
104 *
105 * Streaming conversion support
ll-merge.c
+15 -4
@@ -32,6 +32,20 @@ struct ll_merge_driver {
32 char *cmdline;
33 };
34
35 +static struct attr_check *merge_attributes;
36 +static struct attr_check *load_merge_attributes(void)
37 +{
38 + if (!merge_attributes)
39 + merge_attributes = attr_check_initl("merge", "conflict-marker-size", NULL);
40 + return merge_attributes;
41 +}
42 +
43 +void reset_merge_attributes(void)
44 +{
45 + attr_check_free(merge_attributes);
46 + merge_attributes = NULL;
47 +}
48 +
49 /*
50 * Built-in low-levels
51 */
@@ -354,7 +368,7 @@ int ll_merge(mmbuffer_t *result_buf,
368 struct index_state *istate,
369 const struct ll_merge_options *opts)
370 {
357 - static struct attr_check *check;
371 + struct attr_check *check = load_merge_attributes();
372 static const struct ll_merge_options default_opts;
373 const char *ll_driver_name = NULL;
374 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
@@ -369,9 +383,6 @@ int ll_merge(mmbuffer_t *result_buf,
383 normalize_file(theirs, path, istate);
384 }
385
372 - if (!check)
373 - check = attr_check_initl("merge", "conflict-marker-size", NULL);
374 -
386 git_check_attr(istate, path, check);
387 ll_driver_name = check->items[0].value;
388 if (check->items[1].value) {
ll-merge.h
+1
@@ -26,5 +26,6 @@ int ll_merge(mmbuffer_t *result_buf,
26 const struct ll_merge_options *opts);
27
28 int ll_merge_marker_size(struct index_state *istate, const char *path);
29 +void reset_merge_attributes(void);
30
31 #endif
t/t3400-rebase.sh
+36
@@ -301,6 +301,42 @@ test_expect_success 'rebase--am.sh and --show-current-patch' '
301 )
302 '
303
304 +test_expect_success 'rebase --am and .gitattributes' '
305 + test_create_repo attributes &&
306 + (
307 + cd attributes &&
308 + test_commit init &&
309 + git config filter.test.clean "sed -e '\''s/smudged/clean/g'\''" &&
310 + git config filter.test.smudge "sed -e '\''s/clean/smudged/g'\''" &&
311 +
312 + test_commit second &&
313 + git checkout -b test HEAD^ &&
314 +
315 + echo "*.txt filter=test" >.gitattributes &&
316 + git add .gitattributes &&
317 + test_commit third &&
318 +
319 + echo "This text is smudged." >a.txt &&
320 + git add a.txt &&
321 + test_commit fourth &&
322 +
323 + git checkout -b removal HEAD^ &&
324 + git rm .gitattributes &&
325 + git add -u &&
326 + test_commit fifth &&
327 + git cherry-pick test &&
328 +
329 + git checkout test &&
330 + git rebase master &&
331 + grep "smudged" a.txt &&
332 +
333 + git checkout removal &&
334 + git reset --hard &&
335 + git rebase master &&
336 + grep "clean" a.txt
337 + )
338 +'
339 +
340 test_expect_success 'rebase--merge.sh and --show-current-patch' '
341 test_create_repo conflict-merge &&
342 (
t/t4150-am.sh
+52
@@ -1061,4 +1061,56 @@ test_expect_success 'am --quit keeps HEAD where it is' '
1061 test_cmp expected actual
1062 '
1063
1064 +test_expect_success 'am and .gitattibutes' '
1065 + test_create_repo attributes &&
1066 + (
1067 + cd attributes &&
1068 + test_commit init &&
1069 + git config filter.test.clean "sed -e '\''s/smudged/clean/g'\''" &&
1070 + git config filter.test.smudge "sed -e '\''s/clean/smudged/g'\''" &&
1071 +
1072 + test_commit second &&
1073 + git checkout -b test HEAD^ &&
1074 +
1075 + echo "*.txt filter=test conflict-marker-size=10" >.gitattributes &&
1076 + git add .gitattributes &&
1077 + test_commit third &&
1078 +
1079 + echo "This text is smudged." >a.txt &&
1080 + git add a.txt &&
1081 + test_commit fourth &&
1082 +
1083 + git checkout -b removal HEAD^ &&
1084 + git rm .gitattributes &&
1085 + git add -u &&
1086 + test_commit fifth &&
1087 + git cherry-pick test &&
1088 +
1089 + git checkout -b conflict third &&
1090 + echo "This text is different." >a.txt &&
1091 + git add a.txt &&
1092 + test_commit sixth &&
1093 +
1094 + git checkout test &&
1095 + git format-patch --stdout master..HEAD >patches &&
1096 + git reset --hard master &&
1097 + git am patches &&
1098 + grep "smudged" a.txt &&
1099 +
1100 + git checkout removal &&
1101 + git reset --hard &&
1102 + git format-patch --stdout master..HEAD >patches &&
1103 + git reset --hard master &&
1104 + git am patches &&
1105 + grep "clean" a.txt &&
1106 +
1107 + git checkout conflict &&
1108 + git reset --hard &&
1109 + git format-patch --stdout master..HEAD >patches &&
1110 + git reset --hard fourth &&
1111 + test_must_fail git am -3 patches &&
1112 + grep "<<<<<<<<<<" a.txt
1113 + )
1114 +'
1115 +
1116 test_done