Merge branch 'pw/rebase-i-ignore-cherry-pick-help-environment'
[alt-git.git] / diffcore-order.c
blobe7d20ebd2d1b45b073ac2825b9371986d305ee29
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"
8 #include "wildmatch.h"
10 static char **order;
11 static int order_cnt;
13 static void prepare_order(const char *orderfile)
15 int cnt, pass;
16 struct strbuf sb = STRBUF_INIT;
17 void *map;
18 char *cp, *endp;
19 ssize_t sz;
21 if (order)
22 return;
24 sz = strbuf_read_file(&sb, orderfile, 0);
25 if (sz < 0)
26 die_errno(_("failed to read orderfile '%s'"), orderfile);
27 map = strbuf_detach(&sb, NULL);
28 endp = (char *) map + sz;
30 for (pass = 0; pass < 2; pass++) {
31 cnt = 0;
32 cp = map;
33 while (cp < endp) {
34 char *ep;
35 for (ep = cp; ep < endp && *ep != '\n'; ep++)
37 /* cp to ep has one line */
38 if (*cp == '\n' || *cp == '#')
39 ; /* comment */
40 else if (pass == 0)
41 cnt++;
42 else {
43 if (*ep == '\n') {
44 *ep = 0;
45 order[cnt] = cp;
46 } else {
47 order[cnt] = xmemdupz(cp, ep - cp);
49 cnt++;
51 if (ep < endp)
52 ep++;
53 cp = ep;
55 if (pass == 0) {
56 order_cnt = cnt;
57 ALLOC_ARRAY(order, cnt);
62 static int match_order(const char *path)
64 int i;
65 static struct strbuf p = STRBUF_INIT;
67 for (i = 0; i < order_cnt; i++) {
68 strbuf_reset(&p);
69 strbuf_addstr(&p, path);
70 while (p.buf[0]) {
71 char *cp;
72 if (!wildmatch(order[i], p.buf, 0))
73 return i;
74 cp = strrchr(p.buf, '/');
75 if (!cp)
76 break;
77 *cp = 0;
80 return order_cnt;
83 static int compare_objs_order(const void *a_, const void *b_)
85 struct obj_order const *a, *b;
86 a = (struct obj_order const *)a_;
87 b = (struct obj_order const *)b_;
88 if (a->order != b->order)
89 return a->order - b->order;
90 return a->orig_order - b->orig_order;
93 void order_objects(const char *orderfile, obj_path_fn_t obj_path,
94 struct obj_order *objs, int nr)
96 int i;
98 if (!nr)
99 return;
101 prepare_order(orderfile);
102 for (i = 0; i < nr; i++) {
103 objs[i].orig_order = i;
104 objs[i].order = match_order(obj_path(objs[i].obj));
106 QSORT(objs, nr, compare_objs_order);
109 static const char *pair_pathtwo(void *obj)
111 struct diff_filepair *pair = (struct diff_filepair *)obj;
113 return pair->two->path;
116 void diffcore_order(const char *orderfile)
118 struct diff_queue_struct *q = &diff_queued_diff;
119 struct obj_order *o;
120 int i;
122 if (!q->nr)
123 return;
125 ALLOC_ARRAY(o, q->nr);
126 for (i = 0; i < q->nr; i++)
127 o[i].obj = q->queue[i];
128 order_objects(orderfile, pair_pathtwo, o, q->nr);
129 for (i = 0; i < q->nr; i++)
130 q->queue[i] = o[i].obj;
131 free(o);
132 return;