@@ -805,9 +805,15 @@ enum cover_from_description {
805
805
COVER_FROM_AUTO
806
806
};
807
807
808
+ enum auto_base_setting {
809
+ AUTO_BASE_NEVER ,
810
+ AUTO_BASE_ALWAYS ,
811
+ AUTO_BASE_WHEN_ABLE
812
+ };
813
+
808
814
static enum thread_level thread ;
809
815
static int do_signoff ;
810
- static int base_auto ;
816
+ static enum auto_base_setting auto_base ;
811
817
static char * from ;
812
818
static const char * signature = git_version_string ;
813
819
static const char * signature_file ;
@@ -906,7 +912,11 @@ static int git_format_config(const char *var, const char *value, void *cb)
906
912
if (!strcmp (var , "format.outputdirectory" ))
907
913
return git_config_string (& config_output_directory , var , value );
908
914
if (!strcmp (var , "format.useautobase" )) {
909
- base_auto = git_config_bool (var , value );
915
+ if (value && !strcasecmp (value , "whenAble" )) {
916
+ auto_base = AUTO_BASE_WHEN_ABLE ;
917
+ return 0 ;
918
+ }
919
+ auto_base = git_config_bool (var , value ) ? AUTO_BASE_ALWAYS : AUTO_BASE_NEVER ;
910
920
return 0 ;
911
921
}
912
922
if (!strcmp (var , "format.from" )) {
@@ -1425,6 +1435,23 @@ static int from_callback(const struct option *opt, const char *arg, int unset)
1425
1435
return 0 ;
1426
1436
}
1427
1437
1438
+ static int base_callback (const struct option * opt , const char * arg , int unset )
1439
+ {
1440
+ const char * * base_commit = opt -> value ;
1441
+
1442
+ if (unset ) {
1443
+ auto_base = AUTO_BASE_NEVER ;
1444
+ * base_commit = NULL ;
1445
+ } else if (!strcmp (arg , "auto" )) {
1446
+ auto_base = AUTO_BASE_ALWAYS ;
1447
+ * base_commit = NULL ;
1448
+ } else {
1449
+ auto_base = AUTO_BASE_NEVER ;
1450
+ * base_commit = arg ;
1451
+ }
1452
+ return 0 ;
1453
+ }
1454
+
1428
1455
struct base_tree_info {
1429
1456
struct object_id base_commit ;
1430
1457
int nr_patch_id , alloc_patch_id ;
@@ -1437,33 +1464,69 @@ static struct commit *get_base_commit(const char *base_commit,
1437
1464
{
1438
1465
struct commit * base = NULL ;
1439
1466
struct commit * * rev ;
1440
- int i = 0 , rev_nr = 0 ;
1467
+ int i = 0 , rev_nr = 0 , auto_select , die_on_failure ;
1441
1468
1442
- if (base_commit && strcmp (base_commit , "auto" )) {
1469
+ switch (auto_base ) {
1470
+ case AUTO_BASE_NEVER :
1471
+ if (base_commit ) {
1472
+ auto_select = 0 ;
1473
+ die_on_failure = 1 ;
1474
+ } else {
1475
+ /* no base information is requested */
1476
+ return NULL ;
1477
+ }
1478
+ break ;
1479
+ case AUTO_BASE_ALWAYS :
1480
+ case AUTO_BASE_WHEN_ABLE :
1481
+ if (base_commit ) {
1482
+ BUG ("requested automatic base selection but a commit was provided" );
1483
+ } else {
1484
+ auto_select = 1 ;
1485
+ die_on_failure = auto_base == AUTO_BASE_ALWAYS ;
1486
+ }
1487
+ break ;
1488
+ default :
1489
+ BUG ("unexpected automatic base selection method" );
1490
+ }
1491
+
1492
+ if (!auto_select ) {
1443
1493
base = lookup_commit_reference_by_name (base_commit );
1444
1494
if (!base )
1445
1495
die (_ ("unknown commit %s" ), base_commit );
1446
- } else if (( base_commit && ! strcmp ( base_commit , "auto" ))) {
1496
+ } else {
1447
1497
struct branch * curr_branch = branch_get (NULL );
1448
1498
const char * upstream = branch_get_upstream (curr_branch , NULL );
1449
1499
if (upstream ) {
1450
1500
struct commit_list * base_list ;
1451
1501
struct commit * commit ;
1452
1502
struct object_id oid ;
1453
1503
1454
- if (get_oid (upstream , & oid ))
1455
- die (_ ("failed to resolve '%s' as a valid ref" ), upstream );
1504
+ if (get_oid (upstream , & oid )) {
1505
+ if (die_on_failure )
1506
+ die (_ ("failed to resolve '%s' as a valid ref" ), upstream );
1507
+ else
1508
+ return NULL ;
1509
+ }
1456
1510
commit = lookup_commit_or_die (& oid , "upstream base" );
1457
1511
base_list = get_merge_bases_many (commit , total , list );
1458
1512
/* There should be one and only one merge base. */
1459
- if (!base_list || base_list -> next )
1460
- die (_ ("could not find exact merge base" ));
1513
+ if (!base_list || base_list -> next ) {
1514
+ if (die_on_failure ) {
1515
+ die (_ ("could not find exact merge base" ));
1516
+ } else {
1517
+ free_commit_list (base_list );
1518
+ return NULL ;
1519
+ }
1520
+ }
1461
1521
base = base_list -> item ;
1462
1522
free_commit_list (base_list );
1463
1523
} else {
1464
- die (_ ("failed to get upstream, if you want to record base commit automatically,\n"
1465
- "please use git branch --set-upstream-to to track a remote branch.\n"
1466
- "Or you could specify base commit by --base=<base-commit-id> manually" ));
1524
+ if (die_on_failure )
1525
+ die (_ ("failed to get upstream, if you want to record base commit automatically,\n"
1526
+ "please use git branch --set-upstream-to to track a remote branch.\n"
1527
+ "Or you could specify base commit by --base=<base-commit-id> manually" ));
1528
+ else
1529
+ return NULL ;
1467
1530
}
1468
1531
}
1469
1532
@@ -1480,8 +1543,14 @@ static struct commit *get_base_commit(const char *base_commit,
1480
1543
for (i = 0 ; i < rev_nr / 2 ; i ++ ) {
1481
1544
struct commit_list * merge_base ;
1482
1545
merge_base = get_merge_bases (rev [2 * i ], rev [2 * i + 1 ]);
1483
- if (!merge_base || merge_base -> next )
1484
- die (_ ("failed to find exact merge base" ));
1546
+ if (!merge_base || merge_base -> next ) {
1547
+ if (die_on_failure ) {
1548
+ die (_ ("failed to find exact merge base" ));
1549
+ } else {
1550
+ free (rev );
1551
+ return NULL ;
1552
+ }
1553
+ }
1485
1554
1486
1555
rev [i ] = merge_base -> item ;
1487
1556
}
@@ -1491,12 +1560,24 @@ static struct commit *get_base_commit(const char *base_commit,
1491
1560
rev_nr = DIV_ROUND_UP (rev_nr , 2 );
1492
1561
}
1493
1562
1494
- if (!in_merge_bases (base , rev [0 ]))
1495
- die (_ ("base commit should be the ancestor of revision list" ));
1563
+ if (!in_merge_bases (base , rev [0 ])) {
1564
+ if (die_on_failure ) {
1565
+ die (_ ("base commit should be the ancestor of revision list" ));
1566
+ } else {
1567
+ free (rev );
1568
+ return NULL ;
1569
+ }
1570
+ }
1496
1571
1497
1572
for (i = 0 ; i < total ; i ++ ) {
1498
- if (base == list [i ])
1499
- die (_ ("base commit shouldn't be in revision list" ));
1573
+ if (base == list [i ]) {
1574
+ if (die_on_failure ) {
1575
+ die (_ ("base commit shouldn't be in revision list" ));
1576
+ } else {
1577
+ free (rev );
1578
+ return NULL ;
1579
+ }
1580
+ }
1500
1581
}
1501
1582
1502
1583
free (rev );
@@ -1639,6 +1720,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1639
1720
char * branch_name = NULL ;
1640
1721
char * base_commit = NULL ;
1641
1722
struct base_tree_info bases ;
1723
+ struct commit * base ;
1642
1724
int show_progress = 0 ;
1643
1725
struct progress * progress = NULL ;
1644
1726
struct oid_array idiff_prev = OID_ARRAY_INIT ;
@@ -1715,8 +1797,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1715
1797
PARSE_OPT_OPTARG , thread_callback ),
1716
1798
OPT_STRING (0 , "signature" , & signature , N_ ("signature" ),
1717
1799
N_ ("add a signature" )),
1718
- OPT_STRING (0 , "base" , & base_commit , N_ ("base-commit" ),
1719
- N_ ("add prerequisite tree info to the patch series" )),
1800
+ OPT_CALLBACK_F (0 , "base" , & base_commit , N_ ("base-commit" ),
1801
+ N_ ("add prerequisite tree info to the patch series" ),
1802
+ 0 , base_callback ),
1720
1803
OPT_FILENAME (0 , "signature-file" , & signature_file ,
1721
1804
N_ ("add a signature from a file" )),
1722
1805
OPT__QUIET (& quiet , N_ ("don't print the patch filenames" )),
@@ -1753,9 +1836,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1753
1836
s_r_opt .def = "HEAD" ;
1754
1837
s_r_opt .revarg_opt = REVARG_COMMITTISH ;
1755
1838
1756
- if (base_auto )
1757
- base_commit = "auto" ;
1758
-
1759
1839
if (default_attach ) {
1760
1840
rev .mime_boundary = default_attach ;
1761
1841
rev .no_inline = 1 ;
@@ -2019,8 +2099,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
2019
2099
}
2020
2100
2021
2101
memset (& bases , 0 , sizeof (bases ));
2022
- if (base_commit ) {
2023
- struct commit * base = get_base_commit ( base_commit , list , nr );
2102
+ base = get_base_commit (base_commit , list , nr );
2103
+ if ( base ) {
2024
2104
reset_revision_walk ();
2025
2105
clear_object_flags (UNINTERESTING );
2026
2106
prepare_bases (& bases , base , list , nr );
0 commit comments