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 "--exclude-per-directory=.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 they need to be overwritten by a merge (either a two-way "switch branches" merge, or a three-way merge). Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Dec 4, 2006 at 16:00 UTC
f8a9d4287277ed15d3f0d61004f4510c59f1f392
6 files changed
+119
-15
builtin-read-tree.c
+20
@@ -10,6 +10,7 @@
10
#include "tree-walk.h"
11
#include "cache-tree.h"
12
#include "unpack-trees.h"
13
+#include "dir.h"
14
#include "builtin.h"
15
16
static struct object_list *trees;
@@ -178,6 +179,23 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
179
continue;
180
}
181
182
+ if (!strncmp(arg, "--exclude-per-directory=", 24)) {
183
+ struct dir_struct *dir;
184
+
185
+ if (opts.dir)
186
+ die("more than one --exclude-per-directory are given.");
187
+
188
+ dir = calloc(1, sizeof(*opts.dir));
189
+ dir->show_ignored = 1;
190
+ dir->exclude_per_dir = arg + 24;
191
+ opts.dir = dir;
192
+ /* We do not need to nor want to do read-directory
193
+ * here; we are merely interested in reusing the
194
+ * per directory ignore stack mechanism.
195
+ */
196
+ continue;
197
+ }
198
+
199
/* using -u and -i at the same time makes no sense */
200
if (1 < opts.index_only + opts.update)
201
usage(read_tree_usage);
@@ -190,6 +208,8 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
208
}
209
if ((opts.update||opts.index_only) && !opts.merge)
210
usage(read_tree_usage);
211
+ if ((opts.dir && !opts.update))
212
+ die("--exclude-per-directory is meaningless unless -u");
213
214
if (opts.prefix) {
215
int pfxlen = strlen(opts.prefix);
dir.c
+2
-2
@@ -156,7 +156,7 @@ void add_excludes_from_file(struct dir_struct *dir, const char *fname)
156
die("cannot use %s as an exclude file", fname);
157
}
158
159
-static int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
159
+int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
160
{
161
char exclude_file[PATH_MAX];
162
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,
170
return current_nr;
171
}
172
173
-static void pop_exclude_per_directory(struct dir_struct *dir, int stk)
173
+void pop_exclude_per_directory(struct dir_struct *dir, int stk)
174
{
175
struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
176
dir.h
+3
@@ -43,6 +43,9 @@ extern int common_prefix(const char **pathspec);
43
extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
44
45
extern int read_directory(struct dir_struct *, const char *path, const char *base, int baselen);
46
+extern int push_exclude_per_directory(struct dir_struct *, const char *, int);
47
+extern void pop_exclude_per_directory(struct dir_struct *, int);
48
+
49
extern int excluded(struct dir_struct *, const char *);
50
extern void add_excludes_from_file(struct dir_struct *, const char *fname);
51
extern void add_exclude(const char *string, const char *base,
t/t1004-read-tree-m-u-wf.sh
+74
-8
@@ -8,23 +8,27 @@ test_description='read-tree -m -u checks working tree files'
8
9
test_expect_success 'two-way setup' '
10
11
+ mkdir subdir &&
12
echo >file1 file one &&
13
echo >file2 file two &&
13
- git update-index --add file1 file2 &&
14
+ echo >subdir/file1 file one in subdirectory &&
15
+ echo >subdir/file2 file two in subdirectory &&
16
+ git update-index --add file1 file2 subdir/file1 subdir/file2 &&
17
git commit -m initial &&
18
19
git branch side &&
20
git tag -f branch-point &&
21
22
echo file2 is not tracked on the master anymore &&
20
- rm -f file2 &&
21
- git update-index --remove file2 &&
22
- git commit -a -m "master removes file2"
23
+ rm -f file2 subdir/file2 &&
24
+ git update-index --remove file2 subdir/file2 &&
25
+ git commit -a -m "master removes file2 and subdir/file2"
26
'
27
28
test_expect_success 'two-way not clobbering' '
29
30
echo >file2 master creates untracked file2 &&
31
+ echo >subdir/file2 master creates untracked subdir/file2 &&
32
if err=`git read-tree -m -u master side 2>&1`
33
then
34
echo should have complained
@@ -34,20 +38,82 @@ test_expect_success 'two-way not clobbering' '
38
fi
39
'
40
41
+echo file2 >.gitignore
42
+
43
+test_expect_success 'two-way with incorrect --exclude-per-directory (1)' '
44
+
45
+ if err=`git read-tree -m --exclude-per-directory=.gitignore master side 2>&1`
46
+ then
47
+ echo should have complained
48
+ false
49
+ else
50
+ echo "happy to see $err"
51
+ fi
52
+'
53
+
54
+test_expect_success 'two-way with incorrect --exclude-per-directory (2)' '
55
+
56
+ if err=`git read-tree -m -u --exclude-per-directory=foo --exclude-per-directory=.gitignore master side 2>&1`
57
+ then
58
+ echo should have complained
59
+ false
60
+ else
61
+ echo "happy to see $err"
62
+ fi
63
+'
64
+
65
+test_expect_success 'two-way clobbering a ignored file' '
66
+
67
+ git read-tree -m -u --exclude-per-directory=.gitignore master side
68
+'
69
+
70
+rm -f .gitignore
71
+
72
# three-tree test
73
39
-test_expect_success 'three-way not complaining' '
74
+test_expect_success 'three-way not complaining on an untracked path in both' '
75
41
- rm -f file2 &&
76
+ rm -f file2 subdir/file2 &&
77
git checkout side &&
78
echo >file3 file three &&
44
- git update-index --add file3 &&
45
- git commit -a -m "side adds file3" &&
79
+ echo >subdir/file3 file three &&
80
+ git update-index --add file3 subdir/file3 &&
81
+ git commit -a -m "side adds file3 and removes file2" &&
82
83
git checkout master &&
84
echo >file2 file two is untracked on the master side &&
85
+ echo >subdir/file2 file two is untracked on the master side &&
86
87
git-read-tree -m -u branch-point master side
88
'
89
90
+test_expect_success 'three-way not cloberring a working tree file' '
91
+
92
+ git reset --hard &&
93
+ rm -f file2 subdir/file2 file3 subdir/file3 &&
94
+ git checkout master &&
95
+ echo >file3 file three created in master, untracked &&
96
+ echo >subdir/file3 file three created in master, untracked &&
97
+ if err=`git read-tree -m -u branch-point master side 2>&1`
98
+ then
99
+ echo should have complained
100
+ false
101
+ else
102
+ echo "happy to see $err"
103
+ fi
104
+'
105
+
106
+echo >.gitignore file3
107
+
108
+test_expect_success 'three-way not complaining on an untracked file' '
109
+
110
+ git reset --hard &&
111
+ rm -f file2 subdir/file2 file3 subdir/file3 &&
112
+ git checkout master &&
113
+ echo >file3 file three created in master, untracked &&
114
+ echo >subdir/file3 file three created in master, untracked &&
115
+
116
+ git read-tree -m -u --exclude-per-directory=.gitignore branch-point master side
117
+'
118
+
119
test_done
unpack-trees.c
+19
-5
@@ -1,6 +1,7 @@
1
#include <signal.h>
2
#include <sys/time.h>
3
#include "cache.h"
4
+#include "dir.h"
5
#include "tree.h"
6
#include "tree-walk.h"
7
#include "cache-tree.h"
@@ -77,6 +78,12 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
78
{
79
int baselen = strlen(base);
80
int src_size = len + 1;
81
+ int i_stk = i_stk;
82
+ int retval = 0;
83
+
84
+ if (o->dir)
85
+ i_stk = push_exclude_per_directory(o->dir, base, strlen(base));
86
+
87
do {
88
int i;
89
const char *first;
@@ -143,7 +150,7 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
150
}
151
/* No name means we're done */
152
if (!first)
146
- return 0;
153
+ goto leave_directory;
154
155
pathlen = strlen(first);
156
ce_size = cache_entry_size(baselen + pathlen);
@@ -240,13 +247,20 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
247
newbase[baselen + pathlen] = '/';
248
newbase[baselen + pathlen + 1] = '\0';
249
if (unpack_trees_rec(subposns, len, newbase, o,
243
- indpos, df_conflict_list))
244
- return -1;
250
+ indpos, df_conflict_list)) {
251
+ retval = -1;
252
+ goto leave_directory;
253
+ }
254
free(newbase);
255
}
256
free(subposns);
257
free(src);
258
} while (1);
259
+
260
+ leave_directory:
261
+ if (o->dir)
262
+ pop_exclude_per_directory(o->dir, i_stk);
263
+ return retval;
264
}
265
266
/* Unlink the last component and attempt to remove leading
@@ -456,7 +470,7 @@ static void invalidate_ce_path(struct cache_entry *ce)
470
471
/*
472
* We do not want to remove or overwrite a working tree file that
459
- * is not tracked.
473
+ * is not tracked, unless it is ignored.
474
*/
475
static void verify_absent(const char *path, const char *action,
476
struct unpack_trees_options *o)
@@ -465,7 +479,7 @@ static void verify_absent(const char *path, const char *action,
479
480
if (o->index_only || o->reset || !o->update)
481
return;
468
- if (!lstat(path, &st))
482
+ if (!lstat(path, &st) && !(o->dir && excluded(o->dir, path)))
483
die("Untracked working tree file '%s' "
484
"would be %s by merge.", path, action);
485
}
unpack-trees.h
+1
@@ -16,6 +16,7 @@ struct unpack_trees_options {
16
int verbose_update;
17
int aggressive;
18
const char *prefix;
19
+ struct dir_struct *dir;
20
merge_fn_t fn;
21
22
int head_idx;