reset: handle submodule with trailing slash

When using tab-completion, a directory path will often end with a trailing slash which currently confuses "git reset" when dealing with submodules. Now that we have parse_pathspec we can easily handle this by simply adding the PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP flag. To do this, we need to move the read_cache() call before the parse_pathspec() call. All of the existing paths through cmd_reset() that do not die early already call read_cache() at some point, so there is no performance impact to doing this in the common case. Signed-off-by: John Keeping <john@keeping.me.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

John Keeping committed Sep 12, 2013 at 20:25 UTC 2c63d6eb46da4ef0abc9c040c927818e437d2fcc
2 files changed +10 -4
builtin/reset.c
+6 -2
@@ -143,7 +143,6 @@ static int read_from_tree(const struct pathspec *pathspec,
143 opt.output_format = DIFF_FORMAT_CALLBACK;
144 opt.format_callback = update_index_from_diff;
145
146 - read_cache();
146 if (do_diff_cache(tree_sha1, &opt))
147 return 1;
148 diffcore_std(&opt);
@@ -169,7 +168,7 @@ static void set_reflog_message(struct strbuf *sb, const char *action,
168
169 static void die_if_unmerged_cache(int reset_type)
170 {
172 - if (is_merge() || read_cache() < 0 || unmerged_cache())
171 + if (is_merge() || unmerged_cache())
172 die(_("Cannot do a %s reset in the middle of a merge."),
173 _(reset_type_names[reset_type]));
174
@@ -220,8 +219,13 @@ static void parse_args(struct pathspec *pathspec,
219 }
220 }
221 *rev_ret = rev;
222 +
223 + if (read_cache() < 0)
224 + die(_("index file corrupt"));
225 +
226 parse_pathspec(pathspec, 0,
227 PATHSPEC_PREFER_FULL |
228 + PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP |
229 (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0),
230 prefix, argv);
231 }
t/t7400-submodule-basic.sh
+4 -2
@@ -481,7 +481,7 @@ test_expect_success 'do not add files from a submodule' '
481
482 '
483
484 -test_expect_success 'gracefully add submodule with a trailing slash' '
484 +test_expect_success 'gracefully add/reset submodule with a trailing slash' '
485
486 git reset --hard &&
487 git commit -m "commit subproject" init &&
@@ -495,7 +495,9 @@ test_expect_success 'gracefully add submodule with a trailing slash' '
495 git add init/ &&
496 test_must_fail git diff --exit-code --cached init &&
497 test $commit = $(git ls-files --stage |
498 - sed -n "s/^160000 \([^ ]*\).*/\1/p")
498 + sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
499 + git reset init/ &&
500 + git diff --exit-code --cached init
501
502 '
503