diff options
-rw-r--r-- | diffcore-order.c | 51 | ||||
-rw-r--r-- | diffcore.h | 14 |
2 files changed, 46 insertions, 19 deletions
diff --git a/diffcore-order.c b/diffcore-order.c index fe7f1f4647..1bfcc39f90 100644 --- a/diffcore-order.c +++ b/diffcore-order.c @@ -57,12 +57,6 @@ static void prepare_order(const char *orderfile) } } -struct pair_order { - struct diff_filepair *pair; - int orig_order; - int order; -}; - static int match_order(const char *path) { int i; @@ -84,35 +78,54 @@ static int match_order(const char *path) return order_cnt; } -static int compare_pair_order(const void *a_, const void *b_) +static int compare_objs_order(const void *a_, const void *b_) { - struct pair_order const *a, *b; - a = (struct pair_order const *)a_; - b = (struct pair_order const *)b_; + struct obj_order const *a, *b; + a = (struct obj_order const *)a_; + b = (struct obj_order const *)b_; if (a->order != b->order) return a->order - b->order; return a->orig_order - b->orig_order; } +void order_objects(const char *orderfile, obj_path_fn_t obj_path, + struct obj_order *objs, int nr) +{ + int i; + + if (!nr) + return; + + prepare_order(orderfile); + for (i = 0; i < nr; i++) { + objs[i].orig_order = i; + objs[i].order = match_order(obj_path(objs[i].obj)); + } + qsort(objs, nr, sizeof(*objs), compare_objs_order); +} + +static const char *pair_pathtwo(void *obj) +{ + struct diff_filepair *pair = (struct diff_filepair *)obj; + + return pair->two->path; +} + void diffcore_order(const char *orderfile) { struct diff_queue_struct *q = &diff_queued_diff; - struct pair_order *o; + struct obj_order *o; int i; if (!q->nr) return; o = xmalloc(sizeof(*o) * q->nr); - prepare_order(orderfile); - for (i = 0; i < q->nr; i++) { - o[i].pair = q->queue[i]; - o[i].orig_order = i; - o[i].order = match_order(o[i].pair->two->path); - } - qsort(o, q->nr, sizeof(*o), compare_pair_order); for (i = 0; i < q->nr; i++) - q->queue[i] = o[i].pair; + o[i].obj = q->queue[i]; + order_objects(orderfile, pair_pathtwo, o, q->nr); + for (i = 0; i < q->nr; i++) + q->queue[i] = o[i].obj; free(o); return; } diff --git a/diffcore.h b/diffcore.h index 79de8cf28d..cbe9e62b49 100644 --- a/diffcore.h +++ b/diffcore.h @@ -109,6 +109,20 @@ extern void diffcore_merge_broken(void); extern void diffcore_pickaxe(struct diff_options *); extern void diffcore_order(const char *orderfile); +/* low-level interface to diffcore_order */ +struct obj_order { + void *obj; /* setup by caller */ + + /* setup/used by order_objects() */ + int orig_order; + int order; +}; + +typedef const char *(*obj_path_fn_t)(void *obj); + +void order_objects(const char *orderfile, obj_path_fn_t obj_path, + struct obj_order *objs, int nr); + #define DIFF_DEBUG 0 #if DIFF_DEBUG void diff_debug_filespec(struct diff_filespec *, int, const char *); |