All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Chris Torek via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Chris Torek <chris.torek@gmail.com>, Chris Torek <chris.torek@gmail.com>
Subject: [PATCH v2] git-mv: improve error message for conflicted file
Date: Mon, 20 Jul 2020 06:17:52 +0000	[thread overview]
Message-ID: <pull.678.v2.git.1595225873014.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.678.git.1595028293855.gitgitgadget@gmail.com>

From: Chris Torek <chris.torek@gmail.com>

'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>
---
    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 [chris.torek@gmail.com]
    
    
    ------------------------------------------------------------------------
    
    Tests updated, and took Junio's suggestion to reduce the cache lookup to
    one call.
    
    I put in the shortened "conflicted" here but did not shorten the
    existing "not under version control" message (to minimize the visible
    and translations-required changes).
    
    I like the idea of renaming all stages and keeping them at their current
    stages, but that's too much for this patch.
    
    I'll be traveling next week and not sure if I will get to any followups
    for a while.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-678%2Fchris3torek%2Fgit-mv-message-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-678/chris3torek/git-mv-message-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/678

Range-diff vs v1:

 1:  0b617e74f7 ! 1:  f2e251a7ea git-mv: improve error message for conflicted file
     @@ Commit message
          Signed-off-by: Chris Torek <chris.torek@gmail.com>
      
       ## builtin/mv.c ##
     +@@ builtin/mv.c: int cmd_mv(int argc, const char **argv, const char *prefix)
     + 	struct stat st;
     + 	struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
     + 	struct lock_file lock_file = LOCK_INIT;
     ++	struct cache_entry *ce;
     + 
     + 	git_config(git_default_config, NULL);
     + 
      @@ builtin/mv.c: int cmd_mv(int argc, const char **argv, const char *prefix)
       				}
       				argc += last - first;
       			}
      -		} else if (cache_name_pos(src, length) < 0)
     --			bad = _("not under version control");
     ++		} else if (!(ce = cache_file_exists(src, length, ignore_case))) {
     + 			bad = _("not under version control");
      -		else if (lstat(dst, &st) == 0 &&
     -+		} else if (cache_name_pos(src, length) < 0) {
     -+			/*
     -+			 * This occurs for both untracked files *and*
     -+			 * files that are in merge-conflict state, so
     -+			 * let's distinguish between those two.
     -+			 */
     -+			struct cache_entry *ce = cache_file_exists(src, length, ignore_case);
     -+			if (ce == NULL)
     -+				bad = _("not under version control");
     -+			else
     -+				bad = _("must resolve merge conflict first");
     ++		} else if (ce_stage(ce)) {
     ++			bad = _("conflicted");
      +		} else if (lstat(dst, &st) == 0 &&
       			 (!ignore_case || strcasecmp(src, dst))) {
       			bad = _("destination exists");
     @@ t/t7001-mv.sh: test_expect_success 'git mv should not change sha1 of moved cache
      +test_expect_success 'git mv error on conflicted file' '
      +	rm -fr .git &&
      +	git init &&
     -+	touch conflicted &&
     -+	cfhash=$(git hash-object -w conflicted) &&
     -+	git update-index --index-info <<-EOF &&
     -+	$(printf "0 $cfhash 0\tconflicted\n")
     -+	$(printf "100644 $cfhash 1\tconflicted\n")
     ++	>conflict &&
     ++	test_when_finished "rm -f conflict" &&
     ++	cfhash=$(git hash-object -w conflict) &&
     ++	q_to_tab <<-EOF | git update-index --index-info &&
     ++	0 $cfhash 0Qconflict
     ++	100644 $cfhash 1Qconflict
      +	EOF
      +
     -+	test_must_fail git mv conflicted newname 2>actual &&
     -+	test_i18ngrep "merge.conflict" actual
     ++	test_must_fail git mv conflict newname 2>actual &&
     ++	test_i18ngrep "conflicted" actual
      +'
     -+
     -+rm -f conflicted
      +
       test_expect_success 'git mv should overwrite symlink to a file' '
       


 builtin/mv.c  |  7 +++++--
 t/t7001-mv.sh | 17 +++++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/builtin/mv.c b/builtin/mv.c
index be15ba7044..7dac714af9 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -132,6 +132,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 	struct stat st;
 	struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
 	struct lock_file lock_file = LOCK_INIT;
+	struct cache_entry *ce;
 
 	git_config(git_default_config, NULL);
 
@@ -220,9 +221,11 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 				}
 				argc += last - first;
 			}
-		} else if (cache_name_pos(src, length) < 0)
+		} else if (!(ce = cache_file_exists(src, length, ignore_case))) {
 			bad = _("not under version control");
-		else if (lstat(dst, &st) == 0 &&
+		} else if (ce_stage(ce)) {
+			bad = _("conflicted");
+		} else if (lstat(dst, &st) == 0 &&
 			 (!ignore_case || strcasecmp(src, dst))) {
 			bad = _("destination exists");
 			if (force) {
diff --git a/t/t7001-mv.sh b/t/t7001-mv.sh
index 36b50d0b4c..c978b6dee4 100755
--- a/t/t7001-mv.sh
+++ b/t/t7001-mv.sh
@@ -248,6 +248,23 @@ test_expect_success 'git mv should not change sha1 of moved cache entry' '
 
 rm -f dirty dirty2
 
+# NB: This test is about the error message
+# as well as the failure.
+test_expect_success 'git mv error on conflicted file' '
+	rm -fr .git &&
+	git init &&
+	>conflict &&
+	test_when_finished "rm -f conflict" &&
+	cfhash=$(git hash-object -w conflict) &&
+	q_to_tab <<-EOF | git update-index --index-info &&
+	0 $cfhash 0Qconflict
+	100644 $cfhash 1Qconflict
+	EOF
+
+	test_must_fail git mv conflict newname 2>actual &&
+	test_i18ngrep "conflicted" actual
+'
+
 test_expect_success 'git mv should overwrite symlink to a file' '
 
 	rm -fr .git &&

base-commit: ae46588be0cd730430dded4491246dfb4eac5557
-- 
gitgitgadget

  parent reply	other threads:[~2020-07-20  6:17 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-17 23:24 [PATCH] git-mv: improve error message for conflicted file Chris Torek via GitGitGadget
2020-07-17 23:47 ` Eric Sunshine
2020-07-18  1:35   ` Chris Torek
2020-07-18  6:55     ` Eric Sunshine
2020-07-18  0:07 ` Junio C Hamano
2020-07-18  2:00   ` Elijah Newren
2020-07-18 17:46     ` Junio C Hamano
2020-07-19  1:48       ` Elijah Newren
2020-07-19  6:16         ` Junio C Hamano
2020-07-20  6:17 ` Chris Torek via GitGitGadget [this message]
2020-07-20 18:28   ` [PATCH v2] " Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.678.v2.git.1595225873014.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=chris.torek@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.