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
use std::{fmt, path::Path};

use crate::{
    diff,
    file::{self, File},
    Error, Result,
};
#[allow(unused)]
use tracing::{debug, error, info, instrument, span, warn};

pub struct Repo {
    pub(crate) internal: Internal,
    history: undo::History<Change>,
}

impl Repo {
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let internal = Internal::open(path)?;
        let history = undo::History::new();
        Ok(Self { internal, history })
    }

    pub fn can_undo(&self) -> bool {
        self.history.can_undo()
    }

    pub fn can_redo(&self) -> bool {
        self.history.can_redo()
    }

    pub fn undo(&mut self) -> Result<()> {
        self.history
            .undo(&mut self.internal)
            .ok_or(Error::UndoEmpty)
            .flatten()
    }

    pub fn redo(&mut self) -> Result<()> {
        self.history
            .redo(&mut self.internal)
            .ok_or(Error::RedoEmpty)
            .flatten()
    }

    pub fn path(&self) -> &Path {
        self.internal.path()
    }

    pub fn uncommitted_files(&self) -> Result<Vec<diff::Meta>> {
        self.internal.uncommitted_files()
    }

    pub fn diff_details(&self, diff: &diff::Meta) -> Result<diff::Details> {
        self.internal.diff_details(diff)
    }

    pub fn stage_file(&mut self, file: File) -> Result<()> {
        self.apply(Change::StageFile(file))
    }

    pub fn unstage_file(&mut self, file: File) -> Result<()> {
        self.apply(Change::UnstageFile(file))
    }

    pub fn stage_contents(&mut self, file: File, contents: file::Contents) -> Result<()> {
        self.apply(Change::StageContents(file, contents))
    }

    fn apply(&mut self, change: Change) -> Result<()> {
        self.history.apply(&mut self.internal, change)
    }
}

impl fmt::Debug for Repo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let history = format!("{}", self.history.display());
        f.debug_struct("Repo")
            .field("internal", &self.internal)
            .field("history", &history)
            .finish_non_exhaustive()
    }
}

#[derive(Debug, Clone)]
enum Change {
    StageFile(File),
    UnstageFile(File),
    StageContents(File, file::Contents),
}

impl undo::Action for Change {
    type Target = Internal;
    type Output = ();
    type Error = Error;

    fn apply(&mut self, target: &mut Self::Target) -> undo::Result<Self> {
        match self {
            Change::StageFile(file) => target.stage_file(file),
            Change::UnstageFile(file) => target.unstage_file(file),
            Change::StageContents(file, contents) => target.stage_contents(file, contents),
        }
    }

    fn undo(&mut self, target: &mut Self::Target) -> undo::Result<Self> {
        match self {
            Change::StageFile(file) => target.unstage_file(file),
            Change::UnstageFile(file) => target.stage_file(file),
            Change::StageContents(file, contents) => todo!(),
        }
    }
}

impl fmt::Display for Change {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

/// Internal manages everything that doesn't require history. This is so that
/// actions on the history can mutably borrow something that doesn't contain the
/// history itself.
pub(crate) struct Internal {
    git: git2::Repository,
}

impl Internal {
    fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let git = git2::Repository::open(path)?;
        Ok(Self { git })
    }

    pub(crate) fn path(&self) -> &Path {
        let path = self.git.path();
        if path.ends_with(".git") {
            path.parent().expect("If non-bare has parent")
        } else {
            path
        }
    }

    fn head_assuming_born(&self) -> std::result::Result<git2::Tree, git2::Error> {
        self.git.head()?.peel_to_commit()?.tree()
    }

    fn head(&self) -> Result<Option<git2::Tree>> {
        match self.head_assuming_born() {
            Ok(head) => Ok(Some(head)),
            Err(err) if err.code() == git2::ErrorCode::UnbornBranch => Ok(None),
            Err(err) => Err(err.into()),
        }
    }

    fn uncommitted_files(&self) -> Result<Vec<diff::Meta>> {
        let head = self.head()?;

        let mut opts = Self::uncommitted_opts();
        opts.include_unmodified(true); // needed for rename/copy tracking
        opts.recurse_untracked_dirs(true);

        let mut diff = self
            .git
            .diff_tree_to_workdir_with_index(head.as_ref(), Some(&mut opts))?;

        // Detect renames
        let mut opts = git2::DiffFindOptions::new();
        opts.renames(true)
            .copies(true)
            .copies_from_unmodified(true)
            .for_untracked(true)
            .ignore_whitespace(true)
            .break_rewrites_for_renames_only(true)
            // Don't keep them, since we only want for copy/rename tracking
            // If we included them we'd have to search them in `diff_details`
            .remove_unmodified(true);
        diff.find_similar(Some(&mut opts))?;

        diff.deltas()
            .map(|delta| diff::Meta::from_git2(&delta))
            .collect()
    }

    fn diff_details(&self, meta: &diff::Meta) -> Result<diff::Details> {
        match meta {
            crate::Meta::Added(f)
            | crate::Meta::Deleted(f)
            | crate::Meta::Modified { new: f, .. }
            | crate::Meta::Renamed { new: f, .. }
            | crate::Meta::Copied { new: f, .. }
            | crate::Meta::Ignored(f)
            | crate::Meta::Untracked(f)
            | crate::Meta::Typechange { new: f, .. }
            | crate::Meta::Unreadable(f)
            | crate::Meta::Conflicted { new: f, .. } => self._diff_details(f),
        }
    }

    fn _diff_details(&self, file: &File) -> Result<diff::Details> {
        let head = self.head()?;

        let mut opts = Self::uncommitted_opts();
        opts.pathspec(file.rel_path());

        let mut meta: Option<Result<diff::Meta>> = None;
        let mut file_cb = |delta: git2::DiffDelta<'_>, _progress| {
            if let Some(delta_path) = Self::delta_path(&delta) {
                if delta_path == file.rel_path() {
                    meta = Some(diff::Meta::from_git2(&delta));
                    return true;
                }
            }

            // NOTE: If we ask to stop once we get the target lines_cb isn't
            // called, so we exit on the first subsequent delta.

            meta.is_none()
        };

        let mut lines = vec![];
        let mut line_cb = |delta: git2::DiffDelta<'_>,
                           _hunk: Option<git2::DiffHunk<'_>>,
                           line: git2::DiffLine<'_>| {
            if let Some(delta_path) = Self::delta_path(&delta) {
                if delta_path == file.rel_path() {
                    let line = diff::Line::from_git2(&line);
                    lines.push(line);
                }
            }

            true
        };

        match self
            .git
            .diff_tree_to_workdir_with_index(head.as_ref(), Some(&mut opts))?
            .foreach(&mut file_cb, None, None, Some(&mut line_cb))
        {
            Ok(()) => (),
            Err(err) if err.code() == git2::ErrorCode::User => (),
            Err(err) => return Err(err.into()),
        }

        let meta = meta
            .ok_or_else(|| Error::PathNotFound(file.clone()))
            .flatten()?;

        Ok(diff::Details::new(meta, lines))
    }

    fn delta_path<'a, 'b>(delta: &'a git2::DiffDelta<'b>) -> Option<&'b Path> {
        delta
            .new_file()
            .path()
            .map_or_else(|| delta.old_file().path(), |delta_path| Some(delta_path))
    }

    fn uncommitted_opts() -> git2::DiffOptions {
        let mut opts = git2::DiffOptions::new();
        opts.include_untracked(true)
            .include_typechange(true)
            .include_unmodified(false)
            .include_unreadable(true)
            .include_untracked(true)
            .include_ignored(true);
        opts
    }

    fn stage_file(&mut self, file: &File) -> Result<()> {
        self.ensure_not_ignored(file)?;
        self.git.index()?.add_path(file.rel_path())?;

        Ok(())
    }

    fn unstage_file(&mut self, file: &File) -> Result<()> {
        self.ensure_not_ignored(file)?;
        self.git.index()?.remove_path(file.rel_path())?;
        Ok(())
    }

    fn stage_contents(&self, file: &File, contents: &file::Contents) -> Result<()> {
        self.ensure_not_ignored(file)?;

        // It appears we only have to fill out the path and mode
        // See <https://github.com/libgit2/libgit2/blob/508361401fbb5d87118045eaeae3356a729131aa/tests/index/filemodes.c#L179>
        let entry = git2::IndexEntry {
            ctime: git2::IndexTime::new(0, 0),
            mtime: git2::IndexTime::new(0, 0),
            dev: 0,
            ino: 0,
            mode: libgit2_sys::GIT_FILEMODE_BLOB,
            uid: 0,
            gid: 0,
            file_size: 0,
            id: git2::Oid::zero(),
            flags: 0,
            flags_extended: 0,
            path: file.rel_path_bytes()?.into_bytes(),
        };

        let mut index = self.git.index()?;
        index.add_frombuffer(&entry, contents.buffer())?;
        index.write()?;

        Ok(())
    }

    fn ensure_not_ignored(&self, file: &File) -> Result<()> {
        if self.git.status_should_ignore(file.rel_path())? {
            return Err(Error::Ignored(file.clone()));
        }
        Ok(())
    }
}

impl fmt::Debug for Internal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RepoInternal")
            .field("path", &self.git.path())
            .finish_non_exhaustive()
    }
}