tests: turn on network daemon tests by default
[git] / diffcore-order.c
1 /*
2  * Copyright (C) 2005 Junio C Hamano
3  */
4 #include "cache.h"
5 #include "diff.h"
6 #include "diffcore.h"
7
8 static char **order;
9 static int order_cnt;
10
11 static void prepare_order(const char *orderfile)
12 {
13         int cnt, pass;
14         struct strbuf sb = STRBUF_INIT;
15         void *map;
16         char *cp, *endp;
17         ssize_t sz;
18
19         if (order)
20                 return;
21
22         sz = strbuf_read_file(&sb, orderfile, 0);
23         if (sz < 0)
24                 die_errno(_("failed to read orderfile '%s'"), orderfile);
25         map = strbuf_detach(&sb, NULL);
26         endp = (char *) map + sz;
27
28         for (pass = 0; pass < 2; pass++) {
29                 cnt = 0;
30                 cp = map;
31                 while (cp < endp) {
32                         char *ep;
33                         for (ep = cp; ep < endp && *ep != '\n'; ep++)
34                                 ;
35                         /* cp to ep has one line */
36                         if (*cp == '\n' || *cp == '#')
37                                 ; /* comment */
38                         else if (pass == 0)
39                                 cnt++;
40                         else {
41                                 if (*ep == '\n') {
42                                         *ep = 0;
43                                         order[cnt] = cp;
44                                 } else {
45                                         order[cnt] = xmemdupz(cp, ep - cp);
46                                 }
47                                 cnt++;
48                         }
49                         if (ep < endp)
50                                 ep++;
51                         cp = ep;
52                 }
53                 if (pass == 0) {
54                         order_cnt = cnt;
55                         order = xmalloc(sizeof(*order) * cnt);
56                 }
57         }
58 }
59
60 struct pair_order {
61         struct diff_filepair *pair;
62         int orig_order;
63         int order;
64 };
65
66 static int match_order(const char *path)
67 {
68         int i;
69         static struct strbuf p = STRBUF_INIT;
70
71         for (i = 0; i < order_cnt; i++) {
72                 strbuf_reset(&p);
73                 strbuf_addstr(&p, path);
74                 while (p.buf[0]) {
75                         char *cp;
76                         if (!fnmatch(order[i], p.buf, 0))
77                                 return i;
78                         cp = strrchr(p.buf, '/');
79                         if (!cp)
80                                 break;
81                         *cp = 0;
82                 }
83         }
84         return order_cnt;
85 }
86
87 static int compare_pair_order(const void *a_, const void *b_)
88 {
89         struct pair_order const *a, *b;
90         a = (struct pair_order const *)a_;
91         b = (struct pair_order const *)b_;
92         if (a->order != b->order)
93                 return a->order - b->order;
94         return a->orig_order - b->orig_order;
95 }
96
97 void diffcore_order(const char *orderfile)
98 {
99         struct diff_queue_struct *q = &diff_queued_diff;
100         struct pair_order *o;
101         int i;
102
103         if (!q->nr)
104                 return;
105
106         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);
114         for (i = 0; i < q->nr; i++)
115                 q->queue[i] = o[i].pair;
116         free(o);
117         return;
118 }