Skip to content

Commit 1df4320

Browse files
Kirill Smelkovgitster
authored andcommitted
diffcore-order: export generic ordering interface
diffcore_order() interface only accepts a queue of `struct diff_filepair`. In the next patches, we'll want to order `struct combine_diff_path` by path, so let's first rework diffcore-order to also provide generic low-level interface for ordering arbitrary objects, provided they have path accessors. The new interface is: - `struct obj_order` for describing objects to ordering routine, and - order_objects() for actually doing the ordering work. Signed-off-by: Kirill Smelkov <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7146e66 commit 1df4320

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

diffcore-order.c

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@ static void prepare_order(const char *orderfile)
5757
}
5858
}
5959

60-
struct pair_order {
61-
struct diff_filepair *pair;
62-
int orig_order;
63-
int order;
64-
};
65-
6660
static int match_order(const char *path)
6761
{
6862
int i;
@@ -84,35 +78,54 @@ static int match_order(const char *path)
8478
return order_cnt;
8579
}
8680

87-
static int compare_pair_order(const void *a_, const void *b_)
81+
static int compare_objs_order(const void *a_, const void *b_)
8882
{
89-
struct pair_order const *a, *b;
90-
a = (struct pair_order const *)a_;
91-
b = (struct pair_order const *)b_;
83+
struct obj_order const *a, *b;
84+
a = (struct obj_order const *)a_;
85+
b = (struct obj_order const *)b_;
9286
if (a->order != b->order)
9387
return a->order - b->order;
9488
return a->orig_order - b->orig_order;
9589
}
9690

91+
void order_objects(const char *orderfile, obj_path_fn_t obj_path,
92+
struct obj_order *objs, int nr)
93+
{
94+
int i;
95+
96+
if (!nr)
97+
return;
98+
99+
prepare_order(orderfile);
100+
for (i = 0; i < nr; i++) {
101+
objs[i].orig_order = i;
102+
objs[i].order = match_order(obj_path(objs[i].obj));
103+
}
104+
qsort(objs, nr, sizeof(*objs), compare_objs_order);
105+
}
106+
107+
static const char *pair_pathtwo(void *obj)
108+
{
109+
struct diff_filepair *pair = (struct diff_filepair *)obj;
110+
111+
return pair->two->path;
112+
}
113+
97114
void diffcore_order(const char *orderfile)
98115
{
99116
struct diff_queue_struct *q = &diff_queued_diff;
100-
struct pair_order *o;
117+
struct obj_order *o;
101118
int i;
102119

103120
if (!q->nr)
104121
return;
105122

106123
o = xmalloc(sizeof(*o) * q->nr);
107-
prepare_order(orderfile);
108-
for (i = 0; i < q->nr; i++) {
109-
o[i].pair = q->queue[i];
110-
o[i].orig_order = i;
111-
o[i].order = match_order(o[i].pair->two->path);
112-
}
113-
qsort(o, q->nr, sizeof(*o), compare_pair_order);
114124
for (i = 0; i < q->nr; i++)
115-
q->queue[i] = o[i].pair;
125+
o[i].obj = q->queue[i];
126+
order_objects(orderfile, pair_pathtwo, o, q->nr);
127+
for (i = 0; i < q->nr; i++)
128+
q->queue[i] = o[i].obj;
116129
free(o);
117130
return;
118131
}

diffcore.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ extern void diffcore_merge_broken(void);
109109
extern void diffcore_pickaxe(struct diff_options *);
110110
extern void diffcore_order(const char *orderfile);
111111

112+
/* low-level interface to diffcore_order */
113+
struct obj_order {
114+
void *obj; /* setup by caller */
115+
116+
/* setup/used by order_objects() */
117+
int orig_order;
118+
int order;
119+
};
120+
121+
typedef const char *(*obj_path_fn_t)(void *obj);
122+
123+
void order_objects(const char *orderfile, obj_path_fn_t obj_path,
124+
struct obj_order *objs, int nr);
125+
112126
#define DIFF_DEBUG 0
113127
#if DIFF_DEBUG
114128
void diff_debug_filespec(struct diff_filespec *, int, const char *);

0 commit comments

Comments
 (0)