blob: e7d20ebd2d1b45b073ac2825b9371986d305ee29 [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"
Elijah Newrendd77d582023-05-16 06:34:03 +00008#include "wildmatch.h"
Junio C Hamanoaf5323e2005-05-30 00:09:07 -07009
10static char **order;
11static int order_cnt;
12
13static void prepare_order(const char *orderfile)
14{
Samuel Bronsona21bae32013-12-18 19:08:11 -050015 int cnt, pass;
16 struct strbuf sb = STRBUF_INIT;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070017 void *map;
18 char *cp, *endp;
Samuel Bronsona21bae32013-12-18 19:08:11 -050019 ssize_t sz;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070020
21 if (order)
22 return;
23
Samuel Bronsona21bae32013-12-18 19:08:11 -050024 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);
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -050028 endp = (char *) map + sz;
Samuel Bronsona21bae32013-12-18 19:08:11 -050029
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070030 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++)
36 ;
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;
Pierre Habouzit182af832007-09-16 00:32:36 +020046 } else {
47 order[cnt] = xmemdupz(cp, ep - cp);
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070048 }
49 cnt++;
50 }
51 if (ep < endp)
52 ep++;
53 cp = ep;
54 }
55 if (pass == 0) {
56 order_cnt = cnt;
Jeff Kingb32fa952016-02-22 17:44:25 -050057 ALLOC_ARRAY(order, cnt);
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070058 }
59 }
60}
61
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070062static int match_order(const char *path)
63{
64 int i;
Antoine Pelissefc2b6212013-12-14 12:31:16 +010065 static struct strbuf p = STRBUF_INIT;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070066
67 for (i = 0; i < order_cnt; i++) {
Antoine Pelissefc2b6212013-12-14 12:31:16 +010068 strbuf_reset(&p);
69 strbuf_addstr(&p, path);
70 while (p.buf[0]) {
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070071 char *cp;
Ævar Arnfjörð Bjarmason55d34262017-06-22 21:38:08 +000072 if (!wildmatch(order[i], p.buf, 0))
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070073 return i;
Antoine Pelissefc2b6212013-12-14 12:31:16 +010074 cp = strrchr(p.buf, '/');
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070075 if (!cp)
76 break;
77 *cp = 0;
78 }
79 }
80 return order_cnt;
81}
82
Kirill Smelkov1df43202014-01-20 20:20:38 +040083static int compare_objs_order(const void *a_, const void *b_)
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070084{
Kirill Smelkov1df43202014-01-20 20:20:38 +040085 struct obj_order const *a, *b;
86 a = (struct obj_order const *)a_;
87 b = (struct obj_order const *)b_;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -070088 if (a->order != b->order)
89 return a->order - b->order;
90 return a->orig_order - b->orig_order;
91}
92
Kirill Smelkov1df43202014-01-20 20:20:38 +040093void order_objects(const char *orderfile, obj_path_fn_t obj_path,
94 struct obj_order *objs, int nr)
95{
96 int i;
97
98 if (!nr)
99 return;
100
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));
105 }
René Scharfe9ed0d8d2016-09-29 17:27:31 +0200106 QSORT(objs, nr, compare_objs_order);
Kirill Smelkov1df43202014-01-20 20:20:38 +0400107}
108
109static const char *pair_pathtwo(void *obj)
110{
111 struct diff_filepair *pair = (struct diff_filepair *)obj;
112
113 return pair->two->path;
114}
115
Junio C Hamanoaf5323e2005-05-30 00:09:07 -0700116void diffcore_order(const char *orderfile)
117{
118 struct diff_queue_struct *q = &diff_queued_diff;
Kirill Smelkov1df43202014-01-20 20:20:38 +0400119 struct obj_order *o;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -0700120 int i;
121
Junio C Hamano7e4a2a82005-12-26 12:34:56 -0800122 if (!q->nr)
123 return;
124
Jeff Kingb32fa952016-02-22 17:44:25 -0500125 ALLOC_ARRAY(o, q->nr);
Junio C Hamanoaf5323e2005-05-30 00:09:07 -0700126 for (i = 0; i < q->nr; i++)
Kirill Smelkov1df43202014-01-20 20:20:38 +0400127 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;
Junio C Hamanoaf5323e2005-05-30 00:09:07 -0700131 free(o);
132 return;
133}