2 * Copyright (C) 2005 Junio C Hamano
11 static void prepare_order(const char *orderfile)
14 struct strbuf sb = STRBUF_INIT;
22 sz = strbuf_read_file(&sb, orderfile, 0);
24 die_errno(_("failed to read orderfile '%s'"), orderfile);
25 map = strbuf_detach(&sb, NULL);
26 endp = (char *) map + sz;
28 for (pass = 0; pass < 2; pass++) {
33 for (ep = cp; ep < endp && *ep != '\n'; ep++)
35 /* cp to ep has one line */
36 if (*cp == '\n' || *cp == '#')
45 order[cnt] = xmemdupz(cp, ep - cp);
55 ALLOC_ARRAY(order, cnt);
60 static int match_order(const char *path)
63 static struct strbuf p = STRBUF_INIT;
65 for (i = 0; i < order_cnt; i++) {
67 strbuf_addstr(&p, path);
70 if (!wildmatch(order[i], p.buf, 0))
72 cp = strrchr(p.buf, '/');
81 static int compare_objs_order(const void *a_, const void *b_)
83 struct obj_order const *a, *b;
84 a = (struct obj_order const *)a_;
85 b = (struct obj_order const *)b_;
86 if (a->order != b->order)
87 return a->order - b->order;
88 return a->orig_order - b->orig_order;
91 void order_objects(const char *orderfile, obj_path_fn_t obj_path,
92 struct obj_order *objs, int nr)
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));
104 QSORT(objs, nr, compare_objs_order);
107 static const char *pair_pathtwo(void *obj)
109 struct diff_filepair *pair = (struct diff_filepair *)obj;
111 return pair->two->path;
114 void diffcore_order(const char *orderfile)
116 struct diff_queue_struct *q = &diff_queued_diff;
123 ALLOC_ARRAY(o, q->nr);
124 for (i = 0; i < q->nr; i++)
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;