@@ -19,6 +19,7 @@ pub struct Config<'a> {
1919 pub rebase_options : & ' a Vec < & ' a str > ,
2020 pub whole_file : bool ,
2121 pub one_fixup_per_commit : bool ,
22+ pub squash : bool ,
2223 pub message : Option < & ' a str > ,
2324}
2425
@@ -321,7 +322,8 @@ fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository
321322 . stats ( ) ?;
322323 if !config. dry_run {
323324 head_tree = new_head_tree;
324- let mut message = format ! ( "fixup! {}\n " , dest_commit_locator) ;
325+ let verb = if config. squash { "squash" } else { "fixup" } ;
326+ let mut message = format ! ( "{}! {}\n " , verb, dest_commit_locator) ;
325327 if let Some ( m) = config. message . filter ( |m| !m. is_empty ( ) ) {
326328 message. push ( '\n' ) ;
327329 message. push_str ( m) ;
@@ -718,6 +720,15 @@ mod tests {
718720 let pre_absorb_ref_commit = ctx. repo . refname_to_id ( "PRE_ABSORB_HEAD" ) . unwrap ( ) ;
719721 assert_eq ! ( pre_absorb_ref_commit, actual_pre_absorb_commit) ;
720722
723+ assert_eq ! (
724+ extract_commit_messages( & ctx. repo) ,
725+ vec![
726+ "fixup! Initial commit.\n " ,
727+ "fixup! Initial commit.\n " ,
728+ "Initial commit." ,
729+ ]
730+ ) ;
731+
721732 log_utils:: assert_log_messages_are (
722733 capturing_logger. visible_logs ( ) ,
723734 vec ! [
@@ -1495,6 +1506,74 @@ mod tests {
14951506 assert ! ( is_something_in_index) ;
14961507 }
14971508
1509+ #[ test]
1510+ fn squash_flag ( ) {
1511+ let ctx = repo_utils:: prepare_and_stage ( ) ;
1512+
1513+ // run 'git-absorb'
1514+ let mut capturing_logger = log_utils:: CapturingLogger :: new ( ) ;
1515+ let config = Config {
1516+ squash : true ,
1517+ ..DEFAULT_CONFIG
1518+ } ;
1519+ run_with_repo ( & capturing_logger. logger , & config, & ctx. repo ) . unwrap ( ) ;
1520+
1521+ assert_eq ! (
1522+ extract_commit_messages( & ctx. repo) ,
1523+ vec![
1524+ "squash! Initial commit.\n " ,
1525+ "squash! Initial commit.\n " ,
1526+ "Initial commit." ,
1527+ ]
1528+ ) ;
1529+
1530+ log_utils:: assert_log_messages_are (
1531+ capturing_logger. visible_logs ( ) ,
1532+ vec ! [
1533+ & json!( { "level" : "INFO" , "msg" : "committed" } ) ,
1534+ & json!( { "level" : "INFO" , "msg" : "committed" } ) ,
1535+ & json!( {
1536+ "level" : "INFO" ,
1537+ "msg" : "To squash the new commits, rebase:" ,
1538+ "command" : "git rebase --interactive --autosquash --autostash --root" ,
1539+ } ) ,
1540+ ] ,
1541+ ) ;
1542+ }
1543+
1544+ #[ test]
1545+ fn run_with_squash_config_option ( ) {
1546+ let ctx = repo_utils:: prepare_and_stage ( ) ;
1547+
1548+ repo_utils:: set_config_flag ( & ctx. repo , "absorb.createSquashCommits" ) ;
1549+
1550+ // run 'git-absorb'
1551+ let mut capturing_logger = log_utils:: CapturingLogger :: new ( ) ;
1552+ run_with_repo ( & capturing_logger. logger , & DEFAULT_CONFIG , & ctx. repo ) . unwrap ( ) ;
1553+
1554+ assert_eq ! (
1555+ extract_commit_messages( & ctx. repo) ,
1556+ vec![
1557+ "squash! Initial commit.\n " ,
1558+ "squash! Initial commit.\n " ,
1559+ "Initial commit." ,
1560+ ]
1561+ ) ;
1562+
1563+ log_utils:: assert_log_messages_are (
1564+ capturing_logger. visible_logs ( ) ,
1565+ vec ! [
1566+ & json!( { "level" : "INFO" , "msg" : "committed" } ) ,
1567+ & json!( { "level" : "INFO" , "msg" : "committed" } ) ,
1568+ & json!( {
1569+ "level" : "INFO" ,
1570+ "msg" : "To squash the new commits, rebase:" ,
1571+ "command" : "git rebase --interactive --autosquash --autostash --root" ,
1572+ } ) ,
1573+ ] ,
1574+ ) ;
1575+ }
1576+
14981577 #[ test]
14991578 fn dry_run_flag ( ) {
15001579 let ctx = repo_utils:: prepare_and_stage ( ) ;
@@ -1814,6 +1893,23 @@ mod tests {
18141893 assert_eq ! ( actual_msg, expected_msg) ;
18151894 }
18161895
1896+ /// Perform a revwalk from HEAD, extracting the commit messages.
1897+ fn extract_commit_messages ( repo : & git2:: Repository ) -> Vec < String > {
1898+ let mut revwalk = repo. revwalk ( ) . unwrap ( ) ;
1899+ revwalk. push_head ( ) . unwrap ( ) ;
1900+
1901+ let mut messages = Vec :: new ( ) ;
1902+
1903+ for oid in revwalk {
1904+ let commit = repo. find_commit ( oid. unwrap ( ) ) . unwrap ( ) ;
1905+ if let Some ( message) = commit. message ( ) {
1906+ messages. push ( message. to_string ( ) ) ;
1907+ }
1908+ }
1909+
1910+ messages
1911+ }
1912+
18171913 const DEFAULT_CONFIG : Config = Config {
18181914 dry_run : false ,
18191915 force_author : false ,
@@ -1823,6 +1919,7 @@ mod tests {
18231919 rebase_options : & Vec :: new ( ) ,
18241920 whole_file : false ,
18251921 one_fixup_per_commit : false ,
1922+ squash : false ,
18261923 message : None ,
18271924 } ;
18281925}
0 commit comments