Skip to content

Commit 9d67b61

Browse files
aspiersgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4b78d7b commit 9d67b61

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

builtin/add.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,31 +121,19 @@ static char *prune_directory(struct dir_struct *dir, const char **pathspec, int
121121
return seen;
122122
}
123123

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+
*/
124128
static void treat_gitlinks(const char **pathspec)
125129
{
126130
int i;
127131

128132
if (!pathspec || !*pathspec)
129133
return;
130134

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]);
149137
}
150138

151139
static void refresh(int verbose, const char **pathspec)

pathspec.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,34 @@ char *find_pathspecs_matching_against_index(const char **pathspec)
5656
add_pathspec_matches_against_index(pathspec, seen, i);
5757
return seen;
5858
}
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
extern char *find_pathspecs_matching_against_index(const char **pathspec);
55
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);
67

78
#endif /* PATHSPEC_H */

0 commit comments

Comments
 (0)