@@ -157,28 +157,17 @@ static char *get_encoding(const char *message)
157
157
return NULL ;
158
158
}
159
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 )
171
161
{
172
162
const char * p = message ;
173
163
while (* p && (* p != '\n' || p [1 ] != '\n' ))
174
164
p ++ ;
175
165
176
166
if (!* p )
177
- add_to_msg ( sha1_to_hex (commit -> object .sha1 ));
167
+ strbuf_addstr ( msgbuf , sha1_to_hex (commit -> object .sha1 ));
178
168
179
169
p += 2 ;
180
- add_to_msg (p );
181
- return ;
170
+ strbuf_addstr (msgbuf , p );
182
171
}
183
172
184
173
static void set_author_ident_env (const char * message )
@@ -254,6 +243,19 @@ static char *help_msg(const char *name)
254
243
return strbuf_detach (& helpbuf , NULL );
255
244
}
256
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
+
257
259
static struct tree * empty_tree (void )
258
260
{
259
261
struct tree * tree = xcalloc (1 , sizeof (struct tree ));
@@ -289,6 +291,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
289
291
struct merge_options o ;
290
292
struct tree * result , * next_tree , * base_tree , * head_tree ;
291
293
static struct lock_file index_lock ;
294
+ struct strbuf msgbuf = STRBUF_INIT ;
292
295
293
296
git_config (git_default_config , NULL );
294
297
me = action == REVERT ? "revert" : "cherry-pick" ;
@@ -363,35 +366,32 @@ static int revert_or_cherry_pick(int argc, const char **argv)
363
366
* reverse of it if we are revert.
364
367
*/
365
368
366
- msg_fd = hold_lock_file_for_update (& msg_file , defmsg ,
367
- LOCK_DIE_ON_ERROR );
368
-
369
369
if (action == REVERT ) {
370
370
base = commit ;
371
371
base_label = msg .label ;
372
372
next = parent ;
373
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
378
379
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
382
}
383
- add_to_msg ( ".\n" );
383
+ strbuf_addstr ( & msgbuf , ".\n" );
384
384
} else {
385
385
base = parent ;
386
386
base_label = msg .parent_label ;
387
387
next = commit ;
388
388
next_label = msg .label ;
389
389
set_author_ident_env (msg .message );
390
- add_message_to_msg (msg .message );
390
+ add_message_to_msg (& msgbuf , msg .message );
391
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
395
}
396
396
}
397
397
@@ -416,27 +416,25 @@ static int revert_or_cherry_pick(int argc, const char **argv)
416
416
rollback_lock_file (& index_lock );
417
417
418
418
if (!clean ) {
419
- add_to_msg ( "\nConflicts:\n\n" );
419
+ strbuf_addstr ( & msgbuf , "\nConflicts:\n\n" );
420
420
for (i = 0 ; i < active_nr ;) {
421
421
struct cache_entry * ce = active_cache [i ++ ];
422
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
426
while (i < active_nr && !strcmp (ce -> name ,
427
427
active_cache [i ]-> name ))
428
428
i ++ ;
429
429
}
430
430
}
431
- if (commit_lock_file (& msg_file ) < 0 )
432
- die ("Error wrapping up %s" , defmsg );
431
+ write_message (& msgbuf , defmsg );
433
432
fprintf (stderr , "Automatic %s failed.%s\n" ,
434
433
me , help_msg (commit_name ));
435
434
rerere (allow_rerere_auto );
436
435
exit (1 );
437
436
}
438
- if (commit_lock_file (& msg_file ) < 0 )
439
- die ("Error wrapping up %s" , defmsg );
437
+ write_message (& msgbuf , defmsg );
440
438
fprintf (stderr , "Finished one %s.\n" , me );
441
439
442
440
/*
0 commit comments