Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx> --- builtin/add.c | 100 +++++++++++++++++++++++++--------------------------------- pathspec.c | 43 ------------------------- 2 files changed, 43 insertions(+), 100 deletions(-) diff --git a/builtin/add.c b/builtin/add.c index ab1c9e8..ec6fbe3 100644 --- a/builtin/add.c +++ b/builtin/add.c @@ -121,21 +121,6 @@ static char *prune_directory(struct dir_struct *dir, const char **pathspec, int return seen; } -/* - * Checks the index to see whether any path in pathspec refers to - * something inside a submodule. If so, dies with an error message. - */ -static void treat_gitlinks(const char **pathspec) -{ - int i; - - if (!pathspec || !*pathspec) - return; - - for (i = 0; pathspec[i]; i++) - pathspec[i] = check_path_for_gitlink(pathspec[i]); -} - static void refresh(int verbose, const char **pathspec) { char *seen; @@ -153,25 +138,6 @@ static void refresh(int verbose, const char **pathspec) free(seen); } -/* - * Normalizes argv relative to prefix, via get_pathspec(), and then - * runs die_if_path_beyond_symlink() on each path in the normalized - * list. - */ -static const char **validate_pathspec(const char **argv, const char *prefix) -{ - const char **pathspec = get_pathspec(prefix, argv); - - if (pathspec) { - const char **p; - for (p = pathspec; *p; p++) { - die_if_path_beyond_symlink(*p, prefix); - } - } - - return pathspec; -} - int run_add_interactive(const char *revision, const char *patch_mode, const char **pathspec) { @@ -203,17 +169,23 @@ int run_add_interactive(const char *revision, const char *patch_mode, int interactive_add(int argc, const char **argv, const char *prefix, int patch) { - const char **pathspec = NULL; + struct pathspec pathspec; - if (argc) { - pathspec = validate_pathspec(argv, prefix); - if (!pathspec) - return -1; - } + /* + * git-add--interactive itself does not parse pathspec. It + * simply passes the pathspec to other builtin commands. Let's + * hope all of them support all magic, or we'll need to limit + * the magic here. There is still a problem with prefix. But + * that'll be worked on later on. + */ + parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP, + PATHSPEC_PREFER_FULL | + PATHSPEC_SYMLINK_LEADING_PATH, + prefix, argv); return run_add_interactive(NULL, patch ? "--patch" : NULL, - pathspec); + pathspec.raw); } static int edit_patch(int argc, const char **argv, const char *prefix) @@ -354,7 +326,7 @@ int cmd_add(int argc, const char **argv, const char *prefix) { int exit_status = 0; int newfd; - const char **pathspec; + struct pathspec pathspec; struct dir_struct dir; int flags; int add_new_files; @@ -415,11 +387,19 @@ int cmd_add(int argc, const char **argv, const char *prefix) fprintf(stderr, _("Maybe you wanted to say 'git add .'?\n")); return 0; } - pathspec = validate_pathspec(argv, prefix); if (read_cache() < 0) die(_("index file corrupt")); - treat_gitlinks(pathspec); + + /* + * Check the "pathspec '%s' did not match any files" block + * below before enabling new magic. + */ + parse_pathspec(&pathspec, 0, + PATHSPEC_PREFER_FULL | + PATHSPEC_SYMLINK_LEADING_PATH | + PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE, + prefix, argv); if (add_new_files) { int baselen; @@ -432,33 +412,39 @@ int cmd_add(int argc, const char **argv, const char *prefix) } /* This picks up the paths that are not tracked */ - baselen = fill_directory(&dir, pathspec); - if (pathspec) - seen = prune_directory(&dir, pathspec, baselen); + baselen = fill_directory(&dir, pathspec.raw); + if (pathspec.nr) + seen = prune_directory(&dir, pathspec.raw, baselen); } if (refresh_only) { - refresh(verbose, pathspec); + refresh(verbose, pathspec.raw); goto finish; } - if (pathspec) { + if (pathspec.nr) { int i; struct path_exclude_check check; path_exclude_check_init(&check, &dir); if (!seen) - seen = find_pathspecs_matching_against_index(pathspec); - for (i = 0; pathspec[i]; i++) { - if (!seen[i] && pathspec[i][0] - && !file_exists(pathspec[i])) { + seen = find_pathspecs_matching_against_index(pathspec.raw); + + /* + * file_exists() assumes exact match + */ + GUARD_PATHSPEC(&pathspec, PATHSPEC_FROMTOP); + + for (i = 0; pathspec.raw[i]; i++) { + if (!seen[i] && pathspec.raw[i][0] + && !file_exists(pathspec.raw[i])) { if (ignore_missing) { int dtype = DT_UNKNOWN; - if (is_path_excluded(&check, pathspec[i], -1, &dtype)) - dir_add_ignored(&dir, pathspec[i], strlen(pathspec[i])); + if (is_path_excluded(&check, pathspec.raw[i], -1, &dtype)) + dir_add_ignored(&dir, pathspec.raw[i], strlen(pathspec.raw[i])); } else die(_("pathspec '%s' did not match any files"), - pathspec[i]); + pathspec.raw[i]); } } free(seen); @@ -467,7 +453,7 @@ int cmd_add(int argc, const char **argv, const char *prefix) plug_bulk_checkin(); - exit_status |= add_files_to_cache(prefix, pathspec, flags); + exit_status |= add_files_to_cache(prefix, pathspec.raw, flags); if (add_new_files) exit_status |= add_files(&dir, flags); diff --git a/pathspec.c b/pathspec.c index f531038..0b658cb 100644 --- a/pathspec.c +++ b/pathspec.c @@ -58,49 +58,6 @@ char *find_pathspecs_matching_against_index(const char **pathspec) } /* - * Check the index to see whether path refers to a submodule, or - * something inside a submodule. If the former, returns the path with - * any trailing slash stripped. If the latter, dies with an error - * message. - */ -const char *check_path_for_gitlink(const char *path) -{ - int i, path_len = strlen(path); - for (i = 0; i < active_nr; i++) { - struct cache_entry *ce = active_cache[i]; - if (S_ISGITLINK(ce->ce_mode)) { - int ce_len = ce_namelen(ce); - if (path_len <= ce_len || path[ce_len] != '/' || - memcmp(ce->name, path, ce_len)) - /* path does not refer to this - * submodule or anything inside it */ - continue; - if (path_len == ce_len + 1) { - /* path refers to submodule; - * strip trailing slash */ - return xstrndup(ce->name, ce_len); - } else { - die (_("Path '%s' is in submodule '%.*s'"), - path, ce_len, ce->name); - } - } - } - return path; -} - -/* - * Dies if the given path refers to a file inside a symlinked - * directory in the index. - */ -void die_if_path_beyond_symlink(const char *path, const char *prefix) -{ - if (has_symlink_leading_path(path, strlen(path))) { - int len = prefix ? strlen(prefix) : 0; - die(_("'%s' is beyond a symbolic link"), path + len); - } -} - -/* * Magic pathspec * * Possible future magic semantics include stuff like: -- 1.8.0.rc0.19.g7bbb31d -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html