2005-05-30 11:09:07 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 Junio C Hamano
|
|
|
|
*/
|
|
|
|
#include "cache.h"
|
|
|
|
#include "diff.h"
|
|
|
|
#include "diffcore.h"
|
|
|
|
|
|
|
|
static char **order;
|
|
|
|
static int order_cnt;
|
|
|
|
|
|
|
|
static void prepare_order(const char *orderfile)
|
|
|
|
{
|
diff: let "git diff -O" read orderfile from any file and fail properly
The -O flag really shouldn't silently fail to do anything when given
a path that it can't read from.
However, it should be able to read from un-mmappable files, such as:
* pipes/fifos
* /dev/null: It's a character device (at least on Linux)
* ANY empty file:
Quoting Linux mmap(2), "SUSv3 specifies that mmap() should fail if
length is 0. However, in kernels before 2.6.12, mmap() succeeded in
this case: no mapping was created and the call returned addr. Since
kernel 2.6.12, mmap() fails with the error EINVAL for this case."
We especially want "-O/dev/null" to work, since we will be documenting
it as the way to cancel "diff.orderfile" when we add that.
(Note: "-O/dev/null" did have the right effect, since the existing error
handling essentially worked out to "silently ignore the orderfile". But
this was probably more coincidence than anything else.)
So, lets toss all of that logic to get the file mmapped and just use
strbuf_read_file() instead, which gives us decent error handling
practically for free.
Signed-off-by: Samuel Bronson <naesten@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-12-19 04:08:11 +04:00
|
|
|
int cnt, pass;
|
|
|
|
struct strbuf sb = STRBUF_INIT;
|
2005-05-30 11:09:07 +04:00
|
|
|
void *map;
|
|
|
|
char *cp, *endp;
|
diff: let "git diff -O" read orderfile from any file and fail properly
The -O flag really shouldn't silently fail to do anything when given
a path that it can't read from.
However, it should be able to read from un-mmappable files, such as:
* pipes/fifos
* /dev/null: It's a character device (at least on Linux)
* ANY empty file:
Quoting Linux mmap(2), "SUSv3 specifies that mmap() should fail if
length is 0. However, in kernels before 2.6.12, mmap() succeeded in
this case: no mapping was created and the call returned addr. Since
kernel 2.6.12, mmap() fails with the error EINVAL for this case."
We especially want "-O/dev/null" to work, since we will be documenting
it as the way to cancel "diff.orderfile" when we add that.
(Note: "-O/dev/null" did have the right effect, since the existing error
handling essentially worked out to "silently ignore the orderfile". But
this was probably more coincidence than anything else.)
So, lets toss all of that logic to get the file mmapped and just use
strbuf_read_file() instead, which gives us decent error handling
practically for free.
Signed-off-by: Samuel Bronson <naesten@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-12-19 04:08:11 +04:00
|
|
|
ssize_t sz;
|
2005-05-30 11:09:07 +04:00
|
|
|
|
|
|
|
if (order)
|
|
|
|
return;
|
|
|
|
|
diff: let "git diff -O" read orderfile from any file and fail properly
The -O flag really shouldn't silently fail to do anything when given
a path that it can't read from.
However, it should be able to read from un-mmappable files, such as:
* pipes/fifos
* /dev/null: It's a character device (at least on Linux)
* ANY empty file:
Quoting Linux mmap(2), "SUSv3 specifies that mmap() should fail if
length is 0. However, in kernels before 2.6.12, mmap() succeeded in
this case: no mapping was created and the call returned addr. Since
kernel 2.6.12, mmap() fails with the error EINVAL for this case."
We especially want "-O/dev/null" to work, since we will be documenting
it as the way to cancel "diff.orderfile" when we add that.
(Note: "-O/dev/null" did have the right effect, since the existing error
handling essentially worked out to "silently ignore the orderfile". But
this was probably more coincidence than anything else.)
So, lets toss all of that logic to get the file mmapped and just use
strbuf_read_file() instead, which gives us decent error handling
practically for free.
Signed-off-by: Samuel Bronson <naesten@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-12-19 04:08:11 +04:00
|
|
|
sz = strbuf_read_file(&sb, orderfile, 0);
|
|
|
|
if (sz < 0)
|
|
|
|
die_errno(_("failed to read orderfile '%s'"), orderfile);
|
|
|
|
map = strbuf_detach(&sb, NULL);
|
2007-03-07 04:44:37 +03:00
|
|
|
endp = (char *) map + sz;
|
diff: let "git diff -O" read orderfile from any file and fail properly
The -O flag really shouldn't silently fail to do anything when given
a path that it can't read from.
However, it should be able to read from un-mmappable files, such as:
* pipes/fifos
* /dev/null: It's a character device (at least on Linux)
* ANY empty file:
Quoting Linux mmap(2), "SUSv3 specifies that mmap() should fail if
length is 0. However, in kernels before 2.6.12, mmap() succeeded in
this case: no mapping was created and the call returned addr. Since
kernel 2.6.12, mmap() fails with the error EINVAL for this case."
We especially want "-O/dev/null" to work, since we will be documenting
it as the way to cancel "diff.orderfile" when we add that.
(Note: "-O/dev/null" did have the right effect, since the existing error
handling essentially worked out to "silently ignore the orderfile". But
this was probably more coincidence than anything else.)
So, lets toss all of that logic to get the file mmapped and just use
strbuf_read_file() instead, which gives us decent error handling
practically for free.
Signed-off-by: Samuel Bronson <naesten@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-12-19 04:08:11 +04:00
|
|
|
|
2005-05-30 11:09:07 +04:00
|
|
|
for (pass = 0; pass < 2; pass++) {
|
|
|
|
cnt = 0;
|
|
|
|
cp = map;
|
|
|
|
while (cp < endp) {
|
|
|
|
char *ep;
|
|
|
|
for (ep = cp; ep < endp && *ep != '\n'; ep++)
|
|
|
|
;
|
|
|
|
/* cp to ep has one line */
|
|
|
|
if (*cp == '\n' || *cp == '#')
|
|
|
|
; /* comment */
|
|
|
|
else if (pass == 0)
|
|
|
|
cnt++;
|
|
|
|
else {
|
|
|
|
if (*ep == '\n') {
|
|
|
|
*ep = 0;
|
|
|
|
order[cnt] = cp;
|
2007-09-16 02:32:36 +04:00
|
|
|
} else {
|
|
|
|
order[cnt] = xmemdupz(cp, ep - cp);
|
2005-05-30 11:09:07 +04:00
|
|
|
}
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
if (ep < endp)
|
|
|
|
ep++;
|
|
|
|
cp = ep;
|
|
|
|
}
|
|
|
|
if (pass == 0) {
|
|
|
|
order_cnt = cnt;
|
2016-02-23 01:44:25 +03:00
|
|
|
ALLOC_ARRAY(order, cnt);
|
2005-05-30 11:09:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int match_order(const char *path)
|
|
|
|
{
|
|
|
|
int i;
|
2013-12-14 15:31:16 +04:00
|
|
|
static struct strbuf p = STRBUF_INIT;
|
2005-05-30 11:09:07 +04:00
|
|
|
|
|
|
|
for (i = 0; i < order_cnt; i++) {
|
2013-12-14 15:31:16 +04:00
|
|
|
strbuf_reset(&p);
|
|
|
|
strbuf_addstr(&p, path);
|
|
|
|
while (p.buf[0]) {
|
2005-05-30 11:09:07 +04:00
|
|
|
char *cp;
|
2014-02-15 06:01:46 +04:00
|
|
|
if (!wildmatch(order[i], p.buf, 0, NULL))
|
2005-05-30 11:09:07 +04:00
|
|
|
return i;
|
2013-12-14 15:31:16 +04:00
|
|
|
cp = strrchr(p.buf, '/');
|
2005-05-30 11:09:07 +04:00
|
|
|
if (!cp)
|
|
|
|
break;
|
|
|
|
*cp = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return order_cnt;
|
|
|
|
}
|
|
|
|
|
2014-01-20 20:20:38 +04:00
|
|
|
static int compare_objs_order(const void *a_, const void *b_)
|
2005-05-30 11:09:07 +04:00
|
|
|
{
|
2014-01-20 20:20:38 +04:00
|
|
|
struct obj_order const *a, *b;
|
|
|
|
a = (struct obj_order const *)a_;
|
|
|
|
b = (struct obj_order const *)b_;
|
2005-05-30 11:09:07 +04:00
|
|
|
if (a->order != b->order)
|
|
|
|
return a->order - b->order;
|
|
|
|
return a->orig_order - b->orig_order;
|
|
|
|
}
|
|
|
|
|
2014-01-20 20:20:38 +04:00
|
|
|
void order_objects(const char *orderfile, obj_path_fn_t obj_path,
|
|
|
|
struct obj_order *objs, int nr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!nr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
prepare_order(orderfile);
|
|
|
|
for (i = 0; i < nr; i++) {
|
|
|
|
objs[i].orig_order = i;
|
|
|
|
objs[i].order = match_order(obj_path(objs[i].obj));
|
|
|
|
}
|
2016-09-29 18:27:31 +03:00
|
|
|
QSORT(objs, nr, compare_objs_order);
|
2014-01-20 20:20:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *pair_pathtwo(void *obj)
|
|
|
|
{
|
|
|
|
struct diff_filepair *pair = (struct diff_filepair *)obj;
|
|
|
|
|
|
|
|
return pair->two->path;
|
|
|
|
}
|
|
|
|
|
2005-05-30 11:09:07 +04:00
|
|
|
void diffcore_order(const char *orderfile)
|
|
|
|
{
|
|
|
|
struct diff_queue_struct *q = &diff_queued_diff;
|
2014-01-20 20:20:38 +04:00
|
|
|
struct obj_order *o;
|
2005-05-30 11:09:07 +04:00
|
|
|
int i;
|
|
|
|
|
2005-12-26 23:34:56 +03:00
|
|
|
if (!q->nr)
|
|
|
|
return;
|
|
|
|
|
2016-02-23 01:44:25 +03:00
|
|
|
ALLOC_ARRAY(o, q->nr);
|
2005-05-30 11:09:07 +04:00
|
|
|
for (i = 0; i < q->nr; i++)
|
2014-01-20 20:20:38 +04:00
|
|
|
o[i].obj = q->queue[i];
|
|
|
|
order_objects(orderfile, pair_pathtwo, o, q->nr);
|
|
|
|
for (i = 0; i < q->nr; i++)
|
|
|
|
q->queue[i] = o[i].obj;
|
2005-05-30 11:09:07 +04:00
|
|
|
free(o);
|
|
|
|
return;
|
|
|
|
}
|