diff-files: integrate with sparse index
[git.git] / diffcore-order.c
blob57ccab284645693322301e59b07f6fceb62d6224
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include "git-compat-util.h"
5 #include "gettext.h"
6 #include "diff.h"
7 #include "diffcore.h"
9 static char **order;
10 static int order_cnt;
12 static void prepare_order(const char *orderfile)
14 int cnt, pass;
15 struct strbuf sb = STRBUF_INIT;
16 void *map;
17 char *cp, *endp;
18 ssize_t sz;
20 if (order)
21 return;
23 sz = strbuf_read_file(&sb, orderfile, 0);
24 if (sz < 0)
25 die_errno(_("failed to read orderfile '%s'"), orderfile);
26 map = strbuf_detach(&sb, NULL);
27 endp = (char *) map + sz;
29 for (pass = 0; pass < 2; pass++) {
30 cnt = 0;
31 cp = map;
32 while (cp < endp) {
33 char *ep;
34 for (ep = cp; ep < endp && *ep != '\n'; ep++)
36 /* cp to ep has one line */
37 if (*cp == '\n' || *cp == '#')
38 ; /* comment */
39 else if (pass == 0)
40 cnt++;
41 else {
42 if (*ep == '\n') {
43 *ep = 0;
44 order[cnt] = cp;
45 } else {
46 order[cnt] = xmemdupz(cp, ep - cp);
48 cnt++;
50 if (ep < endp)
51 ep++;
52 cp = ep;
54 if (pass == 0) {
55 order_cnt = cnt;
56 ALLOC_ARRAY(order, cnt);
61 static int match_order(const char *path)
63 int i;
64 static struct strbuf p = STRBUF_INIT;
66 for (i = 0; i < order_cnt; i++) {
67 strbuf_reset(&p);
68 strbuf_addstr(&p, path);
69 while (p.buf[0]) {
70 char *cp;
71 if (!wildmatch(order[i], p.buf, 0))
72 return i;
73 cp = strrchr(p.buf, '/');
74 if (!cp)
75 break;
76 *cp = 0;
79 return order_cnt;
82 static int compare_objs_order(const void *a_, const void *b_)
84 struct obj_order const *a, *b;
85 a = (struct obj_order const *)a_;
86 b = (struct obj_order const *)b_;
87 if (a->order != b->order)
88 return a->order - b->order;
89 return a->orig_order - b->orig_order;
92 void order_objects(const char *orderfile, obj_path_fn_t obj_path,
93 struct obj_order *objs, int nr)
95 int i;
97 if (!nr)
98 return;
100 prepare_order(orderfile);
101 for (i = 0; i < nr; i++) {
102 objs[i].orig_order = i;
103 objs[i].order = match_order(obj_path(objs[i].obj));
105 QSORT(objs, nr, compare_objs_order);
108 static const char *pair_pathtwo(void *obj)
110 struct diff_filepair *pair = (struct diff_filepair *)obj;
112 return pair->two->path;
115 void diffcore_order(const char *orderfile)
117 struct diff_queue_struct *q = &diff_queued_diff;
118 struct obj_order *o;
119 int i;
121 if (!q->nr)
122 return;
124 ALLOC_ARRAY(o, q->nr);
125 for (i = 0; i < q->nr; i++)
126 o[i].obj = q->queue[i];
127 order_objects(orderfile, pair_pathtwo, o, q->nr);
128 for (i = 0; i < q->nr; i++)
129 q->queue[i] = o[i].obj;
130 free(o);
131 return;