@@ -62,8 +62,6 @@ N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\
62
62
"\n"
63
63
"Otherwise, please use 'git reset'\n" );
64
64
65
- static unsigned char head_sha1 [20 ];
66
-
67
65
static const char * use_message_buffer ;
68
66
static const char commit_editmsg [] = "COMMIT_EDITMSG" ;
69
67
static struct lock_file index_lock ; /* real index */
@@ -102,7 +100,7 @@ static enum {
102
100
static char * cleanup_arg ;
103
101
104
102
static enum commit_whence whence ;
105
- static int use_editor = 1 , initial_commit , include_status = 1 ;
103
+ static int use_editor = 1 , include_status = 1 ;
106
104
static int show_ignored_in_status ;
107
105
static const char * only_include_assumed ;
108
106
static struct strbuf message ;
@@ -296,13 +294,13 @@ static void add_remove_files(struct string_list *list)
296
294
}
297
295
}
298
296
299
- static void create_base_index (void )
297
+ static void create_base_index (const struct commit * current_head )
300
298
{
301
299
struct tree * tree ;
302
300
struct unpack_trees_options opts ;
303
301
struct tree_desc t ;
304
302
305
- if (initial_commit ) {
303
+ if (! current_head ) {
306
304
discard_cache ();
307
305
return ;
308
306
}
@@ -315,7 +313,7 @@ static void create_base_index(void)
315
313
opts .dst_index = & the_index ;
316
314
317
315
opts .fn = oneway_merge ;
318
- tree = parse_tree_indirect (head_sha1 );
316
+ tree = parse_tree_indirect (current_head -> object . sha1 );
319
317
if (!tree )
320
318
die (_ ("failed to unpack HEAD tree object" ));
321
319
parse_tree (tree );
@@ -334,7 +332,8 @@ static void refresh_cache_or_die(int refresh_flags)
334
332
die_resolve_conflict ("commit" );
335
333
}
336
334
337
- static char * prepare_index (int argc , const char * * argv , const char * prefix , int is_status )
335
+ static char * prepare_index (int argc , const char * * argv , const char * prefix ,
336
+ const struct commit * current_head , int is_status )
338
337
{
339
338
int fd ;
340
339
struct string_list partial ;
@@ -450,7 +449,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
450
449
451
450
memset (& partial , 0 , sizeof (partial ));
452
451
partial .strdup_strings = 1 ;
453
- if (list_paths (& partial , initial_commit ? NULL : "HEAD" , prefix , pathspec ))
452
+ if (list_paths (& partial , ! current_head ? NULL : "HEAD" , prefix , pathspec ))
454
453
exit (1 );
455
454
456
455
discard_cache ();
@@ -469,7 +468,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
469
468
(uintmax_t ) getpid ()),
470
469
LOCK_DIE_ON_ERROR );
471
470
472
- create_base_index ();
471
+ create_base_index (current_head );
473
472
add_remove_files (& partial );
474
473
refresh_cache (REFRESH_QUIET );
475
474
@@ -518,12 +517,9 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
518
517
return s -> commitable ;
519
518
}
520
519
521
- static int is_a_merge (const unsigned char * sha1 )
520
+ static int is_a_merge (const struct commit * current_head )
522
521
{
523
- struct commit * commit = lookup_commit (sha1 );
524
- if (!commit || parse_commit (commit ))
525
- die (_ ("could not parse HEAD commit" ));
526
- return !!(commit -> parents && commit -> parents -> next );
522
+ return !!(current_head -> parents && current_head -> parents -> next );
527
523
}
528
524
529
525
static const char sign_off_header [] = "Signed-off-by: " ;
@@ -627,6 +623,7 @@ static char *cut_ident_timestamp_part(char *string)
627
623
}
628
624
629
625
static int prepare_to_commit (const char * index_file , const char * prefix ,
626
+ struct commit * current_head ,
630
627
struct wt_status * s ,
631
628
struct strbuf * author_ident )
632
629
{
@@ -848,7 +845,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
848
845
* empty due to conflict resolution, which the user should okay.
849
846
*/
850
847
if (!commitable && whence != FROM_MERGE && !allow_empty &&
851
- !(amend && is_a_merge (head_sha1 ))) {
848
+ !(amend && is_a_merge (current_head ))) {
852
849
run_status (stdout , index_file , prefix , 0 , s );
853
850
if (amend )
854
851
fputs (_ (empty_amend_advice ), stderr );
@@ -1006,6 +1003,7 @@ static const char *read_commit_message(const char *name)
1006
1003
static int parse_and_validate_options (int argc , const char * argv [],
1007
1004
const char * const usage [],
1008
1005
const char * prefix ,
1006
+ struct commit * current_head ,
1009
1007
struct wt_status * s )
1010
1008
{
1011
1009
int f = 0 ;
@@ -1026,11 +1024,8 @@ static int parse_and_validate_options(int argc, const char *argv[],
1026
1024
if (!use_editor )
1027
1025
setenv ("GIT_EDITOR" , ":" , 1 );
1028
1026
1029
- if (get_sha1 ("HEAD" , head_sha1 ))
1030
- initial_commit = 1 ;
1031
-
1032
1027
/* Sanity check options */
1033
- if (amend && initial_commit )
1028
+ if (amend && ! current_head )
1034
1029
die (_ ("You have nothing to amend." ));
1035
1030
if (amend && whence != FROM_COMMIT )
1036
1031
die (_ ("You are in the middle of a %s -- cannot amend." ), whence_s ());
@@ -1102,12 +1097,12 @@ static int parse_and_validate_options(int argc, const char *argv[],
1102
1097
}
1103
1098
1104
1099
static int dry_run_commit (int argc , const char * * argv , const char * prefix ,
1105
- struct wt_status * s )
1100
+ const struct commit * current_head , struct wt_status * s )
1106
1101
{
1107
1102
int commitable ;
1108
1103
const char * index_file ;
1109
1104
1110
- index_file = prepare_index (argc , argv , prefix , 1 );
1105
+ index_file = prepare_index (argc , argv , prefix , current_head , 1 );
1111
1106
commitable = run_status (stdout , index_file , prefix , 0 , s );
1112
1107
rollback_index_files ();
1113
1108
@@ -1260,7 +1255,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
1260
1255
return 0 ;
1261
1256
}
1262
1257
1263
- static void print_summary (const char * prefix , const unsigned char * sha1 )
1258
+ static void print_summary (const char * prefix , const unsigned char * sha1 ,
1259
+ int initial_commit )
1264
1260
{
1265
1261
struct rev_info rev ;
1266
1262
struct commit * commit ;
@@ -1382,12 +1378,13 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1382
1378
struct strbuf author_ident = STRBUF_INIT ;
1383
1379
const char * index_file , * reflog_msg ;
1384
1380
char * nl , * p ;
1385
- unsigned char commit_sha1 [20 ];
1381
+ unsigned char sha1 [20 ];
1386
1382
struct ref_lock * ref_lock ;
1387
1383
struct commit_list * parents = NULL , * * pptr = & parents ;
1388
1384
struct stat statbuf ;
1389
1385
int allow_fast_forward = 1 ;
1390
1386
struct wt_status s ;
1387
+ struct commit * current_head = NULL ;
1391
1388
1392
1389
if (argc == 2 && !strcmp (argv [1 ], "-h" ))
1393
1390
usage_with_options (builtin_commit_usage , builtin_commit_options );
@@ -1398,46 +1395,49 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1398
1395
1399
1396
if (s .use_color == -1 )
1400
1397
s .use_color = git_use_color_default ;
1398
+ if (get_sha1 ("HEAD" , sha1 ))
1399
+ current_head = NULL ;
1400
+ else {
1401
+ current_head = lookup_commit (sha1 );
1402
+ if (!current_head || parse_commit (current_head ))
1403
+ die (_ ("could not parse HEAD commit" ));
1404
+ }
1401
1405
argc = parse_and_validate_options (argc , argv , builtin_commit_usage ,
1402
- prefix , & s );
1406
+ prefix , current_head , & s );
1403
1407
if (dry_run ) {
1404
1408
if (diff_use_color_default == -1 )
1405
1409
diff_use_color_default = git_use_color_default ;
1406
- return dry_run_commit (argc , argv , prefix , & s );
1410
+ return dry_run_commit (argc , argv , prefix , current_head , & s );
1407
1411
}
1408
- index_file = prepare_index (argc , argv , prefix , 0 );
1412
+ index_file = prepare_index (argc , argv , prefix , current_head , 0 );
1409
1413
1410
1414
/* Set up everything for writing the commit object. This includes
1411
1415
running hooks, writing the trees, and interacting with the user. */
1412
- if (!prepare_to_commit (index_file , prefix , & s , & author_ident )) {
1416
+ if (!prepare_to_commit (index_file , prefix ,
1417
+ current_head , & s , & author_ident )) {
1413
1418
rollback_index_files ();
1414
1419
return 1 ;
1415
1420
}
1416
1421
1417
1422
/* Determine parents */
1418
1423
reflog_msg = getenv ("GIT_REFLOG_ACTION" );
1419
- if (initial_commit ) {
1424
+ if (! current_head ) {
1420
1425
if (!reflog_msg )
1421
1426
reflog_msg = "commit (initial)" ;
1422
1427
} else if (amend ) {
1423
1428
struct commit_list * c ;
1424
- struct commit * commit ;
1425
1429
1426
1430
if (!reflog_msg )
1427
1431
reflog_msg = "commit (amend)" ;
1428
- commit = lookup_commit (head_sha1 );
1429
- if (!commit || parse_commit (commit ))
1430
- die (_ ("could not parse HEAD commit" ));
1431
-
1432
- for (c = commit -> parents ; c ; c = c -> next )
1432
+ for (c = current_head -> parents ; c ; c = c -> next )
1433
1433
pptr = & commit_list_insert (c -> item , pptr )-> next ;
1434
1434
} else if (whence == FROM_MERGE ) {
1435
1435
struct strbuf m = STRBUF_INIT ;
1436
1436
FILE * fp ;
1437
1437
1438
1438
if (!reflog_msg )
1439
1439
reflog_msg = "commit (merge)" ;
1440
- pptr = & commit_list_insert (lookup_commit ( head_sha1 ) , pptr )-> next ;
1440
+ pptr = & commit_list_insert (current_head , pptr )-> next ;
1441
1441
fp = fopen (git_path ("MERGE_HEAD" ), "r" );
1442
1442
if (fp == NULL )
1443
1443
die_errno (_ ("could not open '%s' for reading" ),
@@ -1463,7 +1463,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1463
1463
reflog_msg = (whence == FROM_CHERRY_PICK )
1464
1464
? "commit (cherry-pick)"
1465
1465
: "commit" ;
1466
- pptr = & commit_list_insert (lookup_commit ( head_sha1 ) , pptr )-> next ;
1466
+ pptr = & commit_list_insert (current_head , pptr )-> next ;
1467
1467
}
1468
1468
1469
1469
/* Finally, get the commit message */
@@ -1489,15 +1489,17 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1489
1489
exit (1 );
1490
1490
}
1491
1491
1492
- if (commit_tree (sb .buf , active_cache_tree -> sha1 , parents , commit_sha1 ,
1492
+ if (commit_tree (sb .buf , active_cache_tree -> sha1 , parents , sha1 ,
1493
1493
author_ident .buf )) {
1494
1494
rollback_index_files ();
1495
1495
die (_ ("failed to write commit object" ));
1496
1496
}
1497
1497
strbuf_release (& author_ident );
1498
1498
1499
1499
ref_lock = lock_any_ref_for_update ("HEAD" ,
1500
- initial_commit ? NULL : head_sha1 ,
1500
+ !current_head
1501
+ ? NULL
1502
+ : current_head -> object .sha1 ,
1501
1503
0 );
1502
1504
1503
1505
nl = strchr (sb .buf , '\n' );
@@ -1512,7 +1514,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1512
1514
rollback_index_files ();
1513
1515
die (_ ("cannot lock HEAD ref" ));
1514
1516
}
1515
- if (write_ref_sha1 (ref_lock , commit_sha1 , sb .buf ) < 0 ) {
1517
+ if (write_ref_sha1 (ref_lock , sha1 , sb .buf ) < 0 ) {
1516
1518
rollback_index_files ();
1517
1519
die (_ ("cannot update HEAD ref" ));
1518
1520
}
@@ -1534,13 +1536,14 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1534
1536
struct notes_rewrite_cfg * cfg ;
1535
1537
cfg = init_copy_notes_for_rewrite ("amend" );
1536
1538
if (cfg ) {
1537
- copy_note_for_rewrite (cfg , head_sha1 , commit_sha1 );
1539
+ /* we are amending, so current_head is not NULL */
1540
+ copy_note_for_rewrite (cfg , current_head -> object .sha1 , sha1 );
1538
1541
finish_copy_notes_for_rewrite (cfg );
1539
1542
}
1540
- run_rewrite_hook (head_sha1 , commit_sha1 );
1543
+ run_rewrite_hook (current_head -> object . sha1 , sha1 );
1541
1544
}
1542
1545
if (!quiet )
1543
- print_summary (prefix , commit_sha1 );
1546
+ print_summary (prefix , sha1 , ! current_head );
1544
1547
1545
1548
return 0 ;
1546
1549
}
0 commit comments