@@ -39,52 +39,41 @@ pub fn summarize_diff_for_temporary_commit(diff: &Diff) -> eyre::Result<String>
39
39
// this returns something like `1 file changed, 1 deletion(-)`
40
40
// diff.short_stats()
41
41
42
- // this returns something like `test2.txt (-1)` or `2 files (+1/-2)`
42
+ // this builds something like `test2.txt (-1)` or `2 files (+1/-2)`
43
43
let stats = diff. inner . stats ( ) ?;
44
- let prefix = if stats. files_changed ( ) == 1 {
45
- let mut prefix = None ;
46
- // returning false terminates iteration, but that also returns Err, so
47
- // catch and ignore it
44
+ let filename_or_count = if stats. files_changed ( ) == 1 {
45
+ let mut filename = None ;
46
+
47
+ // returning false in the closure terminates iteration, but that also
48
+ // returns an Err, so catch and ignore it
48
49
let _ = diff. inner . foreach (
49
50
& mut |delta : git2:: DiffDelta , _| {
50
- if let Some ( path) = delta. old_file ( ) . path ( ) {
51
- // prefix = Some(format!("{}", path.file_name().unwrap().to_string_lossy()));
52
- prefix = Some ( format ! ( "{}" , path. display( ) ) ) ;
53
- } else if let Some ( path) = delta. new_file ( ) . path ( ) {
54
- prefix = Some ( format ! ( "{}" , path. display( ) ) ) ;
55
- }
56
-
51
+ let relevant_path = delta
52
+ . old_file ( )
53
+ . path ( )
54
+ . or ( delta. new_file ( ) . path ( ) )
55
+ . unwrap_or_else ( || unreachable ! ( "diff should have contained at least 1 file" ) ) ;
56
+ filename = Some ( format ! ( "{}" , relevant_path. display( ) ) ) ;
57
57
false
58
58
} ,
59
59
None ,
60
60
None ,
61
61
None ,
62
62
) ;
63
- prefix
63
+
64
+ filename. unwrap_or_else ( || unreachable ! ( "file name should have been initialized" ) )
64
65
} else {
65
- Some ( format ! ( "{} files" , stats. files_changed( ) ) )
66
+ format ! ( "{} files" , stats. files_changed( ) )
66
67
} ;
67
68
68
- let i = stats. insertions ( ) ;
69
- let d = stats. deletions ( ) ;
70
- Ok ( format ! (
71
- "{prefix} ({i}{slash}{d})" ,
72
- prefix = prefix. unwrap( ) ,
73
- i = if i > 0 {
74
- format!( "+{i}" )
75
- } else {
76
- String :: new( )
77
- } ,
78
- slash = if i > 0 && d > 0 { "/" } else { "" } ,
79
- d = if d > 0 {
80
- format!( "-{d}" )
81
- } else {
82
- String :: new( )
83
- }
84
- ) )
85
- // stats.files_changed()
86
- // stats.insertions()
87
- // stats.deletions()
69
+ let ins_del = match ( stats. insertions ( ) , stats. deletions ( ) ) {
70
+ ( 0 , 0 ) => unreachable ! ( "empty diff" ) ,
71
+ ( i, 0 ) => format ! ( "+{i}" ) ,
72
+ ( 0 , d) => format ! ( "-{d}" ) ,
73
+ ( i, d) => format ! ( "+{i}/-{d}" ) ,
74
+ } ;
75
+
76
+ Ok ( format ! ( "{filename_or_count} ({ins_del})" ) )
88
77
}
89
78
90
79
/// Calculate the diff between the index and the working copy.
0 commit comments