Skip to content

Commit 9d9a2f4

Browse files
committed
resolve-undo: basic tests
Make sure that resolving a failed merge with git add records the conflicted state, committing the result keeps that state, and checking out another commit clears the state. "git ls-files" learns a new option --resolve-undo to show the recorded information. Signed-off-by: Junio C Hamano <[email protected]>
1 parent cfc5789 commit 9d9a2f4

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

builtin-ls-files.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
#include "builtin.h"
1212
#include "tree.h"
1313
#include "parse-options.h"
14+
#include "resolve-undo.h"
15+
#include "string-list.h"
1416

1517
static int abbrev;
1618
static int show_deleted;
1719
static int show_cached;
1820
static int show_others;
1921
static int show_stage;
2022
static int show_unmerged;
23+
static int show_resolve_undo;
2124
static int show_modified;
2225
static int show_killed;
2326
static int show_valid_bit;
@@ -37,6 +40,7 @@ static const char *tag_removed = "";
3740
static const char *tag_other = "";
3841
static const char *tag_killed = "";
3942
static const char *tag_modified = "";
43+
static const char *tag_resolve_undo = "";
4044

4145
static void show_dir_entry(const char *tag, struct dir_entry *ent)
4246
{
@@ -155,6 +159,38 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
155159
write_name_quoted(ce->name + offset, stdout, line_terminator);
156160
}
157161

162+
static int show_one_ru(struct string_list_item *item, void *cbdata)
163+
{
164+
int offset = prefix_offset;
165+
const char *path = item->string;
166+
struct resolve_undo_info *ui = item->util;
167+
int i, len;
168+
169+
len = strlen(path);
170+
if (len < prefix_len)
171+
return 0; /* outside of the prefix */
172+
if (!match_pathspec(pathspec, path, len, prefix_len, ps_matched))
173+
return 0; /* uninterested */
174+
for (i = 0; i < 3; i++) {
175+
if (!ui->mode[i])
176+
continue;
177+
printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
178+
abbrev
179+
? find_unique_abbrev(ui->sha1[i], abbrev)
180+
: sha1_to_hex(ui->sha1[i]),
181+
i + 1);
182+
write_name_quoted(path + offset, stdout, line_terminator);
183+
}
184+
return 0;
185+
}
186+
187+
static void show_ru_info(const char *prefix)
188+
{
189+
if (!the_index.resolve_undo)
190+
return;
191+
for_each_string_list(show_one_ru, the_index.resolve_undo, NULL);
192+
}
193+
158194
static void show_files(struct dir_struct *dir, const char *prefix)
159195
{
160196
int i;
@@ -454,6 +490,8 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
454490
DIR_HIDE_EMPTY_DIRECTORIES),
455491
OPT_BOOLEAN('u', "unmerged", &show_unmerged,
456492
"show unmerged files in the output"),
493+
OPT_BOOLEAN(0, "resolve-undo", &show_resolve_undo,
494+
"show resolve-undo information"),
457495
{ OPTION_CALLBACK, 'x', "exclude", &dir.exclude_list[EXC_CMDL], "pattern",
458496
"skip files matching pattern",
459497
0, option_parse_exclude },
@@ -490,6 +528,7 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
490528
tag_modified = "C ";
491529
tag_other = "? ";
492530
tag_killed = "K ";
531+
tag_resolve_undo = "U ";
493532
}
494533
if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
495534
require_work_tree = 1;
@@ -529,7 +568,7 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
529568

530569
/* With no flags, we default to showing the cached files */
531570
if (!(show_stage | show_deleted | show_others | show_unmerged |
532-
show_killed | show_modified))
571+
show_killed | show_modified | show_resolve_undo))
533572
show_cached = 1;
534573

535574
if (prefix)
@@ -544,6 +583,8 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
544583
overlay_tree_on_cache(with_tree, prefix);
545584
}
546585
show_files(&dir, prefix);
586+
if (show_resolve_undo)
587+
show_ru_info(prefix);
547588

548589
if (ps_matched) {
549590
int bad;

t/t2030-unresolve-info.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/sh
2+
3+
test_description='undoing resolution'
4+
5+
. ./test-lib.sh
6+
7+
check_resolve_undo () {
8+
msg=$1
9+
shift
10+
while case $# in
11+
0) break ;;
12+
1|2|3) die "Bug in check-resolve-undo test" ;;
13+
esac
14+
do
15+
path=$1
16+
shift
17+
for stage in 1 2 3
18+
do
19+
sha1=$1
20+
shift
21+
case "$sha1" in
22+
'') continue ;;
23+
esac
24+
sha1=$(git rev-parse --verify "$sha1")
25+
printf "100644 %s %s\t%s\n" $sha1 $stage $path
26+
done
27+
done >"$msg.expect" &&
28+
git ls-files --resolve-undo >"$msg.actual" &&
29+
test_cmp "$msg.expect" "$msg.actual"
30+
}
31+
32+
prime_resolve_undo () {
33+
git reset --hard &&
34+
git checkout second^0 &&
35+
test_tick &&
36+
test_must_fail git merge third^0 &&
37+
echo merge does not leave anything &&
38+
check_resolve_undo empty &&
39+
echo different >file &&
40+
git add file &&
41+
echo resolving records &&
42+
check_resolve_undo recorded file initial:file second:file third:file
43+
}
44+
45+
test_expect_success setup '
46+
test_commit initial file first &&
47+
git branch side &&
48+
git branch another &&
49+
test_commit second file second &&
50+
git checkout side &&
51+
test_commit third file third &&
52+
git checkout another &&
53+
test_commit fourth file fourth &&
54+
git checkout master
55+
'
56+
57+
test_expect_success 'add records switch clears' '
58+
prime_resolve_undo &&
59+
test_tick &&
60+
git commit -m merged &&
61+
echo committing keeps &&
62+
check_resolve_undo kept file initial:file second:file third:file &&
63+
git checkout second^0 &&
64+
echo switching clears &&
65+
check_resolve_undo cleared
66+
'
67+
68+
test_expect_success 'rm records reset clears' '
69+
prime_resolve_undo &&
70+
test_tick &&
71+
git commit -m merged &&
72+
echo committing keeps &&
73+
check_resolve_undo kept file initial:file second:file third:file &&
74+
75+
echo merge clears upfront &&
76+
test_must_fail git merge fourth^0 &&
77+
check_resolve_undo nuked &&
78+
79+
git rm -f file &&
80+
echo resolving records &&
81+
check_resolve_undo recorded file initial:file HEAD:file fourth:file &&
82+
83+
git reset --hard &&
84+
echo resetting discards &&
85+
check_resolve_undo discarded
86+
'
87+
88+
test_done

0 commit comments

Comments
 (0)