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>
Linus Torvalds committed
Feb 18, 2011 at 19:55 UTC
11f944dd6bdabd003325c85dc60b16389d012361
4 files changed
+13
-11
builtin/describe.c
+2
-2
@@ -63,7 +63,7 @@ static inline struct commit_name *find_commit_name(const unsigned char *peeled)
63
return n;
64
}
65
66
-static int set_util(void *chain)
66
+static int set_util(void *chain, void *data)
67
{
68
struct commit_name *n;
69
for (n = chain; n; n = n->next) {
@@ -289,7 +289,7 @@ static void describe(const char *arg, int last_one)
289
fprintf(stderr, "searching to describe %s\n", arg);
290
291
if (!have_util) {
292
- for_each_hash(&names, set_util);
292
+ for_each_hash(&names, set_util, NULL);
293
have_util = 1;
294
}
295
diffcore-rename.c
+8
-6
@@ -247,7 +247,8 @@ struct file_similarity {
247
};
248
249
static int find_identical_files(struct file_similarity *src,
250
- struct file_similarity *dst)
250
+ struct file_similarity *dst,
251
+ struct diff_options *options)
252
{
253
int renames = 0;
254
@@ -306,11 +307,12 @@ static void free_similarity_list(struct file_similarity *p)
307
}
308
}
309
309
-static int find_same_files(void *ptr)
310
+static int find_same_files(void *ptr, void *data)
311
{
312
int ret;
313
struct file_similarity *p = ptr;
314
struct file_similarity *src = NULL, *dst = NULL;
315
+ struct diff_options *options = data;
316
317
/* Split the hash list up into sources and destinations */
318
do {
@@ -329,7 +331,7 @@ static int find_same_files(void *ptr)
331
* If we have both sources *and* destinations, see if
332
* we can match them up
333
*/
332
- ret = (src && dst) ? find_identical_files(src, dst) : 0;
334
+ ret = (src && dst) ? find_identical_files(src, dst, options) : 0;
335
336
/* Free the hashes and return the number of renames found */
337
free_similarity_list(src);
@@ -377,7 +379,7 @@ static void insert_file_table(struct hash_table *table, int src_dst, int index,
379
* and then during the second round we try to match
380
* cache-dirty entries as well.
381
*/
380
-static int find_exact_renames(void)
382
+static int find_exact_renames(struct diff_options *options)
383
{
384
int i;
385
struct hash_table file_table;
@@ -390,7 +392,7 @@ static int find_exact_renames(void)
392
insert_file_table(&file_table, 1, i, rename_dst[i].two);
393
394
/* Find the renames */
393
- i = for_each_hash(&file_table, find_same_files);
395
+ i = for_each_hash(&file_table, find_same_files, options);
396
397
/* .. and free the hash data structure */
398
free_hash(&file_table);
@@ -467,7 +469,7 @@ void diffcore_rename(struct diff_options *options)
469
* We really want to cull the candidates list early
470
* with cheap tests in order to avoid doing deltas.
471
*/
470
- rename_count = find_exact_renames();
472
+ rename_count = find_exact_renames(options);
473
474
/* Did we only want exact renames? */
475
if (minimum_score == MAX_SCORE)
hash.c
+2
-2
@@ -81,7 +81,7 @@ void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table)
81
return insert_hash_entry(hash, ptr, table);
82
}
83
84
-int for_each_hash(const struct hash_table *table, int (*fn)(void *))
84
+int for_each_hash(const struct hash_table *table, int (*fn)(void *, void *), void *data)
85
{
86
int sum = 0;
87
unsigned int i;
@@ -92,7 +92,7 @@ int for_each_hash(const struct hash_table *table, int (*fn)(void *))
92
void *ptr = array->ptr;
93
array++;
94
if (ptr) {
95
- int val = fn(ptr);
95
+ int val = fn(ptr, data);
96
if (val < 0)
97
return val;
98
sum += val;
hash.h
+1
-1
@@ -30,7 +30,7 @@ struct hash_table {
30
31
extern void *lookup_hash(unsigned int hash, const struct hash_table *table);
32
extern void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table);
33
-extern int for_each_hash(const struct hash_table *table, int (*fn)(void *));
33
+extern int for_each_hash(const struct hash_table *table, int (*fn)(void *, void *), void *data);
34
extern void free_hash(struct hash_table *table);
35
36
static inline void init_hash(struct hash_table *table)