Skip to content

Commit a68e6ce

Browse files
newrengitster
authored andcommitted
merge-ort: introduce wrappers for alternate tree traversal
Add traverse_trees_wrapper() and traverse_trees_wrapper_callback() functions. The former runs traverse_trees() with info->fn set to traverse_trees_wrapper_callback, in order to simply save all the entries without processing or recursing into any of them. This step allows extra computation to be done (e.g. checking some condition across all files) that can be used later. Then, after that is completed, it iterates over all the saved entries and calls the original info->fn callback with the saved data. Currently, this does nothing more than marginally slowing down the tree traversal since we do not take advantage of the opportunity to compute anything special in traverse_trees_wrapper_callback(), and thus the real callback will be called identically as it would have been without this extra wrapper. However, a subsequent commit will add some special computation of some values that the real callback will be able to use. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent beb0614 commit a68e6ce

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

merge-ort.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,77 @@ static char *unique_path(struct strmap *existing_paths,
512512

513513
/*** Function Grouping: functions related to collect_merge_info() ***/
514514

515+
static int traverse_trees_wrapper_callback(int n,
516+
unsigned long mask,
517+
unsigned long dirmask,
518+
struct name_entry *names,
519+
struct traverse_info *info)
520+
{
521+
struct merge_options *opt = info->data;
522+
struct rename_info *renames = &opt->priv->renames;
523+
524+
assert(n==3);
525+
526+
if (!renames->callback_data_traverse_path)
527+
renames->callback_data_traverse_path = xstrdup(info->traverse_path);
528+
529+
ALLOC_GROW(renames->callback_data, renames->callback_data_nr + 1,
530+
renames->callback_data_alloc);
531+
renames->callback_data[renames->callback_data_nr].mask = mask;
532+
renames->callback_data[renames->callback_data_nr].dirmask = dirmask;
533+
COPY_ARRAY(renames->callback_data[renames->callback_data_nr].names,
534+
names, 3);
535+
renames->callback_data_nr++;
536+
537+
return mask;
538+
}
539+
540+
/*
541+
* Much like traverse_trees(), BUT:
542+
* - read all the tree entries FIRST, saving them
543+
* - note that the above step provides an opportunity to compute necessary
544+
* additional details before the "real" traversal
545+
* - loop through the saved entries and call the original callback on them
546+
*/
547+
MAYBE_UNUSED
548+
static int traverse_trees_wrapper(struct index_state *istate,
549+
int n,
550+
struct tree_desc *t,
551+
struct traverse_info *info)
552+
{
553+
int ret, i, old_offset;
554+
traverse_callback_t old_fn;
555+
char *old_callback_data_traverse_path;
556+
struct merge_options *opt = info->data;
557+
struct rename_info *renames = &opt->priv->renames;
558+
559+
old_callback_data_traverse_path = renames->callback_data_traverse_path;
560+
old_fn = info->fn;
561+
old_offset = renames->callback_data_nr;
562+
563+
renames->callback_data_traverse_path = NULL;
564+
info->fn = traverse_trees_wrapper_callback;
565+
ret = traverse_trees(istate, n, t, info);
566+
if (ret < 0)
567+
return ret;
568+
569+
info->traverse_path = renames->callback_data_traverse_path;
570+
info->fn = old_fn;
571+
for (i = old_offset; i < renames->callback_data_nr; ++i) {
572+
info->fn(n,
573+
renames->callback_data[i].mask,
574+
renames->callback_data[i].dirmask,
575+
renames->callback_data[i].names,
576+
info);
577+
}
578+
579+
renames->callback_data_nr = old_offset;
580+
free(renames->callback_data_traverse_path);
581+
renames->callback_data_traverse_path = old_callback_data_traverse_path;
582+
info->traverse_path = NULL;
583+
return 0;
584+
}
585+
515586
static void setup_path_info(struct merge_options *opt,
516587
struct string_list_item *result,
517588
const char *current_dir_name,

0 commit comments

Comments
 (0)