2006-05-01 10:28:15 +04:00
|
|
|
/*
|
|
|
|
* Builtin "git grep"
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Junio C Hamano
|
|
|
|
*/
|
|
|
|
#include "cache.h"
|
|
|
|
#include "blob.h"
|
|
|
|
#include "tree.h"
|
|
|
|
#include "commit.h"
|
|
|
|
#include "tag.h"
|
2006-05-02 02:58:29 +04:00
|
|
|
#include "tree-walk.h"
|
2006-05-01 10:28:15 +04:00
|
|
|
#include "builtin.h"
|
|
|
|
#include <regex.h>
|
2006-09-18 03:02:52 +04:00
|
|
|
#include "grep.h"
|
2006-05-01 23:27:56 +04:00
|
|
|
#include <fnmatch.h>
|
2006-05-15 07:49:15 +04:00
|
|
|
#include <sys/wait.h>
|
2006-05-01 10:28:15 +04:00
|
|
|
|
2006-05-01 23:27:56 +04:00
|
|
|
/*
|
|
|
|
* git grep pathspecs are somewhat different from diff-tree pathspecs;
|
|
|
|
* pathname wildcards are allowed.
|
|
|
|
*/
|
2006-05-02 02:58:29 +04:00
|
|
|
static int pathspec_matches(const char **paths, const char *name)
|
2006-05-01 10:28:15 +04:00
|
|
|
{
|
2006-05-01 23:27:56 +04:00
|
|
|
int namelen, i;
|
2006-05-02 02:58:29 +04:00
|
|
|
if (!paths || !*paths)
|
2006-05-01 10:28:15 +04:00
|
|
|
return 1;
|
|
|
|
namelen = strlen(name);
|
2006-05-02 02:58:29 +04:00
|
|
|
for (i = 0; paths[i]; i++) {
|
|
|
|
const char *match = paths[i];
|
|
|
|
int matchlen = strlen(match);
|
2006-05-03 04:27:07 +04:00
|
|
|
const char *cp, *meta;
|
2006-05-01 23:27:56 +04:00
|
|
|
|
2006-06-21 13:04:12 +04:00
|
|
|
if (!matchlen ||
|
|
|
|
((matchlen <= namelen) &&
|
|
|
|
!strncmp(name, match, matchlen) &&
|
|
|
|
(match[matchlen-1] == '/' ||
|
|
|
|
name[matchlen] == '\0' || name[matchlen] == '/')))
|
2006-05-01 23:27:56 +04:00
|
|
|
return 1;
|
|
|
|
if (!fnmatch(match, name, 0))
|
|
|
|
return 1;
|
|
|
|
if (name[namelen-1] != '/')
|
2006-05-01 10:28:15 +04:00
|
|
|
continue;
|
2006-05-01 23:27:56 +04:00
|
|
|
|
2006-05-03 04:27:07 +04:00
|
|
|
/* We are being asked if the directory ("name") is worth
|
2006-05-01 23:27:56 +04:00
|
|
|
* descending into.
|
|
|
|
*
|
|
|
|
* Find the longest leading directory name that does
|
|
|
|
* not have metacharacter in the pathspec; the name
|
|
|
|
* we are looking at must overlap with that directory.
|
2006-05-01 10:28:15 +04:00
|
|
|
*/
|
2006-05-03 04:27:07 +04:00
|
|
|
for (cp = match, meta = NULL; cp - match < matchlen; cp++) {
|
2006-05-01 23:27:56 +04:00
|
|
|
char ch = *cp;
|
2006-05-03 04:27:07 +04:00
|
|
|
if (ch == '*' || ch == '[' || ch == '?') {
|
|
|
|
meta = cp;
|
2006-05-01 10:28:15 +04:00
|
|
|
break;
|
2006-05-03 04:27:07 +04:00
|
|
|
}
|
2006-05-01 23:27:56 +04:00
|
|
|
}
|
2006-05-03 04:27:07 +04:00
|
|
|
if (!meta)
|
|
|
|
meta = cp; /* fully literal */
|
|
|
|
|
|
|
|
if (namelen <= meta - match) {
|
2006-05-01 23:27:56 +04:00
|
|
|
/* Looking at "Documentation/" and
|
|
|
|
* the pattern says "Documentation/howto/", or
|
2006-05-03 04:27:07 +04:00
|
|
|
* "Documentation/diff*.txt". The name we
|
|
|
|
* have should match prefix.
|
2006-05-01 23:27:56 +04:00
|
|
|
*/
|
|
|
|
if (!memcmp(match, name, namelen))
|
|
|
|
return 1;
|
2006-05-03 04:27:07 +04:00
|
|
|
continue;
|
2006-05-01 23:27:56 +04:00
|
|
|
}
|
2006-05-03 04:27:07 +04:00
|
|
|
|
|
|
|
if (meta - match < namelen) {
|
2006-05-01 23:27:56 +04:00
|
|
|
/* Looking at "Documentation/howto/" and
|
2006-05-03 04:27:07 +04:00
|
|
|
* the pattern says "Documentation/h*";
|
|
|
|
* match up to "Do.../h"; this avoids descending
|
|
|
|
* into "Documentation/technical/".
|
2006-05-01 23:27:56 +04:00
|
|
|
*/
|
2006-05-03 04:27:07 +04:00
|
|
|
if (!memcmp(match, name, meta - match))
|
2006-05-01 23:27:56 +04:00
|
|
|
return 1;
|
2006-05-03 04:27:07 +04:00
|
|
|
continue;
|
2006-05-01 23:27:56 +04:00
|
|
|
}
|
2006-05-01 10:28:15 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-11 11:44:42 +04:00
|
|
|
static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len)
|
2006-05-01 10:28:15 +04:00
|
|
|
{
|
|
|
|
unsigned long size;
|
|
|
|
char *data;
|
|
|
|
char type[20];
|
2006-08-11 11:44:42 +04:00
|
|
|
char *to_free = NULL;
|
2006-05-01 10:28:15 +04:00
|
|
|
int hit;
|
2006-08-11 11:44:42 +04:00
|
|
|
|
2006-05-01 10:28:15 +04:00
|
|
|
data = read_sha1_file(sha1, type, &size);
|
|
|
|
if (!data) {
|
|
|
|
error("'%s': unable to read %s", name, sha1_to_hex(sha1));
|
|
|
|
return 0;
|
|
|
|
}
|
2006-08-11 11:44:42 +04:00
|
|
|
if (opt->relative && opt->prefix_length) {
|
|
|
|
static char name_buf[PATH_MAX];
|
|
|
|
char *cp;
|
|
|
|
int name_len = strlen(name) - opt->prefix_length + 1;
|
|
|
|
|
|
|
|
if (!tree_name_len)
|
|
|
|
name += opt->prefix_length;
|
|
|
|
else {
|
|
|
|
if (ARRAY_SIZE(name_buf) <= name_len)
|
|
|
|
cp = to_free = xmalloc(name_len);
|
|
|
|
else
|
|
|
|
cp = name_buf;
|
|
|
|
memcpy(cp, name, tree_name_len);
|
|
|
|
strcpy(cp + tree_name_len,
|
|
|
|
name + tree_name_len + opt->prefix_length);
|
|
|
|
name = cp;
|
|
|
|
}
|
|
|
|
}
|
2006-05-01 10:28:15 +04:00
|
|
|
hit = grep_buffer(opt, name, data, size);
|
|
|
|
free(data);
|
2006-08-11 11:44:42 +04:00
|
|
|
free(to_free);
|
2006-05-01 10:28:15 +04:00
|
|
|
return hit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int grep_file(struct grep_opt *opt, const char *filename)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int i;
|
|
|
|
char *data;
|
|
|
|
if (lstat(filename, &st) < 0) {
|
|
|
|
err_ret:
|
|
|
|
if (errno != ENOENT)
|
|
|
|
error("'%s': %s", filename, strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!st.st_size)
|
|
|
|
return 0; /* empty file -- no grep hit */
|
|
|
|
if (!S_ISREG(st.st_mode))
|
|
|
|
return 0;
|
|
|
|
i = open(filename, O_RDONLY);
|
|
|
|
if (i < 0)
|
|
|
|
goto err_ret;
|
|
|
|
data = xmalloc(st.st_size + 1);
|
|
|
|
if (st.st_size != xread(i, data, st.st_size)) {
|
|
|
|
error("'%s': short read %s", filename, strerror(errno));
|
|
|
|
close(i);
|
|
|
|
free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
close(i);
|
2006-08-11 11:44:42 +04:00
|
|
|
if (opt->relative && opt->prefix_length)
|
|
|
|
filename += opt->prefix_length;
|
2006-05-01 10:28:15 +04:00
|
|
|
i = grep_buffer(opt, filename, data, st.st_size);
|
|
|
|
free(data);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2006-05-15 07:49:15 +04:00
|
|
|
static int exec_grep(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
argv[argc] = NULL;
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0)
|
|
|
|
return pid;
|
|
|
|
if (!pid) {
|
|
|
|
execvp("grep", (char **) argv);
|
|
|
|
exit(255);
|
|
|
|
}
|
|
|
|
while (waitpid(pid, &status, 0) < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
if (!WEXITSTATUS(status))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAXARGS 1000
|
2006-05-16 00:28:01 +04:00
|
|
|
#define ARGBUF 4096
|
|
|
|
#define push_arg(a) do { \
|
|
|
|
if (nr < MAXARGS) argv[nr++] = (a); \
|
|
|
|
else die("maximum number of args exceeded"); \
|
|
|
|
} while (0)
|
2006-05-15 07:49:15 +04:00
|
|
|
|
|
|
|
static int external_grep(struct grep_opt *opt, const char **paths, int cached)
|
|
|
|
{
|
2006-07-04 13:43:40 +04:00
|
|
|
int i, nr, argc, hit, len, status;
|
2006-05-15 07:49:15 +04:00
|
|
|
const char *argv[MAXARGS+1];
|
2006-05-16 00:28:01 +04:00
|
|
|
char randarg[ARGBUF];
|
|
|
|
char *argptr = randarg;
|
2006-05-15 07:49:15 +04:00
|
|
|
struct grep_pat *p;
|
|
|
|
|
2006-08-11 11:44:42 +04:00
|
|
|
if (opt->extended || (opt->relative && opt->prefix_length))
|
2006-06-30 14:04:05 +04:00
|
|
|
return -1;
|
2006-05-16 00:28:01 +04:00
|
|
|
len = nr = 0;
|
|
|
|
push_arg("grep");
|
|
|
|
if (opt->fixed)
|
2006-05-16 04:54:01 +04:00
|
|
|
push_arg("-F");
|
2006-05-16 00:28:01 +04:00
|
|
|
if (opt->linenum)
|
|
|
|
push_arg("-n");
|
2006-09-14 21:45:12 +04:00
|
|
|
if (!opt->pathname)
|
|
|
|
push_arg("-h");
|
2006-05-16 00:28:01 +04:00
|
|
|
if (opt->regflags & REG_EXTENDED)
|
|
|
|
push_arg("-E");
|
2006-06-07 03:15:16 +04:00
|
|
|
if (opt->regflags & REG_ICASE)
|
|
|
|
push_arg("-i");
|
2006-05-15 07:49:15 +04:00
|
|
|
if (opt->word_regexp)
|
2006-05-16 00:28:01 +04:00
|
|
|
push_arg("-w");
|
2006-05-15 07:49:15 +04:00
|
|
|
if (opt->name_only)
|
2006-05-16 00:28:01 +04:00
|
|
|
push_arg("-l");
|
|
|
|
if (opt->unmatch_name_only)
|
|
|
|
push_arg("-L");
|
|
|
|
if (opt->count)
|
|
|
|
push_arg("-c");
|
|
|
|
if (opt->post_context || opt->pre_context) {
|
|
|
|
if (opt->post_context != opt->pre_context) {
|
|
|
|
if (opt->pre_context) {
|
|
|
|
push_arg("-B");
|
|
|
|
len += snprintf(argptr, sizeof(randarg)-len,
|
|
|
|
"%u", opt->pre_context);
|
|
|
|
if (sizeof(randarg) <= len)
|
|
|
|
die("maximum length of args exceeded");
|
|
|
|
push_arg(argptr);
|
|
|
|
argptr += len;
|
|
|
|
}
|
|
|
|
if (opt->post_context) {
|
|
|
|
push_arg("-A");
|
|
|
|
len += snprintf(argptr, sizeof(randarg)-len,
|
|
|
|
"%u", opt->post_context);
|
|
|
|
if (sizeof(randarg) <= len)
|
|
|
|
die("maximum length of args exceeded");
|
|
|
|
push_arg(argptr);
|
|
|
|
argptr += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
push_arg("-C");
|
|
|
|
len += snprintf(argptr, sizeof(randarg)-len,
|
|
|
|
"%u", opt->post_context);
|
|
|
|
if (sizeof(randarg) <= len)
|
|
|
|
die("maximum length of args exceeded");
|
|
|
|
push_arg(argptr);
|
|
|
|
argptr += len;
|
|
|
|
}
|
|
|
|
}
|
2006-05-15 07:49:15 +04:00
|
|
|
for (p = opt->pattern_list; p; p = p->next) {
|
2006-05-16 00:28:01 +04:00
|
|
|
push_arg("-e");
|
|
|
|
push_arg(p->pattern);
|
2006-05-15 07:49:15 +04:00
|
|
|
}
|
2006-05-17 22:12:22 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* To make sure we get the header printed out when we want it,
|
|
|
|
* add /dev/null to the paths to grep. This is unnecessary
|
|
|
|
* (and wrong) with "-l" or "-L", which always print out the
|
|
|
|
* name anyway.
|
|
|
|
*
|
|
|
|
* GNU grep has "-H", but this is portable.
|
|
|
|
*/
|
|
|
|
if (!opt->name_only && !opt->unmatch_name_only)
|
|
|
|
push_arg("/dev/null");
|
2006-05-15 07:49:15 +04:00
|
|
|
|
|
|
|
hit = 0;
|
|
|
|
argc = nr;
|
|
|
|
for (i = 0; i < active_nr; i++) {
|
|
|
|
struct cache_entry *ce = active_cache[i];
|
2006-05-22 00:45:46 +04:00
|
|
|
char *name;
|
2006-11-26 23:47:52 +03:00
|
|
|
if (!S_ISREG(ntohl(ce->ce_mode)))
|
2006-05-15 07:49:15 +04:00
|
|
|
continue;
|
|
|
|
if (!pathspec_matches(paths, ce->name))
|
|
|
|
continue;
|
2006-05-17 22:12:22 +04:00
|
|
|
name = ce->name;
|
|
|
|
if (name[0] == '-') {
|
|
|
|
int len = ce_namelen(ce);
|
|
|
|
name = xmalloc(len + 3);
|
|
|
|
memcpy(name, "./", 2);
|
|
|
|
memcpy(name + 2, ce->name, len + 1);
|
|
|
|
}
|
|
|
|
argv[argc++] = name;
|
2006-11-26 23:47:52 +03:00
|
|
|
if (argc < MAXARGS && !ce_stage(ce))
|
2006-05-15 07:49:15 +04:00
|
|
|
continue;
|
2006-07-04 13:43:40 +04:00
|
|
|
status = exec_grep(argc, argv);
|
|
|
|
if (0 < status)
|
|
|
|
hit = 1;
|
2006-05-15 07:49:15 +04:00
|
|
|
argc = nr;
|
2006-11-26 23:47:52 +03:00
|
|
|
if (ce_stage(ce)) {
|
|
|
|
do {
|
|
|
|
i++;
|
|
|
|
} while (i < active_nr &&
|
|
|
|
!strcmp(ce->name, active_cache[i]->name));
|
|
|
|
i--; /* compensate for loop control */
|
|
|
|
}
|
2006-05-15 07:49:15 +04:00
|
|
|
}
|
2006-07-04 13:43:40 +04:00
|
|
|
if (argc > nr) {
|
|
|
|
status = exec_grep(argc, argv);
|
|
|
|
if (0 < status)
|
|
|
|
hit = 1;
|
|
|
|
}
|
|
|
|
return hit;
|
2006-05-15 07:49:15 +04:00
|
|
|
}
|
|
|
|
|
2006-05-02 02:58:29 +04:00
|
|
|
static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
|
2006-05-01 10:28:15 +04:00
|
|
|
{
|
|
|
|
int hit = 0;
|
|
|
|
int nr;
|
|
|
|
read_cache();
|
|
|
|
|
2006-05-15 07:49:15 +04:00
|
|
|
#ifdef __unix__
|
|
|
|
/*
|
|
|
|
* Use the external "grep" command for the case where
|
|
|
|
* we grep through the checked-out files. It tends to
|
|
|
|
* be a lot more optimized
|
|
|
|
*/
|
|
|
|
if (!cached) {
|
|
|
|
hit = external_grep(opt, paths, cached);
|
|
|
|
if (hit >= 0)
|
|
|
|
return hit;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-05-01 10:28:15 +04:00
|
|
|
for (nr = 0; nr < active_nr; nr++) {
|
|
|
|
struct cache_entry *ce = active_cache[nr];
|
2006-11-26 23:47:52 +03:00
|
|
|
if (!S_ISREG(ntohl(ce->ce_mode)))
|
2006-05-01 10:28:15 +04:00
|
|
|
continue;
|
2006-05-02 02:58:29 +04:00
|
|
|
if (!pathspec_matches(paths, ce->name))
|
2006-05-01 10:28:15 +04:00
|
|
|
continue;
|
2006-11-26 23:47:52 +03:00
|
|
|
if (cached) {
|
|
|
|
if (ce_stage(ce))
|
|
|
|
continue;
|
2006-08-11 11:44:42 +04:00
|
|
|
hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
|
2006-11-26 23:47:52 +03:00
|
|
|
}
|
2006-05-01 10:28:15 +04:00
|
|
|
else
|
|
|
|
hit |= grep_file(opt, ce->name);
|
2006-11-26 23:47:52 +03:00
|
|
|
if (ce_stage(ce)) {
|
|
|
|
do {
|
|
|
|
nr++;
|
|
|
|
} while (nr < active_nr &&
|
|
|
|
!strcmp(ce->name, active_cache[nr]->name));
|
|
|
|
nr--; /* compensate for loop control */
|
|
|
|
}
|
2006-05-01 10:28:15 +04:00
|
|
|
}
|
2006-09-28 03:27:10 +04:00
|
|
|
free_grep_patterns(opt);
|
2006-05-01 10:28:15 +04:00
|
|
|
return hit;
|
|
|
|
}
|
|
|
|
|
2006-05-02 02:58:29 +04:00
|
|
|
static int grep_tree(struct grep_opt *opt, const char **paths,
|
2006-05-01 10:28:15 +04:00
|
|
|
struct tree_desc *tree,
|
|
|
|
const char *tree_name, const char *base)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
int hit = 0;
|
tree_entry(): new tree-walking helper function
This adds a "tree_entry()" function that combines the common operation of
doing a "tree_entry_extract()" + "update_tree_entry()".
It also has a simplified calling convention, designed for simple loops
that traverse over a whole tree: the arguments are pointers to the tree
descriptor and a name_entry structure to fill in, and it returns a boolean
"true" if there was an entry left to be gotten in the tree.
This allows tree traversal with
struct tree_desc desc;
struct name_entry entry;
desc.buf = tree->buffer;
desc.size = tree->size;
while (tree_entry(&desc, &entry) {
... use "entry.{path, sha1, mode, pathlen}" ...
}
which is not only shorter than writing it out in full, it's hopefully less
error prone too.
[ It's actually a tad faster too - we don't need to recalculate the entry
pathlength in both extract and update, but need to do it only once.
Also, some callers can avoid doing a "strlen()" on the result, since
it's returned as part of the name_entry structure.
However, by now we're talking just 1% speedup on "git-rev-list --objects
--all", and we're definitely at the point where tree walking is no
longer the issue any more. ]
NOTE! Not everybody wants to use this new helper function, since some of
the tree walkers very much on purpose do the descriptor update separately
from the entry extraction. So the "extract + update" sequence still
remains as the core sequence, this is just a simplified interface.
We should probably add a silly two-line inline helper function for
initializing the descriptor from the "struct tree" too, just to cut down
on the noise from that common "desc" initializer.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-05-30 20:45:45 +04:00
|
|
|
struct name_entry entry;
|
2006-05-01 23:27:56 +04:00
|
|
|
char *down;
|
2006-08-11 11:44:42 +04:00
|
|
|
int tn_len = strlen(tree_name);
|
|
|
|
char *path_buf = xmalloc(PATH_MAX + tn_len + 100);
|
2006-05-01 10:28:15 +04:00
|
|
|
|
2006-08-11 11:44:42 +04:00
|
|
|
if (tn_len) {
|
|
|
|
tn_len = sprintf(path_buf, "%s:", tree_name);
|
|
|
|
down = path_buf + tn_len;
|
2006-05-01 23:27:56 +04:00
|
|
|
strcat(down, base);
|
2006-05-01 10:28:15 +04:00
|
|
|
}
|
|
|
|
else {
|
2006-05-01 23:27:56 +04:00
|
|
|
down = path_buf;
|
|
|
|
strcpy(down, base);
|
2006-05-01 10:28:15 +04:00
|
|
|
}
|
|
|
|
len = strlen(path_buf);
|
|
|
|
|
tree_entry(): new tree-walking helper function
This adds a "tree_entry()" function that combines the common operation of
doing a "tree_entry_extract()" + "update_tree_entry()".
It also has a simplified calling convention, designed for simple loops
that traverse over a whole tree: the arguments are pointers to the tree
descriptor and a name_entry structure to fill in, and it returns a boolean
"true" if there was an entry left to be gotten in the tree.
This allows tree traversal with
struct tree_desc desc;
struct name_entry entry;
desc.buf = tree->buffer;
desc.size = tree->size;
while (tree_entry(&desc, &entry) {
... use "entry.{path, sha1, mode, pathlen}" ...
}
which is not only shorter than writing it out in full, it's hopefully less
error prone too.
[ It's actually a tad faster too - we don't need to recalculate the entry
pathlength in both extract and update, but need to do it only once.
Also, some callers can avoid doing a "strlen()" on the result, since
it's returned as part of the name_entry structure.
However, by now we're talking just 1% speedup on "git-rev-list --objects
--all", and we're definitely at the point where tree walking is no
longer the issue any more. ]
NOTE! Not everybody wants to use this new helper function, since some of
the tree walkers very much on purpose do the descriptor update separately
from the entry extraction. So the "extract + update" sequence still
remains as the core sequence, this is just a simplified interface.
We should probably add a silly two-line inline helper function for
initializing the descriptor from the "struct tree" too, just to cut down
on the noise from that common "desc" initializer.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-05-30 20:45:45 +04:00
|
|
|
while (tree_entry(tree, &entry)) {
|
|
|
|
strcpy(path_buf + len, entry.path);
|
2006-05-01 10:28:15 +04:00
|
|
|
|
tree_entry(): new tree-walking helper function
This adds a "tree_entry()" function that combines the common operation of
doing a "tree_entry_extract()" + "update_tree_entry()".
It also has a simplified calling convention, designed for simple loops
that traverse over a whole tree: the arguments are pointers to the tree
descriptor and a name_entry structure to fill in, and it returns a boolean
"true" if there was an entry left to be gotten in the tree.
This allows tree traversal with
struct tree_desc desc;
struct name_entry entry;
desc.buf = tree->buffer;
desc.size = tree->size;
while (tree_entry(&desc, &entry) {
... use "entry.{path, sha1, mode, pathlen}" ...
}
which is not only shorter than writing it out in full, it's hopefully less
error prone too.
[ It's actually a tad faster too - we don't need to recalculate the entry
pathlength in both extract and update, but need to do it only once.
Also, some callers can avoid doing a "strlen()" on the result, since
it's returned as part of the name_entry structure.
However, by now we're talking just 1% speedup on "git-rev-list --objects
--all", and we're definitely at the point where tree walking is no
longer the issue any more. ]
NOTE! Not everybody wants to use this new helper function, since some of
the tree walkers very much on purpose do the descriptor update separately
from the entry extraction. So the "extract + update" sequence still
remains as the core sequence, this is just a simplified interface.
We should probably add a silly two-line inline helper function for
initializing the descriptor from the "struct tree" too, just to cut down
on the noise from that common "desc" initializer.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-05-30 20:45:45 +04:00
|
|
|
if (S_ISDIR(entry.mode))
|
2006-05-01 23:27:56 +04:00
|
|
|
/* Match "abc/" against pathspec to
|
|
|
|
* decide if we want to descend into "abc"
|
|
|
|
* directory.
|
|
|
|
*/
|
tree_entry(): new tree-walking helper function
This adds a "tree_entry()" function that combines the common operation of
doing a "tree_entry_extract()" + "update_tree_entry()".
It also has a simplified calling convention, designed for simple loops
that traverse over a whole tree: the arguments are pointers to the tree
descriptor and a name_entry structure to fill in, and it returns a boolean
"true" if there was an entry left to be gotten in the tree.
This allows tree traversal with
struct tree_desc desc;
struct name_entry entry;
desc.buf = tree->buffer;
desc.size = tree->size;
while (tree_entry(&desc, &entry) {
... use "entry.{path, sha1, mode, pathlen}" ...
}
which is not only shorter than writing it out in full, it's hopefully less
error prone too.
[ It's actually a tad faster too - we don't need to recalculate the entry
pathlength in both extract and update, but need to do it only once.
Also, some callers can avoid doing a "strlen()" on the result, since
it's returned as part of the name_entry structure.
However, by now we're talking just 1% speedup on "git-rev-list --objects
--all", and we're definitely at the point where tree walking is no
longer the issue any more. ]
NOTE! Not everybody wants to use this new helper function, since some of
the tree walkers very much on purpose do the descriptor update separately
from the entry extraction. So the "extract + update" sequence still
remains as the core sequence, this is just a simplified interface.
We should probably add a silly two-line inline helper function for
initializing the descriptor from the "struct tree" too, just to cut down
on the noise from that common "desc" initializer.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-05-30 20:45:45 +04:00
|
|
|
strcpy(path_buf + len + entry.pathlen, "/");
|
2006-05-01 23:27:56 +04:00
|
|
|
|
2006-05-02 02:58:29 +04:00
|
|
|
if (!pathspec_matches(paths, down))
|
2006-05-01 10:28:15 +04:00
|
|
|
;
|
tree_entry(): new tree-walking helper function
This adds a "tree_entry()" function that combines the common operation of
doing a "tree_entry_extract()" + "update_tree_entry()".
It also has a simplified calling convention, designed for simple loops
that traverse over a whole tree: the arguments are pointers to the tree
descriptor and a name_entry structure to fill in, and it returns a boolean
"true" if there was an entry left to be gotten in the tree.
This allows tree traversal with
struct tree_desc desc;
struct name_entry entry;
desc.buf = tree->buffer;
desc.size = tree->size;
while (tree_entry(&desc, &entry) {
... use "entry.{path, sha1, mode, pathlen}" ...
}
which is not only shorter than writing it out in full, it's hopefully less
error prone too.
[ It's actually a tad faster too - we don't need to recalculate the entry
pathlength in both extract and update, but need to do it only once.
Also, some callers can avoid doing a "strlen()" on the result, since
it's returned as part of the name_entry structure.
However, by now we're talking just 1% speedup on "git-rev-list --objects
--all", and we're definitely at the point where tree walking is no
longer the issue any more. ]
NOTE! Not everybody wants to use this new helper function, since some of
the tree walkers very much on purpose do the descriptor update separately
from the entry extraction. So the "extract + update" sequence still
remains as the core sequence, this is just a simplified interface.
We should probably add a silly two-line inline helper function for
initializing the descriptor from the "struct tree" too, just to cut down
on the noise from that common "desc" initializer.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-05-30 20:45:45 +04:00
|
|
|
else if (S_ISREG(entry.mode))
|
2006-08-11 11:44:42 +04:00
|
|
|
hit |= grep_sha1(opt, entry.sha1, path_buf, tn_len);
|
tree_entry(): new tree-walking helper function
This adds a "tree_entry()" function that combines the common operation of
doing a "tree_entry_extract()" + "update_tree_entry()".
It also has a simplified calling convention, designed for simple loops
that traverse over a whole tree: the arguments are pointers to the tree
descriptor and a name_entry structure to fill in, and it returns a boolean
"true" if there was an entry left to be gotten in the tree.
This allows tree traversal with
struct tree_desc desc;
struct name_entry entry;
desc.buf = tree->buffer;
desc.size = tree->size;
while (tree_entry(&desc, &entry) {
... use "entry.{path, sha1, mode, pathlen}" ...
}
which is not only shorter than writing it out in full, it's hopefully less
error prone too.
[ It's actually a tad faster too - we don't need to recalculate the entry
pathlength in both extract and update, but need to do it only once.
Also, some callers can avoid doing a "strlen()" on the result, since
it's returned as part of the name_entry structure.
However, by now we're talking just 1% speedup on "git-rev-list --objects
--all", and we're definitely at the point where tree walking is no
longer the issue any more. ]
NOTE! Not everybody wants to use this new helper function, since some of
the tree walkers very much on purpose do the descriptor update separately
from the entry extraction. So the "extract + update" sequence still
remains as the core sequence, this is just a simplified interface.
We should probably add a silly two-line inline helper function for
initializing the descriptor from the "struct tree" too, just to cut down
on the noise from that common "desc" initializer.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-05-30 20:45:45 +04:00
|
|
|
else if (S_ISDIR(entry.mode)) {
|
2006-05-01 10:28:15 +04:00
|
|
|
char type[20];
|
|
|
|
struct tree_desc sub;
|
|
|
|
void *data;
|
tree_entry(): new tree-walking helper function
This adds a "tree_entry()" function that combines the common operation of
doing a "tree_entry_extract()" + "update_tree_entry()".
It also has a simplified calling convention, designed for simple loops
that traverse over a whole tree: the arguments are pointers to the tree
descriptor and a name_entry structure to fill in, and it returns a boolean
"true" if there was an entry left to be gotten in the tree.
This allows tree traversal with
struct tree_desc desc;
struct name_entry entry;
desc.buf = tree->buffer;
desc.size = tree->size;
while (tree_entry(&desc, &entry) {
... use "entry.{path, sha1, mode, pathlen}" ...
}
which is not only shorter than writing it out in full, it's hopefully less
error prone too.
[ It's actually a tad faster too - we don't need to recalculate the entry
pathlength in both extract and update, but need to do it only once.
Also, some callers can avoid doing a "strlen()" on the result, since
it's returned as part of the name_entry structure.
However, by now we're talking just 1% speedup on "git-rev-list --objects
--all", and we're definitely at the point where tree walking is no
longer the issue any more. ]
NOTE! Not everybody wants to use this new helper function, since some of
the tree walkers very much on purpose do the descriptor update separately
from the entry extraction. So the "extract + update" sequence still
remains as the core sequence, this is just a simplified interface.
We should probably add a silly two-line inline helper function for
initializing the descriptor from the "struct tree" too, just to cut down
on the noise from that common "desc" initializer.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-05-30 20:45:45 +04:00
|
|
|
data = read_sha1_file(entry.sha1, type, &sub.size);
|
2006-05-01 10:28:15 +04:00
|
|
|
if (!data)
|
|
|
|
die("unable to read tree (%s)",
|
tree_entry(): new tree-walking helper function
This adds a "tree_entry()" function that combines the common operation of
doing a "tree_entry_extract()" + "update_tree_entry()".
It also has a simplified calling convention, designed for simple loops
that traverse over a whole tree: the arguments are pointers to the tree
descriptor and a name_entry structure to fill in, and it returns a boolean
"true" if there was an entry left to be gotten in the tree.
This allows tree traversal with
struct tree_desc desc;
struct name_entry entry;
desc.buf = tree->buffer;
desc.size = tree->size;
while (tree_entry(&desc, &entry) {
... use "entry.{path, sha1, mode, pathlen}" ...
}
which is not only shorter than writing it out in full, it's hopefully less
error prone too.
[ It's actually a tad faster too - we don't need to recalculate the entry
pathlength in both extract and update, but need to do it only once.
Also, some callers can avoid doing a "strlen()" on the result, since
it's returned as part of the name_entry structure.
However, by now we're talking just 1% speedup on "git-rev-list --objects
--all", and we're definitely at the point where tree walking is no
longer the issue any more. ]
NOTE! Not everybody wants to use this new helper function, since some of
the tree walkers very much on purpose do the descriptor update separately
from the entry extraction. So the "extract + update" sequence still
remains as the core sequence, this is just a simplified interface.
We should probably add a silly two-line inline helper function for
initializing the descriptor from the "struct tree" too, just to cut down
on the noise from that common "desc" initializer.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-05-30 20:45:45 +04:00
|
|
|
sha1_to_hex(entry.sha1));
|
2006-05-01 10:28:15 +04:00
|
|
|
sub.buf = data;
|
2006-05-02 02:58:29 +04:00
|
|
|
hit |= grep_tree(opt, paths, &sub, tree_name, down);
|
2006-05-01 10:28:15 +04:00
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hit;
|
|
|
|
}
|
|
|
|
|
2006-05-02 02:58:29 +04:00
|
|
|
static int grep_object(struct grep_opt *opt, const char **paths,
|
2006-05-01 10:28:15 +04:00
|
|
|
struct object *obj, const char *name)
|
|
|
|
{
|
2006-07-12 07:45:31 +04:00
|
|
|
if (obj->type == OBJ_BLOB)
|
2006-08-11 11:44:42 +04:00
|
|
|
return grep_sha1(opt, obj->sha1, name, 0);
|
2006-07-12 07:45:31 +04:00
|
|
|
if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
|
2006-05-01 10:28:15 +04:00
|
|
|
struct tree_desc tree;
|
|
|
|
void *data;
|
|
|
|
int hit;
|
|
|
|
data = read_object_with_reference(obj->sha1, tree_type,
|
|
|
|
&tree.size, NULL);
|
|
|
|
if (!data)
|
|
|
|
die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
|
|
|
|
tree.buf = data;
|
2006-05-02 02:58:29 +04:00
|
|
|
hit = grep_tree(opt, paths, &tree, name, "");
|
2006-05-01 10:28:15 +04:00
|
|
|
free(data);
|
|
|
|
return hit;
|
|
|
|
}
|
Shrink "struct object" a bit
This shrinks "struct object" by a small amount, by getting rid of the
"struct type *" pointer and replacing it with a 3-bit bitfield instead.
In addition, we merge the bitfields and the "flags" field, which
incidentally should also remove a useless 4-byte padding from the object
when in 64-bit mode.
Now, our "struct object" is still too damn large, but it's now less
obviously bloated, and of the remaining fields, only the "util" (which is
not used by most things) is clearly something that should be eventually
discarded.
This shrinks the "git-rev-list --all" memory use by about 2.5% on the
kernel archive (and, perhaps more importantly, on the larger mozilla
archive). That may not sound like much, but I suspect it's more on a
64-bit platform.
There are other remaining inefficiencies (the parent lists, for example,
probably have horrible malloc overhead), but this was pretty obvious.
Most of the patch is just changing the comparison of the "type" pointer
from one of the constant string pointers to the appropriate new TYPE_xxx
small integer constant.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-06-15 03:45:13 +04:00
|
|
|
die("unable to grep from object of type %s", typename(obj->type));
|
2006-05-01 10:28:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char builtin_grep_usage[] =
|
|
|
|
"git-grep <option>* <rev>* [-e] <pattern> [<path>...]";
|
|
|
|
|
2006-07-04 13:44:48 +04:00
|
|
|
static const char emsg_invalid_context_len[] =
|
|
|
|
"%s: invalid context length argument";
|
|
|
|
static const char emsg_missing_context_len[] =
|
|
|
|
"missing context length argument";
|
|
|
|
static const char emsg_missing_argument[] =
|
|
|
|
"option requires an argument -%s";
|
|
|
|
|
2006-07-29 09:44:25 +04:00
|
|
|
int cmd_grep(int argc, const char **argv, const char *prefix)
|
2006-05-01 10:28:15 +04:00
|
|
|
{
|
|
|
|
int hit = 0;
|
|
|
|
int cached = 0;
|
2006-05-09 10:55:47 +04:00
|
|
|
int seen_dashdash = 0;
|
2006-05-01 10:28:15 +04:00
|
|
|
struct grep_opt opt;
|
Add "named object array" concept
We've had this notion of a "object_list" for a long time, which eventually
grew a "name" member because some users (notably git-rev-list) wanted to
name each object as it is generated.
That object_list is great for some things, but it isn't all that wonderful
for others, and the "name" member is generally not used by everybody.
This patch splits the users of the object_list array up into two: the
traditional list users, who want the list-like format, and who don't
actually use or want the name. And another class of users that really used
the list as an extensible array, and generally wanted to name the objects.
The patch is fairly straightforward, but it's also biggish. Most of it
really just cleans things up: switching the revision parsing and listing
over to the array makes things like the builtin-diff usage much simpler
(we now see exactly how many members the array has, and we don't get the
objects reversed from the order they were on the command line).
One of the main reasons for doing this at all is that the malloc overhead
of the simple object list was actually pretty high, and the array is just
a lot denser. So this patch brings down memory usage by git-rev-list by
just under 3% (on top of all the other memory use optimizations) on the
mozilla archive.
It does add more lines than it removes, and more importantly, it adds a
whole new infrastructure for maintaining lists of objects, but on the
other hand, the new dynamic array code is pretty obvious. The change to
builtin-diff-tree.c shows a fairly good example of why an array interface
is sometimes more natural, and just much simpler for everybody.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-06-20 04:42:35 +04:00
|
|
|
struct object_array list = { 0, 0, NULL };
|
2006-05-02 02:58:29 +04:00
|
|
|
const char **paths = NULL;
|
2006-05-09 10:55:47 +04:00
|
|
|
int i;
|
2006-05-01 10:28:15 +04:00
|
|
|
|
|
|
|
memset(&opt, 0, sizeof(opt));
|
2006-08-11 11:44:42 +04:00
|
|
|
opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
|
|
|
|
opt.relative = 1;
|
2006-09-14 21:45:12 +04:00
|
|
|
opt.pathname = 1;
|
2006-05-03 02:40:49 +04:00
|
|
|
opt.pattern_tail = &opt.pattern_list;
|
2006-05-01 10:28:15 +04:00
|
|
|
opt.regflags = REG_NEWLINE;
|
|
|
|
|
|
|
|
/*
|
2006-05-09 10:55:47 +04:00
|
|
|
* If there is no -- then the paths must exist in the working
|
|
|
|
* tree. If there is no explicit pattern specified with -e or
|
|
|
|
* -f, we take the first unrecognized non option to be the
|
|
|
|
* pattern, but then what follows it must be zero or more
|
|
|
|
* valid refs up to the -- (if exists), and then existing
|
|
|
|
* paths. If there is an explicit pattern, then the first
|
2006-07-10 09:50:18 +04:00
|
|
|
* unrecognized non option is the beginning of the refs list
|
2006-05-09 10:55:47 +04:00
|
|
|
* that continues up to the -- (if exists), and then paths.
|
2006-05-01 10:28:15 +04:00
|
|
|
*/
|
2006-05-09 10:55:47 +04:00
|
|
|
|
2006-05-02 02:58:29 +04:00
|
|
|
while (1 < argc) {
|
|
|
|
const char *arg = argv[1];
|
|
|
|
argc--; argv++;
|
|
|
|
if (!strcmp("--cached", arg)) {
|
|
|
|
cached = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-04 08:05:29 +04:00
|
|
|
if (!strcmp("-a", arg) ||
|
|
|
|
!strcmp("--text", arg)) {
|
|
|
|
opt.binary = GREP_BINARY_TEXT;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-02 02:58:29 +04:00
|
|
|
if (!strcmp("-i", arg) ||
|
|
|
|
!strcmp("--ignore-case", arg)) {
|
|
|
|
opt.regflags |= REG_ICASE;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-04 08:05:29 +04:00
|
|
|
if (!strcmp("-I", arg)) {
|
|
|
|
opt.binary = GREP_BINARY_NOMATCH;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-02 02:58:29 +04:00
|
|
|
if (!strcmp("-v", arg) ||
|
|
|
|
!strcmp("--invert-match", arg)) {
|
|
|
|
opt.invert = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp("-E", arg) ||
|
|
|
|
!strcmp("--extended-regexp", arg)) {
|
|
|
|
opt.regflags |= REG_EXTENDED;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-10 05:28:41 +04:00
|
|
|
if (!strcmp("-F", arg) ||
|
|
|
|
!strcmp("--fixed-strings", arg)) {
|
|
|
|
opt.fixed = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-02 02:58:29 +04:00
|
|
|
if (!strcmp("-G", arg) ||
|
|
|
|
!strcmp("--basic-regexp", arg)) {
|
|
|
|
opt.regflags &= ~REG_EXTENDED;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp("-n", arg)) {
|
|
|
|
opt.linenum = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2006-09-14 21:45:12 +04:00
|
|
|
if (!strcmp("-h", arg)) {
|
|
|
|
opt.pathname = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-02 02:58:29 +04:00
|
|
|
if (!strcmp("-H", arg)) {
|
2006-09-14 21:45:12 +04:00
|
|
|
opt.pathname = 1;
|
2006-05-02 02:58:29 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp("-l", arg) ||
|
|
|
|
!strcmp("--files-with-matches", arg)) {
|
|
|
|
opt.name_only = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-04 08:46:29 +04:00
|
|
|
if (!strcmp("-L", arg) ||
|
|
|
|
!strcmp("--files-without-match", arg)) {
|
|
|
|
opt.unmatch_name_only = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-03 02:40:49 +04:00
|
|
|
if (!strcmp("-c", arg) ||
|
|
|
|
!strcmp("--count", arg)) {
|
|
|
|
opt.count = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-03 02:40:49 +04:00
|
|
|
if (!strcmp("-w", arg) ||
|
|
|
|
!strcmp("--word-regexp", arg)) {
|
|
|
|
opt.word_regexp = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-03 02:17:05 +04:00
|
|
|
if (!strncmp("-A", arg, 2) ||
|
|
|
|
!strncmp("-B", arg, 2) ||
|
|
|
|
!strncmp("-C", arg, 2) ||
|
|
|
|
(arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {
|
2006-05-02 02:58:29 +04:00
|
|
|
unsigned num;
|
2006-05-03 02:17:05 +04:00
|
|
|
const char *scan;
|
|
|
|
switch (arg[1]) {
|
|
|
|
case 'A': case 'B': case 'C':
|
|
|
|
if (!arg[2]) {
|
|
|
|
if (argc <= 1)
|
2006-07-04 13:44:48 +04:00
|
|
|
die(emsg_missing_context_len);
|
2006-05-03 02:17:05 +04:00
|
|
|
scan = *++argv;
|
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
scan = arg + 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
scan = arg + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sscanf(scan, "%u", &num) != 1)
|
2006-07-04 13:44:48 +04:00
|
|
|
die(emsg_invalid_context_len, scan);
|
2006-05-02 02:58:29 +04:00
|
|
|
switch (arg[1]) {
|
|
|
|
case 'A':
|
|
|
|
opt.post_context = num;
|
|
|
|
break;
|
2006-05-03 02:17:05 +04:00
|
|
|
default:
|
2006-05-02 02:58:29 +04:00
|
|
|
case 'C':
|
|
|
|
opt.post_context = num;
|
|
|
|
case 'B':
|
|
|
|
opt.pre_context = num;
|
|
|
|
break;
|
2006-05-01 10:28:15 +04:00
|
|
|
}
|
2006-05-02 02:58:29 +04:00
|
|
|
continue;
|
|
|
|
}
|
2006-05-09 00:28:27 +04:00
|
|
|
if (!strcmp("-f", arg)) {
|
|
|
|
FILE *patterns;
|
|
|
|
int lno = 0;
|
|
|
|
char buf[1024];
|
|
|
|
if (argc <= 1)
|
2006-07-04 13:44:48 +04:00
|
|
|
die(emsg_missing_argument, arg);
|
2006-05-09 00:28:27 +04:00
|
|
|
patterns = fopen(argv[1], "r");
|
|
|
|
if (!patterns)
|
2006-05-09 10:55:47 +04:00
|
|
|
die("'%s': %s", argv[1], strerror(errno));
|
2006-05-09 00:28:27 +04:00
|
|
|
while (fgets(buf, sizeof(buf), patterns)) {
|
|
|
|
int len = strlen(buf);
|
|
|
|
if (buf[len-1] == '\n')
|
|
|
|
buf[len-1] = 0;
|
|
|
|
/* ignore empty line like grep does */
|
|
|
|
if (!buf[0])
|
|
|
|
continue;
|
2006-09-18 03:02:52 +04:00
|
|
|
append_grep_pattern(&opt, xstrdup(buf),
|
|
|
|
argv[1], ++lno,
|
|
|
|
GREP_PATTERN);
|
2006-05-09 00:28:27 +04:00
|
|
|
}
|
|
|
|
fclose(patterns);
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
continue;
|
|
|
|
}
|
2006-06-30 14:04:05 +04:00
|
|
|
if (!strcmp("--not", arg)) {
|
2006-09-18 03:02:52 +04:00
|
|
|
append_grep_pattern(&opt, arg, "command line", 0,
|
|
|
|
GREP_NOT);
|
2006-06-30 14:04:05 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp("--and", arg)) {
|
2006-09-18 03:02:52 +04:00
|
|
|
append_grep_pattern(&opt, arg, "command line", 0,
|
|
|
|
GREP_AND);
|
2006-06-30 14:04:05 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp("--or", arg))
|
|
|
|
continue; /* no-op */
|
|
|
|
if (!strcmp("(", arg)) {
|
2006-09-18 03:02:52 +04:00
|
|
|
append_grep_pattern(&opt, arg, "command line", 0,
|
|
|
|
GREP_OPEN_PAREN);
|
2006-06-30 14:04:05 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(")", arg)) {
|
2006-09-18 03:02:52 +04:00
|
|
|
append_grep_pattern(&opt, arg, "command line", 0,
|
|
|
|
GREP_CLOSE_PAREN);
|
2006-06-30 14:04:05 +04:00
|
|
|
continue;
|
|
|
|
}
|
2006-09-28 04:50:52 +04:00
|
|
|
if (!strcmp("--all-match", arg)) {
|
|
|
|
opt.all_match = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-02 02:58:29 +04:00
|
|
|
if (!strcmp("-e", arg)) {
|
|
|
|
if (1 < argc) {
|
2006-09-18 03:02:52 +04:00
|
|
|
append_grep_pattern(&opt, argv[1],
|
|
|
|
"-e option", 0,
|
|
|
|
GREP_PATTERN);
|
2006-05-03 02:40:49 +04:00
|
|
|
argv++;
|
2006-05-02 02:58:29 +04:00
|
|
|
argc--;
|
2006-05-01 10:28:15 +04:00
|
|
|
continue;
|
|
|
|
}
|
2006-07-04 13:44:48 +04:00
|
|
|
die(emsg_missing_argument, arg);
|
2006-05-02 02:58:29 +04:00
|
|
|
}
|
2006-08-11 11:44:42 +04:00
|
|
|
if (!strcmp("--full-name", arg)) {
|
|
|
|
opt.relative = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2006-07-04 13:31:50 +04:00
|
|
|
if (!strcmp("--", arg)) {
|
|
|
|
/* later processing wants to have this at argv[1] */
|
|
|
|
argv--;
|
|
|
|
argc++;
|
2006-05-09 10:55:47 +04:00
|
|
|
break;
|
2006-07-04 13:31:50 +04:00
|
|
|
}
|
2006-05-09 10:55:47 +04:00
|
|
|
if (*arg == '-')
|
2006-05-02 02:58:29 +04:00
|
|
|
usage(builtin_grep_usage);
|
2006-05-09 10:55:47 +04:00
|
|
|
|
|
|
|
/* First unrecognized non-option token */
|
2006-05-03 02:40:49 +04:00
|
|
|
if (!opt.pattern_list) {
|
2006-09-18 03:02:52 +04:00
|
|
|
append_grep_pattern(&opt, arg, "command line", 0,
|
|
|
|
GREP_PATTERN);
|
2006-05-02 02:58:29 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* We are looking at the first path or rev;
|
2006-05-09 10:55:47 +04:00
|
|
|
* it is found at argv[1] after leaving the
|
2006-05-02 02:58:29 +04:00
|
|
|
* loop.
|
|
|
|
*/
|
|
|
|
argc++; argv--;
|
|
|
|
break;
|
2006-05-01 10:28:15 +04:00
|
|
|
}
|
|
|
|
}
|
2006-05-09 10:55:47 +04:00
|
|
|
|
2006-05-03 02:40:49 +04:00
|
|
|
if (!opt.pattern_list)
|
2006-05-01 10:28:15 +04:00
|
|
|
die("no pattern given.");
|
2006-05-10 05:28:41 +04:00
|
|
|
if ((opt.regflags != REG_NEWLINE) && opt.fixed)
|
|
|
|
die("cannot mix --fixed-strings and regexp");
|
2006-09-18 03:02:52 +04:00
|
|
|
compile_grep_patterns(&opt);
|
2006-05-09 10:55:47 +04:00
|
|
|
|
|
|
|
/* Check revs and then paths */
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
const char *arg = argv[i];
|
2006-05-02 02:58:29 +04:00
|
|
|
unsigned char sha1[20];
|
2006-05-09 10:55:47 +04:00
|
|
|
/* Is it a rev? */
|
|
|
|
if (!get_sha1(arg, sha1)) {
|
|
|
|
struct object *object = parse_object(sha1);
|
|
|
|
if (!object)
|
|
|
|
die("bad object %s", arg);
|
Add "named object array" concept
We've had this notion of a "object_list" for a long time, which eventually
grew a "name" member because some users (notably git-rev-list) wanted to
name each object as it is generated.
That object_list is great for some things, but it isn't all that wonderful
for others, and the "name" member is generally not used by everybody.
This patch splits the users of the object_list array up into two: the
traditional list users, who want the list-like format, and who don't
actually use or want the name. And another class of users that really used
the list as an extensible array, and generally wanted to name the objects.
The patch is fairly straightforward, but it's also biggish. Most of it
really just cleans things up: switching the revision parsing and listing
over to the array makes things like the builtin-diff usage much simpler
(we now see exactly how many members the array has, and we don't get the
objects reversed from the order they were on the command line).
One of the main reasons for doing this at all is that the malloc overhead
of the simple object list was actually pretty high, and the array is just
a lot denser. So this patch brings down memory usage by git-rev-list by
just under 3% (on top of all the other memory use optimizations) on the
mozilla archive.
It does add more lines than it removes, and more importantly, it adds a
whole new infrastructure for maintaining lists of objects, but on the
other hand, the new dynamic array code is pretty obvious. The change to
builtin-diff-tree.c shows a fairly good example of why an array interface
is sometimes more natural, and just much simpler for everybody.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-06-20 04:42:35 +04:00
|
|
|
add_object_array(object, arg, &list);
|
2006-05-09 10:55:47 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(arg, "--")) {
|
|
|
|
i++;
|
|
|
|
seen_dashdash = 1;
|
|
|
|
}
|
|
|
|
break;
|
2006-05-02 02:58:29 +04:00
|
|
|
}
|
2006-05-09 10:55:47 +04:00
|
|
|
|
|
|
|
/* The rest are paths */
|
|
|
|
if (!seen_dashdash) {
|
|
|
|
int j;
|
2006-05-10 05:15:21 +04:00
|
|
|
for (j = i; j < argc; j++)
|
2006-05-09 10:55:47 +04:00
|
|
|
verify_filename(prefix, argv[j]);
|
|
|
|
}
|
|
|
|
|
2006-08-11 11:44:42 +04:00
|
|
|
if (i < argc) {
|
2006-05-09 10:55:47 +04:00
|
|
|
paths = get_pathspec(prefix, argv + i);
|
2006-08-11 11:44:42 +04:00
|
|
|
if (opt.prefix_length && opt.relative) {
|
|
|
|
/* Make sure we do not get outside of paths */
|
|
|
|
for (i = 0; paths[i]; i++)
|
|
|
|
if (strncmp(prefix, paths[i], opt.prefix_length))
|
|
|
|
die("git-grep: cannot generate relative filenames containing '..'");
|
|
|
|
}
|
|
|
|
}
|
2006-05-02 02:58:29 +04:00
|
|
|
else if (prefix) {
|
|
|
|
paths = xcalloc(2, sizeof(const char *));
|
|
|
|
paths[0] = prefix;
|
|
|
|
paths[1] = NULL;
|
|
|
|
}
|
2006-05-01 10:28:15 +04:00
|
|
|
|
Add "named object array" concept
We've had this notion of a "object_list" for a long time, which eventually
grew a "name" member because some users (notably git-rev-list) wanted to
name each object as it is generated.
That object_list is great for some things, but it isn't all that wonderful
for others, and the "name" member is generally not used by everybody.
This patch splits the users of the object_list array up into two: the
traditional list users, who want the list-like format, and who don't
actually use or want the name. And another class of users that really used
the list as an extensible array, and generally wanted to name the objects.
The patch is fairly straightforward, but it's also biggish. Most of it
really just cleans things up: switching the revision parsing and listing
over to the array makes things like the builtin-diff usage much simpler
(we now see exactly how many members the array has, and we don't get the
objects reversed from the order they were on the command line).
One of the main reasons for doing this at all is that the malloc overhead
of the simple object list was actually pretty high, and the array is just
a lot denser. So this patch brings down memory usage by git-rev-list by
just under 3% (on top of all the other memory use optimizations) on the
mozilla archive.
It does add more lines than it removes, and more importantly, it adds a
whole new infrastructure for maintaining lists of objects, but on the
other hand, the new dynamic array code is pretty obvious. The change to
builtin-diff-tree.c shows a fairly good example of why an array interface
is sometimes more natural, and just much simpler for everybody.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-06-20 04:42:35 +04:00
|
|
|
if (!list.nr)
|
2006-05-02 02:58:29 +04:00
|
|
|
return !grep_cache(&opt, paths, cached);
|
2006-05-09 00:28:27 +04:00
|
|
|
|
2006-05-01 10:28:15 +04:00
|
|
|
if (cached)
|
2006-05-09 00:28:27 +04:00
|
|
|
die("both --cached and trees are given.");
|
2006-05-01 10:28:15 +04:00
|
|
|
|
Add "named object array" concept
We've had this notion of a "object_list" for a long time, which eventually
grew a "name" member because some users (notably git-rev-list) wanted to
name each object as it is generated.
That object_list is great for some things, but it isn't all that wonderful
for others, and the "name" member is generally not used by everybody.
This patch splits the users of the object_list array up into two: the
traditional list users, who want the list-like format, and who don't
actually use or want the name. And another class of users that really used
the list as an extensible array, and generally wanted to name the objects.
The patch is fairly straightforward, but it's also biggish. Most of it
really just cleans things up: switching the revision parsing and listing
over to the array makes things like the builtin-diff usage much simpler
(we now see exactly how many members the array has, and we don't get the
objects reversed from the order they were on the command line).
One of the main reasons for doing this at all is that the malloc overhead
of the simple object list was actually pretty high, and the array is just
a lot denser. So this patch brings down memory usage by git-rev-list by
just under 3% (on top of all the other memory use optimizations) on the
mozilla archive.
It does add more lines than it removes, and more importantly, it adds a
whole new infrastructure for maintaining lists of objects, but on the
other hand, the new dynamic array code is pretty obvious. The change to
builtin-diff-tree.c shows a fairly good example of why an array interface
is sometimes more natural, and just much simpler for everybody.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-06-20 04:42:35 +04:00
|
|
|
for (i = 0; i < list.nr; i++) {
|
2006-05-01 10:28:15 +04:00
|
|
|
struct object *real_obj;
|
Add "named object array" concept
We've had this notion of a "object_list" for a long time, which eventually
grew a "name" member because some users (notably git-rev-list) wanted to
name each object as it is generated.
That object_list is great for some things, but it isn't all that wonderful
for others, and the "name" member is generally not used by everybody.
This patch splits the users of the object_list array up into two: the
traditional list users, who want the list-like format, and who don't
actually use or want the name. And another class of users that really used
the list as an extensible array, and generally wanted to name the objects.
The patch is fairly straightforward, but it's also biggish. Most of it
really just cleans things up: switching the revision parsing and listing
over to the array makes things like the builtin-diff usage much simpler
(we now see exactly how many members the array has, and we don't get the
objects reversed from the order they were on the command line).
One of the main reasons for doing this at all is that the malloc overhead
of the simple object list was actually pretty high, and the array is just
a lot denser. So this patch brings down memory usage by git-rev-list by
just under 3% (on top of all the other memory use optimizations) on the
mozilla archive.
It does add more lines than it removes, and more importantly, it adds a
whole new infrastructure for maintaining lists of objects, but on the
other hand, the new dynamic array code is pretty obvious. The change to
builtin-diff-tree.c shows a fairly good example of why an array interface
is sometimes more natural, and just much simpler for everybody.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-06-20 04:42:35 +04:00
|
|
|
real_obj = deref_tag(list.objects[i].item, NULL, 0);
|
|
|
|
if (grep_object(&opt, paths, real_obj, list.objects[i].name))
|
2006-05-01 10:28:15 +04:00
|
|
|
hit = 1;
|
|
|
|
}
|
2006-09-28 03:27:10 +04:00
|
|
|
free_grep_patterns(&opt);
|
2006-05-01 10:28:15 +04:00
|
|
|
return !hit;
|
|
|
|
}
|