blob: 19e73311f9cd830da70428439b7b6a14d5345eec [file] [log] [blame]
Junio C Hamanoaf5323e2005-05-30 00:09:07 -07001/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4#include "cache.h"
5#include "diff.h"
6#include "diffcore.h"
Junio C Hamanoaf5323e2005-05-30 00:09:07 -07007
8static char **order;
9static int order_cnt;
10
11static void prepare_order(const char *orderfile)
12{
Samuel Bronsona21bae32013-12-18 19:08:11 -050013 int cnt, pass;
14 struct strbuf sb = STRBUF_INIT;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070015 void *map;
16 char *cp, *endp;
Samuel Bronsona21bae32013-12-18 19:08:11 -050017 ssize_t sz;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070018
19 if (order)
20 return;
21
Samuel Bronsona21bae32013-12-18 19:08:11 -050022 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);
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -050026 endp = (char *) map + sz;
Samuel Bronsona21bae32013-12-18 19:08:11 -050027
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070028 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;
Pierre Habouzit182af832007-09-16 00:32:36 +020044 } else {
45 order[cnt] = xmemdupz(cp, ep - cp);
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070046 }
47 cnt++;
48 }
49 if (ep < endp)
50 ep++;
51 cp = ep;
52 }
53 if (pass == 0) {
54 order_cnt = cnt;
Jeff Kingb32fa952016-02-22 17:44:25 -050055 ALLOC_ARRAY(order, cnt);
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070056 }
57 }
58}
59
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070060static int match_order(const char *path)
61{
62 int i;
Antoine Pelissefc2b6212013-12-14 12:31:16 +010063 static struct strbuf p = STRBUF_INIT;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070064
65 for (i = 0; i < order_cnt; i++) {
Antoine Pelissefc2b6212013-12-14 12:31:16 +010066 strbuf_reset(&p);
67 strbuf_addstr(&p, path);
68 while (p.buf[0]) {
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070069 char *cp;
Ævar Arnfjörð Bjarmason55d34262017-06-22 21:38:08 +000070 if (!wildmatch(order[i], p.buf, 0))
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070071 return i;
Antoine Pelissefc2b6212013-12-14 12:31:16 +010072 cp = strrchr(p.buf, '/');
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070073 if (!cp)
74 break;
75 *cp = 0;
76 }
77 }
78 return order_cnt;
79}
80
Kirill Smelkov1df43202014-01-20 20:20:38 +040081static int compare_objs_order(const void *a_, const void *b_)
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070082{
Kirill Smelkov1df43202014-01-20 20:20:38 +040083 struct obj_order const *a, *b;
84 a = (struct obj_order const *)a_;
85 b = (struct obj_order const *)b_;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070086 if (a->order != b->order)
87 return a->order - b->order;
88 return a->orig_order - b->orig_order;
89}
90
Kirill Smelkov1df43202014-01-20 20:20:38 +040091void order_objects(const char *orderfile, obj_path_fn_t obj_path,
92 struct obj_order *objs, int nr)
93{
94 int i;
95
96 if (!nr)
97 return;
98
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));
103 }
René Scharfe9ed0d8d2016-09-29 17:27:31 +0200104 QSORT(objs, nr, compare_objs_order);
Kirill Smelkov1df43202014-01-20 20:20:38 +0400105}
106
107static const char *pair_pathtwo(void *obj)
108{
109 struct diff_filepair *pair = (struct diff_filepair *)obj;
110
111 return pair->two->path;
112}
113
Junio C Hamanoaf5323e2005-05-30 00:09:07 -0700114void diffcore_order(const char *orderfile)
115{
116 struct diff_queue_struct *q = &diff_queued_diff;
Kirill Smelkov1df43202014-01-20 20:20:38 +0400117 struct obj_order *o;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -0700118 int i;
119
Junio C Hamano7e4a2a82005-12-26 12:34:56 -0800120 if (!q->nr)
121 return;
122
Jeff Kingb32fa952016-02-22 17:44:25 -0500123 ALLOC_ARRAY(o, q->nr);
Junio C Hamanoaf5323e2005-05-30 00:09:07 -0700124 for (i = 0; i < q->nr; i++)
Kirill Smelkov1df43202014-01-20 20:20:38 +0400125 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;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -0700129 free(o);
130 return;
131}