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
#![doc = include_str!("../README.md")]

use filetime::FileTime;
use git2::{Diff, Oid, Repository};
use std::collections::{HashMap, HashSet};
use std::io::{Error, ErrorKind};
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use std::{env, error, fs, result};

#[cfg(feature = "cli")]
pub mod cli;

pub type Result<T> = result::Result<T, Box<dyn error::Error>>;

pub type FileSet = HashSet<PathBuf>;

/// Options passed to `reset_mtimes()`
#[derive(Clone, Debug)]
pub struct Options {
    paths: Option<FileSet>,
    dirty: bool,
    ignored: bool,
    verbose: bool,
}

/// foo
impl Default for Options {
    /// Return a new options strut with default values.
    fn default() -> Self {
        Self::new()
    }
}

impl Options {
    /// Return a set of default options.
    pub fn new() -> Options {
        Options {
            paths: None,
            dirty: false,
            ignored: false,
            verbose: false,
        }
    }

    /// Whether or not to touch locally modified files, default is false
    pub fn dirty(&self, flag: bool) -> Options {
        Options {
            paths: self.paths.clone(),
            dirty: flag,
            ignored: self.ignored,
            verbose: self.verbose,
        }
    }

    /// Whether or not to touch ignored files, default is false
    pub fn ignored(&self, flag: bool) -> Options {
        Options {
            paths: self.paths.clone(),
            dirty: self.dirty,
            ignored: flag,
            verbose: self.verbose,
        }
    }

    /// Whether or not to print output when touching or skipping files, default is false
    pub fn verbose(&self, flag: bool) -> Options {
        Options {
            paths: self.paths.clone(),
            dirty: self.dirty,
            ignored: self.ignored,
            verbose: flag,
        }
    }

    /// List of paths to operate on instead of scanning repository
    pub fn paths(&self, input: Option<FileSet>) -> Options {
        Options {
            paths: input,
            dirty: self.dirty,
            ignored: self.ignored,
            verbose: self.verbose,
        }
    }
}

/// Iterate over either the explicit file list or the working directory files, filter out any that
/// have local modifications, are ignored by Git, or are in submodules and reset the file metadata
/// mtime to the commit date of the last commit that affected the file in question.
pub fn reset_mtimes(repo: Repository, opts: Options) -> Result<FileSet> {
    let workdir_files = gather_workdir_files(&repo)?;
    let touchables: FileSet = match opts.paths {
        Some(ref paths) => {
            let not_tracked = paths.difference(&workdir_files);
            if not_tracked.clone().count() > 0 {
                let tracking_error =
                    format!("Paths {not_tracked:?} are not tracked in the repository");
                return Err(Box::new(Error::new(
                    ErrorKind::InvalidInput,
                    tracking_error,
                )));
            }
            workdir_files.intersection(paths).cloned().collect()
        }
        None => {
            let candidates = gather_index_files(&repo, &opts);
            workdir_files.intersection(&candidates).cloned().collect()
        }
    };
    let touched = process_touchables(&repo, touchables, &opts)?;
    Ok(touched)
}

/// Return a repository discovered from from the current working directory or $GIT_DIR settings.
pub fn get_repo() -> Result<Repository> {
    Ok(Repository::open_from_env()?)
}

/// Convert a path relative to the current working directory to be relative to the repository root
pub fn resolve_repo_path(repo: &Repository, path: &String) -> Result<PathBuf> {
    let cwd = env::current_dir()?;
    let root = repo
        .workdir()
        .ok_or("No Git working directory found")?
        .to_path_buf();
    let prefix = cwd.strip_prefix(&root).unwrap();
    let resolved_path = if Path::new(&path).is_absolute() {
        PathBuf::from(path.clone())
    } else {
        prefix.join(path.clone())
    };
    Ok(resolved_path)
}

fn gather_index_files(repo: &Repository, opts: &Options) -> FileSet {
    let mut candidates = FileSet::new();
    let mut status_options = git2::StatusOptions::new();
    status_options
        .include_unmodified(true)
        .exclude_submodules(true)
        .include_ignored(opts.ignored)
        .show(git2::StatusShow::IndexAndWorkdir);
    let statuses = repo.statuses(Some(&mut status_options)).unwrap();
    for entry in statuses.iter() {
        let path = entry.path().unwrap();
        match entry.status() {
            git2::Status::CURRENT => {
                candidates.insert(path.into());
            }
            git2::Status::INDEX_MODIFIED => {
                if opts.dirty {
                    candidates.insert(path.into());
                } else if opts.verbose {
                    println!("Ignored file with staged modifications: {path}");
                }
            }
            git2::Status::WT_MODIFIED => {
                if opts.dirty {
                    candidates.insert(path.into());
                } else if opts.verbose {
                    println!("Ignored file with local modifications: {path}");
                }
            }
            git_state => {
                if opts.verbose {
                    println!("Ignored file in state {git_state:?}: {path}");
                }
            }
        }
    }
    candidates
}

fn gather_workdir_files(repo: &Repository) -> Result<FileSet> {
    let mut workdir_files = FileSet::new();
    let head = repo.head()?;
    let tree = head.peel_to_tree()?;
    tree.walk(git2::TreeWalkMode::PostOrder, |dir, entry| {
        let file = format!("{}{}", dir, entry.name().unwrap());
        let path = Path::new(&file);
        if path.is_dir() {
            return git2::TreeWalkResult::Skip;
        }
        workdir_files.insert(file.into());
        git2::TreeWalkResult::Ok
    })
    .unwrap();
    Ok(workdir_files)
}

fn diff_affects_oid(diff: &Diff, oid: &Oid, touchable_path: &mut PathBuf) -> bool {
    diff.deltas().any(|delta| {
        delta.new_file().id() == *oid
            && delta
                .new_file()
                .path()
                .filter(|path| *path == touchable_path)
                .is_some()
    })
}

fn touch_if_older(path: PathBuf, time: i64, verbose: bool) -> bool {
    let commit_time = FileTime::from_unix_time(time, 0);
    let metadata = fs::metadata(&path).unwrap();
    let file_mtime = FileTime::from_last_modification_time(&metadata);
    if file_mtime != commit_time {
        filetime::set_file_mtime(&path, commit_time).unwrap();
        if verbose {
            let pathstring = path.clone().into_os_string().into_string().unwrap();
            println!("Rewound the clock: {pathstring}");
        }
        return true;
    }
    false
}

fn process_touchables(repo: &Repository, touchables: FileSet, opts: &Options) -> Result<FileSet> {
    let touched = Arc::new(RwLock::new(FileSet::new()));
    let mut touchable_oids: HashMap<Oid, PathBuf> = HashMap::new();
    let mut revwalk = repo.revwalk().unwrap();
    // See https://github.com/arkark/git-hist/blob/main/src/app/git.rs
    revwalk.push_head().unwrap();
    revwalk.simplify_first_parent().unwrap();
    let commits: Vec<_> = revwalk
        .map(|oid| oid.and_then(|oid| repo.find_commit(oid)).unwrap())
        .collect();
    let latest_tree = commits.first().unwrap().tree().unwrap();
    touchables.iter().for_each(|path| {
        let touchable_path = Path::new(&path).to_path_buf();
        let current_oid = latest_tree
            .get_path(&touchable_path)
            .and_then(|entry| {
                if let Some(git2::ObjectType::Blob) = entry.kind() {
                    Ok(entry)
                } else {
                    Err(git2::Error::new(
                        git2::ErrorCode::NotFound,
                        git2::ErrorClass::Tree,
                        "no blob",
                    ))
                }
            })
            .unwrap()
            .id();
        touchable_oids.insert(current_oid, touchable_path);
    });
    commits.iter().try_for_each(|commit| {
        let old_tree = commit.parent(0).and_then(|p| p.tree()).ok();
        let new_tree = commit.tree().ok();
        let mut diff = repo
            .diff_tree_to_tree(old_tree.as_ref(), new_tree.as_ref(), None)
            .unwrap();
        diff.find_similar(Some(git2::DiffFindOptions::new().renames(true)))
            .unwrap();
        touchable_oids.retain(|oid, touchable_path| {
            let affected = diff_affects_oid(&diff, oid, touchable_path);
            if affected {
                let time = commit.time().seconds();
                if touch_if_older(touchable_path.to_path_buf(), time, opts.verbose) {
                    touched
                        .write()
                        .unwrap()
                        .insert(touchable_path.to_path_buf());
                }
            };
            !affected
        });
        if !touchable_oids.is_empty() {
            Some(())
        } else {
            None
        }
    });
    let touched: RwLock<FileSet> = Arc::into_inner(touched).unwrap();
    let touched: FileSet = RwLock::into_inner(touched).unwrap();
    Ok(touched)
}