Use strbuf API in buitin-rerere.c
Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
af6eb82262
commit
19b358e8da
@ -1,6 +1,7 @@
|
|||||||
#include "builtin.h"
|
#include "builtin.h"
|
||||||
#include "cache.h"
|
#include "cache.h"
|
||||||
#include "path-list.h"
|
#include "path-list.h"
|
||||||
|
#include "strbuf.h"
|
||||||
#include "xdiff/xdiff.h"
|
#include "xdiff/xdiff.h"
|
||||||
#include "xdiff-interface.h"
|
#include "xdiff-interface.h"
|
||||||
|
|
||||||
@ -66,41 +67,20 @@ static int write_rr(struct path_list *rr, int out_fd)
|
|||||||
return commit_lock_file(&write_lock);
|
return commit_lock_file(&write_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct buffer {
|
|
||||||
char *ptr;
|
|
||||||
int nr, alloc;
|
|
||||||
};
|
|
||||||
|
|
||||||
static void append_line(struct buffer *buffer, const char *line)
|
|
||||||
{
|
|
||||||
int len = strlen(line);
|
|
||||||
|
|
||||||
if (buffer->nr + len > buffer->alloc) {
|
|
||||||
buffer->alloc = alloc_nr(buffer->nr + len);
|
|
||||||
buffer->ptr = xrealloc(buffer->ptr, buffer->alloc);
|
|
||||||
}
|
|
||||||
memcpy(buffer->ptr + buffer->nr, line, len);
|
|
||||||
buffer->nr += len;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void clear_buffer(struct buffer *buffer)
|
|
||||||
{
|
|
||||||
free(buffer->ptr);
|
|
||||||
buffer->ptr = NULL;
|
|
||||||
buffer->nr = buffer->alloc = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int handle_file(const char *path,
|
static int handle_file(const char *path,
|
||||||
unsigned char *sha1, const char *output)
|
unsigned char *sha1, const char *output)
|
||||||
{
|
{
|
||||||
SHA_CTX ctx;
|
SHA_CTX ctx;
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
int hunk = 0, hunk_no = 0;
|
int hunk = 0, hunk_no = 0;
|
||||||
struct buffer minus = { NULL, 0, 0 }, plus = { NULL, 0, 0 };
|
struct strbuf minus, plus;
|
||||||
struct buffer *one = &minus, *two = +
|
struct strbuf *one = &minus, *two = +
|
||||||
FILE *f = fopen(path, "r");
|
FILE *f = fopen(path, "r");
|
||||||
FILE *out;
|
FILE *out;
|
||||||
|
|
||||||
|
strbuf_init(&minus);
|
||||||
|
strbuf_init(&plus);
|
||||||
|
|
||||||
if (!f)
|
if (!f)
|
||||||
return error("Could not open %s", path);
|
return error("Could not open %s", path);
|
||||||
|
|
||||||
@ -122,36 +102,36 @@ static int handle_file(const char *path,
|
|||||||
else if (!prefixcmp(buf, "======="))
|
else if (!prefixcmp(buf, "======="))
|
||||||
hunk = 2;
|
hunk = 2;
|
||||||
else if (!prefixcmp(buf, ">>>>>>> ")) {
|
else if (!prefixcmp(buf, ">>>>>>> ")) {
|
||||||
int one_is_longer = (one->nr > two->nr);
|
int one_is_longer = (one->len > two->len);
|
||||||
int common_len = one_is_longer ? two->nr : one->nr;
|
int common_len = one_is_longer ? two->len : one->len;
|
||||||
int cmp = memcmp(one->ptr, two->ptr, common_len);
|
int cmp = memcmp(one->buf, two->buf, common_len);
|
||||||
|
|
||||||
hunk_no++;
|
hunk_no++;
|
||||||
hunk = 0;
|
hunk = 0;
|
||||||
if ((cmp > 0) || ((cmp == 0) && one_is_longer)) {
|
if ((cmp > 0) || ((cmp == 0) && one_is_longer)) {
|
||||||
struct buffer *swap = one;
|
struct strbuf *swap = one;
|
||||||
one = two;
|
one = two;
|
||||||
two = swap;
|
two = swap;
|
||||||
}
|
}
|
||||||
if (out) {
|
if (out) {
|
||||||
fputs("<<<<<<<\n", out);
|
fputs("<<<<<<<\n", out);
|
||||||
fwrite(one->ptr, one->nr, 1, out);
|
fwrite(one->buf, one->len, 1, out);
|
||||||
fputs("=======\n", out);
|
fputs("=======\n", out);
|
||||||
fwrite(two->ptr, two->nr, 1, out);
|
fwrite(two->buf, two->len, 1, out);
|
||||||
fputs(">>>>>>>\n", out);
|
fputs(">>>>>>>\n", out);
|
||||||
}
|
}
|
||||||
if (sha1) {
|
if (sha1) {
|
||||||
SHA1_Update(&ctx, one->ptr, one->nr);
|
SHA1_Update(&ctx, one->buf, one->len);
|
||||||
SHA1_Update(&ctx, "\0", 1);
|
SHA1_Update(&ctx, "\0", 1);
|
||||||
SHA1_Update(&ctx, two->ptr, two->nr);
|
SHA1_Update(&ctx, two->buf, two->len);
|
||||||
SHA1_Update(&ctx, "\0", 1);
|
SHA1_Update(&ctx, "\0", 1);
|
||||||
}
|
}
|
||||||
clear_buffer(one);
|
strbuf_release(one);
|
||||||
clear_buffer(two);
|
strbuf_release(two);
|
||||||
} else if (hunk == 1)
|
} else if (hunk == 1)
|
||||||
append_line(one, buf);
|
strbuf_addstr(one, buf);
|
||||||
else if (hunk == 2)
|
else if (hunk == 2)
|
||||||
append_line(two, buf);
|
strbuf_addstr(two, buf);
|
||||||
else if (out)
|
else if (out)
|
||||||
fputs(buf, out);
|
fputs(buf, out);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user