Skip to content

Commit bc84a7f

Browse files
chriscoolgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7ca56aa commit bc84a7f

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

builtin/revert.c

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -157,28 +157,17 @@ static char *get_encoding(const char *message)
157157
return NULL;
158158
}
159159

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)
171161
{
172162
const char *p = message;
173163
while (*p && (*p != '\n' || p[1] != '\n'))
174164
p++;
175165

176166
if (!*p)
177-
add_to_msg(sha1_to_hex(commit->object.sha1));
167+
strbuf_addstr(msgbuf, sha1_to_hex(commit->object.sha1));
178168

179169
p += 2;
180-
add_to_msg(p);
181-
return;
170+
strbuf_addstr(msgbuf, p);
182171
}
183172

184173
static void set_author_ident_env(const char *message)
@@ -254,6 +243,19 @@ static char *help_msg(const char *name)
254243
return strbuf_detach(&helpbuf, NULL);
255244
}
256245

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+
257259
static struct tree *empty_tree(void)
258260
{
259261
struct tree *tree = xcalloc(1, sizeof(struct tree));
@@ -289,6 +291,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
289291
struct merge_options o;
290292
struct tree *result, *next_tree, *base_tree, *head_tree;
291293
static struct lock_file index_lock;
294+
struct strbuf msgbuf = STRBUF_INIT;
292295

293296
git_config(git_default_config, NULL);
294297
me = action == REVERT ? "revert" : "cherry-pick";
@@ -363,35 +366,32 @@ static int revert_or_cherry_pick(int argc, const char **argv)
363366
* reverse of it if we are revert.
364367
*/
365368

366-
msg_fd = hold_lock_file_for_update(&msg_file, defmsg,
367-
LOCK_DIE_ON_ERROR);
368-
369369
if (action == REVERT) {
370370
base = commit;
371371
base_label = msg.label;
372372
next = parent;
373373
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));
378378

379379
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));
382382
}
383-
add_to_msg(".\n");
383+
strbuf_addstr(&msgbuf, ".\n");
384384
} else {
385385
base = parent;
386386
base_label = msg.parent_label;
387387
next = commit;
388388
next_label = msg.label;
389389
set_author_ident_env(msg.message);
390-
add_message_to_msg(msg.message);
390+
add_message_to_msg(&msgbuf, msg.message);
391391
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");
395395
}
396396
}
397397

@@ -416,27 +416,25 @@ static int revert_or_cherry_pick(int argc, const char **argv)
416416
rollback_lock_file(&index_lock);
417417

418418
if (!clean) {
419-
add_to_msg("\nConflicts:\n\n");
419+
strbuf_addstr(&msgbuf, "\nConflicts:\n\n");
420420
for (i = 0; i < active_nr;) {
421421
struct cache_entry *ce = active_cache[i++];
422422
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');
426426
while (i < active_nr && !strcmp(ce->name,
427427
active_cache[i]->name))
428428
i++;
429429
}
430430
}
431-
if (commit_lock_file(&msg_file) < 0)
432-
die ("Error wrapping up %s", defmsg);
431+
write_message(&msgbuf, defmsg);
433432
fprintf(stderr, "Automatic %s failed.%s\n",
434433
me, help_msg(commit_name));
435434
rerere(allow_rerere_auto);
436435
exit(1);
437436
}
438-
if (commit_lock_file(&msg_file) < 0)
439-
die ("Error wrapping up %s", defmsg);
437+
write_message(&msgbuf, defmsg);
440438
fprintf(stderr, "Finished one %s.\n", me);
441439

442440
/*

0 commit comments

Comments
 (0)