@matheusbsbs / matheus-bagliano / commits / bc84a7fbac
Fork 0

revert: use strbuf to refactor the code that writes the merge message

The code in this commit was written by Stephan Beyer for the sequencer GSoC project: git://repo.or.cz/git/sbeyer.git Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Mar 31, 2010 at 21:22 UTC bc84a7fbac4ce85bb93eeb57b5cb39548d286ad0
1 file changed +34 -36
builtin/revert.c
+34 -36
@@ -157,28 +157,17 @@ static char *get_encoding(const char *message)
157 return NULL;
158 }
159
160 -static struct lock_file msg_file;
161 -static int msg_fd;
162 -
163 -static void add_to_msg(const char *string)
164 -{
165 - int len = strlen(string);
166 - if (write_in_full(msg_fd, string, len) < 0)
167 - die_errno ("Could not write to MERGE_MSG");
168 -}
169 -
170 -static void add_message_to_msg(const char *message)
160 +static void add_message_to_msg(struct strbuf *msgbuf, const char *message)
161 {
162 const char *p = message;
163 while (*p && (*p != '\n' || p[1] != '\n'))
164 p++;
165
166 if (!*p)
177 - add_to_msg(sha1_to_hex(commit->object.sha1));
167 + strbuf_addstr(msgbuf, sha1_to_hex(commit->object.sha1));
168
169 p += 2;
180 - add_to_msg(p);
181 - return;
170 + strbuf_addstr(msgbuf, p);
171 }
172
173 static void set_author_ident_env(const char *message)
@@ -254,6 +243,19 @@ static char *help_msg(const char *name)
243 return strbuf_detach(&helpbuf, NULL);
244 }
245
246 +static void write_message(struct strbuf *msgbuf, const char *filename)
247 +{
248 + static struct lock_file msg_file;
249 +
250 + int msg_fd = hold_lock_file_for_update(&msg_file, filename,
251 + LOCK_DIE_ON_ERROR);
252 + if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0)
253 + die_errno("Could not write to %s.", filename);
254 + strbuf_release(msgbuf);
255 + if (commit_lock_file(&msg_file) < 0)
256 + die("Error wrapping up %s", filename);
257 +}
258 +
259 static struct tree *empty_tree(void)
260 {
261 struct tree *tree = xcalloc(1, sizeof(struct tree));
@@ -289,6 +291,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
291 struct merge_options o;
292 struct tree *result, *next_tree, *base_tree, *head_tree;
293 static struct lock_file index_lock;
294 + struct strbuf msgbuf = STRBUF_INIT;
295
296 git_config(git_default_config, NULL);
297 me = action == REVERT ? "revert" : "cherry-pick";
@@ -363,35 +366,32 @@ static int revert_or_cherry_pick(int argc, const char **argv)
366 * reverse of it if we are revert.
367 */
368
366 - msg_fd = hold_lock_file_for_update(&msg_file, defmsg,
367 - LOCK_DIE_ON_ERROR);
368 -
369 if (action == REVERT) {
370 base = commit;
371 base_label = msg.label;
372 next = parent;
373 next_label = msg.parent_label;
374 - add_to_msg("Revert \"");
375 - add_to_msg(msg.subject);
376 - add_to_msg("\"\n\nThis reverts commit ");
377 - add_to_msg(sha1_to_hex(commit->object.sha1));
374 + strbuf_addstr(&msgbuf, "Revert \"");
375 + strbuf_addstr(&msgbuf, msg.subject);
376 + strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit ");
377 + strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
378
379 if (commit->parents->next) {
380 - add_to_msg(", reversing\nchanges made to ");
381 - add_to_msg(sha1_to_hex(parent->object.sha1));
380 + strbuf_addstr(&msgbuf, ", reversing\nchanges made to ");
381 + strbuf_addstr(&msgbuf, sha1_to_hex(parent->object.sha1));
382 }
383 - add_to_msg(".\n");
383 + strbuf_addstr(&msgbuf, ".\n");
384 } else {
385 base = parent;
386 base_label = msg.parent_label;
387 next = commit;
388 next_label = msg.label;
389 set_author_ident_env(msg.message);
390 - add_message_to_msg(msg.message);
390 + add_message_to_msg(&msgbuf, msg.message);
391 if (no_replay) {
392 - add_to_msg("(cherry picked from commit ");
393 - add_to_msg(sha1_to_hex(commit->object.sha1));
394 - add_to_msg(")\n");
392 + strbuf_addstr(&msgbuf, "(cherry picked from commit ");
393 + strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
394 + strbuf_addstr(&msgbuf, ")\n");
395 }
396 }
397
@@ -416,27 +416,25 @@ static int revert_or_cherry_pick(int argc, const char **argv)
416 rollback_lock_file(&index_lock);
417
418 if (!clean) {
419 - add_to_msg("\nConflicts:\n\n");
419 + strbuf_addstr(&msgbuf, "\nConflicts:\n\n");
420 for (i = 0; i < active_nr;) {
421 struct cache_entry *ce = active_cache[i++];
422 if (ce_stage(ce)) {
423 - add_to_msg("\t");
424 - add_to_msg(ce->name);
425 - add_to_msg("\n");
423 + strbuf_addch(&msgbuf, '\t');
424 + strbuf_addstr(&msgbuf, ce->name);
425 + strbuf_addch(&msgbuf, '\n');
426 while (i < active_nr && !strcmp(ce->name,
427 active_cache[i]->name))
428 i++;
429 }
430 }
431 - if (commit_lock_file(&msg_file) < 0)
432 - die ("Error wrapping up %s", defmsg);
431 + write_message(&msgbuf, defmsg);
432 fprintf(stderr, "Automatic %s failed.%s\n",
433 me, help_msg(commit_name));
434 rerere(allow_rerere_auto);
435 exit(1);
436 }
438 - if (commit_lock_file(&msg_file) < 0)
439 - die ("Error wrapping up %s", defmsg);
437 + write_message(&msgbuf, defmsg);
438 fprintf(stderr, "Finished one %s.\n", me);
439
440 /*