Skip to content

Commit 156d6ed

Browse files
committed
Merge branch 'bk/refresh-missing-ok-in-merge-recursive'
Allow "merge-recursive" to work in an empty (temporary) working tree again when there are renames involved, correcting an old regression in 1.7.7 era. * bk/refresh-missing-ok-in-merge-recursive: merge-recursive.c: tolerate missing files while refreshing index read-cache.c: extend make_cache_entry refresh flag with options read-cache.c: refactor --ignore-missing implementation t3030-merge-recursive: test known breakage with empty work tree
2 parents 7da5fd6 + 6e2068a commit 156d6ed

File tree

4 files changed

+70
-14
lines changed

4 files changed

+70
-14
lines changed

cache.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ extern int remove_file_from_index(struct index_state *, const char *path);
485485
#define ADD_CACHE_IMPLICIT_DOT 32 /* internal to "git add -u/-A" */
486486
extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
487487
extern int add_file_to_index(struct index_state *, const char *path, int flags);
488-
extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh);
488+
extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, unsigned int refresh_options);
489489
extern int ce_same_name(const struct cache_entry *a, const struct cache_entry *b);
490490
extern int index_name_is_other(const struct index_state *, const char *, int);
491491
extern void *read_blob_data_from_index(struct index_state *, const char *, unsigned long *);
@@ -496,6 +496,10 @@ extern void *read_blob_data_from_index(struct index_state *, const char *, unsig
496496
#define CE_MATCH_RACY_IS_DIRTY 02
497497
/* do stat comparison even if CE_SKIP_WORKTREE is true */
498498
#define CE_MATCH_IGNORE_SKIP_WORKTREE 04
499+
/* ignore non-existent files during stat update */
500+
#define CE_MATCH_IGNORE_MISSING 0x08
501+
/* enable stat refresh */
502+
#define CE_MATCH_REFRESH 0x10
499503
extern int ie_match_stat(const struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
500504
extern int ie_modified(const struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
501505

merge-recursive.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
201201
const char *path, int stage, int refresh, int options)
202202
{
203203
struct cache_entry *ce;
204-
ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
204+
ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage,
205+
(refresh ? (CE_MATCH_REFRESH |
206+
CE_MATCH_IGNORE_MISSING) : 0 ));
205207
if (!ce)
206208
return error(_("addinfo_cache failed for path '%s'"), path);
207209
return add_cache_entry(ce, options);

read-cache.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#include "strbuf.h"
1616
#include "varint.h"
1717

18-
static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really);
18+
static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,
19+
unsigned int options);
1920

2021
/* Mask for the name length in ce_flags in the on-disk index */
2122

@@ -700,7 +701,7 @@ int add_file_to_index(struct index_state *istate, const char *path, int flags)
700701

701702
struct cache_entry *make_cache_entry(unsigned int mode,
702703
const unsigned char *sha1, const char *path, int stage,
703-
int refresh)
704+
unsigned int refresh_options)
704705
{
705706
int size, len;
706707
struct cache_entry *ce;
@@ -720,10 +721,7 @@ struct cache_entry *make_cache_entry(unsigned int mode,
720721
ce->ce_namelen = len;
721722
ce->ce_mode = create_ce_mode(mode);
722723

723-
if (refresh)
724-
return refresh_cache_entry(ce, 0);
725-
726-
return ce;
724+
return refresh_cache_entry(ce, refresh_options);
727725
}
728726

729727
int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)
@@ -1033,10 +1031,12 @@ static struct cache_entry *refresh_cache_ent(struct index_state *istate,
10331031
struct stat st;
10341032
struct cache_entry *updated;
10351033
int changed, size;
1034+
int refresh = options & CE_MATCH_REFRESH;
10361035
int ignore_valid = options & CE_MATCH_IGNORE_VALID;
10371036
int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;
1037+
int ignore_missing = options & CE_MATCH_IGNORE_MISSING;
10381038

1039-
if (ce_uptodate(ce))
1039+
if (!refresh || ce_uptodate(ce))
10401040
return ce;
10411041

10421042
/*
@@ -1054,6 +1054,8 @@ static struct cache_entry *refresh_cache_ent(struct index_state *istate,
10541054
}
10551055

10561056
if (lstat(ce->name, &st) < 0) {
1057+
if (ignore_missing && errno == ENOENT)
1058+
return ce;
10571059
if (err)
10581060
*err = errno;
10591061
return NULL;
@@ -1131,7 +1133,9 @@ int refresh_index(struct index_state *istate, unsigned int flags,
11311133
int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) != 0;
11321134
int first = 1;
11331135
int in_porcelain = (flags & REFRESH_IN_PORCELAIN);
1134-
unsigned int options = really ? CE_MATCH_IGNORE_VALID : 0;
1136+
unsigned int options = (CE_MATCH_REFRESH |
1137+
(really ? CE_MATCH_IGNORE_VALID : 0) |
1138+
(not_new ? CE_MATCH_IGNORE_MISSING : 0));
11351139
const char *modified_fmt;
11361140
const char *deleted_fmt;
11371141
const char *typechange_fmt;
@@ -1180,8 +1184,6 @@ int refresh_index(struct index_state *istate, unsigned int flags,
11801184
if (!new) {
11811185
const char *fmt;
11821186

1183-
if (not_new && cache_errno == ENOENT)
1184-
continue;
11851187
if (really && cache_errno == EINVAL) {
11861188
/* If we are doing --really-refresh that
11871189
* means the index is not valid anymore.
@@ -1211,9 +1213,10 @@ int refresh_index(struct index_state *istate, unsigned int flags,
12111213
return has_errors;
12121214
}
12131215

1214-
static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really)
1216+
static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,
1217+
unsigned int options)
12151218
{
1216-
return refresh_cache_ent(&the_index, ce, really, NULL, NULL);
1219+
return refresh_cache_ent(&the_index, ce, options, NULL, NULL);
12171220
}
12181221

12191222

t/t3030-merge-recursive.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ test_expect_success 'setup 8' '
257257
git add e &&
258258
test_tick &&
259259
git commit -m "rename a->e" &&
260+
c7=$(git rev-parse --verify HEAD) &&
260261
git checkout rename-ln &&
261262
git mv a e &&
262263
test_ln_s_add e a &&
@@ -517,6 +518,52 @@ test_expect_success 'reset and bind merge' '
517518
518519
'
519520

521+
test_expect_success 'merge-recursive w/ empty work tree - ours has rename' '
522+
(
523+
GIT_WORK_TREE="$PWD/ours-has-rename-work" &&
524+
export GIT_WORK_TREE &&
525+
GIT_INDEX_FILE="$PWD/ours-has-rename-index" &&
526+
export GIT_INDEX_FILE &&
527+
mkdir "$GIT_WORK_TREE" &&
528+
git read-tree -i -m $c7 &&
529+
git update-index --ignore-missing --refresh &&
530+
git merge-recursive $c0 -- $c7 $c3 &&
531+
git ls-files -s >actual-files
532+
) 2>actual-err &&
533+
>expected-err &&
534+
cat >expected-files <<-EOF &&
535+
100644 $o3 0 b/c
536+
100644 $o0 0 c
537+
100644 $o0 0 d/e
538+
100644 $o0 0 e
539+
EOF
540+
test_cmp expected-files actual-files &&
541+
test_cmp expected-err actual-err
542+
'
543+
544+
test_expect_success 'merge-recursive w/ empty work tree - theirs has rename' '
545+
(
546+
GIT_WORK_TREE="$PWD/theirs-has-rename-work" &&
547+
export GIT_WORK_TREE &&
548+
GIT_INDEX_FILE="$PWD/theirs-has-rename-index" &&
549+
export GIT_INDEX_FILE &&
550+
mkdir "$GIT_WORK_TREE" &&
551+
git read-tree -i -m $c3 &&
552+
git update-index --ignore-missing --refresh &&
553+
git merge-recursive $c0 -- $c3 $c7 &&
554+
git ls-files -s >actual-files
555+
) 2>actual-err &&
556+
>expected-err &&
557+
cat >expected-files <<-EOF &&
558+
100644 $o3 0 b/c
559+
100644 $o0 0 c
560+
100644 $o0 0 d/e
561+
100644 $o0 0 e
562+
EOF
563+
test_cmp expected-files actual-files &&
564+
test_cmp expected-err actual-err
565+
'
566+
520567
test_expect_success 'merge removes empty directories' '
521568
522569
git reset --hard master &&

0 commit comments

Comments
 (0)