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 2/2] Convert read_tree{,_recursive} to support struct pathspec
Date: Thu, 24 Mar 2011 21:41:15 +0700	[thread overview]
Message-ID: <1300977675-6243-2-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1300977675-6243-1-git-send-email-pclouds@gmail.com>

This patch changes behavior of the two functions. Previously it does
prefix matching only. Now it can also do wildcard matching.

All callers are updated. Some gain wildcard matching (archive,
checkout), others reset pathspec_item.has_wildcard to retain old
behavior (ls-files, ls-tree as they are plumbing).

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 I should add a few more tests for other touched commands too. Later..

 archive.c                    |   14 ++++++++++----
 builtin/checkout.c           |    5 ++++-
 builtin/ls-files.c           |    9 +++++----
 builtin/ls-tree.c            |   12 +++++++-----
 merge-recursive.c            |    4 +++-
 t/t3102-ls-tree-wildcards.sh |   22 ++++++++++++++++++++++
 tree.c                       |   13 ++++---------
 tree.h                       |    4 ++--
 8 files changed, 57 insertions(+), 26 deletions(-)
 create mode 100755 t/t3102-ls-tree-wildcards.sh

diff --git a/archive.c b/archive.c
index 1944ed4..6eb5837 100644
--- a/archive.c
+++ b/archive.c
@@ -157,6 +157,7 @@ int write_archive_entries(struct archiver_args *args,
 	struct archiver_context context;
 	struct unpack_trees_options opts;
 	struct tree_desc t;
+	struct pathspec pathspecs;
 	int err;
 
 	if (args->baselen > 0 && args->base[args->baselen - 1] == '/') {
@@ -191,8 +192,10 @@ int write_archive_entries(struct archiver_args *args,
 		git_attr_set_direction(GIT_ATTR_INDEX, &the_index);
 	}
 
-	err = read_tree_recursive(args->tree, "", 0, 0, args->pathspec,
+	init_pathspec(&pathspecs, args->pathspec);
+	err = read_tree_recursive(args->tree, "", 0, 0, &pathspecs,
 				  write_archive_entry, &context);
+	free_pathspec(&pathspecs);
 	if (err == READ_TREE_RECURSIVE)
 		err = 0;
 	return err;
@@ -222,10 +225,13 @@ static int reject_entry(const unsigned char *sha1, const char *base,
 static int path_exists(struct tree *tree, const char *path)
 {
 	const char *pathspec[] = { path, NULL };
+	struct pathspec pathspecs;
+	int ret;
 
-	if (read_tree_recursive(tree, "", 0, 0, pathspec, reject_entry, NULL))
-		return 1;
-	return 0;
+	init_pathspec(&pathspecs, pathspec);
+	ret = read_tree_recursive(tree, "", 0, 0, &pathspecs, reject_entry, NULL);
+	free_pathspec(&pathspecs);
+	return ret != 0;
 }
 
 static void parse_pathspec_arg(const char **pathspec,
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 2bf02f2..1558910 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -79,7 +79,10 @@ static int update_some(const unsigned char *sha1, const char *base, int baselen,
 
 static int read_tree_some(struct tree *tree, const char **pathspec)
 {
-	read_tree_recursive(tree, "", 0, 0, pathspec, update_some, NULL);
+	struct pathspec pathspecs;
+	init_pathspec(&pathspecs, pathspec);
+	read_tree_recursive(tree, "", 0, 0, &pathspecs, update_some, NULL);
+	free_pathspec(&pathspecs);
 
 	/* update the index with the given tree's info
 	 * for all args, expanding wildcards, and exit
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index fb2d5f4..2e001e1 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -338,7 +338,7 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
 {
 	struct tree *tree;
 	unsigned char sha1[20];
-	const char **match;
+	struct pathspec pathspecs;
 	struct cache_entry *last_stage0 = NULL;
 	int i;
 
@@ -360,10 +360,11 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
 		static const char *(matchbuf[2]);
 		matchbuf[0] = prefix;
 		matchbuf[1] = NULL;
-		match = matchbuf;
+		init_pathspec(&pathspecs, matchbuf);
+		pathspecs.items[0].has_wildcard = 0;
 	} else
-		match = NULL;
-	if (read_tree(tree, 1, match))
+		init_pathspec(&pathspecs, NULL);
+	if (read_tree(tree, 1, &pathspecs))
 		die("unable to read tree entries %s", tree_name);
 
 	for (i = 0; i < active_nr; i++) {
diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c
index f73e6bd..9c074de 100644
--- a/builtin/ls-tree.c
+++ b/builtin/ls-tree.c
@@ -19,7 +19,7 @@ static int line_termination = '\n';
 #define LS_SHOW_SIZE 16
 static int abbrev;
 static int ls_options;
-static const char **pathspec;
+static struct pathspec pathspecs;
 static int chomp_prefix;
 static const char *ls_tree_prefix;
 
@@ -35,7 +35,7 @@ static int show_recursive(const char *base, int baselen, const char *pathname)
 	if (ls_options & LS_RECURSIVE)
 		return 1;
 
-	s = pathspec;
+	s = pathspecs.raw;
 	if (!s)
 		return 0;
 
@@ -120,7 +120,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
 {
 	unsigned char sha1[20];
 	struct tree *tree;
-	int full_tree = 0;
+	int i, full_tree = 0;
 	const struct option ls_tree_options[] = {
 		OPT_BIT('d', NULL, &ls_options, "only show trees",
 			LS_TREE_ONLY),
@@ -166,11 +166,13 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
 	if (get_sha1(argv[0], sha1))
 		die("Not a valid object name %s", argv[0]);
 
-	pathspec = get_pathspec(prefix, argv + 1);
+	init_pathspec(&pathspecs, get_pathspec(prefix, argv + 1));
+	for (i = 0; i < pathspecs.nr; i++)
+		pathspecs.items[i].has_wildcard = 0;
 	tree = parse_tree_indirect(sha1);
 	if (!tree)
 		die("not a tree object");
-	read_tree_recursive(tree, "", 0, 0, pathspec, show_tree, NULL);
+	read_tree_recursive(tree, "", 0, 0, &pathspecs, show_tree, NULL);
 
 	return 0;
 }
diff --git a/merge-recursive.c b/merge-recursive.c
index 8e82a8b..89887de 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -278,7 +278,9 @@ static int save_files_dirs(const unsigned char *sha1,
 static int get_files_dirs(struct merge_options *o, struct tree *tree)
 {
 	int n;
-	if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, o))
+	struct pathspec match_all;
+	init_pathspec(&match_all, NULL);
+	if (read_tree_recursive(tree, "", 0, 0, &match_all, save_files_dirs, o))
 		return 0;
 	n = o->current_file_set.nr + o->current_directory_set.nr;
 	return n;
diff --git a/t/t3102-ls-tree-wildcards.sh b/t/t3102-ls-tree-wildcards.sh
new file mode 100755
index 0000000..f2b2a52
--- /dev/null
+++ b/t/t3102-ls-tree-wildcards.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+test_description='ls-tree with(out) wildcards'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	mkdir a aa "a*" &&
+	touch a/one aa/two "a*/three" &&
+	git add a/one aa/two "a*/three" &&
+	git commit -m test
+'
+
+test_expect_success 'ls-tree a* matches literally' '
+	cat >expected <<EOF &&
+100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391	a*/three
+EOF
+	git ls-tree -r HEAD "a*" >actual &&
+	test_cmp expected actual
+'
+
+test_done
diff --git a/tree.c b/tree.c
index 2b23381..9261b9e 100644
--- a/tree.c
+++ b/tree.c
@@ -120,20 +120,15 @@ static int read_tree_1(struct tree *tree, struct strbuf *base,
 
 int read_tree_recursive(struct tree *tree,
 			const char *base, int baselen,
-			int stage, const char **match,
+			int stage, struct pathspec *pathspecs,
 			read_tree_fn_t fn, void *context)
 {
 	struct strbuf sb = STRBUF_INIT;
-	struct pathspec pathspecs;
-	int i, ret;
+	int ret;
 
-	init_pathspec(&pathspecs, match);
-	for (i = 0; i < pathspecs.nr; i++)
-		pathspecs.items[i].has_wildcard = 0;
 	strbuf_add(&sb, base, baselen);
-	ret = read_tree_1(tree, &sb, stage, &pathspecs, fn, context);
+	ret = read_tree_1(tree, &sb, stage, pathspecs, fn, context);
 	strbuf_release(&sb);
-	free_pathspec(&pathspecs);
 	return ret;
 }
 
@@ -147,7 +142,7 @@ static int cmp_cache_name_compare(const void *a_, const void *b_)
 				  ce2->name, ce2->ce_flags);
 }
 
-int read_tree(struct tree *tree, int stage, const char **match)
+int read_tree(struct tree *tree, int stage, struct pathspec *match)
 {
 	read_tree_fn_t fn = NULL;
 	int i, err;
diff --git a/tree.h b/tree.h
index 2ff01a4..6e5f195 100644
--- a/tree.h
+++ b/tree.h
@@ -25,9 +25,9 @@ typedef int (*read_tree_fn_t)(const unsigned char *, const char *, int, const ch
 
 extern int read_tree_recursive(struct tree *tree,
 			       const char *base, int baselen,
-			       int stage, const char **match,
+			       int stage, struct pathspec *pathspecs,
 			       read_tree_fn_t fn, void *context);
 
-extern int read_tree(struct tree *tree, int stage, const char **paths);
+extern int read_tree(struct tree *tree, int stage, struct pathspec *pathspecs);
 
 #endif /* TREE_H */
-- 
1.7.4.74.g639db

  reply	other threads:[~2011-03-24 14:41 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-23 10:33 Relative ls-files John Tapsell
2011-03-23 10:49 ` Michael J Gruber
2011-03-23 11:27   ` John Tapsell
2011-03-23 11:28     ` demerphq
2011-03-23 11:42       ` Michael J Gruber
2011-03-23 13:54         ` Martin Langhoff
2011-03-23 14:09           ` Junio C Hamano
2011-03-23 14:14             ` Nguyen Thai Ngoc Duy
2011-03-23 15:20               ` Junio C Hamano
2011-03-23 15:41                 ` Nguyen Thai Ngoc Duy
2011-03-23 22:44                   ` Junio C Hamano
2011-03-24 13:26                     ` Nguyen Thai Ngoc Duy
2011-03-24 14:41                       ` [PATCH 1/2] Reimplement read_tree_recursive() using tree_entry_interesting() Nguyễn Thái Ngọc Duy
2011-03-24 14:41                         ` Nguyễn Thái Ngọc Duy [this message]
2011-03-24 14:49                           ` [PATCH 2/2] Convert read_tree{,_recursive} to support struct pathspec Nguyen Thai Ngoc Duy
2011-03-24 19:58                           ` Junio C Hamano
2011-03-24 19:07                         ` [PATCH 1/2] Reimplement read_tree_recursive() using tree_entry_interesting() Junio C Hamano
2011-03-24 19:55                         ` Junio C Hamano
2011-03-25  9:34                           ` [PATCH 1/3] " Nguyễn Thái Ngọc Duy
2011-03-25  9:34                             ` [PATCH 2/3] Convert read_tree{,_recursive} to support struct pathspec Nguyễn Thái Ngọc Duy
2011-03-25  9:34                             ` [PATCH 3/3] Improve tree_entry_interesting() handling code Nguyễn Thái Ngọc Duy
2011-03-24 14:47                       ` Relative ls-files Junio C Hamano
2011-03-23 14:46             ` Martin Langhoff
2011-03-23 15:24               ` Junio C Hamano
2011-03-23 17:35             ` Junio C Hamano
2011-03-23 15:42         ` Junio C Hamano
2011-03-23 15:57           ` Michael J Gruber
2011-03-23 16:10             ` Nguyen Thai Ngoc Duy

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=1300977675-6243-2-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).