@matheusbsbs / matheus-bagliano / commits / 19b358e8da
Fork 0

Use strbuf API in buitin-rerere.c

Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pierre Habouzit committed Sep 6, 2007 at 13:20 UTC 19b358e8daafdfe5a5d25ff7972e041493b05156
1 file changed +18 -38
builtin-rerere.c
+18 -38
@@ -1,6 +1,7 @@
1 #include "builtin.h"
2 #include "cache.h"
3 #include "path-list.h"
4 +#include "strbuf.h"
5 #include "xdiff/xdiff.h"
6 #include "xdiff-interface.h"
7
@@ -66,41 +67,20 @@ static int write_rr(struct path_list *rr, int out_fd)
67 return commit_lock_file(&write_lock);
68 }
69
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 -
70 static int handle_file(const char *path,
71 unsigned char *sha1, const char *output)
72 {
73 SHA_CTX ctx;
74 char buf[1024];
75 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;
78 FILE *f = fopen(path, "r");
79 FILE *out;
80
81 + strbuf_init(&minus);
82 + strbuf_init(&plus);
83 +
84 if (!f)
85 return error("Could not open %s", path);
86
@@ -122,36 +102,36 @@ static int handle_file(const char *path,
102 else if (!prefixcmp(buf, "======="))
103 hunk = 2;
104 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);
108
109 hunk_no++;
110 hunk = 0;
111 if ((cmp > 0) || ((cmp == 0) && one_is_longer)) {
132 - struct buffer *swap = one;
112 + struct strbuf *swap = one;
113 one = two;
114 two = swap;
115 }
116 if (out) {
117 fputs("<<<<<<<\n", out);
138 - fwrite(one->ptr, one->nr, 1, out);
118 + fwrite(one->buf, one->len, 1, out);
119 fputs("=======\n", out);
140 - fwrite(two->ptr, two->nr, 1, out);
120 + fwrite(two->buf, two->len, 1, out);
121 fputs(">>>>>>>\n", out);
122 }
123 if (sha1) {
144 - SHA1_Update(&ctx, one->ptr, one->nr);
124 + SHA1_Update(&ctx, one->buf, one->len);
125 SHA1_Update(&ctx, "\0", 1);
146 - SHA1_Update(&ctx, two->ptr, two->nr);
126 + SHA1_Update(&ctx, two->buf, two->len);
127 SHA1_Update(&ctx, "\0", 1);
128 }
149 - clear_buffer(one);
150 - clear_buffer(two);
129 + strbuf_release(one);
130 + strbuf_release(two);
131 } else if (hunk == 1)
152 - append_line(one, buf);
132 + strbuf_addstr(one, buf);
133 else if (hunk == 2)
154 - append_line(two, buf);
134 + strbuf_addstr(two, buf);
135 else if (out)
136 fputs(buf, out);
137 }