add.c: extract check_path_for_gitlink() from treat_gitlinks() for reuse

Extract the body of the for loop in treat_gitlinks() into a separate check_path_for_gitlink() function so that it can be reused elsewhere. This paves the way for a new check-ignore sub-command. Also document treat_gitlinks(). Signed-off-by: Adam Spiers <git@adamspiers.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Adam Spiers committed Jan 6, 2013 at 16:58 UTC 9d67b61f739a1a898bc8c9960047ebff0e9c9d09
3 files changed +38 -18
builtin/add.c
+6 -18
@@ -121,6 +121,10 @@ static char *prune_directory(struct dir_struct *dir, const char **pathspec, int
121 return seen;
122 }
123
124 +/*
125 + * Checks the index to see whether any path in pathspec refers to
126 + * something inside a submodule. If so, dies with an error message.
127 + */
128 static void treat_gitlinks(const char **pathspec)
129 {
130 int i;
@@ -128,24 +132,8 @@ static void treat_gitlinks(const char **pathspec)
132 if (!pathspec || !*pathspec)
133 return;
134
131 - for (i = 0; i < active_nr; i++) {
132 - struct cache_entry *ce = active_cache[i];
133 - if (S_ISGITLINK(ce->ce_mode)) {
134 - int len = ce_namelen(ce), j;
135 - for (j = 0; pathspec[j]; j++) {
136 - int len2 = strlen(pathspec[j]);
137 - if (len2 <= len || pathspec[j][len] != '/' ||
138 - memcmp(ce->name, pathspec[j], len))
139 - continue;
140 - if (len2 == len + 1)
141 - /* strip trailing slash */
142 - pathspec[j] = xstrndup(ce->name, len);
143 - else
144 - die (_("Path '%s' is in submodule '%.*s'"),
145 - pathspec[j], len, ce->name);
146 - }
147 - }
148 - }
135 + for (i = 0; pathspec[i]; i++)
136 + pathspec[i] = check_path_for_gitlink(pathspec[i]);
137 }
138
139 static void refresh(int verbose, const char **pathspec)
pathspec.c
+31
@@ -56,3 +56,34 @@ char *find_pathspecs_matching_against_index(const char **pathspec)
56 add_pathspec_matches_against_index(pathspec, seen, i);
57 return seen;
58 }
59 +
60 +/*
61 + * Check the index to see whether path refers to a submodule, or
62 + * something inside a submodule. If the former, returns the path with
63 + * any trailing slash stripped. If the latter, dies with an error
64 + * message.
65 + */
66 +const char *check_path_for_gitlink(const char *path)
67 +{
68 + int i, path_len = strlen(path);
69 + for (i = 0; i < active_nr; i++) {
70 + struct cache_entry *ce = active_cache[i];
71 + if (S_ISGITLINK(ce->ce_mode)) {
72 + int ce_len = ce_namelen(ce);
73 + if (path_len <= ce_len || path[ce_len] != '/' ||
74 + memcmp(ce->name, path, ce_len))
75 + /* path does not refer to this
76 + * submodule or anything inside it */
77 + continue;
78 + if (path_len == ce_len + 1) {
79 + /* path refers to submodule;
80 + * strip trailing slash */
81 + return xstrndup(ce->name, ce_len);
82 + } else {
83 + die (_("Path '%s' is in submodule '%.*s'"),
84 + path, ce_len, ce->name);
85 + }
86 + }
87 + }
88 + return path;
89 +}
pathspec.h
+1
@@ -3,5 +3,6 @@
3
4 extern char *find_pathspecs_matching_against_index(const char **pathspec);
5 extern void add_pathspec_matches_against_index(const char **pathspec, char *seen, int specs);
6 +extern const char *check_path_for_gitlink(const char *path);
7
8 #endif /* PATHSPEC_H */