commit: replace the raw buffer with strbuf in read_graft_line

This simplifies function declaration and allows for use of strbuf_rtrim instead of modifying buffer directly. Signed-off-by: Patryk Obara <patryk.obara@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patryk Obara committed Aug 18, 2017 at 20:33 UTC 9a9340329a7151697c943794369950115963879f
3 files changed +13 -14
builtin/blame.c
+1 -1
@@ -488,7 +488,7 @@ static int read_ancestry(const char *graft_file)
488 return -1;
489 while (!strbuf_getwholeline(&buf, fp, '\n')) {
490 /* The format is just "Commit Parent1 Parent2 ...\n" */
491 - struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
491 + struct commit_graft *graft = read_graft_line(&buf);
492 if (graft)
493 register_commit_graft(graft, 0);
494 }
commit.c
+11 -12
@@ -134,34 +134,33 @@ int register_commit_graft(struct commit_graft *graft, int ignore_dups)
134 return 0;
135 }
136
137 -struct commit_graft *read_graft_line(char *buf, int len)
137 +struct commit_graft *read_graft_line(struct strbuf *line)
138 {
139 /* The format is just "Commit Parent1 Parent2 ...\n" */
140 int i;
141 struct commit_graft *graft = NULL;
142 const int entry_size = GIT_SHA1_HEXSZ + 1;
143
144 - while (len && isspace(buf[len-1]))
145 - buf[--len] = '\0';
146 - if (buf[0] == '#' || buf[0] == '\0')
144 + strbuf_rtrim(line);
145 + if (!line->len || line->buf[0] == '#')
146 return NULL;
148 - if ((len + 1) % entry_size)
147 + if ((line->len + 1) % entry_size)
148 goto bad_graft_data;
150 - i = (len + 1) / entry_size - 1;
149 + i = (line->len + 1) / entry_size - 1;
150 graft = xmalloc(st_add(sizeof(*graft), st_mult(GIT_SHA1_RAWSZ, i)));
151 graft->nr_parent = i;
153 - if (get_oid_hex(buf, &graft->oid))
152 + if (get_oid_hex(line->buf, &graft->oid))
153 goto bad_graft_data;
155 - for (i = GIT_SHA1_HEXSZ; i < len; i += entry_size) {
156 - if (buf[i] != ' ')
154 + for (i = GIT_SHA1_HEXSZ; i < line->len; i += entry_size) {
155 + if (line->buf[i] != ' ')
156 goto bad_graft_data;
158 - if (get_sha1_hex(buf + i + 1, graft->parent[i/entry_size].hash))
157 + if (get_sha1_hex(line->buf + i + 1, graft->parent[i/entry_size].hash))
158 goto bad_graft_data;
159 }
160 return graft;
161
162 bad_graft_data:
164 - error("bad graft data: %s", buf);
163 + error("bad graft data: %s", line->buf);
164 free(graft);
165 return NULL;
166 }
@@ -174,7 +173,7 @@ static int read_graft_file(const char *graft_file)
173 return -1;
174 while (!strbuf_getwholeline(&buf, fp, '\n')) {
175 /* The format is just "Commit Parent1 Parent2 ...\n" */
177 - struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
176 + struct commit_graft *graft = read_graft_line(&buf);
177 if (!graft)
178 continue;
179 if (register_commit_graft(graft, 1))
commit.h
+1 -1
@@ -247,7 +247,7 @@ struct commit_graft {
247 };
248 typedef int (*each_commit_graft_fn)(const struct commit_graft *, void *);
249
250 -struct commit_graft *read_graft_line(char *buf, int len);
250 +struct commit_graft *read_graft_line(struct strbuf *line);
251 int register_commit_graft(struct commit_graft *, int);
252 struct commit_graft *lookup_commit_graft(const struct object_id *oid);
253