2 * Copyright (C) 2005 Junio C Hamano
4 #include "git-compat-util.h"
13 static void prepare_order(const char *orderfile
)
16 struct strbuf sb
= STRBUF_INIT
;
24 sz
= strbuf_read_file(&sb
, orderfile
, 0);
26 die_errno(_("failed to read orderfile '%s'"), orderfile
);
27 map
= strbuf_detach(&sb
, NULL
);
28 endp
= (char *) map
+ sz
;
30 for (pass
= 0; pass
< 2; pass
++) {
35 for (ep
= cp
; ep
< endp
&& *ep
!= '\n'; ep
++)
37 /* cp to ep has one line */
38 if (*cp
== '\n' || *cp
== '#')
47 order
[cnt
] = xmemdupz(cp
, ep
- cp
);
57 ALLOC_ARRAY(order
, cnt
);
62 static int match_order(const char *path
)
65 static struct strbuf p
= STRBUF_INIT
;
67 for (i
= 0; i
< order_cnt
; i
++) {
69 strbuf_addstr(&p
, path
);
72 if (!wildmatch(order
[i
], p
.buf
, 0))
74 cp
= strrchr(p
.buf
, '/');
83 static int compare_objs_order(const void *a_
, const void *b_
)
85 struct obj_order
const *a
, *b
;
86 a
= (struct obj_order
const *)a_
;
87 b
= (struct obj_order
const *)b_
;
88 if (a
->order
!= b
->order
)
89 return a
->order
- b
->order
;
90 return a
->orig_order
- b
->orig_order
;
93 void order_objects(const char *orderfile
, obj_path_fn_t obj_path
,
94 struct obj_order
*objs
, int nr
)
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
));
106 QSORT(objs
, nr
, compare_objs_order
);
109 static const char *pair_pathtwo(void *obj
)
111 struct diff_filepair
*pair
= (struct diff_filepair
*)obj
;
113 return pair
->two
->path
;
116 void diffcore_order(const char *orderfile
)
118 struct diff_queue_struct
*q
= &diff_queued_diff
;
125 ALLOC_ARRAY(o
, q
->nr
);
126 for (i
= 0; i
< q
->nr
; i
++)
127 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
;