Merge branch 'ew/empty-merge-with-dirty-index-maint' into ew/empty-merge-with-dirty-index

* ew/empty-merge-with-dirty-index-maint:
  merge-recursive: avoid incorporating uncommitted changes in a merge
  move index_has_changes() from builtin/am.c to merge.c for reuse
  t6044: recursive can silently incorporate dirty changes in a merge
This commit is contained in:
Junio C Hamano 2017-12-22 12:48:38 -08:00
Родитель 936d1b9894 65170c07d4
Коммит b6825b5c8e
5 изменённых файлов: 70 добавлений и 42 удалений

Просмотреть файл

@ -1142,43 +1142,6 @@ static void refresh_and_write_cache(void)
die(_("unable to write index file"));
}
/**
* Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
* branch, returns 1 if there are entries in the index, 0 otherwise. If an
* strbuf is provided, the space-separated list of files that differ will be
* appended to it.
*/
static int index_has_changes(struct strbuf *sb)
{
struct object_id head;
int i;
if (!get_oid_tree("HEAD", &head)) {
struct diff_options opt;
diff_setup(&opt);
opt.flags.exit_with_status = 1;
if (!sb)
opt.flags.quick = 1;
do_diff_cache(&head, &opt);
diffcore_std(&opt);
for (i = 0; sb && i < diff_queued_diff.nr; i++) {
if (i)
strbuf_addch(sb, ' ');
strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
}
diff_flush(&opt);
return opt.flags.has_changes != 0;
} else {
for (i = 0; sb && i < active_nr; i++) {
if (i)
strbuf_addch(sb, ' ');
strbuf_addstr(sb, active_cache[i]->name);
}
return !!active_nr;
}
}
/**
* Dies with a user-friendly message on how to proceed after resolving the
* problem. This message can be overridden with state->resolvemsg.

Просмотреть файл

@ -644,6 +644,15 @@ extern int write_locked_index(struct index_state *, struct lock_file *lock, unsi
extern int discard_index(struct index_state *);
extern void move_index_extensions(struct index_state *dst, struct index_state *src);
extern int unmerged_index(const struct index_state *);
/**
* Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
* branch, returns 1 if there are entries in the index, 0 otherwise. If an
* strbuf is provided, the space-separated list of files that differ will be
* appended to it.
*/
extern int index_has_changes(struct strbuf *sb);
extern int verify_path(const char *path);
extern int strcmp_offset(const char *s1, const char *s2, size_t *first_change);
extern int index_dir_exists(struct index_state *istate, const char *name, int namelen);

Просмотреть файл

@ -1952,6 +1952,13 @@ int merge_trees(struct merge_options *o,
}
if (oid_eq(&common->object.oid, &merge->object.oid)) {
struct strbuf sb = STRBUF_INIT;
if (index_has_changes(&sb)) {
err(o, _("Dirty index: cannot merge (dirty: %s)"),
sb.buf);
return 0;
}
output(o, 0, _("Already up to date!"));
*result = head;
return 1;

33
merge.c
Просмотреть файл

@ -1,4 +1,6 @@
#include "cache.h"
#include "diff.h"
#include "diffcore.h"
#include "lockfile.h"
#include "commit.h"
#include "run-command.h"
@ -15,6 +17,37 @@ static const char *merge_argument(struct commit *commit)
return EMPTY_TREE_SHA1_HEX;
}
int index_has_changes(struct strbuf *sb)
{
struct object_id head;
int i;
if (!get_oid_tree("HEAD", &head)) {
struct diff_options opt;
diff_setup(&opt);
opt.flags.exit_with_status = 1;
if (!sb)
opt.flags.quick = 1;
do_diff_cache(&head, &opt);
diffcore_std(&opt);
for (i = 0; sb && i < diff_queued_diff.nr; i++) {
if (i)
strbuf_addch(sb, ' ');
strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
}
diff_flush(&opt);
return opt.flags.has_changes != 0;
} else {
for (i = 0; sb && i < active_nr; i++) {
if (i)
strbuf_addch(sb, ' ');
strbuf_addstr(sb, active_cache[i]->name);
}
return !!active_nr;
}
}
int try_merge_command(const char *strategy, size_t xopts_nr,
const char **xopts, struct commit_list *common,
const char *head_arg, struct commit_list *remotes)

Просмотреть файл

@ -6,18 +6,21 @@ test_description="merges with unrelated index changes"
# Testcase for some simple merges
# A
# o-----o B
# o-------o B
# \
# \---o C
# \-----o C
# \
# \-o D
# \---o D
# \
# o E
# \-o E
# \
# o F
# Commit A: some file a
# Commit B: adds file b, modifies end of a
# Commit C: adds file c
# Commit D: adds file d, modifies beginning of a
# Commit E: renames a->subdir/a, adds subdir/e
# Commit F: empty commit
test_expect_success 'setup trivial merges' '
test_seq 1 10 >a &&
@ -29,6 +32,7 @@ test_expect_success 'setup trivial merges' '
git branch C &&
git branch D &&
git branch E &&
git branch F &&
git checkout B &&
echo b >b &&
@ -52,7 +56,10 @@ test_expect_success 'setup trivial merges' '
git mv a subdir/a &&
echo e >subdir/e &&
git add subdir &&
test_tick && git commit -m E
test_tick && git commit -m E &&
git checkout F &&
test_tick && git commit --allow-empty -m F
'
test_expect_success 'ff update' '
@ -105,6 +112,15 @@ test_expect_success 'recursive' '
test_must_fail git merge -s recursive C^0
'
test_expect_success 'recursive, when merge branch matches merge base' '
git reset --hard &&
git checkout B^0 &&
touch random_file && git add random_file &&
test_must_fail git merge -s recursive F^0
'
test_expect_success 'octopus, unrelated file touched' '
git reset --hard &&
git checkout B^0 &&