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

[WIP]: make merge nicer to the user

From
Guillaume Cogoni <cogoni.guillaume@gmail.com>
Date
Mar 27, 2022, 15:41 UTC
Message-ID
<CAA0Qn1sBF=PAduCQCXbYkeu4cphw7O+AnvwFNMWijuKYskaT8g@mail.gmail.com>

Hi, We were working on a patch to make merge nicer to the user on tracked/untracked merge conflicts. You can see that idea on this page: https://git.wiki.kernel.org/index.php/SmallProjectsIdeas

When merging a commit which has tracked files with the same name as local untracked files, Git refuses to proceed. We want to change this behaviour. The idea is to check if the untracked and the tracked file has the same content, so we can overwrite it.

Examples of use cases where it can be interesting: The scenarios are the following:

A team member is modifying the templates for a website we are working on. They are adding some images to the images directory (but forgets to add them under source control). They are sending the images by mail, later, to me. I'm adding the images under the source control and pushing them to GitHub together with other changes They cannot pull updates from GitHub because Git doesn't want to overwrite their files. Source : https://stackoverflow.com/questions/1125968/how-do-i-force-git-pull-to-overwrite-local-files

When using rsync to get files from a distant directory, but then those files are pushed on a repo from the distant directory, you don't want to reset the change when you just need to pull the repo because files are the same.

The following parts is our test file:
diff --git a/t/t7615-merge-conflict.sh b/t/t7615-merge-conflict.sh
new file mode 100644
index 0000000000..4d89fe99ed
--- /dev/null
+++ b/t/t7615-merge-conflict.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+#
+# Copyright (c) 2022 Cogoni Guillaume and Bressat Jonathan
+#
+test_description='merge conflitct'
+. ./test-lib.sh
+
+test_expect_success '[FAST_FORWARD] merge conflict when untracked
file and tracked file have the same name and content' '
+ echo content >readme.md &&
+ test_commit "README" readme.md &&
+ git branch B &&
+ git checkout -b A &&
+ echo content >file &&
+ test_commit "tracked_file" file &&
+ git switch B &&
+ echo content >file &&
+ test_merge merge A
+'
+
+test_expect_success '[MERGE] merge conflict when untracked file and
tracked file have the same name and content' '
+ echo content >readme.md &&
+ test_commit "README" readme.md &&
+ git branch A &&
+ git checkout -b B &&
+ echo content1 >file1 &&
+ test_commit "B_tracked_file" file1 &&
+ git checkout A &&
+ echo content2 >file2 &&
+ test_commit "A_tracked_file" file2 &&
+ git switch B &&
+ echo content2 >file2 &&
+ test_merge merge A
+'
+
+test_expect_thatfailure 'merge conflict when untracked file and tracked
file have not the same content but the same name' '
+ echo content >readme.md &&
+ test_commit "README" readme.md &&
+ git branch B &&
+ git checkout -b A &&
+ echo content1 >file &&
+ test_commit "tracked_file" file &&
+ git switch B &&
+ echo content2 >file &&
+ test_merge merge A
+'
+
+test_done
Those tests must have assert in the end but it's just to explain our idea.

Our research lead us to these functions:

verify_absent_1() from /unpack-trees.c seems to be called for all files
and it check if a file from the merged branch exists in the current
branch in regard of the name and the path (In our test above, if a file
from the branch A exist in the branch B.). Then call check_ok_to_remove()
from /unpack-trees.c when an untracked file with the same name than a
tracked file on the merged branch is spotted.

static int verify_absent_1(const struct cache_entry *ce,
enum unpack_trees_error_types error_type,
enum absent_checking_type absent_type,
struct unpack_trees_options *o);

static int check_ok_to_remove(const char *name, int len, int dtype,
const struct cache_entry *ce, struct stat *st,
enum unpack_trees_error_types error_type,
enum absent_checking_type absent_type,
struct unpack_trees_options *o);


We think that a good way to solve this problem is to check the hash
of the tracked and untracked file in check_ok_to_remove and then if
they are similar we can
overwrite it (return 0). The hash is in ce->object_id.
In fact, it's more efficient than decompress the file and check
the content.

Do you think, we are going in the good way, and is it a good idea ?

thanks for your help and review.

Guillaume Cogoni and
Jonathan Bressat
Next: Jonathan
Message 1 of 27 in “[WIP]: make merge nicer to the user”
  1. Guillaume CogoniMar 27, 2022
  2. 0/1 Be nicer to the user on tracked/untracked merge conflictsJonathan, Apr 12, 2022
  3. 1/1 Merge with untracked file that are the same without failure and testJonathan, Apr 12, 2022
  4. Ævar Arnfjörð BjarmasonApr 12, 2022
  5. Junio C HamanoApr 13, 2022
  6. 0/2 Be nicer to the user on tracked/untracked merge conflictsJonathan, Apr 25, 2022
  7. 1/2 t7615: test how merge behave when there is untracked fileJonathan, Apr 25, 2022
  8. 2/2 merge with untracked file that are the same without failureJonathan, Apr 25, 2022
  9. Junio C HamanoApr 25, 2022
  10. Guillaume CogoniApr 25, 2022
  11. Junio C HamanoApr 25, 2022
  12. Ævar Arnfjörð BjarmasonApr 12, 2022
  13. Jonathan BressatApr 14, 2022
  14. Matthieu MoyApr 26, 2022
  15. Junio C HamanoApr 26, 2022
  16. Jonathan BressatApr 28, 2022
  17. 0/4 Be nicer to the user on tracked/untracked merge conflictsJonathan Bressat, May 27, 2022
  18. 1/4 t6436: tests how merge behave when there is untracked file with the same contentJonathan Bressat, May 27, 2022
  19. 2/4 merge with untracked file that are the same without failureJonathan Bressat, May 27, 2022
  20. 3/4 add configuration variable corresponding to --overwrite-same-contentJonathan Bressat, May 27, 2022
  21. 4/4 error message now advice to use the new optionJonathan Bressat, May 27, 2022
  22. Matthieu MoyApr 26, 2022
  23. Matthieu MoyJun 4, 2022
  24. Guillaume CogoniJun 10, 2022
  25. Matthieu MoyJun 4, 2022
  26. Matthieu MoyJun 4, 2022
  27. Matthieu MoyJun 4, 2022

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.