git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
	Michael J Gruber <git@drmicha.warpmail.net>
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 4/5] Implement new get_pathspec()
Date: Sat,  9 Apr 2011 23:54:19 +0700	[thread overview]
Message-ID: <1302368060-23827-5-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1302368060-23827-1-git-send-email-pclouds@gmail.com>

This function is as same as get_pathspec_old() except that it produces
struct pathspec directly.

no_prefix code path is necessary because init_pathspec() can be called
without get_pathspec_old() in "diff --no-index" case. Without this
exception, prefix_path() will be eventually called and die because
there is not worktree.
---
 cache.h |    1 +
 dir.c   |   74 ++++++++++++++++++++++++++++++++++++++++++++++----------------
 2 files changed, 56 insertions(+), 19 deletions(-)

diff --git a/cache.h b/cache.h
index 08b4633..de57dff 100644
--- a/cache.h
+++ b/cache.h
@@ -534,6 +534,7 @@ struct pathspec {
 };
 
 extern int init_pathspec(struct pathspec *, const char **);
+extern int get_pathspec(struct pathspec *, const char *, int, const char **);
 extern void free_pathspec(struct pathspec *);
 extern int ce_path_match(const struct cache_entry *ce, const struct pathspec *pathspec);
 extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path, int format_check);
diff --git a/dir.c b/dir.c
index 58cedd3..64348a1 100644
--- a/dir.c
+++ b/dir.c
@@ -1265,33 +1265,59 @@ static int pathspec_item_cmp(const void *a_, const void *b_)
 	return strcmp(a->match, b->match);
 }
 
-int init_pathspec(struct pathspec *pathspec, const char **paths)
+extern int prefix_pathspec(struct pathspec_item *item, const char *prefix,
+			   int prefixlen, const char *elt);
+const char *no_prefix = "We do not want outside repository check.";
+int get_pathspec(struct pathspec *pathspec, const char *prefix,
+		 int argc, const char **argv)
 {
-	const char **p = paths;
-	int i;
+	struct pathspec_item *pitem;
+	const char **dst;
+	int prefixlen;
 
 	memset(pathspec, 0, sizeof(*pathspec));
-	if (!p)
+
+	if ((!prefix || prefix == no_prefix) && !argc)
 		return 0;
-	while (*p)
-		p++;
-	pathspec->raw = paths;
-	pathspec->nr = p - paths;
-	if (!pathspec->nr)
+
+	if (!argc) {
+		static const char *spec[2];
+		spec[0] = prefix;
+		spec[1] = NULL;
+		init_pathspec(pathspec, spec);
+		pathspec->items[0].plain_len = pathspec->items[0].len;
 		return 0;
+	}
 
-	pathspec->items = xmalloc(sizeof(struct pathspec_item)*pathspec->nr);
-	memset(pathspec->items, 0, sizeof(struct pathspec_item)*pathspec->nr);
-	for (i = 0; i < pathspec->nr; i++) {
-		struct pathspec_item *item = pathspec->items+i;
-		const char *path = paths[i];
+	if (!strcmp(*argv, ":"))
+		return 0;
 
-		item->match = path;
-		item->len = strlen(path);
-		if (no_wildcard(path))
-			item->magic |= PATHSPEC_NOGLOB;
+	prefixlen = prefix && prefix != no_prefix ? strlen(prefix) : 0;
+	pathspec->nr = argc; /* be optimistic, lower it later if necessary */
+	pathspec->items = xmalloc(sizeof(struct pathspec_item) * argc);
+	pathspec->raw = argv;
+	pitem = pathspec->items;
+	dst = argv;
+
+	while (*argv) {
+		if (prefix == no_prefix) {
+			memset(pitem, 0, sizeof(*pitem));
+			pitem->match = *argv;
+			pitem->len = strlen(pitem->match);
+		}
+		else
+			prefix_pathspec(pitem, prefix, prefixlen, *argv);
+		*dst = *argv++;
+		if (pitem->match) {
+			if (no_wildcard(pitem->match + pitem->plain_len))
+				pitem->magic |= PATHSPEC_NOGLOB;
+			else
+				pathspec->use_wildcard = 1;
+			pitem++;
+			dst++;
+		}
 		else
-			pathspec->use_wildcard = 1;
+			pathspec->nr--;
 	}
 
 	qsort(pathspec->items, pathspec->nr,
@@ -1300,6 +1326,16 @@ int init_pathspec(struct pathspec *pathspec, const char **paths)
 	return 0;
 }
 
+int init_pathspec(struct pathspec *pathspec, const char **paths)
+{
+	const char **p = paths;
+
+	while (p && *p)
+		p++;
+
+	return get_pathspec(pathspec, no_prefix, p - paths, paths);
+}
+
 void free_pathspec(struct pathspec *pathspec)
 {
 	free(pathspec->items);
-- 
1.7.4.74.g639db

  parent reply	other threads:[~2011-04-09 16:55 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-09 16:54 [PATCH 0/5] New get_pathspec() Nguyễn Thái Ngọc Duy
2011-04-09 16:54 ` [PATCH 1/5] Rename functions in preparation for get_pathspec() restructure Nguyễn Thái Ngọc Duy
2011-04-09 16:54 ` [PATCH 2/5] Replace has_wildcard with PATHSPEC_NOGLOB Nguyễn Thái Ngọc Duy
2011-04-10  0:49   ` Junio C Hamano
2011-04-10  7:23     ` Nguyen Thai Ngoc Duy
2011-04-09 16:54 ` [PATCH 3/5] Convert prefix_pathspec() to produce struct pathspec_item Nguyễn Thái Ngọc Duy
2011-04-09 16:54 ` Nguyễn Thái Ngọc Duy [this message]
2011-04-09 16:54 ` [PATCH 5/5] grep: convert to use the new get_pathspec() Nguyễn Thái Ngọc Duy
2011-04-10  0:56   ` Junio C Hamano
2011-04-10  7:26     ` Nguyen Thai Ngoc Duy
2011-04-09 21:41 ` [PATCH 0/5] New get_pathspec() Junio C Hamano

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1302368060-23827-5-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@drmicha.warpmail.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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.
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 NNTP newsgroup(s).