Add output flushing before fork()
[git/git-p4.git] / diffcore-order.c
blob23e93852d8c701760d56e7e728dba7c08367fbe8
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include "cache.h"
5 #include "diff.h"
6 #include "diffcore.h"
8 static char **order;
9 static int order_cnt;
11 static void prepare_order(const char *orderfile)
13 int fd, cnt, pass;
14 void *map;
15 char *cp, *endp;
16 struct stat st;
17 size_t sz;
19 if (order)
20 return;
22 fd = open(orderfile, O_RDONLY);
23 if (fd < 0)
24 return;
25 if (fstat(fd, &st)) {
26 close(fd);
27 return;
29 sz = xsize_t(st.st_size);
30 map = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
31 close(fd);
32 if (map == MAP_FAILED)
33 return;
34 endp = (char *) map + sz;
35 for (pass = 0; pass < 2; pass++) {
36 cnt = 0;
37 cp = map;
38 while (cp < endp) {
39 char *ep;
40 for (ep = cp; ep < endp && *ep != '\n'; ep++)
42 /* cp to ep has one line */
43 if (*cp == '\n' || *cp == '#')
44 ; /* comment */
45 else if (pass == 0)
46 cnt++;
47 else {
48 if (*ep == '\n') {
49 *ep = 0;
50 order[cnt] = cp;
51 } else {
52 order[cnt] = xmemdupz(cp, ep - cp);
54 cnt++;
56 if (ep < endp)
57 ep++;
58 cp = ep;
60 if (pass == 0) {
61 order_cnt = cnt;
62 order = xmalloc(sizeof(*order) * cnt);
67 struct pair_order {
68 struct diff_filepair *pair;
69 int orig_order;
70 int order;
73 static int match_order(const char *path)
75 int i;
76 char p[PATH_MAX];
78 for (i = 0; i < order_cnt; i++) {
79 strcpy(p, path);
80 while (p[0]) {
81 char *cp;
82 if (!fnmatch(order[i], p, 0))
83 return i;
84 cp = strrchr(p, '/');
85 if (!cp)
86 break;
87 *cp = 0;
90 return order_cnt;
93 static int compare_pair_order(const void *a_, const void *b_)
95 struct pair_order const *a, *b;
96 a = (struct pair_order const *)a_;
97 b = (struct pair_order const *)b_;
98 if (a->order != b->order)
99 return a->order - b->order;
100 return a->orig_order - b->orig_order;
103 void diffcore_order(const char *orderfile)
105 struct diff_queue_struct *q = &diff_queued_diff;
106 struct pair_order *o;
107 int i;
109 if (!q->nr)
110 return;
112 o = xmalloc(sizeof(*o) * q->nr);
113 prepare_order(orderfile);
114 for (i = 0; i < q->nr; i++) {
115 o[i].pair = q->queue[i];
116 o[i].orig_order = i;
117 o[i].order = match_order(o[i].pair->two->path);
119 qsort(o, q->nr, sizeof(*o), compare_pair_order);
120 for (i = 0; i < q->nr; i++)
121 q->queue[i] = o[i].pair;
122 free(o);
123 return;