git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] mergetool: respect autocrlf by using checkout-index

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 26, 2009, 22:08 UTC
Message-ID
<7vskn53em1.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<7v7i4h4v19.fsf@gitster.siamese.dyndns.org>
Junio C Hamano <gitster@pobox.com> writes:
Show 22 quoted lines
> Charles Bailey <charles@hashpling.org> writes:
>
>> I suspect that the LF endings in the file is due to the fact that in
>> builtin-merge-file.c, the file is opened (fopen) in binary mode
>> ("wb"), but xdl_merge terminates all lines with a raw '\n'.
>>
>> The obvious fix would be to change fopen in builtin-file-merge.c to
>> use "w" instead, but this doesn't work in a number of scenarios. In
>> particular, it is wrong for repositories on windows with core.autocrlf
>> set to false, and would not fix non-windows repositories with
>> core.autocrlf set to true.
>>
>> Currently, I've no idea as to what the solution should be.
>
> "git file-merge" is designed to be a replacement for stock RCS merge, and
> unfortunately it does not call convert_to_working_tree(), nor has any way
> to know for which path it should take the attributes to apply to affect
> what convert_to_working_tree() should do even if it were to call it.
>
> I think we would need a new option to the command that says "pretend this
> is about merging this path, and use the gitattributes specified for it
> when writing out the result."
Perhaps something along this line to teach
    $ git merge-file --attribute-path=frotz.c file1 orig_file file2

to merge what happened since orig_file to file2 into file1, and deposit the result after converting it appropriately for path "frotz.c" obeying core.autocrlf and gitattribute rules.

I see rerere.c::merge() has the exact same issue, but its breakage is half hidden by its use of fopen(path, "w"). It should explicitly use convert_to_working_tree() like this patch does, and write the results out in binary mode.

 builtin-merge-file.c |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)
diff --git i/builtin-merge-file.c w/builtin-merge-file.c
index 96edb97..61d1092 100644
--- i/builtin-merge-file.c
+++ w/builtin-merge-file.c
@@ -5,7 +5,7 @@
 #include "parse-options.h"
 
 static const char *const merge_file_usage[] = {
-	"git merge-file [options] [-L name1 [-L orig [-L name2]]] file1 orig_file file2",
+	"git merge-file [options] [-L name1 [-L orig [-L name2]]] [--attribute-path path] file1 orig_file file2",
 	NULL
 };
 
@@ -30,10 +30,13 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
 	int merge_level = XDL_MERGE_ZEALOUS_ALNUM;
 	int merge_style = 0, quiet = 0;
 	int nongit;
+	char *attribute_path = NULL;
 
 	struct option options[] = {
 		OPT_BOOLEAN('p', "stdout", &to_stdout, "send results to standard output"),
 		OPT_SET_INT(0, "diff3", &merge_style, "use a diff3 based merge", XDL_MERGE_DIFF3),
+		OPT_STRING('a', "attribute-path", &attribute_path, "path",
+			   "apply work-tree conversion for the path"),
 		OPT__QUIET(&quiet),
 		OPT_CALLBACK('L', NULL, names, "name",
 			     "set labels for file1/orig_file/file2", &label_cb),
@@ -73,6 +76,19 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
 	for (i = 0; i < 3; i++)
 		free(mmfs[i].ptr);
 
+	if (ret >= 0 && attribute_path) {
+		struct strbuf buf = STRBUF_INIT;
+		ret = convert_to_working_tree(attribute_path,
+					      result.ptr, result.size,
+					      &buf);
+		free(result.ptr);
+		if (!ret) {
+			size_t len;
+			result.ptr = strbuf_detach(&buf, &len);
+			result.size = len;
+		}
+	}
+
 	if (ret >= 0) {
 		const char *filename = argv[0];
 		FILE *f = to_stdout ? stdout : fopen(filename, "wb");
Previous: Junio C HamanoNext: Junio C Hamano
Message 10 of 12 in “git diff, git mergetool and CRLF conversion”
  1. Hannu KoivistoJan 21, 2009
  2. Charles BaileyJan 21, 2009
  3. Theodore TsoJan 21, 2009
  4. mergetool: respect autocrlf by using checkout-indexCharles Bailey, Jan 21, 2009
  5. Junio C HamanoJan 23, 2009
  6. Charles BaileyJan 23, 2009
  7. Hannu KoivistoJan 26, 2009
  8. Charles BaileyJan 26, 2009
  9. Junio C HamanoJan 26, 2009
  10. Junio C HamanoJan 26, 2009
  11. Junio C HamanoJan 26, 2009
  12. Hannu KoivistoJan 27, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.