summaryrefslogtreecommitdiffstats
path: root/vendor/git2/examples/diff.rs
blob: 62f165db395aa63944ceb292bc67bb4b2f2bf183 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * libgit2 "diff" example - shows how to use the diff API
 *
 * Written by the libgit2 contributors
 *
 * To the extent possible under law, the author(s) have dedicated all copyright
 * and related and neighboring rights to this software to the public domain
 * worldwide. This software is distributed without any warranty.
 *
 * You should have received a copy of the CC0 Public Domain Dedication along
 * with this software. If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

#![deny(warnings)]

use git2::{Blob, Diff, DiffOptions, Error, Object, ObjectType, Oid, Repository};
use git2::{DiffDelta, DiffFindOptions, DiffFormat, DiffHunk, DiffLine};
use std::str;
use structopt::StructOpt;

#[derive(StructOpt)]
#[allow(non_snake_case)]
struct Args {
    #[structopt(name = "from_oid")]
    arg_from_oid: Option<String>,
    #[structopt(name = "to_oid")]
    arg_to_oid: Option<String>,
    #[structopt(name = "blobs", long)]
    /// treat from_oid and to_oid as blob ids
    flag_blobs: bool,
    #[structopt(name = "patch", short, long)]
    /// show output in patch format
    flag_patch: bool,
    #[structopt(name = "cached", long)]
    /// use staged changes as diff
    flag_cached: bool,
    #[structopt(name = "nocached", long)]
    /// do not use staged changes
    flag_nocached: bool,
    #[structopt(name = "name-only", long)]
    /// show only names of changed files
    flag_name_only: bool,
    #[structopt(name = "name-status", long)]
    /// show only names and status changes
    flag_name_status: bool,
    #[structopt(name = "raw", long)]
    /// generate the raw format
    flag_raw: bool,
    #[structopt(name = "format", long)]
    /// specify format for stat summary
    flag_format: Option<String>,
    #[structopt(name = "color", long)]
    /// use color output
    flag_color: bool,
    #[structopt(name = "no-color", long)]
    /// never use color output
    flag_no_color: bool,
    #[structopt(short = "R")]
    /// swap two inputs
    flag_R: bool,
    #[structopt(name = "text", short = "a", long)]
    /// treat all files as text
    flag_text: bool,
    #[structopt(name = "ignore-space-at-eol", long)]
    /// ignore changes in whitespace at EOL
    flag_ignore_space_at_eol: bool,
    #[structopt(name = "ignore-space-change", short = "b", long)]
    /// ignore changes in amount of whitespace
    flag_ignore_space_change: bool,
    #[structopt(name = "ignore-all-space", short = "w", long)]
    /// ignore whitespace when comparing lines
    flag_ignore_all_space: bool,
    #[structopt(name = "ignored", long)]
    /// show untracked files
    flag_ignored: bool,
    #[structopt(name = "untracked", long)]
    /// generate diff using the patience algorithm
    flag_untracked: bool,
    #[structopt(name = "patience", long)]
    /// show ignored files as well
    flag_patience: bool,
    #[structopt(name = "minimal", long)]
    /// spend extra time to find smallest diff
    flag_minimal: bool,
    #[structopt(name = "stat", long)]
    /// generate a diffstat
    flag_stat: bool,
    #[structopt(name = "numstat", long)]
    /// similar to --stat, but more machine friendly
    flag_numstat: bool,
    #[structopt(name = "shortstat", long)]
    /// only output last line of --stat
    flag_shortstat: bool,
    #[structopt(name = "summary", long)]
    /// output condensed summary of header info
    flag_summary: bool,
    #[structopt(name = "find-renames", short = "M", long)]
    /// set threshold for finding renames (default 50)
    flag_find_renames: Option<u16>,
    #[structopt(name = "find-copies", short = "C", long)]
    /// set threshold for finding copies (default 50)
    flag_find_copies: Option<u16>,
    #[structopt(name = "find-copies-harder", long)]
    /// inspect unmodified files for sources of copies
    flag_find_copies_harder: bool,
    #[structopt(name = "break_rewrites", short = "B", long)]
    /// break complete rewrite changes into pairs
    flag_break_rewrites: bool,
    #[structopt(name = "unified", short = "U", long)]
    /// lints of context to show
    flag_unified: Option<u32>,
    #[structopt(name = "inter-hunk-context", long)]
    /// maximum lines of change between hunks
    flag_inter_hunk_context: Option<u32>,
    #[structopt(name = "abbrev", long)]
    /// length to abbreviate commits to
    flag_abbrev: Option<u16>,
    #[structopt(name = "src-prefix", long)]
    /// show given source prefix instead of 'a/'
    flag_src_prefix: Option<String>,
    #[structopt(name = "dst-prefix", long)]
    /// show given destination prefix instead of 'b/'
    flag_dst_prefix: Option<String>,
    #[structopt(name = "path", long = "git-dir")]
    /// path to git repository to use
    flag_git_dir: Option<String>,
}

const RESET: &str = "\u{1b}[m";
const BOLD: &str = "\u{1b}[1m";
const RED: &str = "\u{1b}[31m";
const GREEN: &str = "\u{1b}[32m";
const CYAN: &str = "\u{1b}[36m";

#[derive(PartialEq, Eq, Copy, Clone)]
enum Cache {
    Normal,
    Only,
    None,
}

fn line_color(line: &DiffLine) -> Option<&'static str> {
    match line.origin() {
        '+' => Some(GREEN),
        '-' => Some(RED),
        '>' => Some(GREEN),
        '<' => Some(RED),
        'F' => Some(BOLD),
        'H' => Some(CYAN),
        _ => None,
    }
}

fn print_diff_line(
    _delta: DiffDelta,
    _hunk: Option<DiffHunk>,
    line: DiffLine,
    args: &Args,
) -> bool {
    if args.color() {
        print!("{}", RESET);
        if let Some(color) = line_color(&line) {
            print!("{}", color);
        }
    }
    match line.origin() {
        '+' | '-' | ' ' => print!("{}", line.origin()),
        _ => {}
    }
    print!("{}", str::from_utf8(line.content()).unwrap());
    true
}

fn run(args: &Args) -> Result<(), Error> {
    let path = args.flag_git_dir.as_ref().map(|s| &s[..]).unwrap_or(".");
    let repo = Repository::open(path)?;

    // Prepare our diff options based on the arguments given
    let mut opts = DiffOptions::new();
    opts.reverse(args.flag_R)
        .force_text(args.flag_text)
        .ignore_whitespace_eol(args.flag_ignore_space_at_eol)
        .ignore_whitespace_change(args.flag_ignore_space_change)
        .ignore_whitespace(args.flag_ignore_all_space)
        .include_ignored(args.flag_ignored)
        .include_untracked(args.flag_untracked)
        .patience(args.flag_patience)
        .minimal(args.flag_minimal);
    if let Some(amt) = args.flag_unified {
        opts.context_lines(amt);
    }
    if let Some(amt) = args.flag_inter_hunk_context {
        opts.interhunk_lines(amt);
    }
    if let Some(amt) = args.flag_abbrev {
        opts.id_abbrev(amt);
    }
    if let Some(ref s) = args.flag_src_prefix {
        opts.old_prefix(&s);
    }
    if let Some(ref s) = args.flag_dst_prefix {
        opts.new_prefix(&s);
    }
    if let Some("diff-index") = args.flag_format.as_ref().map(|s| &s[..]) {
        opts.id_abbrev(40);
    }

    if args.flag_blobs {
        let b1 = resolve_blob(&repo, args.arg_from_oid.as_ref())?;
        let b2 = resolve_blob(&repo, args.arg_to_oid.as_ref())?;
        repo.diff_blobs(
            b1.as_ref(),
            None,
            b2.as_ref(),
            None,
            Some(&mut opts),
            None,
            None,
            None,
            Some(&mut |d, h, l| print_diff_line(d, h, l, args)),
        )?;
        if args.color() {
            print!("{}", RESET);
        }
        return Ok(());
    }

    // Prepare the diff to inspect
    let t1 = tree_to_treeish(&repo, args.arg_from_oid.as_ref())?;
    let t2 = tree_to_treeish(&repo, args.arg_to_oid.as_ref())?;
    let head = tree_to_treeish(&repo, Some(&"HEAD".to_string()))?.unwrap();
    let mut diff = match (t1, t2, args.cache()) {
        (Some(t1), Some(t2), _) => {
            repo.diff_tree_to_tree(t1.as_tree(), t2.as_tree(), Some(&mut opts))?
        }
        (t1, None, Cache::None) => {
            let t1 = t1.unwrap_or(head);
            repo.diff_tree_to_workdir(t1.as_tree(), Some(&mut opts))?
        }
        (t1, None, Cache::Only) => {
            let t1 = t1.unwrap_or(head);
            repo.diff_tree_to_index(t1.as_tree(), None, Some(&mut opts))?
        }
        (Some(t1), None, _) => {
            repo.diff_tree_to_workdir_with_index(t1.as_tree(), Some(&mut opts))?
        }
        (None, None, _) => repo.diff_index_to_workdir(None, Some(&mut opts))?,
        (None, Some(_), _) => unreachable!(),
    };

    // Apply rename and copy detection if requested
    if args.flag_break_rewrites
        || args.flag_find_copies_harder
        || args.flag_find_renames.is_some()
        || args.flag_find_copies.is_some()
    {
        let mut opts = DiffFindOptions::new();
        if let Some(t) = args.flag_find_renames {
            opts.rename_threshold(t);
            opts.renames(true);
        }
        if let Some(t) = args.flag_find_copies {
            opts.copy_threshold(t);
            opts.copies(true);
        }
        opts.copies_from_unmodified(args.flag_find_copies_harder)
            .rewrites(args.flag_break_rewrites);
        diff.find_similar(Some(&mut opts))?;
    }

    // Generate simple output
    let stats = args.flag_stat | args.flag_numstat | args.flag_shortstat | args.flag_summary;
    if stats {
        print_stats(&diff, args)?;
    }
    if args.flag_patch || !stats {
        diff.print(args.diff_format(), |d, h, l| print_diff_line(d, h, l, args))?;
        if args.color() {
            print!("{}", RESET);
        }
    }

    Ok(())
}

fn print_stats(diff: &Diff, args: &Args) -> Result<(), Error> {
    let stats = diff.stats()?;
    let mut format = git2::DiffStatsFormat::NONE;
    if args.flag_stat {
        format |= git2::DiffStatsFormat::FULL;
    }
    if args.flag_shortstat {
        format |= git2::DiffStatsFormat::SHORT;
    }
    if args.flag_numstat {
        format |= git2::DiffStatsFormat::NUMBER;
    }
    if args.flag_summary {
        format |= git2::DiffStatsFormat::INCLUDE_SUMMARY;
    }
    let buf = stats.to_buf(format, 80)?;
    print!("{}", str::from_utf8(&*buf).unwrap());
    Ok(())
}

fn tree_to_treeish<'a>(
    repo: &'a Repository,
    arg: Option<&String>,
) -> Result<Option<Object<'a>>, Error> {
    let arg = match arg {
        Some(s) => s,
        None => return Ok(None),
    };
    let obj = repo.revparse_single(arg)?;
    let tree = obj.peel(ObjectType::Tree)?;
    Ok(Some(tree))
}

fn resolve_blob<'a>(repo: &'a Repository, arg: Option<&String>) -> Result<Option<Blob<'a>>, Error> {
    let arg = match arg {
        Some(s) => Oid::from_str(s)?,
        None => return Ok(None),
    };
    repo.find_blob(arg).map(|b| Some(b))
}

impl Args {
    fn cache(&self) -> Cache {
        if self.flag_cached {
            Cache::Only
        } else if self.flag_nocached {
            Cache::None
        } else {
            Cache::Normal
        }
    }
    fn color(&self) -> bool {
        self.flag_color && !self.flag_no_color
    }
    fn diff_format(&self) -> DiffFormat {
        if self.flag_patch {
            DiffFormat::Patch
        } else if self.flag_name_only {
            DiffFormat::NameOnly
        } else if self.flag_name_status {
            DiffFormat::NameStatus
        } else if self.flag_raw {
            DiffFormat::Raw
        } else {
            match self.flag_format.as_ref().map(|s| &s[..]) {
                Some("name") => DiffFormat::NameOnly,
                Some("name-status") => DiffFormat::NameStatus,
                Some("raw") => DiffFormat::Raw,
                Some("diff-index") => DiffFormat::Raw,
                _ => DiffFormat::Patch,
            }
        }
    }
}

fn main() {
    let args = Args::from_args();
    match run(&args) {
        Ok(()) => {}
        Err(e) => println!("error: {}", e),
    }
}