aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: 7adfb2fb730a4b20a6c8bcb34cf1ed30e144bdba (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
// Copyright (c) 2020  Teddy Wing
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.


#![warn(rust_2018_idioms)]

use std::io::Write;
use std::path::{Path, PathBuf};

use git2::{DiffOptions, Repository, Tree};
use thiserror::Error;


#[derive(Error, Debug)]
pub enum Error {
    #[error(transparent)]
    Git(#[from] git2::Error),
}


pub struct Todos<'a> {
    pub repo: &'a Repository,
}

impl Todos<'_> {
    /// Write TODO lines.
    ///
    /// Writes TODO lines since `tree` to `write_to`.
    ///
    /// # Panics
    ///
    /// Panics if writing to `write_to` fails.
    pub fn write_since<W: Write>(
        &self,
        tree: Tree<'_>,
        write_to: &mut W,
    ) -> Result<(), Error> {
        let mut diff_options = DiffOptions::new();
        diff_options
            .show_untracked_content(true)
            .recurse_untracked_dirs(true);

        let diff = self.repo.diff_tree_to_workdir_with_index(
            Some(&tree),
            Some(&mut diff_options),
        )?;

        diff.foreach(
            &mut |_file, _progress| {
                true
            },
            None,
            None,
            Some(
                &mut |delta, _hunk, line| {
                    if let Some(line_number) = line.new_lineno() {
                        if let Ok(l) = std::str::from_utf8(line.content()) {
                            if l.contains("TODO") {
                                if let Some(path) = delta.new_file().path() {
                                    write!(
                                        write_to,
                                        "{}:{}:{}",
                                        relative_path(self.repo, path).display(),
                                        line_number,
                                        l,
                                    ).expect("write error");
                                }
                            }
                        }
                    }

                    true
                }
            ),
        )?;

        Ok(())
    }

    /// Get a Git tree for the master branch.
    pub fn master_tree(&self) -> Result<Tree<'_>, Error> {
        let master = self.repo.find_branch("master", git2::BranchType::Local)?;

        Ok(master.get().peel_to_tree()?)
    }
}

/// Get `path` relative to the current directory.
///
/// Attempt to remove the current directory prefix from `path` to turn it into a
/// relative path from the current directory.
fn relative_path(repo: &Repository, path: &Path) -> PathBuf {
    let workdir = match repo.workdir() {
        Some(d) => d,
        None => return path.to_path_buf(),
    };

    let current_dir = match std::env::current_dir() {
        Ok(d) => d,
        Err(_) => return path.to_path_buf(),
    };

    let current_dir_relative = match current_dir.strip_prefix(workdir) {
        Ok(d) => d,
        Err(_) => return path.to_path_buf(),
    };

    match path.strip_prefix(current_dir_relative) {
        Ok(d) => d.to_path_buf(),
        Err(_) => path.to_path_buf(),
    }
}