Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Junio C Hamano |
| 3 | */ |
| 4 | #include "cache.h" |
| 5 | #include "diff.h" |
| 6 | #include "diffcore.h" |
Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 7 | |
| 8 | static char **order; |
| 9 | static int order_cnt; |
| 10 | |
| 11 | static void prepare_order(const char *orderfile) |
| 12 | { |
Samuel Bronson | a21bae3 | 2013-12-18 19:08:11 -0500 | [diff] [blame] | 13 | int cnt, pass; |
| 14 | struct strbuf sb = STRBUF_INIT; |
Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 15 | void *map; |
| 16 | char *cp, *endp; |
Samuel Bronson | a21bae3 | 2013-12-18 19:08:11 -0500 | [diff] [blame] | 17 | ssize_t sz; |
Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 18 | |
| 19 | if (order) |
| 20 | return; |
| 21 | |
Samuel Bronson | a21bae3 | 2013-12-18 19:08:11 -0500 | [diff] [blame] | 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); |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 26 | endp = (char *) map + sz; |
Samuel Bronson | a21bae3 | 2013-12-18 19:08:11 -0500 | [diff] [blame] | 27 | |
Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 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; |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 44 | } else { |
| 45 | order[cnt] = xmemdupz(cp, ep - cp); |
Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 46 | } |
| 47 | cnt++; |
| 48 | } |
| 49 | if (ep < endp) |
| 50 | ep++; |
| 51 | cp = ep; |
| 52 | } |
| 53 | if (pass == 0) { |
| 54 | order_cnt = cnt; |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 55 | ALLOC_ARRAY(order, cnt); |
Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 60 | static int match_order(const char *path) |
| 61 | { |
| 62 | int i; |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 63 | static struct strbuf p = STRBUF_INIT; |
Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 64 | |
| 65 | for (i = 0; i < order_cnt; i++) { |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 66 | strbuf_reset(&p); |
| 67 | strbuf_addstr(&p, path); |
| 68 | while (p.buf[0]) { |
Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 69 | char *cp; |
Ævar Arnfjörð Bjarmason | 55d3426 | 2017-06-22 21:38:08 +0000 | [diff] [blame] | 70 | if (!wildmatch(order[i], p.buf, 0)) |
Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 71 | return i; |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 72 | cp = strrchr(p.buf, '/'); |
Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 73 | if (!cp) |
| 74 | break; |
| 75 | *cp = 0; |
| 76 | } |
| 77 | } |
| 78 | return order_cnt; |
| 79 | } |
| 80 | |
Kirill Smelkov | 1df4320 | 2014-01-20 20:20:38 +0400 | [diff] [blame] | 81 | static int compare_objs_order(const void *a_, const void *b_) |
Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 82 | { |
Kirill Smelkov | 1df4320 | 2014-01-20 20:20:38 +0400 | [diff] [blame] | 83 | struct obj_order const *a, *b; |
| 84 | a = (struct obj_order const *)a_; |
| 85 | b = (struct obj_order const *)b_; |
Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 86 | if (a->order != b->order) |
| 87 | return a->order - b->order; |
| 88 | return a->orig_order - b->orig_order; |
| 89 | } |
| 90 | |
Kirill Smelkov | 1df4320 | 2014-01-20 20:20:38 +0400 | [diff] [blame] | 91 | void 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é Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 104 | QSORT(objs, nr, compare_objs_order); |
Kirill Smelkov | 1df4320 | 2014-01-20 20:20:38 +0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static 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 Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 114 | void diffcore_order(const char *orderfile) |
| 115 | { |
| 116 | struct diff_queue_struct *q = &diff_queued_diff; |
Kirill Smelkov | 1df4320 | 2014-01-20 20:20:38 +0400 | [diff] [blame] | 117 | struct obj_order *o; |
Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 118 | int i; |
| 119 | |
Junio C Hamano | 7e4a2a8 | 2005-12-26 12:34:56 -0800 | [diff] [blame] | 120 | if (!q->nr) |
| 121 | return; |
| 122 | |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 123 | ALLOC_ARRAY(o, q->nr); |
Junio C Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 124 | for (i = 0; i < q->nr; i++) |
Kirill Smelkov | 1df4320 | 2014-01-20 20:20:38 +0400 | [diff] [blame] | 125 | 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 Hamano | af5323e | 2005-05-30 00:09:07 -0700 | [diff] [blame] | 129 | free(o); |
| 130 | return; |
| 131 | } |