for_each_hash: allow passing a 'void *data' pointer to callback

For the find_exact_renames() function, this allows us to pass the
diff_options structure pointer to the low-level routines.  We will use
that to distinguish between the "rename" and "copy" cases.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Linus Torvalds 2011-02-18 19:55:19 -08:00 committed by Junio C Hamano
parent 7ed863a85a
commit 11f944dd6b
4 changed files with 13 additions and 11 deletions

View File

@ -63,7 +63,7 @@ static inline struct commit_name *find_commit_name(const unsigned char *peeled)
return n; return n;
} }
static int set_util(void *chain) static int set_util(void *chain, void *data)
{ {
struct commit_name *n; struct commit_name *n;
for (n = chain; n; n = n->next) { for (n = chain; n; n = n->next) {
@ -289,7 +289,7 @@ static void describe(const char *arg, int last_one)
fprintf(stderr, "searching to describe %s\n", arg); fprintf(stderr, "searching to describe %s\n", arg);
if (!have_util) { if (!have_util) {
for_each_hash(&names, set_util); for_each_hash(&names, set_util, NULL);
have_util = 1; have_util = 1;
} }

View File

@ -247,7 +247,8 @@ struct file_similarity {
}; };
static int find_identical_files(struct file_similarity *src, static int find_identical_files(struct file_similarity *src,
struct file_similarity *dst) struct file_similarity *dst,
struct diff_options *options)
{ {
int renames = 0; int renames = 0;
@ -306,11 +307,12 @@ static void free_similarity_list(struct file_similarity *p)
} }
} }
static int find_same_files(void *ptr) static int find_same_files(void *ptr, void *data)
{ {
int ret; int ret;
struct file_similarity *p = ptr; struct file_similarity *p = ptr;
struct file_similarity *src = NULL, *dst = NULL; struct file_similarity *src = NULL, *dst = NULL;
struct diff_options *options = data;
/* Split the hash list up into sources and destinations */ /* Split the hash list up into sources and destinations */
do { do {
@ -329,7 +331,7 @@ static int find_same_files(void *ptr)
* If we have both sources *and* destinations, see if * If we have both sources *and* destinations, see if
* we can match them up * we can match them up
*/ */
ret = (src && dst) ? find_identical_files(src, dst) : 0; ret = (src && dst) ? find_identical_files(src, dst, options) : 0;
/* Free the hashes and return the number of renames found */ /* Free the hashes and return the number of renames found */
free_similarity_list(src); free_similarity_list(src);
@ -377,7 +379,7 @@ static void insert_file_table(struct hash_table *table, int src_dst, int index,
* and then during the second round we try to match * and then during the second round we try to match
* cache-dirty entries as well. * cache-dirty entries as well.
*/ */
static int find_exact_renames(void) static int find_exact_renames(struct diff_options *options)
{ {
int i; int i;
struct hash_table file_table; struct hash_table file_table;
@ -390,7 +392,7 @@ static int find_exact_renames(void)
insert_file_table(&file_table, 1, i, rename_dst[i].two); insert_file_table(&file_table, 1, i, rename_dst[i].two);
/* Find the renames */ /* Find the renames */
i = for_each_hash(&file_table, find_same_files); i = for_each_hash(&file_table, find_same_files, options);
/* .. and free the hash data structure */ /* .. and free the hash data structure */
free_hash(&file_table); free_hash(&file_table);
@ -467,7 +469,7 @@ void diffcore_rename(struct diff_options *options)
* We really want to cull the candidates list early * We really want to cull the candidates list early
* with cheap tests in order to avoid doing deltas. * with cheap tests in order to avoid doing deltas.
*/ */
rename_count = find_exact_renames(); rename_count = find_exact_renames(options);
/* Did we only want exact renames? */ /* Did we only want exact renames? */
if (minimum_score == MAX_SCORE) if (minimum_score == MAX_SCORE)

4
hash.c
View File

@ -81,7 +81,7 @@ void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table)
return insert_hash_entry(hash, ptr, table); return insert_hash_entry(hash, ptr, table);
} }
int for_each_hash(const struct hash_table *table, int (*fn)(void *)) int for_each_hash(const struct hash_table *table, int (*fn)(void *, void *), void *data)
{ {
int sum = 0; int sum = 0;
unsigned int i; unsigned int i;
@ -92,7 +92,7 @@ int for_each_hash(const struct hash_table *table, int (*fn)(void *))
void *ptr = array->ptr; void *ptr = array->ptr;
array++; array++;
if (ptr) { if (ptr) {
int val = fn(ptr); int val = fn(ptr, data);
if (val < 0) if (val < 0)
return val; return val;
sum += val; sum += val;

2
hash.h
View File

@ -30,7 +30,7 @@ struct hash_table {
extern void *lookup_hash(unsigned int hash, const struct hash_table *table); extern void *lookup_hash(unsigned int hash, const struct hash_table *table);
extern void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table); extern void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table);
extern int for_each_hash(const struct hash_table *table, int (*fn)(void *)); extern int for_each_hash(const struct hash_table *table, int (*fn)(void *, void *), void *data);
extern void free_hash(struct hash_table *table); extern void free_hash(struct hash_table *table);
static inline void init_hash(struct hash_table *table) static inline void init_hash(struct hash_table *table)