Skip to content

Commit 19b358e

Browse files
MadCodergitster
authored andcommitted
Use strbuf API in buitin-rerere.c
Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent af6eb82 commit 19b358e

File tree

1 file changed

+18
-38
lines changed

1 file changed

+18
-38
lines changed

builtin-rerere.c

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "builtin.h"
22
#include "cache.h"
33
#include "path-list.h"
4+
#include "strbuf.h"
45
#include "xdiff/xdiff.h"
56
#include "xdiff-interface.h"
67

@@ -66,41 +67,20 @@ static int write_rr(struct path_list *rr, int out_fd)
6667
return commit_lock_file(&write_lock);
6768
}
6869

69-
struct buffer {
70-
char *ptr;
71-
int nr, alloc;
72-
};
73-
74-
static void append_line(struct buffer *buffer, const char *line)
75-
{
76-
int len = strlen(line);
77-
78-
if (buffer->nr + len > buffer->alloc) {
79-
buffer->alloc = alloc_nr(buffer->nr + len);
80-
buffer->ptr = xrealloc(buffer->ptr, buffer->alloc);
81-
}
82-
memcpy(buffer->ptr + buffer->nr, line, len);
83-
buffer->nr += len;
84-
}
85-
86-
static void clear_buffer(struct buffer *buffer)
87-
{
88-
free(buffer->ptr);
89-
buffer->ptr = NULL;
90-
buffer->nr = buffer->alloc = 0;
91-
}
92-
9370
static int handle_file(const char *path,
9471
unsigned char *sha1, const char *output)
9572
{
9673
SHA_CTX ctx;
9774
char buf[1024];
9875
int hunk = 0, hunk_no = 0;
99-
struct buffer minus = { NULL, 0, 0 }, plus = { NULL, 0, 0 };
100-
struct buffer *one = &minus, *two = &plus;
76+
struct strbuf minus, plus;
77+
struct strbuf *one = &minus, *two = &plus;
10178
FILE *f = fopen(path, "r");
10279
FILE *out;
10380

81+
strbuf_init(&minus);
82+
strbuf_init(&plus);
83+
10484
if (!f)
10585
return error("Could not open %s", path);
10686

@@ -122,36 +102,36 @@ static int handle_file(const char *path,
122102
else if (!prefixcmp(buf, "======="))
123103
hunk = 2;
124104
else if (!prefixcmp(buf, ">>>>>>> ")) {
125-
int one_is_longer = (one->nr > two->nr);
126-
int common_len = one_is_longer ? two->nr : one->nr;
127-
int cmp = memcmp(one->ptr, two->ptr, common_len);
105+
int one_is_longer = (one->len > two->len);
106+
int common_len = one_is_longer ? two->len : one->len;
107+
int cmp = memcmp(one->buf, two->buf, common_len);
128108

129109
hunk_no++;
130110
hunk = 0;
131111
if ((cmp > 0) || ((cmp == 0) && one_is_longer)) {
132-
struct buffer *swap = one;
112+
struct strbuf *swap = one;
133113
one = two;
134114
two = swap;
135115
}
136116
if (out) {
137117
fputs("<<<<<<<\n", out);
138-
fwrite(one->ptr, one->nr, 1, out);
118+
fwrite(one->buf, one->len, 1, out);
139119
fputs("=======\n", out);
140-
fwrite(two->ptr, two->nr, 1, out);
120+
fwrite(two->buf, two->len, 1, out);
141121
fputs(">>>>>>>\n", out);
142122
}
143123
if (sha1) {
144-
SHA1_Update(&ctx, one->ptr, one->nr);
124+
SHA1_Update(&ctx, one->buf, one->len);
145125
SHA1_Update(&ctx, "\0", 1);
146-
SHA1_Update(&ctx, two->ptr, two->nr);
126+
SHA1_Update(&ctx, two->buf, two->len);
147127
SHA1_Update(&ctx, "\0", 1);
148128
}
149-
clear_buffer(one);
150-
clear_buffer(two);
129+
strbuf_release(one);
130+
strbuf_release(two);
151131
} else if (hunk == 1)
152-
append_line(one, buf);
132+
strbuf_addstr(one, buf);
153133
else if (hunk == 2)
154-
append_line(two, buf);
134+
strbuf_addstr(two, buf);
155135
else if (out)
156136
fputs(buf, out);
157137
}

0 commit comments

Comments
 (0)