From: Adam Spiers <git@adamspiers.org>
To: git list <git@vger.kernel.org>
Subject: [PATCH v4 08/11] add.c: extract check_path_for_gitlink() from treat_gitlinks() for reuse
Date: Sun, 6 Jan 2013 16:58:10 +0000 [thread overview]
Message-ID: <1357491493-11619-9-git-send-email-git@adamspiers.org> (raw)
In-Reply-To: <1357491493-11619-1-git-send-email-git@adamspiers.org>
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>
---
Unlike v3, this series doesn't make treat_gitlinks() public.
builtin/add.c | 24 ++++++------------------
pathspec.c | 31 +++++++++++++++++++++++++++++++
pathspec.h | 1 +
3 files changed, 38 insertions(+), 18 deletions(-)
diff --git a/builtin/add.c b/builtin/add.c
index 8c3fdf9..f95ded2 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -121,6 +121,10 @@ 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;
@@ -128,24 +132,8 @@ static void treat_gitlinks(const char **pathspec)
if (!pathspec || !*pathspec)
return;
- for (i = 0; i < active_nr; i++) {
- struct cache_entry *ce = active_cache[i];
- if (S_ISGITLINK(ce->ce_mode)) {
- int len = ce_namelen(ce), j;
- for (j = 0; pathspec[j]; j++) {
- int len2 = strlen(pathspec[j]);
- if (len2 <= len || pathspec[j][len] != '/' ||
- memcmp(ce->name, pathspec[j], len))
- continue;
- if (len2 == len + 1)
- /* strip trailing slash */
- pathspec[j] = xstrndup(ce->name, len);
- else
- die (_("Path '%s' is in submodule '%.*s'"),
- pathspec[j], len, ce->name);
- }
- }
- }
+ for (i = 0; pathspec[i]; i++)
+ pathspec[i] = check_path_for_gitlink(pathspec[i]);
}
static void refresh(int verbose, const char **pathspec)
diff --git a/pathspec.c b/pathspec.c
index b73b15c..02d3344 100644
--- a/pathspec.c
+++ b/pathspec.c
@@ -56,3 +56,34 @@ char *find_pathspecs_matching_against_index(const char **pathspec)
add_pathspec_matches_against_index(pathspec, seen, i);
return seen;
}
+
+/*
+ * 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;
+}
diff --git a/pathspec.h b/pathspec.h
index 3852bc0..bf8eb96 100644
--- a/pathspec.h
+++ b/pathspec.h
@@ -3,5 +3,6 @@
extern char *find_pathspecs_matching_against_index(const char **pathspec);
extern void add_pathspec_matches_against_index(const char **pathspec, char *seen, int specs);
+extern const char *check_path_for_gitlink(const char *path);
#endif /* PATHSPEC_H */
--
1.7.11.7.33.gb8feba5
next prev parent reply other threads:[~2013-01-06 16:59 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-01-03 19:17 What's cooking in git.git (Jan 2013, #02; Thu, 3) Junio C Hamano
2013-01-04 17:23 ` Adam Spiers
2013-01-04 21:13 ` as/check-ignore (was Re: What's cooking in git.git (Jan 2013, #02; Thu, 3)) Junio C Hamano
2013-01-06 16:17 ` Adam Spiers
2013-01-06 16:58 ` [PATCH v4 00/11] new git check-ignore sub-command Adam Spiers
2013-01-06 16:58 ` [PATCH v4 01/11] dir.c: use a single struct exclude_list per source of excludes Adam Spiers
2013-01-06 16:58 ` [PATCH v4 02/11] dir.c: keep track of where patterns came from Adam Spiers
2013-01-06 16:58 ` [PATCH v4 03/11] dir.c: provide clear_directory() for reclaiming dir_struct memory Adam Spiers
2013-01-06 16:58 ` [PATCH v4 04/11] dir.c: improve docs for match_pathspec() and match_pathspec_depth() Adam Spiers
2013-01-06 16:58 ` [PATCH v4 05/11] add.c: remove unused argument from validate_pathspec() Adam Spiers
2013-01-06 16:58 ` [PATCH v4 06/11] add.c: move pathspec matchers into new pathspec.c for reuse Adam Spiers
2013-01-06 16:58 ` [PATCH v4 07/11] pathspec.c: rename newly public functions for clarity Adam Spiers
2013-01-06 16:58 ` Adam Spiers [this message]
2013-01-06 16:58 ` [PATCH v4 09/11] add.c: extract new die_if_path_beyond_symlink() for reuse Adam Spiers
2013-01-06 16:58 ` [PATCH v4 10/11] setup.c: document get_pathspec() Adam Spiers
2013-01-06 16:58 ` [PATCH v4 11/11] add git-check-ignore sub-command Adam Spiers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1357491493-11619-9-git-send-email-git@adamspiers.org \
--to=git@adamspiers.org \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).