Skip to contentSkip to conversation

read-tree: further loosen "working file will be lost" check.

PATCH In review Junio C Hamano 2 participants 2 messages Aug 11, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 read-tree: further loosen "working file will be lost" check.

This follows up commit ed93b449 where we removed overcautious "working file will be lost" check.

A new option "--ignore=.gitignore" can be used to tell the "git-read-tree" command that the user does not mind losing contents in untracked files in the working tree if it needs to be overwritten by a merge (either a two-way "switch branch" merge, or a three-way merge).

---
 builtin-read-tree.c         |   20 ++++++++++
 dir.c                       |    4 +-
 dir.h                       |    3 ++
 t/t1004-read-tree-m-u-wf.sh |   82 ++++++++++++++++++++++++++++++++++++++----
 unpack-trees.c              |   24 ++++++++++---
 unpack-trees.h              |    1 +
 6 files changed, 119 insertions(+), 15 deletions(-)
builtin-read-tree.c +20 -0
index c1867d2..37b2214 100644
@@ -10,6 +10,7 @@
 #include "tree-walk.h"
 #include "cache-tree.h"
 #include "unpack-trees.h"
+#include "dir.h"
 #include "builtin.h"
 
 static struct object_list *trees;
@@ -178,6 +179,23 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
 			continue;
 		}
 
+		if (!strncmp(arg, "--ignore=", 9)) {
+			struct dir_struct *dir;
+
+			if (opts.dir)
+				die("more than one --ignore is given.");
+
+			dir = calloc(1, sizeof(*opts.dir));
+			dir->show_ignored = 1;
+			dir->exclude_per_dir = arg + 9;
+			opts.dir = dir;
+			/* We do not need to nor want to do read-directory
+			 * here; we are merely interested in reusing the
+			 * per directory ignore stack mechanism.
+			 */
+			continue;
+		}
+
 		/* using -u and -i at the same time makes no sense */
 		if (1 < opts.index_only + opts.update)
 			usage(read_tree_usage);
@@ -190,6 +208,8 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
 	}
 	if ((opts.update||opts.index_only) && !opts.merge)
 		usage(read_tree_usage);
+	if ((opts.dir && !opts.update))
+		die("--ignore is meaningless unless -u");
 
 	if (opts.prefix) {
 		int pfxlen = strlen(opts.prefix);
dir.c +2 -2
index 96389b3..e6a61ee 100644
@@ -156,7 +156,7 @@ void add_excludes_from_file(struct dir_struct *dir, const char *fname)
 		die("cannot use %s as an exclude file", fname);
 }
 
-static int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
+int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
 {
 	char exclude_file[PATH_MAX];
 	struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
@@ -170,7 +170,7 @@ static int push_exclude_per_directory(struct dir_struct *dir, const char *base,
 	return current_nr;
 }
 
-static void pop_exclude_per_directory(struct dir_struct *dir, int stk)
+void pop_exclude_per_directory(struct dir_struct *dir, int stk)
 {
 	struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
 
dir.h +3 -0
index 313f8ab..550551a 100644
@@ -43,6 +43,9 @@ extern int common_prefix(const char **pathspec);
 extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
 
 extern int read_directory(struct dir_struct *, const char *path, const char *base, int baselen);
+extern int push_exclude_per_directory(struct dir_struct *, const char *, int);
+extern void pop_exclude_per_directory(struct dir_struct *, int);
+
 extern int excluded(struct dir_struct *, const char *);
 extern void add_excludes_from_file(struct dir_struct *, const char *fname);
 extern void add_exclude(const char *string, const char *base,
t/t1004-read-tree-m-u-wf.sh +74 -8

117 lines not on this page · raw message

unpack-trees.c +19 -5
index 7cfd628..79d21e2 100644
@@ -1,6 +1,7 @@
 #include <signal.h>
 #include <sys/time.h>
 #include "cache.h"
+#include "dir.h"
 #include "tree.h"
 #include "tree-walk.h"
 #include "cache-tree.h"
@@ -77,6 +78,12 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
 {
 	int baselen = strlen(base);
 	int src_size = len + 1;
+	int i_stk = i_stk;
+	int retval = 0;
+
+	if (o->dir)
+		i_stk = push_exclude_per_directory(o->dir, base, strlen(base));
+
 	do {
 		int i;
 		const char *first;
@@ -143,7 +150,7 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
 		}
 		/* No name means we're done */
 		if (!first)
-			return 0;
+			goto leave_directory;
 
 		pathlen = strlen(first);
 		ce_size = cache_entry_size(baselen + pathlen);
@@ -240,13 +247,20 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
 			newbase[baselen + pathlen] = '/';
 			newbase[baselen + pathlen + 1] = '\0';
 			if (unpack_trees_rec(subposns, len, newbase, o,
-					     indpos, df_conflict_list))
-				return -1;
+					     indpos, df_conflict_list)) {
+				retval = -1;
+				goto leave_directory;
+			}
 			free(newbase);
 		}
 		free(subposns);
 		free(src);
 	} while (1);
+
+ leave_directory:
+	if (o->dir)
+		pop_exclude_per_directory(o->dir, i_stk);
+	return retval;
 }
 
 /* Unlink the last component and attempt to remove leading
@@ -456,7 +470,7 @@ static void invalidate_ce_path(struct cache_entry *ce)
 
 /*
  * We do not want to remove or overwrite a working tree file that
- * is not tracked.
+ * is not tracked, unless it is ignored.
  */
 static void verify_absent(const char *path, const char *action,
 		struct unpack_trees_options *o)
@@ -465,7 +479,7 @@ static void verify_absent(const char *path, const char *action,
 
 	if (o->index_only || o->reset || !o->update)
 		return;
-	if (!lstat(path, &st))
+	if (!lstat(path, &st) && !(o->dir && excluded(o->dir, path)))
 		die("Untracked working tree file '%s' "
 		    "would be %s by merge.", path, action);
 }
unpack-trees.h +1 -0
index c460162..191f744 100644
@@ -16,6 +16,7 @@ struct unpack_trees_options {
 	int verbose_update;
 	int aggressive;
 	const char *prefix;
+	struct dir_struct *dir;
 	merge_fn_t fn;
 
 	int head_idx;
Signature · 1 line
1.4.4.1.ga37e
#1 lore raw
Quoted 5 lines · Junio C Hamano

A new option "--ignore=.gitignore" can be used to tell the "git-read-tree" command that the user does not mind losing contents in untracked files in the working tree if it needs to be overwritten by a merge (either a two-way "switch branch" merge, or a three-way merge).

[...]

Quoted 7 lines

 builtin-read-tree.c         |   20 ++++++++++
 dir.c                       |    4 +-
 dir.h                       |    3 ++ t/t1004-read-tree-m-u-wf.sh |   82 ++++++++++++++++++++++++++++++++++++++---- unpack-trees.c              |   24 ++++++++++---
 unpack-trees.h              |    1 +

 6 files changed, 119 insertions(+), 15 deletions(-)

Documentation, please?

Signature · 3 lines
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git