commit: detect misspelled pathspec while making a partial commit.
When you say "git commit Documentaiton" to make partial commit for the files only in that directory, we did not detect that as a misspelled pathname and attempted to commit index without change. If nothing matched, there is no harm done, but if the index gets modified otherwise by having another valid pathspec or after an explicit update-index, a user will not notice without paying attention to the "git status" preview. This introduces --error-unmatch option to ls-files, and uses it to detect this common user error. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Feb 14, 2006 at 12:40 UTC
bba319b5cecacaccef85ce3c4cf9e3593532e328
2 files changed
+53
-17
git-commit.sh
+9
-10
@@ -180,6 +180,7 @@ verify=t
180
verbose=
181
signoff=
182
force_author=
183
+only_include_assumed=
184
while case "$#" in 0) break;; esac
185
do
186
case "$1" in
@@ -340,15 +341,8 @@ case "$#,$also$only" in
341
0,)
342
;;
343
*,)
343
- echo >&2 "assuming --include paths..."
344
+ only_include_assumed="# Explicit paths specified without -i nor -o; assuming --include paths..."
345
also=t
345
- # Later when switch the defaults, we will replace them with these:
346
- # echo >&2 "assuming --only paths..."
347
- # also=
348
-
349
- # If we are going to launch an editor, the message won't be
350
- # shown without this...
351
- test -z "$log_given$status_only" && sleep 1
346
;;
347
esac
348
unset only
@@ -383,6 +377,8 @@ t,)
377
;;
378
,t)
379
save_index &&
380
+ git-ls-files --error-unmatch -- "$@" >/dev/null || exit
381
+
382
git-diff-files --name-only -z -- "$@" |
383
(
384
cd "$TOP"
@@ -411,7 +407,7 @@ t,)
407
refuse_partial "Different in index and the last commit:
408
$dirty_in_index"
409
fi
414
- commit_only=`git-ls-files -- "$@"`
410
+ commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
411
412
# Build the temporary index and update the real index
413
# the same way.
@@ -572,7 +568,10 @@ else
568
PARENTS=""
569
fi
570
575
-run_status >>"$GIT_DIR"/COMMIT_EDITMSG
571
+{
572
+ test -z "$only_include_assumed" || echo "$only_include_assumed"
573
+ run_status
574
+} >>"$GIT_DIR"/COMMIT_EDITMSG
575
if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
576
then
577
rm -f "$GIT_DIR/COMMIT_EDITMSG"
ls-files.c
+44
-7
@@ -25,6 +25,8 @@ static int line_terminator = '\n';
25
static int prefix_len = 0, prefix_offset = 0;
26
static const char *prefix = NULL;
27
static const char **pathspec = NULL;
28
+static int error_unmatch = 0;
29
+static char *ps_matched = NULL;
30
31
static const char *tag_cached = "";
32
static const char *tag_unmerged = "";
@@ -325,7 +327,8 @@ static int cmp_name(const void *p1, const void *p2)
327
* Match a pathspec against a filename. The first "len" characters
328
* are the common prefix
329
*/
328
-static int match(const char **spec, const char *filename, int len)
330
+static int match(const char **spec, char *ps_matched,
331
+ const char *filename, int len)
332
{
333
const char *m;
334
@@ -333,17 +336,24 @@ static int match(const char **spec, const char *filename, int len)
336
int matchlen = strlen(m + len);
337
338
if (!matchlen)
336
- return 1;
339
+ goto matched;
340
if (!strncmp(m + len, filename + len, matchlen)) {
341
if (m[len + matchlen - 1] == '/')
339
- return 1;
342
+ goto matched;
343
switch (filename[len + matchlen]) {
344
case '/': case '\0':
342
- return 1;
345
+ goto matched;
346
}
347
}
348
if (!fnmatch(m + len, filename + len, 0))
346
- return 1;
349
+ goto matched;
350
+ if (ps_matched)
351
+ ps_matched++;
352
+ continue;
353
+ matched:
354
+ if (ps_matched)
355
+ *ps_matched = 1;
356
+ return 1;
357
}
358
return 0;
359
}
@@ -356,7 +366,7 @@ static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
366
if (len >= ent->len)
367
die("git-ls-files: internal error - directory entry not superset of prefix");
368
359
- if (pathspec && !match(pathspec, ent->name, len))
369
+ if (pathspec && !match(pathspec, ps_matched, ent->name, len))
370
return;
371
372
fputs(tag, stdout);
@@ -444,7 +454,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
454
if (len >= ce_namelen(ce))
455
die("git-ls-files: internal error - cache entry not superset of prefix");
456
447
- if (pathspec && !match(pathspec, ce->name, len))
457
+ if (pathspec && !match(pathspec, ps_matched, ce->name, len))
458
return;
459
460
if (!show_stage) {
@@ -699,6 +709,10 @@ int main(int argc, const char **argv)
709
prefix_offset = 0;
710
continue;
711
}
712
+ if (!strcmp(arg, "--error-unmatch")) {
713
+ error_unmatch = 1;
714
+ continue;
715
+ }
716
if (*arg == '-')
717
usage(ls_files_usage);
718
break;
@@ -710,6 +724,14 @@ int main(int argc, const char **argv)
724
if (pathspec)
725
verify_pathspec();
726
727
+ /* Treat unmatching pathspec elements as errors */
728
+ if (pathspec && error_unmatch) {
729
+ int num;
730
+ for (num = 0; pathspec[num]; num++)
731
+ ;
732
+ ps_matched = xcalloc(1, num);
733
+ }
734
+
735
if (show_ignored && !exc_given) {
736
fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
737
argv[0]);
@@ -725,5 +747,20 @@ int main(int argc, const char **argv)
747
if (prefix)
748
prune_cache();
749
show_files();
750
+
751
+ if (ps_matched) {
752
+ /* We need to make sure all pathspec matched otherwise
753
+ * it is an error.
754
+ */
755
+ int num, errors = 0;
756
+ for (num = 0; pathspec[num]; num++) {
757
+ if (ps_matched[num])
758
+ continue;
759
+ error("pathspec '%s' did not match any.",
760
+ pathspec[num] + prefix_len);
761
+ }
762
+ return errors ? 1 : 0;
763
+ }
764
+
765
return 0;
766
}