blob: 57ccab284645693322301e59b07f6fceb62d6224 [file] [log] [blame]
Junio C Hamanoaf5323e2005-05-30 00:09:07 -07001/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
Elijah Newrenfc7bd512023-02-24 00:09:34 +00004#include "git-compat-util.h"
5#include "gettext.h"
Junio C Hamanoaf5323e2005-05-30 00:09:07 -07006#include "diff.h"
7#include "diffcore.h"
Junio C Hamanoaf5323e2005-05-30 00:09:07 -07008
9static char **order;
10static int order_cnt;
11
12static void prepare_order(const char *orderfile)
13{
Samuel Bronsona21bae32013-12-18 19:08:11 -050014 int cnt, pass;
15 struct strbuf sb = STRBUF_INIT;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070016 void *map;
17 char *cp, *endp;
Samuel Bronsona21bae32013-12-18 19:08:11 -050018 ssize_t sz;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070019
20 if (order)
21 return;
22
Samuel Bronsona21bae32013-12-18 19:08:11 -050023 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);
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -050027 endp = (char *) map + sz;
Samuel Bronsona21bae32013-12-18 19:08:11 -050028
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070029 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++)
35 ;
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;
Pierre Habouzit182af832007-09-16 00:32:36 +020045 } else {
46 order[cnt] = xmemdupz(cp, ep - cp);
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070047 }
48 cnt++;
49 }
50 if (ep < endp)
51 ep++;
52 cp = ep;
53 }
54 if (pass == 0) {
55 order_cnt = cnt;
Jeff Kingb32fa952016-02-22 17:44:25 -050056 ALLOC_ARRAY(order, cnt);
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070057 }
58 }
59}
60
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070061static int match_order(const char *path)
62{
63 int i;
Antoine Pelissefc2b6212013-12-14 12:31:16 +010064 static struct strbuf p = STRBUF_INIT;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070065
66 for (i = 0; i < order_cnt; i++) {
Antoine Pelissefc2b6212013-12-14 12:31:16 +010067 strbuf_reset(&p);
68 strbuf_addstr(&p, path);
69 while (p.buf[0]) {
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070070 char *cp;
Ævar Arnfjörð Bjarmason55d34262017-06-22 21:38:08 +000071 if (!wildmatch(order[i], p.buf, 0))
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070072 return i;
Antoine Pelissefc2b6212013-12-14 12:31:16 +010073 cp = strrchr(p.buf, '/');
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070074 if (!cp)
75 break;
76 *cp = 0;
77 }
78 }
79 return order_cnt;
80}
81
Kirill Smelkov1df43202014-01-20 20:20:38 +040082static int compare_objs_order(const void *a_, const void *b_)
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070083{
Kirill Smelkov1df43202014-01-20 20:20:38 +040084 struct obj_order const *a, *b;
85 a = (struct obj_order const *)a_;
86 b = (struct obj_order const *)b_;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070087 if (a->order != b->order)
88 return a->order - b->order;
89 return a->orig_order - b->orig_order;
90}
91
Kirill Smelkov1df43202014-01-20 20:20:38 +040092void order_objects(const char *orderfile, obj_path_fn_t obj_path,
93 struct obj_order *objs, int nr)
94{
95 int i;
96
97 if (!nr)
98 return;
99
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));
104 }
René Scharfe9ed0d8d2016-09-29 17:27:31 +0200105 QSORT(objs, nr, compare_objs_order);
Kirill Smelkov1df43202014-01-20 20:20:38 +0400106}
107
108static const char *pair_pathtwo(void *obj)
109{
110 struct diff_filepair *pair = (struct diff_filepair *)obj;
111
112 return pair->two->path;
113}
114
Junio C Hamanoaf5323e2005-05-30 00:09:07 -0700115void diffcore_order(const char *orderfile)
116{
117 struct diff_queue_struct *q = &diff_queued_diff;
Kirill Smelkov1df43202014-01-20 20:20:38 +0400118 struct obj_order *o;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -0700119 int i;
120
Junio C Hamano7e4a2a82005-12-26 12:34:56 -0800121 if (!q->nr)
122 return;
123
Jeff Kingb32fa952016-02-22 17:44:25 -0500124 ALLOC_ARRAY(o, q->nr);
Junio C Hamanoaf5323e2005-05-30 00:09:07 -0700125 for (i = 0; i < q->nr; i++)
Kirill Smelkov1df43202014-01-20 20:20:38 +0400126 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;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -0700130 free(o);
131 return;
132}