14
14
15
15
#![ deny( warnings) ]
16
16
17
- use git2:: { Diff , DiffOptions , Error , Object , ObjectType , Repository } ;
18
- use git2:: { DiffFindOptions , DiffFormat } ;
17
+ use git2:: { Blob , Diff , DiffOptions , Error , Object , ObjectType , Oid , Repository } ;
18
+ use git2:: { DiffDelta , DiffFindOptions , DiffFormat , DiffHunk , DiffLine } ;
19
19
use std:: str;
20
20
use structopt:: StructOpt ;
21
21
@@ -26,6 +26,9 @@ struct Args {
26
26
arg_from_oid : Option < String > ,
27
27
#[ structopt( name = "to_oid" ) ]
28
28
arg_to_oid : Option < String > ,
29
+ #[ structopt( name = "blobs" , long) ]
30
+ /// treat from_oid and to_oid as blob ids
31
+ flag_blobs : bool ,
29
32
#[ structopt( name = "patch" , short, long) ]
30
33
/// show output in patch format
31
34
flag_patch : bool ,
@@ -137,6 +140,11 @@ enum Cache {
137
140
None ,
138
141
}
139
142
143
+ fn print_diff ( _delta : DiffDelta , _hunk : Option < DiffHunk > , line : DiffLine ) -> bool {
144
+ print ! ( "{}{}" , line. origin( ) , str :: from_utf8( line. content( ) ) . unwrap( ) ) ;
145
+ true
146
+ }
147
+
140
148
fn run ( args : & Args ) -> Result < ( ) , Error > {
141
149
let path = args. flag_git_dir . as_ref ( ) . map ( |s| & s[ ..] ) . unwrap_or ( "." ) ;
142
150
let repo = Repository :: open ( path) ?;
@@ -171,6 +179,23 @@ fn run(args: &Args) -> Result<(), Error> {
171
179
opts. id_abbrev ( 40 ) ;
172
180
}
173
181
182
+ if args. flag_blobs {
183
+ let b1 = resolve_blob ( & repo, args. arg_from_oid . as_ref ( ) ) ?;
184
+ let b2 = resolve_blob ( & repo, args. arg_to_oid . as_ref ( ) ) ?;
185
+ repo. diff_blobs (
186
+ b1. as_ref ( ) ,
187
+ None ,
188
+ b2. as_ref ( ) ,
189
+ None ,
190
+ Some ( & mut opts) ,
191
+ None ,
192
+ None ,
193
+ None ,
194
+ Some ( & mut print_diff) ,
195
+ ) ?;
196
+ return Ok ( ( ) ) ;
197
+ }
198
+
174
199
// Prepare the diff to inspect
175
200
let t1 = tree_to_treeish ( & repo, args. arg_from_oid . as_ref ( ) ) ?;
176
201
let t2 = tree_to_treeish ( & repo, args. arg_to_oid . as_ref ( ) ) ?;
@@ -292,6 +317,17 @@ fn tree_to_treeish<'a>(
292
317
Ok ( Some ( tree) )
293
318
}
294
319
320
+ fn resolve_blob < ' a > (
321
+ repo : & ' a Repository ,
322
+ arg : Option < & String > ,
323
+ ) -> Result < Option < Blob < ' a > > , Error > {
324
+ let arg = match arg {
325
+ Some ( s) => Oid :: from_str ( s) ?,
326
+ None => return Ok ( None ) ,
327
+ } ;
328
+ repo. find_blob ( arg) . map ( |b| Some ( b) )
329
+ }
330
+
295
331
impl Args {
296
332
fn cache ( & self ) -> Cache {
297
333
if self . flag_cached {
0 commit comments