Browse Source

add context pointer to read_tree_recursive()

Add a pointer parameter to read_tree_recursive(), which is passed to the
callback function.  This allows callers of read_tree_recursive() to
share data with the callback without resorting to global variables.  All
current callers pass NULL.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
René Scharfe 17 years ago committed by Junio C Hamano
parent
commit
671f070721
  1. 11
      archive-tar.c
  2. 11
      archive-zip.c
  3. 4
      builtin-checkout.c
  4. 4
      builtin-log.c
  5. 4
      builtin-ls-tree.c
  6. 4
      builtin-merge-recursive.c
  7. 12
      tree.c
  8. 4
      tree.h

11
archive-tar.c

@ -234,9 +234,9 @@ static int git_tar_config(const char *var, const char *value, void *cb)
return git_default_config(var, value, cb); return git_default_config(var, value, cb);
} }


static int write_tar_entry(const unsigned char *sha1, static int write_tar_entry(const unsigned char *sha1, const char *base,
const char *base, int baselen, int baselen, const char *filename, unsigned mode, int stage,
const char *filename, unsigned mode, int stage) void *context)
{ {
static struct strbuf path = STRBUF_INIT; static struct strbuf path = STRBUF_INIT;
void *buffer; void *buffer;
@ -286,11 +286,12 @@ int write_tar_archive(struct archiver_args *args)


while (baselen > 0 && base[baselen - 1] == '/') while (baselen > 0 && base[baselen - 1] == '/')
base[--baselen] = '\0'; base[--baselen] = '\0';
write_tar_entry(args->tree->object.sha1, "", 0, base, 040777, 0); write_tar_entry(args->tree->object.sha1, "", 0, base, 040777,
0, NULL);
free(base); free(base);
} }
read_tree_recursive(args->tree, args->base, plen, 0, read_tree_recursive(args->tree, args->base, plen, 0,
args->pathspec, write_tar_entry); args->pathspec, write_tar_entry, NULL);
write_trailer(); write_trailer();


return 0; return 0;

11
archive-zip.c

@ -152,9 +152,9 @@ static char *construct_path(const char *base, int baselen,
return path; return path;
} }


static int write_zip_entry(const unsigned char *sha1, static int write_zip_entry(const unsigned char *sha1, const char *base,
const char *base, int baselen, int baselen, const char *filename, unsigned mode, int stage,
const char *filename, unsigned mode, int stage) void *context)
{ {
struct zip_local_header header; struct zip_local_header header;
struct zip_dir_header dirent; struct zip_dir_header dirent;
@ -332,11 +332,12 @@ int write_zip_archive(struct archiver_args *args)


while (baselen > 0 && base[baselen - 1] == '/') while (baselen > 0 && base[baselen - 1] == '/')
base[--baselen] = '\0'; base[--baselen] = '\0';
write_zip_entry(args->tree->object.sha1, "", 0, base, 040777, 0); write_zip_entry(args->tree->object.sha1, "", 0, base, 040777,
0, NULL);
free(base); free(base);
} }
read_tree_recursive(args->tree, args->base, plen, 0, read_tree_recursive(args->tree, args->base, plen, 0,
args->pathspec, write_zip_entry); args->pathspec, write_zip_entry, NULL);
write_zip_trailer(args->commit_sha1); write_zip_trailer(args->commit_sha1);


free(zip_dir); free(zip_dir);

4
builtin-checkout.c

@ -43,7 +43,7 @@ static int post_checkout_hook(struct commit *old, struct commit *new,
} }


static int update_some(const unsigned char *sha1, const char *base, int baselen, static int update_some(const unsigned char *sha1, const char *base, int baselen,
const char *pathname, unsigned mode, int stage) const char *pathname, unsigned mode, int stage, void *context)
{ {
int len; int len;
struct cache_entry *ce; struct cache_entry *ce;
@ -67,7 +67,7 @@ static int update_some(const unsigned char *sha1, const char *base, int baselen,


static int read_tree_some(struct tree *tree, const char **pathspec) static int read_tree_some(struct tree *tree, const char **pathspec)
{ {
read_tree_recursive(tree, "", 0, 0, pathspec, update_some); read_tree_recursive(tree, "", 0, 0, pathspec, update_some, NULL);


/* update the index with the given tree's info /* update the index with the given tree's info
* for all args, expanding wildcards, and exit * for all args, expanding wildcards, and exit

4
builtin-log.c

@ -313,7 +313,7 @@ static int show_object(const unsigned char *sha1, int show_tag_object,


static int show_tree_object(const unsigned char *sha1, static int show_tree_object(const unsigned char *sha1,
const char *base, int baselen, const char *base, int baselen,
const char *pathname, unsigned mode, int stage) const char *pathname, unsigned mode, int stage, void *context)
{ {
printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : ""); printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
return 0; return 0;
@ -366,7 +366,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)
name, name,
diff_get_color_opt(&rev.diffopt, DIFF_RESET)); diff_get_color_opt(&rev.diffopt, DIFF_RESET));
read_tree_recursive((struct tree *)o, "", 0, 0, NULL, read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
show_tree_object); show_tree_object, NULL);
break; break;
case OBJ_COMMIT: case OBJ_COMMIT:
rev.pending.nr = rev.pending.alloc = 0; rev.pending.nr = rev.pending.alloc = 0;

4
builtin-ls-tree.c

@ -56,7 +56,7 @@ static int show_recursive(const char *base, int baselen, const char *pathname)
} }


static int show_tree(const unsigned char *sha1, const char *base, int baselen, static int show_tree(const unsigned char *sha1, const char *base, int baselen,
const char *pathname, unsigned mode, int stage) const char *pathname, unsigned mode, int stage, void *context)
{ {
int retval = 0; int retval = 0;
const char *type = blob_type; const char *type = blob_type;
@ -189,7 +189,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
tree = parse_tree_indirect(sha1); tree = parse_tree_indirect(sha1);
if (!tree) if (!tree)
die("not a tree object"); die("not a tree object");
read_tree_recursive(tree, "", 0, 0, pathspec, show_tree); read_tree_recursive(tree, "", 0, 0, pathspec, show_tree, NULL);


return 0; return 0;
} }

4
builtin-merge-recursive.c

@ -256,7 +256,7 @@ struct tree *write_tree_from_memory(void)


static int save_files_dirs(const unsigned char *sha1, static int save_files_dirs(const unsigned char *sha1,
const char *base, int baselen, const char *path, const char *base, int baselen, const char *path,
unsigned int mode, int stage) unsigned int mode, int stage, void *context)
{ {
int len = strlen(path); int len = strlen(path);
char *newpath = xmalloc(baselen + len + 1); char *newpath = xmalloc(baselen + len + 1);
@ -276,7 +276,7 @@ static int save_files_dirs(const unsigned char *sha1,
static int get_files_dirs(struct tree *tree) static int get_files_dirs(struct tree *tree)
{ {
int n; int n;
if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs) != 0) if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, NULL))
return 0; return 0;
n = current_file_set.nr + current_directory_set.nr; n = current_file_set.nr + current_directory_set.nr;
return n; return n;

12
tree.c

@ -29,7 +29,7 @@ static int read_one_entry_opt(const unsigned char *sha1, const char *base, int b
return add_cache_entry(ce, opt); return add_cache_entry(ce, opt);
} }


static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage) static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
{ {
return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage, return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
@ -39,7 +39,7 @@ static int read_one_entry(const unsigned char *sha1, const char *base, int basel
* This is used when the caller knows there is no existing entries at * This is used when the caller knows there is no existing entries at
* the stage that will conflict with the entry being added. * the stage that will conflict with the entry being added.
*/ */
static int read_one_entry_quick(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage) static int read_one_entry_quick(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
{ {
return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage, return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
ADD_CACHE_JUST_APPEND); ADD_CACHE_JUST_APPEND);
@ -92,7 +92,7 @@ static int match_tree_entry(const char *base, int baselen, const char *path, uns
int read_tree_recursive(struct tree *tree, int read_tree_recursive(struct tree *tree,
const char *base, int baselen, const char *base, int baselen,
int stage, const char **match, int stage, const char **match,
read_tree_fn_t fn) read_tree_fn_t fn, void *context)
{ {
struct tree_desc desc; struct tree_desc desc;
struct name_entry entry; struct name_entry entry;
@ -106,7 +106,7 @@ int read_tree_recursive(struct tree *tree,
if (!match_tree_entry(base, baselen, entry.path, entry.mode, match)) if (!match_tree_entry(base, baselen, entry.path, entry.mode, match))
continue; continue;


switch (fn(entry.sha1, base, baselen, entry.path, entry.mode, stage)) { switch (fn(entry.sha1, base, baselen, entry.path, entry.mode, stage, context)) {
case 0: case 0:
continue; continue;
case READ_TREE_RECURSIVE: case READ_TREE_RECURSIVE:
@ -126,7 +126,7 @@ int read_tree_recursive(struct tree *tree,
retval = read_tree_recursive(lookup_tree(entry.sha1), retval = read_tree_recursive(lookup_tree(entry.sha1),
newbase, newbase,
baselen + pathlen + 1, baselen + pathlen + 1,
stage, match, fn); stage, match, fn, context);
free(newbase); free(newbase);
if (retval) if (retval)
return -1; return -1;
@ -174,7 +174,7 @@ int read_tree(struct tree *tree, int stage, const char **match)


if (!fn) if (!fn)
fn = read_one_entry_quick; fn = read_one_entry_quick;
err = read_tree_recursive(tree, "", 0, stage, match, fn); err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL);
if (fn == read_one_entry || err) if (fn == read_one_entry || err)
return err; return err;



4
tree.h

@ -21,12 +21,12 @@ int parse_tree(struct tree *tree);
struct tree *parse_tree_indirect(const unsigned char *sha1); struct tree *parse_tree_indirect(const unsigned char *sha1);


#define READ_TREE_RECURSIVE 1 #define READ_TREE_RECURSIVE 1
typedef int (*read_tree_fn_t)(const unsigned char *, const char *, int, const char *, unsigned int, int); typedef int (*read_tree_fn_t)(const unsigned char *, const char *, int, const char *, unsigned int, int, void *);


extern int read_tree_recursive(struct tree *tree, extern int read_tree_recursive(struct tree *tree,
const char *base, int baselen, const char *base, int baselen,
int stage, const char **match, int stage, const char **match,
read_tree_fn_t fn); read_tree_fn_t fn, void *context);


extern int read_tree(struct tree *tree, int stage, const char **paths); extern int read_tree(struct tree *tree, int stage, const char **paths);



Loading…
Cancel
Save