rename pathspec_prefix() to common_prefix() and move to dir.[ch]

Also make common_prefix_len() static as this refactoring makes dir.c itself the only caller of this helper function. Signed-off-by: Clemens Buchacher <drizzd@aon.at> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Clemens Buchacher committed Sep 4, 2011 at 12:42 UTC f950eb956092831730182daa7160eaa352277fa9
6 files changed +15 -12
builtin/commit.c
+1 -1
@@ -257,7 +257,7 @@ static int list_paths(struct string_list *list, const char *with_tree,
257 m = xcalloc(1, i);
258
259 if (with_tree) {
260 - char *max_prefix = pathspec_prefix(pattern);
260 + char *max_prefix = common_prefix(pattern);
261 overlay_tree_on_cache(with_tree, max_prefix ? max_prefix : prefix);
262 free(max_prefix);
263 }
builtin/ls-files.c
+1 -1
@@ -541,7 +541,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
541 strip_trailing_slash_from_submodules();
542
543 /* Find common prefix for all pathspec's */
544 - max_prefix = pathspec_prefix(pathspec);
544 + max_prefix = common_prefix(pathspec);
545 max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
546
547 /* Treat unmatching pathspec elements as errors */
cache.h
-1
@@ -426,7 +426,6 @@ extern void set_git_work_tree(const char *tree);
426 #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
427
428 extern const char **get_pathspec(const char *prefix, const char **pathspec);
429 -extern char *pathspec_prefix(const char **pathspec);
429 extern void setup_work_tree(void);
430 extern const char *setup_git_directory_gently(int *);
431 extern const char *setup_git_directory(void);
dir.c
+12 -1
@@ -34,7 +34,7 @@ int fnmatch_icase(const char *pattern, const char *string, int flags)
34 return fnmatch(pattern, string, flags | (ignore_case ? FNM_CASEFOLD : 0));
35 }
36
37 -size_t common_prefix_len(const char **pathspec)
37 +static size_t common_prefix_len(const char **pathspec)
38 {
39 const char *n, *first;
40 size_t max = 0;
@@ -61,6 +61,17 @@ size_t common_prefix_len(const char **pathspec)
61 return max;
62 }
63
64 +/*
65 + * Returns a copy of the longest leading path common among all
66 + * pathspecs.
67 + */
68 +char *common_prefix(const char **pathspec)
69 +{
70 + unsigned long len = common_prefix_len(pathspec);
71 +
72 + return len ? xmemdupz(*pathspec, len) : NULL;
73 +}
74 +
75 int fill_directory(struct dir_struct *dir, const char **pathspec)
76 {
77 const char *path;
dir.h
+1 -1
@@ -64,7 +64,7 @@ struct dir_struct {
64 #define MATCHED_RECURSIVELY 1
65 #define MATCHED_FNMATCH 2
66 #define MATCHED_EXACTLY 3
67 -extern size_t common_prefix_len(const char **pathspec);
67 +extern char *common_prefix(const char **pathspec);
68 extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
69 extern int match_pathspec_depth(const struct pathspec *pathspec,
70 const char *name, int namelen,
setup.c
-7
@@ -264,13 +264,6 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
264 return pathspec;
265 }
266
267 -char *pathspec_prefix(const char **pathspec)
268 -{
269 - size_t len = common_prefix_len(pathspec);
270 -
271 - return len ? xmemdupz(*pathspec, len) : NULL;
272 -}
273 -
267 /*
268 * Test if it looks like we're at a git directory.
269 * We want to see: