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

[PATCH v2 2/2] unpack-trees: support core.allIgnoredFilesArePreciousWhenMerging

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Nov 26, 2018, 19:38 UTC
Message-ID
<20181126193804.30741-3-pclouds@gmail.com>
In-Reply-To
<20181126193804.30741-1-pclouds@gmail.com>

Ignored files can be marked precious to prevent being overwritten during a merge (or even a branch switch). If you really want to make sure no ignored files are overwritten, this config variable is for you.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/config/core.txt |  6 ++++++
 Documentation/gitignore.txt   |  5 +++--
 unpack-trees.c                | 21 ++++++++++++++++++++-
 3 files changed, 29 insertions(+), 3 deletions(-)
diff --git a/Documentation/config/core.txt b/Documentation/config/core.txt
index d0e6635fe0..bff5834c13 100644
--- a/Documentation/config/core.txt
+++ b/Documentation/config/core.txt
@@ -1,3 +1,9 @@
+core.allIgnoredFilesArePreciousWhenMerging::
+	During a merge operation, if "precious" attribute is unset,
+	consider it set. You can explicitly remove "precious"
+	attribute if needed. See linkgit:gitattributes[5] for more
+	information.
+
 core.fileMode::
 	Tells Git if the executable bit of files in the working tree
 	is to be honored.
diff --git a/Documentation/gitignore.txt b/Documentation/gitignore.txt
index 0e9614289e..2832df7178 100644
--- a/Documentation/gitignore.txt
+++ b/Documentation/gitignore.txt
@@ -142,8 +142,9 @@ To stop tracking a file that is currently tracked, use
 'git rm --cached'.
 
 Ignored files are generally considered discardable. See `precious`
-attribute in linkgit:gitattributes[5] to change the behavior regarding
-ignored files.
+attribute in linkgit:gitattributes[5] and
+`core.allIgnoredFilesArePreciousWhenMerging` in linkgit:git-config[1]
+to change the behavior regarding ignored files.
 
 EXAMPLES
 --------
diff --git a/unpack-trees.c b/unpack-trees.c
index 9a5aadc084..df3b163e2e 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -1877,6 +1877,25 @@ static int icase_exists(struct unpack_trees_options *o, const char *name, int le
 	return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
 }
 
+static int is_precious_ignored_file(struct index_state *istate, const char *path)
+{
+	static struct attr_check *check;
+	int all_precious;
+
+	if (!check)
+		check = attr_check_initl("precious", NULL);
+	if (!check)
+		return 0;
+
+	git_check_attr(istate, path, check);
+	if (ATTR_UNSET(check->items[0].value) &&
+	    !git_config_get_bool("core.allignoredfilesarepreciouswhenmerging",
+				 &all_precious) &&
+	    all_precious)
+		return 1;
+	return ATTR_TRUE(check->items[0].value);
+}
+
 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,
@@ -1895,7 +1914,7 @@ static int check_ok_to_remove(const char *name, int len, int dtype,
 		return 0;
 
 	if (o->dir &&
-	    !is_precious_file(o->src_index, name) &&
+	    !is_precious_ignored_file(o->src_index, name) &&
 	    is_excluded(o->dir, o->src_index, name, &dtype))
 		/*
 		 * ce->name is explicitly excluded, so it is Ok to
-- 
2.19.1.1327.g328c130451.dirty
Previous: Nguyễn Thái Ngọc Duy
Message 40 of 40 in “Introduce "precious" file concept”
  1. Introduce "precious" file conceptNguyễn Thái Ngọc Duy, Nov 11, 2018
  2. Bert WesargNov 11, 2018
  3. Ævar Arnfjörð BjarmasonNov 11, 2018
  4. Ævar Arnfjörð BjarmasonNov 11, 2018
  5. Duy NguyenNov 12, 2018
  6. Duy NguyenNov 11, 2018
  7. Ævar Arnfjörð BjarmasonNov 11, 2018
  8. Per LundbergNov 12, 2018
  9. Matthieu MoyNov 12, 2018
  10. Ævar Arnfjörð BjarmasonNov 12, 2018
  11. Junio C HamanoNov 12, 2018
  12. Ævar Arnfjörð BjarmasonNov 12, 2018
  13. Junio C HamanoNov 12, 2018
  14. Duy NguyenNov 12, 2018
  15. brian m. carlsonNov 12, 2018
  16. Per LundbergNov 26, 2018
  17. Ævar Arnfjörð BjarmasonNov 26, 2018
  18. Junio C HamanoNov 26, 2018
  19. Ævar Arnfjörð BjarmasonNov 27, 2018
  20. Junio C HamanoNov 28, 2018
  21. Ævar Arnfjörð BjarmasonNov 28, 2018
  22. Junio C HamanoNov 29, 2018
  23. Duy NguyenDec 1, 2018
  24. Duy NguyenNov 26, 2018
  25. Ævar Arnfjörð BjarmasonNov 26, 2018
  26. Duy NguyenNov 26, 2018
  27. Ævar Arnfjörð BjarmasonNov 26, 2018
  28. Duy NguyenNov 26, 2018
  29. Per LundbergNov 27, 2018
  30. Jacob KellerNov 27, 2018
  31. Per LundbergNov 27, 2018
  32. brian m. carlsonNov 28, 2018
  33. Per LundbergNov 28, 2018
  34. Duy NguyenNov 27, 2018
  35. Duy NguyenDec 6, 2018
  36. Eckhard MaaßNov 26, 2018
  37. Junio C HamanoNov 11, 2018
  38. 0/2 Precios files round twoNguyễn Thái Ngọc Duy, Nov 26, 2018
  39. 1/2 Introduce "precious" file conceptNguyễn Thái Ngọc Duy, Nov 26, 2018
  40. 2/2 unpack-trees: support core.allIgnoredFilesArePreciousWhenMergingNguyễn Thái Ngọc Duy, Nov 26, 2018

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.