Skip to content

Commit 0b617e7

Browse files
committed
git-mv: improve error message for conflicted file
'git mv' has always complained about renaming a conflicted file, as it cannot handle multiple index entries for one file. However, the error message it uses has been the same as the one for an untracked file: fatal: not under version control, src=... which is patently wrong. Distinguish the two cases and add a test to make sure we produce the correct message. Signed-off-by: Chris Torek <chris.torek@gmail.com>
1 parent b6a658b commit 0b617e7

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

builtin/mv.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,18 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
220220
}
221221
argc += last - first;
222222
}
223-
} else if (cache_name_pos(src, length) < 0)
224-
bad = _("not under version control");
225-
else if (lstat(dst, &st) == 0 &&
223+
} else if (cache_name_pos(src, length) < 0) {
224+
/*
225+
* This occurs for both untracked files *and*
226+
* files that are in merge-conflict state, so
227+
* let's distinguish between those two.
228+
*/
229+
struct cache_entry *ce = cache_file_exists(src, length, ignore_case);
230+
if (ce == NULL)
231+
bad = _("not under version control");
232+
else
233+
bad = _("must resolve merge conflict first");
234+
} else if (lstat(dst, &st) == 0 &&
226235
(!ignore_case || strcasecmp(src, dst))) {
227236
bad = _("destination exists");
228237
if (force) {

t/t7001-mv.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,24 @@ test_expect_success 'git mv should not change sha1 of moved cache entry' '
248248

249249
rm -f dirty dirty2
250250

251+
# NB: This test is about the error message
252+
# as well as the failure.
253+
test_expect_success 'git mv error on conflicted file' '
254+
rm -fr .git &&
255+
git init &&
256+
touch conflicted &&
257+
cfhash=$(git hash-object -w conflicted) &&
258+
git update-index --index-info <<-EOF &&
259+
$(printf "0 $cfhash 0\tconflicted\n")
260+
$(printf "100644 $cfhash 1\tconflicted\n")
261+
EOF
262+
263+
test_must_fail git mv conflicted newname 2>actual &&
264+
test_i18ngrep "merge.conflict" actual
265+
'
266+
267+
rm -f conflicted
268+
251269
test_expect_success 'git mv should overwrite symlink to a file' '
252270
253271
rm -fr .git &&

0 commit comments

Comments
 (0)