2006-05-01 08:28:15 +02: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 00:58:29 +02:00
|
|
|
#include "tree-walk.h"
|
2006-05-01 08:28:15 +02:00
|
|
|
#include "builtin.h"
|
2006-09-18 01:02:52 +02:00
|
|
|
#include "grep.h"
|
2006-05-01 08:28:15 +02:00
|
|
|
|
2006-05-01 21:27:56 +02:00
|
|
|
/*
|
|
|
|
* git grep pathspecs are somewhat different from diff-tree pathspecs;
|
|
|
|
* pathname wildcards are allowed.
|
|
|
|
*/
|
2006-05-02 00:58:29 +02:00
|
|
|
static int pathspec_matches(const char **paths, const char *name)
|
2006-05-01 08:28:15 +02:00
|
|
|
{
|
2006-05-01 21:27:56 +02:00
|
|
|
int namelen, i;
|
2006-05-02 00:58:29 +02:00
|
|
|
if (!paths || !*paths)
|
2006-05-01 08:28:15 +02:00
|
|
|
return 1;
|
|
|
|
namelen = strlen(name);
|
2006-05-02 00:58:29 +02:00
|
|
|
for (i = 0; paths[i]; i++) {
|
|
|
|
const char *match = paths[i];
|
|
|
|
int matchlen = strlen(match);
|
2006-05-03 02:27:07 +02:00
|
|
|
const char *cp, *meta;
|
2006-05-01 21:27:56 +02:00
|
|
|
|
2006-06-21 11:04:12 +02:00
|
|
|
if (!matchlen ||
|
|
|
|
((matchlen <= namelen) &&
|
|
|
|
!strncmp(name, match, matchlen) &&
|
|
|
|
(match[matchlen-1] == '/' ||
|
|
|
|
name[matchlen] == '\0' || name[matchlen] == '/')))
|
2006-05-01 21:27:56 +02:00
|
|
|
return 1;
|
|
|
|
if (!fnmatch(match, name, 0))
|
|
|
|
return 1;
|
|
|
|
if (name[namelen-1] != '/')
|
2006-05-01 08:28:15 +02:00
|
|
|
continue;
|
2006-05-01 21:27:56 +02:00
|
|
|
|
2006-05-03 02:27:07 +02:00
|
|
|
/* We are being asked if the directory ("name") is worth
|
2006-05-01 21:27:56 +02: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 08:28:15 +02:00
|
|
|
*/
|
2006-05-03 02:27:07 +02:00
|
|
|
for (cp = match, meta = NULL; cp - match < matchlen; cp++) {
|
2006-05-01 21:27:56 +02:00
|
|
|
char ch = *cp;
|
2006-05-03 02:27:07 +02:00
|
|
|
if (ch == '*' || ch == '[' || ch == '?') {
|
|
|
|
meta = cp;
|
2006-05-01 08:28:15 +02:00
|
|
|
break;
|
2006-05-03 02:27:07 +02:00
|
|
|
}
|
2006-05-01 21:27:56 +02:00
|
|
|
}
|
2006-05-03 02:27:07 +02:00
|
|
|
if (!meta)
|
|
|
|
meta = cp; /* fully literal */
|
|
|
|
|
|
|
|
if (namelen <= meta - match) {
|
2006-05-01 21:27:56 +02:00
|
|
|
/* Looking at "Documentation/" and
|
|
|
|
* the pattern says "Documentation/howto/", or
|
2006-05-03 02:27:07 +02:00
|
|
|
* "Documentation/diff*.txt". The name we
|
|
|
|
* have should match prefix.
|
2006-05-01 21:27:56 +02:00
|
|
|
*/
|
|
|
|
if (!memcmp(match, name, namelen))
|
|
|
|
return 1;
|
2006-05-03 02:27:07 +02:00
|
|
|
continue;
|
2006-05-01 21:27:56 +02:00
|
|
|
}
|
2006-05-03 02:27:07 +02:00
|
|
|
|
|
|
|
if (meta - match < namelen) {
|
2006-05-01 21:27:56 +02:00
|
|
|
/* Looking at "Documentation/howto/" and
|
2006-05-03 02:27:07 +02:00
|
|
|
* the pattern says "Documentation/h*";
|
|
|
|
* match up to "Do.../h"; this avoids descending
|
|
|
|
* into "Documentation/technical/".
|
2006-05-01 21:27:56 +02:00
|
|
|
*/
|
2006-05-03 02:27:07 +02:00
|
|
|
if (!memcmp(match, name, meta - match))
|
2006-05-01 21:27:56 +02:00
|
|
|
return 1;
|
2006-05-03 02:27:07 +02:00
|
|
|
continue;
|
2006-05-01 21:27:56 +02:00
|
|
|
}
|
2006-05-01 08:28:15 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-11 09:44:42 +02:00
|
|
|
static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len)
|
2006-05-01 08:28:15 +02:00
|
|
|
{
|
|
|
|
unsigned long size;
|
|
|
|
char *data;
|
2007-02-26 20:55:59 +01:00
|
|
|
enum object_type type;
|
2006-08-11 09:44:42 +02:00
|
|
|
char *to_free = NULL;
|
2006-05-01 08:28:15 +02:00
|
|
|
int hit;
|
2006-08-11 09:44:42 +02:00
|
|
|
|
2007-02-26 20:55:59 +01:00
|
|
|
data = read_sha1_file(sha1, &type, &size);
|
2006-05-01 08:28:15 +02:00
|
|
|
if (!data) {
|
|
|
|
error("'%s': unable to read %s", name, sha1_to_hex(sha1));
|
|
|
|
return 0;
|
|
|
|
}
|
2006-08-11 09:44:42 +02: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 08:28:15 +02:00
|
|
|
hit = grep_buffer(opt, name, data, size);
|
|
|
|
free(data);
|
2006-08-11 09:44:42 +02:00
|
|
|
free(to_free);
|
2006-05-01 08:28:15 +02:00
|
|
|
return hit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int grep_file(struct grep_opt *opt, const char *filename)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int i;
|
|
|
|
char *data;
|
2007-03-07 02:44:37 +01:00
|
|
|
size_t sz;
|
|
|
|
|
2006-05-01 08:28:15 +02:00
|
|
|
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;
|
2007-03-07 02:44:37 +01:00
|
|
|
sz = xsize_t(st.st_size);
|
2006-05-01 08:28:15 +02:00
|
|
|
i = open(filename, O_RDONLY);
|
|
|
|
if (i < 0)
|
|
|
|
goto err_ret;
|
2007-03-07 02:44:37 +01:00
|
|
|
data = xmalloc(sz + 1);
|
|
|
|
if (st.st_size != read_in_full(i, data, sz)) {
|
2006-05-01 08:28:15 +02:00
|
|
|
error("'%s': short read %s", filename, strerror(errno));
|
|
|
|
close(i);
|
|
|
|
free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
close(i);
|
2006-08-11 09:44:42 +02:00
|
|
|
if (opt->relative && opt->prefix_length)
|
|
|
|
filename += opt->prefix_length;
|
2007-03-07 02:44:37 +01:00
|
|
|
i = grep_buffer(opt, filename, data, sz);
|
2006-05-01 08:28:15 +02:00
|
|
|
free(data);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2007-03-07 02:44:14 +01:00
|
|
|
#ifdef __unix__
|
2006-05-15 05:49:15 +02: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-15 22:28:01 +02: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 05:49:15 +02:00
|
|
|
|
|
|
|
static int external_grep(struct grep_opt *opt, const char **paths, int cached)
|
|
|
|
{
|
2006-07-04 11:43:40 +02:00
|
|
|
int i, nr, argc, hit, len, status;
|
2006-05-15 05:49:15 +02:00
|
|
|
const char *argv[MAXARGS+1];
|
2006-05-15 22:28:01 +02:00
|
|
|
char randarg[ARGBUF];
|
|
|
|
char *argptr = randarg;
|
2006-05-15 05:49:15 +02:00
|
|
|
struct grep_pat *p;
|
|
|
|
|
2006-08-11 09:44:42 +02:00
|
|
|
if (opt->extended || (opt->relative && opt->prefix_length))
|
2006-06-30 12:04:05 +02:00
|
|
|
return -1;
|
2006-05-15 22:28:01 +02:00
|
|
|
len = nr = 0;
|
|
|
|
push_arg("grep");
|
|
|
|
if (opt->fixed)
|
2006-05-16 02:54:01 +02:00
|
|
|
push_arg("-F");
|
2006-05-15 22:28:01 +02:00
|
|
|
if (opt->linenum)
|
|
|
|
push_arg("-n");
|
2006-09-14 19:45:12 +02:00
|
|
|
if (!opt->pathname)
|
|
|
|
push_arg("-h");
|
2006-05-15 22:28:01 +02:00
|
|
|
if (opt->regflags & REG_EXTENDED)
|
|
|
|
push_arg("-E");
|
2006-06-07 01:15:16 +02:00
|
|
|
if (opt->regflags & REG_ICASE)
|
|
|
|
push_arg("-i");
|
2006-05-15 05:49:15 +02:00
|
|
|
if (opt->word_regexp)
|
2006-05-15 22:28:01 +02:00
|
|
|
push_arg("-w");
|
2006-05-15 05:49:15 +02:00
|
|
|
if (opt->name_only)
|
2006-05-15 22:28:01 +02: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 05:49:15 +02:00
|
|
|
for (p = opt->pattern_list; p; p = p->next) {
|
2006-05-15 22:28:01 +02:00
|
|
|
push_arg("-e");
|
|
|
|
push_arg(p->pattern);
|
2006-05-15 05:49:15 +02:00
|
|
|
}
|
2006-05-17 20:12:22 +02: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 05:49:15 +02:00
|
|
|
|
|
|
|
hit = 0;
|
|
|
|
argc = nr;
|
|
|
|
for (i = 0; i < active_nr; i++) {
|
|
|
|
struct cache_entry *ce = active_cache[i];
|
2006-05-21 22:45:46 +02:00
|
|
|
char *name;
|
2006-11-26 21:47:52 +01:00
|
|
|
if (!S_ISREG(ntohl(ce->ce_mode)))
|
2006-05-15 05:49:15 +02:00
|
|
|
continue;
|
|
|
|
if (!pathspec_matches(paths, ce->name))
|
|
|
|
continue;
|
2006-05-17 20:12:22 +02: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 21:47:52 +01:00
|
|
|
if (argc < MAXARGS && !ce_stage(ce))
|
2006-05-15 05:49:15 +02:00
|
|
|
continue;
|
2006-07-04 11:43:40 +02:00
|
|
|
status = exec_grep(argc, argv);
|
|
|
|
if (0 < status)
|
|
|
|
hit = 1;
|
2006-05-15 05:49:15 +02:00
|
|
|
argc = nr;
|
2006-11-26 21:47:52 +01: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 05:49:15 +02:00
|
|
|
}
|
2006-07-04 11:43:40 +02:00
|
|
|
if (argc > nr) {
|
|
|
|
status = exec_grep(argc, argv);
|
|
|
|
if (0 < status)
|
|
|
|
hit = 1;
|
|
|
|
}
|
|
|
|
return hit;
|
2006-05-15 05:49:15 +02:00
|
|
|
}
|
2007-03-07 02:44:14 +01:00
|
|
|
#endif
|
2006-05-15 05:49:15 +02:00
|
|
|
|
2006-05-02 00:58:29 +02:00
|
|
|
static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
|
2006-05-01 08:28:15 +02:00
|
|
|
{
|
|
|
|
int hit = 0;
|
|
|
|
int nr;
|
|
|
|
read_cache();
|
|
|
|
|
2006-05-15 05:49:15 +02: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 08:28:15 +02:00
|
|
|
for (nr = 0; nr < active_nr; nr++) {
|
|
|
|
struct cache_entry *ce = active_cache[nr];
|
2006-11-26 21:47:52 +01:00
|
|
|
if (!S_ISREG(ntohl(ce->ce_mode)))
|
2006-05-01 08:28:15 +02:00
|
|
|
continue;
|
2006-05-02 00:58:29 +02:00
|
|
|
if (!pathspec_matches(paths, ce->name))
|
2006-05-01 08:28:15 +02:00
|
|
|
continue;
|
2006-11-26 21:47:52 +01:00
|
|
|
if (cached) {
|
|
|
|
if (ce_stage(ce))
|
|
|
|
continue;
|
2006-08-11 09:44:42 +02:00
|
|
|
hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
|
2006-11-26 21:47:52 +01:00
|
|
|
}
|
2006-05-01 08:28:15 +02:00
|
|
|
else
|
|
|
|
hit |= grep_file(opt, ce->name);
|
2006-11-26 21:47:52 +01: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 08:28:15 +02:00
|
|
|
}
|
2006-09-28 01:27:10 +02:00
|
|
|
free_grep_patterns(opt);
|
2006-05-01 08:28:15 +02:00
|
|
|
return hit;
|
|
|
|
}
|
|
|
|
|
2006-05-02 00:58:29 +02:00
|
|
|
static int grep_tree(struct grep_opt *opt, const char **paths,
|
2006-05-01 08:28:15 +02: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 18:45:45 +02:00
|
|
|
struct name_entry entry;
|
2006-05-01 21:27:56 +02:00
|
|
|
char *down;
|
2006-08-11 09:44:42 +02:00
|
|
|
int tn_len = strlen(tree_name);
|
|
|
|
char *path_buf = xmalloc(PATH_MAX + tn_len + 100);
|
2006-05-01 08:28:15 +02:00
|
|
|
|
2006-08-11 09:44:42 +02:00
|
|
|
if (tn_len) {
|
|
|
|
tn_len = sprintf(path_buf, "%s:", tree_name);
|
|
|
|
down = path_buf + tn_len;
|
2006-05-01 21:27:56 +02:00
|
|
|
strcat(down, base);
|
2006-05-01 08:28:15 +02:00
|
|
|
}
|
|
|
|
else {
|
2006-05-01 21:27:56 +02:00
|
|
|
down = path_buf;
|
|
|
|
strcpy(down, base);
|
2006-05-01 08:28:15 +02: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 18:45:45 +02:00
|
|
|
while (tree_entry(tree, &entry)) {
|
|
|
|
strcpy(path_buf + len, entry.path);
|
2006-05-01 08:28:15 +02: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 18:45:45 +02:00
|
|
|
if (S_ISDIR(entry.mode))
|
2006-05-01 21:27:56 +02:00
|
|
|
/* Match "abc/" against pathspec to
|
|
|
|
* decide if we want to descend into "abc"
|
|
|
|
* directory.
|
|
|
|
*/
|
2007-03-21 18:07:46 +01:00
|
|
|
strcpy(path_buf + len + tree_entry_len(entry.path, entry.sha1), "/");
|
2006-05-01 21:27:56 +02:00
|
|
|
|
2006-05-02 00:58:29 +02:00
|
|
|
if (!pathspec_matches(paths, down))
|
2006-05-01 08:28:15 +02: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 18:45:45 +02:00
|
|
|
else if (S_ISREG(entry.mode))
|
2006-08-11 09:44:42 +02: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 18:45:45 +02:00
|
|
|
else if (S_ISDIR(entry.mode)) {
|
2007-02-26 20:55:59 +01:00
|
|
|
enum object_type type;
|
2006-05-01 08:28:15 +02:00
|
|
|
struct tree_desc sub;
|
|
|
|
void *data;
|
2007-03-21 18:08:25 +01:00
|
|
|
unsigned long size;
|
|
|
|
|
|
|
|
data = read_sha1_file(entry.sha1, &type, &size);
|
2006-05-01 08:28:15 +02: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 18:45:45 +02:00
|
|
|
sha1_to_hex(entry.sha1));
|
2007-03-21 18:08:25 +01:00
|
|
|
init_tree_desc(&sub, data, size);
|
2006-05-02 00:58:29 +02:00
|
|
|
hit |= grep_tree(opt, paths, &sub, tree_name, down);
|
2006-05-01 08:28:15 +02:00
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hit;
|
|
|
|
}
|
|
|
|
|
2006-05-02 00:58:29 +02:00
|
|
|
static int grep_object(struct grep_opt *opt, const char **paths,
|
2006-05-01 08:28:15 +02:00
|
|
|
struct object *obj, const char *name)
|
|
|
|
{
|
2006-07-12 05:45:31 +02:00
|
|
|
if (obj->type == OBJ_BLOB)
|
2006-08-11 09:44:42 +02:00
|
|
|
return grep_sha1(opt, obj->sha1, name, 0);
|
2006-07-12 05:45:31 +02:00
|
|
|
if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
|
2006-05-01 08:28:15 +02:00
|
|
|
struct tree_desc tree;
|
|
|
|
void *data;
|
2007-03-21 18:08:25 +01:00
|
|
|
unsigned long size;
|
2006-05-01 08:28:15 +02:00
|
|
|
int hit;
|
|
|
|
data = read_object_with_reference(obj->sha1, tree_type,
|
2007-03-21 18:08:25 +01:00
|
|
|
&size, NULL);
|
2006-05-01 08:28:15 +02:00
|
|
|
if (!data)
|
|
|
|
die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
|
2007-03-21 18:08:25 +01:00
|
|
|
init_tree_desc(&tree, data, size);
|
2006-05-02 00:58:29 +02:00
|
|
|
hit = grep_tree(opt, paths, &tree, name, "");
|
2006-05-01 08:28:15 +02: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 01:45:13 +02:00
|
|
|
die("unable to grep from object of type %s", typename(obj->type));
|
2006-05-01 08:28:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char builtin_grep_usage[] =
|
|
|
|
"git-grep <option>* <rev>* [-e] <pattern> [<path>...]";
|
|
|
|
|
2006-07-04 11:44:48 +02: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";
|
|
|
|
|
git-grep: don't use sscanf
If you use scanf or sscanf to parse integers, your code probably
accepts bogus inputs. For example, builtin-grep (aka git-grep) uses
sscanf(scan, "%u", &num) to parse the integer argument to -A, -B, -C.
Currently, "-C 1,000" and "-C 4294967297" are both treated just like
"-C 1":
$ git-grep -h -C 4294967297 juggle
out and you may find it easier to switch back and forth if you
juggle multiple lines of development simultaneously. Of
course, you will pay the price of more disk usage to hold
The obvious fix is to use strtoul instead. But using a bare strtoul is
too messy, at least when done properly, so I've added a wrapper function.
The new function in the patch below belongs elsewhere if it would be
useful in replacing any of the four remaining uses of sscanf.
One final note: With this change, I get a slightly different
diagnostic depending on the context size:
$ ./git-grep -h -C 4294967296 juggle
fatal: 4294967296: invalid context length argument
[Exit 128]
$ ./git-grep -h -C 4294967295 juggle
grep: 4294967295: invalid context length argument
[Exit 1]
A common convention that makes it easy to identify the source
of a diagnostic is to include the program name before the first ":".
Whether that should be "git" or "git-grep" is another question.
Using "grep" or "fatal" is misleading.
Signed-off-by: Jim Meyering <jim@meyering.net>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-03-12 13:11:29 +01:00
|
|
|
static int strtoul_ui(char const *s, unsigned int *result)
|
|
|
|
{
|
|
|
|
unsigned long ul;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
ul = strtoul(s, &p, 10);
|
|
|
|
if (errno || *p || p == s || (unsigned int) ul != ul)
|
|
|
|
return -1;
|
|
|
|
*result = ul;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-29 07:44:25 +02:00
|
|
|
int cmd_grep(int argc, const char **argv, const char *prefix)
|
2006-05-01 08:28:15 +02:00
|
|
|
{
|
|
|
|
int hit = 0;
|
|
|
|
int cached = 0;
|
2006-05-09 08:55:47 +02:00
|
|
|
int seen_dashdash = 0;
|
2006-05-01 08:28:15 +02: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 02:42:35 +02:00
|
|
|
struct object_array list = { 0, 0, NULL };
|
2006-05-02 00:58:29 +02:00
|
|
|
const char **paths = NULL;
|
2006-05-09 08:55:47 +02:00
|
|
|
int i;
|
2006-05-01 08:28:15 +02:00
|
|
|
|
|
|
|
memset(&opt, 0, sizeof(opt));
|
2006-08-11 09:44:42 +02:00
|
|
|
opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
|
|
|
|
opt.relative = 1;
|
2006-09-14 19:45:12 +02:00
|
|
|
opt.pathname = 1;
|
2006-05-03 00:40:49 +02:00
|
|
|
opt.pattern_tail = &opt.pattern_list;
|
2006-05-01 08:28:15 +02:00
|
|
|
opt.regflags = REG_NEWLINE;
|
|
|
|
|
|
|
|
/*
|
2006-05-09 08:55:47 +02: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 07:50:18 +02:00
|
|
|
* unrecognized non option is the beginning of the refs list
|
2006-05-09 08:55:47 +02:00
|
|
|
* that continues up to the -- (if exists), and then paths.
|
2006-05-01 08:28:15 +02:00
|
|
|
*/
|
2006-05-09 08:55:47 +02:00
|
|
|
|
2006-05-02 00:58:29 +02:00
|
|
|
while (1 < argc) {
|
|
|
|
const char *arg = argv[1];
|
|
|
|
argc--; argv++;
|
|
|
|
if (!strcmp("--cached", arg)) {
|
|
|
|
cached = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-04 06:05:29 +02:00
|
|
|
if (!strcmp("-a", arg) ||
|
|
|
|
!strcmp("--text", arg)) {
|
|
|
|
opt.binary = GREP_BINARY_TEXT;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-02 00:58:29 +02:00
|
|
|
if (!strcmp("-i", arg) ||
|
|
|
|
!strcmp("--ignore-case", arg)) {
|
|
|
|
opt.regflags |= REG_ICASE;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-04 06:05:29 +02:00
|
|
|
if (!strcmp("-I", arg)) {
|
|
|
|
opt.binary = GREP_BINARY_NOMATCH;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-02 00:58:29 +02: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 03:28:41 +02:00
|
|
|
if (!strcmp("-F", arg) ||
|
|
|
|
!strcmp("--fixed-strings", arg)) {
|
|
|
|
opt.fixed = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-02 00:58:29 +02: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 19:45:12 +02:00
|
|
|
if (!strcmp("-h", arg)) {
|
|
|
|
opt.pathname = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-02 00:58:29 +02:00
|
|
|
if (!strcmp("-H", arg)) {
|
2006-09-14 19:45:12 +02:00
|
|
|
opt.pathname = 1;
|
2006-05-02 00:58:29 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp("-l", arg) ||
|
|
|
|
!strcmp("--files-with-matches", arg)) {
|
|
|
|
opt.name_only = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-04 06:46:29 +02:00
|
|
|
if (!strcmp("-L", arg) ||
|
|
|
|
!strcmp("--files-without-match", arg)) {
|
|
|
|
opt.unmatch_name_only = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-03 00:40:49 +02:00
|
|
|
if (!strcmp("-c", arg) ||
|
|
|
|
!strcmp("--count", arg)) {
|
|
|
|
opt.count = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-03 00:40:49 +02:00
|
|
|
if (!strcmp("-w", arg) ||
|
|
|
|
!strcmp("--word-regexp", arg)) {
|
|
|
|
opt.word_regexp = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2007-02-20 10:54:00 +01:00
|
|
|
if (!prefixcmp(arg, "-A") ||
|
|
|
|
!prefixcmp(arg, "-B") ||
|
|
|
|
!prefixcmp(arg, "-C") ||
|
2006-05-03 00:17:05 +02:00
|
|
|
(arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {
|
2006-05-02 00:58:29 +02:00
|
|
|
unsigned num;
|
2006-05-03 00:17:05 +02:00
|
|
|
const char *scan;
|
|
|
|
switch (arg[1]) {
|
|
|
|
case 'A': case 'B': case 'C':
|
|
|
|
if (!arg[2]) {
|
|
|
|
if (argc <= 1)
|
2006-07-04 11:44:48 +02:00
|
|
|
die(emsg_missing_context_len);
|
2006-05-03 00:17:05 +02:00
|
|
|
scan = *++argv;
|
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
scan = arg + 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
scan = arg + 1;
|
|
|
|
break;
|
|
|
|
}
|
git-grep: don't use sscanf
If you use scanf or sscanf to parse integers, your code probably
accepts bogus inputs. For example, builtin-grep (aka git-grep) uses
sscanf(scan, "%u", &num) to parse the integer argument to -A, -B, -C.
Currently, "-C 1,000" and "-C 4294967297" are both treated just like
"-C 1":
$ git-grep -h -C 4294967297 juggle
out and you may find it easier to switch back and forth if you
juggle multiple lines of development simultaneously. Of
course, you will pay the price of more disk usage to hold
The obvious fix is to use strtoul instead. But using a bare strtoul is
too messy, at least when done properly, so I've added a wrapper function.
The new function in the patch below belongs elsewhere if it would be
useful in replacing any of the four remaining uses of sscanf.
One final note: With this change, I get a slightly different
diagnostic depending on the context size:
$ ./git-grep -h -C 4294967296 juggle
fatal: 4294967296: invalid context length argument
[Exit 128]
$ ./git-grep -h -C 4294967295 juggle
grep: 4294967295: invalid context length argument
[Exit 1]
A common convention that makes it easy to identify the source
of a diagnostic is to include the program name before the first ":".
Whether that should be "git" or "git-grep" is another question.
Using "grep" or "fatal" is misleading.
Signed-off-by: Jim Meyering <jim@meyering.net>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-03-12 13:11:29 +01:00
|
|
|
if (strtoul_ui(scan, &num))
|
2006-07-04 11:44:48 +02:00
|
|
|
die(emsg_invalid_context_len, scan);
|
2006-05-02 00:58:29 +02:00
|
|
|
switch (arg[1]) {
|
|
|
|
case 'A':
|
|
|
|
opt.post_context = num;
|
|
|
|
break;
|
2006-05-03 00:17:05 +02:00
|
|
|
default:
|
2006-05-02 00:58:29 +02:00
|
|
|
case 'C':
|
|
|
|
opt.post_context = num;
|
|
|
|
case 'B':
|
|
|
|
opt.pre_context = num;
|
|
|
|
break;
|
2006-05-01 08:28:15 +02:00
|
|
|
}
|
2006-05-02 00:58:29 +02:00
|
|
|
continue;
|
|
|
|
}
|
2006-05-08 22:28:27 +02:00
|
|
|
if (!strcmp("-f", arg)) {
|
|
|
|
FILE *patterns;
|
|
|
|
int lno = 0;
|
|
|
|
char buf[1024];
|
|
|
|
if (argc <= 1)
|
2006-07-04 11:44:48 +02:00
|
|
|
die(emsg_missing_argument, arg);
|
2006-05-08 22:28:27 +02:00
|
|
|
patterns = fopen(argv[1], "r");
|
|
|
|
if (!patterns)
|
2006-05-09 08:55:47 +02:00
|
|
|
die("'%s': %s", argv[1], strerror(errno));
|
2006-05-08 22:28:27 +02: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 01:02:52 +02:00
|
|
|
append_grep_pattern(&opt, xstrdup(buf),
|
|
|
|
argv[1], ++lno,
|
|
|
|
GREP_PATTERN);
|
2006-05-08 22:28:27 +02:00
|
|
|
}
|
|
|
|
fclose(patterns);
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
continue;
|
|
|
|
}
|
2006-06-30 12:04:05 +02:00
|
|
|
if (!strcmp("--not", arg)) {
|
2006-09-18 01:02:52 +02:00
|
|
|
append_grep_pattern(&opt, arg, "command line", 0,
|
|
|
|
GREP_NOT);
|
2006-06-30 12:04:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp("--and", arg)) {
|
2006-09-18 01:02:52 +02:00
|
|
|
append_grep_pattern(&opt, arg, "command line", 0,
|
|
|
|
GREP_AND);
|
2006-06-30 12:04:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp("--or", arg))
|
|
|
|
continue; /* no-op */
|
|
|
|
if (!strcmp("(", arg)) {
|
2006-09-18 01:02:52 +02:00
|
|
|
append_grep_pattern(&opt, arg, "command line", 0,
|
|
|
|
GREP_OPEN_PAREN);
|
2006-06-30 12:04:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(")", arg)) {
|
2006-09-18 01:02:52 +02:00
|
|
|
append_grep_pattern(&opt, arg, "command line", 0,
|
|
|
|
GREP_CLOSE_PAREN);
|
2006-06-30 12:04:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
2006-09-28 02:50:52 +02:00
|
|
|
if (!strcmp("--all-match", arg)) {
|
|
|
|
opt.all_match = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-02 00:58:29 +02:00
|
|
|
if (!strcmp("-e", arg)) {
|
|
|
|
if (1 < argc) {
|
2006-09-18 01:02:52 +02:00
|
|
|
append_grep_pattern(&opt, argv[1],
|
|
|
|
"-e option", 0,
|
|
|
|
GREP_PATTERN);
|
2006-05-03 00:40:49 +02:00
|
|
|
argv++;
|
2006-05-02 00:58:29 +02:00
|
|
|
argc--;
|
2006-05-01 08:28:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
2006-07-04 11:44:48 +02:00
|
|
|
die(emsg_missing_argument, arg);
|
2006-05-02 00:58:29 +02:00
|
|
|
}
|
2006-08-11 09:44:42 +02:00
|
|
|
if (!strcmp("--full-name", arg)) {
|
|
|
|
opt.relative = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2006-07-04 11:31:50 +02:00
|
|
|
if (!strcmp("--", arg)) {
|
|
|
|
/* later processing wants to have this at argv[1] */
|
|
|
|
argv--;
|
|
|
|
argc++;
|
2006-05-09 08:55:47 +02:00
|
|
|
break;
|
2006-07-04 11:31:50 +02:00
|
|
|
}
|
2006-05-09 08:55:47 +02:00
|
|
|
if (*arg == '-')
|
2006-05-02 00:58:29 +02:00
|
|
|
usage(builtin_grep_usage);
|
2006-05-09 08:55:47 +02:00
|
|
|
|
|
|
|
/* First unrecognized non-option token */
|
2006-05-03 00:40:49 +02:00
|
|
|
if (!opt.pattern_list) {
|
2006-09-18 01:02:52 +02:00
|
|
|
append_grep_pattern(&opt, arg, "command line", 0,
|
|
|
|
GREP_PATTERN);
|
2006-05-02 00:58:29 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* We are looking at the first path or rev;
|
2006-05-09 08:55:47 +02:00
|
|
|
* it is found at argv[1] after leaving the
|
2006-05-02 00:58:29 +02:00
|
|
|
* loop.
|
|
|
|
*/
|
|
|
|
argc++; argv--;
|
|
|
|
break;
|
2006-05-01 08:28:15 +02:00
|
|
|
}
|
|
|
|
}
|
2006-05-09 08:55:47 +02:00
|
|
|
|
2006-05-03 00:40:49 +02:00
|
|
|
if (!opt.pattern_list)
|
2006-05-01 08:28:15 +02:00
|
|
|
die("no pattern given.");
|
2006-05-10 03:28:41 +02:00
|
|
|
if ((opt.regflags != REG_NEWLINE) && opt.fixed)
|
|
|
|
die("cannot mix --fixed-strings and regexp");
|
2006-09-18 01:02:52 +02:00
|
|
|
compile_grep_patterns(&opt);
|
2006-05-09 08:55:47 +02:00
|
|
|
|
|
|
|
/* Check revs and then paths */
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
const char *arg = argv[i];
|
2006-05-02 00:58:29 +02:00
|
|
|
unsigned char sha1[20];
|
2006-05-09 08:55:47 +02: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 02:42:35 +02:00
|
|
|
add_object_array(object, arg, &list);
|
2006-05-09 08:55:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(arg, "--")) {
|
|
|
|
i++;
|
|
|
|
seen_dashdash = 1;
|
|
|
|
}
|
|
|
|
break;
|
2006-05-02 00:58:29 +02:00
|
|
|
}
|
2006-05-09 08:55:47 +02:00
|
|
|
|
|
|
|
/* The rest are paths */
|
|
|
|
if (!seen_dashdash) {
|
|
|
|
int j;
|
2006-05-10 03:15:21 +02:00
|
|
|
for (j = i; j < argc; j++)
|
2006-05-09 08:55:47 +02:00
|
|
|
verify_filename(prefix, argv[j]);
|
|
|
|
}
|
|
|
|
|
2006-08-11 09:44:42 +02:00
|
|
|
if (i < argc) {
|
2006-05-09 08:55:47 +02:00
|
|
|
paths = get_pathspec(prefix, argv + i);
|
2006-08-11 09:44:42 +02: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 00:58:29 +02:00
|
|
|
else if (prefix) {
|
|
|
|
paths = xcalloc(2, sizeof(const char *));
|
|
|
|
paths[0] = prefix;
|
|
|
|
paths[1] = NULL;
|
|
|
|
}
|
2006-05-01 08:28:15 +02: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 02:42:35 +02:00
|
|
|
if (!list.nr)
|
2006-05-02 00:58:29 +02:00
|
|
|
return !grep_cache(&opt, paths, cached);
|
2006-05-08 22:28:27 +02:00
|
|
|
|
2006-05-01 08:28:15 +02:00
|
|
|
if (cached)
|
2006-05-08 22:28:27 +02:00
|
|
|
die("both --cached and trees are given.");
|
2006-05-01 08:28:15 +02: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 02:42:35 +02:00
|
|
|
for (i = 0; i < list.nr; i++) {
|
2006-05-01 08:28:15 +02: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 02:42:35 +02: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 08:28:15 +02:00
|
|
|
hit = 1;
|
|
|
|
}
|
2006-09-28 01:27:10 +02:00
|
|
|
free_grep_patterns(&opt);
|
2006-05-01 08:28:15 +02:00
|
|
|
return !hit;
|
|
|
|
}
|