gitj 0.2.0

A gitk-style, Windows 3.1-flavored git repository browser and commit helper built on the Saudade toolkit
Documentation
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! The live [`RepoBackend`] backed by `git2` / libgit2.

use std::collections::HashMap;
use std::path::Path;

use git2::{Commit, Delta, DiffFormat, DiffLineType, DiffOptions, Oid, Repository, Sort};

use super::{
    ChangeStatus, CommitInfo, Diff, DiffLine, DiffLineKind, FileChange, RefKind, RefLabel,
    RepoBackend, WorkingStatus,
};

/// Opens a repository and reads commits/diffs through libgit2.
pub struct Git2Backend {
    path: String,
    repo: Repository,
    commits: Vec<CommitInfo>,
}

impl Git2Backend {
    /// Open the repository at (or above) `path` and load its commit history.
    pub fn open(path: impl AsRef<str>) -> Result<Self, git2::Error> {
        let path = path.as_ref().to_string();
        let repo = Repository::discover(&path)?;
        // Prefer the repository's working-directory path for display; fall
        // back to the .git dir for bare repos.
        let display_path = repo
            .workdir()
            .map(|p| p.display().to_string())
            .unwrap_or_else(|| repo.path().display().to_string());

        let refs = collect_refs(&repo)?;
        let commits = load_commits(&repo, &refs)?;

        Ok(Self {
            path: display_path,
            repo,
            commits,
        })
    }

    fn commit_at(&self, index: usize) -> Option<Commit<'_>> {
        let info = self.commits.get(index)?;
        let oid = Oid::from_str(&info.id).ok()?;
        self.repo.find_commit(oid).ok()
    }

    /// Build a libgit2 diff for a commit against its first parent, optionally
    /// restricted to a single path. Renames are detected so the file list can
    /// show `old -> new`.
    fn build_diff(&self, index: usize, path: Option<&str>) -> Option<git2::Diff<'_>> {
        let commit = self.commit_at(index)?;
        let new_tree = commit.tree().ok()?;
        let parent_tree = commit.parent(0).ok().and_then(|p| p.tree().ok());

        let mut opts = DiffOptions::new();
        opts.context_lines(3);
        if let Some(path) = path {
            opts.pathspec(path);
        }

        let mut diff = self
            .repo
            .diff_tree_to_tree(parent_tree.as_ref(), Some(&new_tree), Some(&mut opts))
            .ok()?;
        // Detect renames/copies so statuses and headers are accurate.
        let _ = diff.find_similar(None);
        Some(diff)
    }

    /// The tree the staged side is diffed against: `HEAD`'s tree normally, or
    /// `HEAD`'s parent's tree when amending. `None` means "no base" (unborn
    /// `HEAD`, or amending the root commit) — the index is then diffed against
    /// the empty tree, so everything reads as additions.
    fn staged_base_tree(&self, amend: bool) -> Option<git2::Tree<'_>> {
        let head = self.repo.head().ok()?.peel_to_commit().ok()?;
        if amend {
            head.parent(0).ok().and_then(|p| p.tree().ok())
        } else {
            head.tree().ok()
        }
    }
}

impl RepoBackend for Git2Backend {
    fn path(&self) -> &str {
        &self.path
    }

    fn commits(&self) -> &[CommitInfo] {
        &self.commits
    }

    fn changed_files(&self, index: usize) -> Vec<FileChange> {
        let Some(diff) = self.build_diff(index, None) else {
            return Vec::new();
        };
        diff.deltas()
            .map(|delta| file_change_from_delta(&delta))
            .collect()
    }

    fn commit_diff(&self, index: usize) -> Diff {
        self.build_diff(index, None)
            .map(render_diff)
            .unwrap_or_default()
    }

    fn file_diff(&self, index: usize, path: &str) -> Diff {
        self.build_diff(index, Some(path))
            .map(render_diff)
            .unwrap_or_default()
    }

    fn working_status(&self, amend: bool) -> WorkingStatus {
        let base = self.staged_base_tree(amend);

        // Staged side: the index against the base tree (HEAD, or HEAD's parent
        // when amending). With no base (unborn / amending the root commit) the
        // whole index reads as additions.
        let mut staged_opts = DiffOptions::new();
        let mut staged = WorkingStatus::default();
        if let Ok(mut diff) =
            self.repo
                .diff_tree_to_index(base.as_ref(), None, Some(&mut staged_opts))
        {
            let _ = diff.find_similar(None);
            for delta in diff.deltas() {
                staged.staged.push(file_change_from_delta(&delta));
            }
        }

        // Unstaged side: the working tree against the index (independent of
        // the amend base), including untracked files.
        let mut wd_opts = DiffOptions::new();
        wd_opts.include_untracked(true).recurse_untracked_dirs(true);
        if let Ok(diff) = self.repo.diff_index_to_workdir(None, Some(&mut wd_opts)) {
            for delta in diff.deltas() {
                staged.unstaged.push(file_change_from_delta(&delta));
            }
        }

        staged
    }

    fn working_diff(&self, path: &str, staged: bool, amend: bool) -> Diff {
        let mut opts = DiffOptions::new();
        opts.context_lines(3).pathspec(path);
        let diff = if staged {
            let base = self.staged_base_tree(amend);
            self.repo
                .diff_tree_to_index(base.as_ref(), None, Some(&mut opts))
        } else {
            opts.include_untracked(true)
                .recurse_untracked_dirs(true)
                .show_untracked_content(true);
            self.repo.diff_index_to_workdir(None, Some(&mut opts))
        };
        diff.ok().map(render_diff).unwrap_or_default()
    }

    fn stage(&self, path: &str) -> Result<(), String> {
        let mut index = self.repo.index().map_err(err_msg)?;
        let p = Path::new(path);
        let in_workdir = self
            .repo
            .workdir()
            .map(|w| w.join(path).exists())
            .unwrap_or(false);
        if in_workdir {
            index.add_path(p).map_err(err_msg)?;
        } else {
            // The file is gone from the working tree — stage its removal.
            index.remove_path(p).map_err(err_msg)?;
        }
        index.write().map_err(err_msg)
    }

    fn unstage(&self, path: &str, amend: bool) -> Result<(), String> {
        // Reset the index entry to the staged base: HEAD normally, HEAD's
        // parent when amending (which drops the path from the amended commit).
        let head = self.repo.head().ok().and_then(|h| h.peel_to_commit().ok());
        let target: Option<git2::Object> = match (amend, head) {
            (false, Some(commit)) => Some(commit.into_object()),
            (true, Some(commit)) => commit.parent(0).ok().map(|p| p.into_object()),
            (_, None) => None,
        };
        match target {
            Some(obj) => self.repo.reset_default(Some(&obj), [path]).map_err(err_msg),
            // No base commit (unborn HEAD, or amending the root commit):
            // unstaging just drops the path back out of the index.
            None => {
                let mut index = self.repo.index().map_err(err_msg)?;
                index.remove_path(Path::new(path)).map_err(err_msg)?;
                index.write().map_err(err_msg)
            }
        }
    }

    fn revert(&self, path: &str) -> Result<(), String> {
        // Rewrite the working-tree file from the index, overwriting any
        // unstaged edits. `update_index(false)` leaves the index untouched, so
        // a partially-staged file keeps its staged changes — only the
        // working-vs-index delta is discarded. An untracked path has no index
        // entry, so the checkout simply skips it.
        let mut opts = git2::build::CheckoutBuilder::new();
        opts.force().update_index(false).path(path);
        self.repo
            .checkout_index(None, Some(&mut opts))
            .map_err(err_msg)
    }

    fn delete_untracked(&self, path: &str) -> Result<(), String> {
        let workdir = self
            .repo
            .workdir()
            .ok_or_else(|| "bare repository has no working tree".to_string())?;
        std::fs::remove_file(workdir.join(path)).map_err(|e| e.to_string())
    }

    fn apply_to_index(&self, patch: &str) -> Result<(), String> {
        let diff = git2::Diff::from_buffer(patch.as_bytes()).map_err(err_msg)?;
        self.repo
            .apply(&diff, git2::ApplyLocation::Index, None)
            .map_err(err_msg)
    }

    fn commit(&self, message: &str, amend: bool) -> Result<(), String> {
        if message.trim().is_empty() {
            return Err("Please enter a commit message.".into());
        }
        let mut index = self.repo.index().map_err(err_msg)?;
        let tree_oid = index.write_tree().map_err(err_msg)?;
        let tree = self.repo.find_tree(tree_oid).map_err(err_msg)?;

        if amend {
            let head = self
                .repo
                .head()
                .and_then(|h| h.peel_to_commit())
                .map_err(err_msg)?;
            // Keep the original author/committer; only the message and tree
            // change. (`None` tells libgit2 to reuse the existing values.)
            head.amend(Some("HEAD"), None, None, None, Some(message), Some(&tree))
                .map_err(err_msg)?;
        } else {
            let sig = self.repo.signature().map_err(|_| {
                "No git identity configured. Set user.name and user.email.".to_string()
            })?;
            let parent = self.repo.head().ok().and_then(|h| h.peel_to_commit().ok());
            let parents: Vec<&Commit> = parent.iter().collect();
            self.repo
                .commit(Some("HEAD"), &sig, &sig, message, &tree, &parents)
                .map_err(err_msg)?;
        }
        Ok(())
    }

    fn head_message(&self) -> Option<String> {
        let commit = self.repo.head().ok()?.peel_to_commit().ok()?;
        Some(commit.message().unwrap_or("").to_string())
    }

    fn signature(&self) -> Option<(String, String)> {
        let sig = self.repo.signature().ok()?;
        Some((sig.name()?.to_string(), sig.email()?.to_string()))
    }
}

/// Map a libgit2 diff delta to our [`FileChange`], collapsing the old path for
/// non-rename changes.
fn file_change_from_delta(delta: &git2::DiffDelta) -> FileChange {
    let new_path = delta.new_file().path().map(|p| p.display().to_string());
    let old_path = delta.old_file().path().map(|p| p.display().to_string());
    let status = status_from_delta(delta.status());
    let path = new_path
        .clone()
        .or_else(|| old_path.clone())
        .unwrap_or_default();
    FileChange {
        path,
        old_path: old_path.filter(|o| Some(o) != new_path.as_ref()),
        status,
    }
}

/// Render a libgit2 error as a short message for the UI's dialog.
fn err_msg(e: git2::Error) -> String {
    e.message().to_string()
}

/// Walk all references once and group branch/tag labels by the commit they
/// resolve to. The currently checked-out branch is tagged [`RefKind::Head`];
/// a detached HEAD becomes a [`RefKind::DetachedHead`] label.
fn collect_refs(repo: &Repository) -> Result<HashMap<Oid, Vec<RefLabel>>, git2::Error> {
    let mut map: HashMap<Oid, Vec<RefLabel>> = HashMap::new();

    let head = repo.head().ok();
    let head_branch = head
        .as_ref()
        .filter(|h| h.is_branch())
        .and_then(|h| h.shorthand())
        .map(str::to_string);
    let detached = repo.head_detached().unwrap_or(false);

    if detached && let Some(oid) = head.as_ref().and_then(|h| h.target()) {
        map.entry(oid).or_default().push(RefLabel {
            name: "HEAD".into(),
            kind: RefKind::DetachedHead,
        });
    }

    if let Ok(references) = repo.references() {
        for reference in references.flatten() {
            let Ok(commit) = reference.peel_to_commit() else {
                continue;
            };
            let oid = commit.id();
            let Some(name) = reference.shorthand().map(str::to_string) else {
                continue;
            };
            let kind = if reference.is_tag() {
                RefKind::Tag
            } else if reference.is_remote() {
                // Skip the synthetic origin/HEAD pointer; it's noise.
                if name.ends_with("/HEAD") {
                    continue;
                }
                RefKind::RemoteBranch
            } else if reference.is_branch() {
                if head_branch.as_deref() == Some(name.as_str()) {
                    RefKind::Head
                } else {
                    RefKind::LocalBranch
                }
            } else {
                continue;
            };
            map.entry(oid).or_default().push(RefLabel { name, kind });
        }
    }

    // Stable, readable ordering: HEAD/branch first, remotes, then tags.
    for labels in map.values_mut() {
        labels.sort_by_key(|l| match l.kind {
            RefKind::Head | RefKind::DetachedHead => 0,
            RefKind::LocalBranch => 1,
            RefKind::RemoteBranch => 2,
            RefKind::Tag => 3,
        });
    }

    Ok(map)
}

/// Run a reverse-topological, newest-first revwalk from every ref tip and
/// build a [`CommitInfo`] per commit.
fn load_commits(
    repo: &Repository,
    refs: &HashMap<Oid, Vec<RefLabel>>,
) -> Result<Vec<CommitInfo>, git2::Error> {
    let mut revwalk = repo.revwalk()?;
    revwalk.set_sorting(Sort::TIME | Sort::TOPOLOGICAL)?;
    // Show history reachable from all branches/tags, not just HEAD, so the
    // browser behaves like `gitk --all`.
    if revwalk.push_glob("refs/heads/*").is_err() {
        let _ = revwalk.push_head();
    }
    let _ = revwalk.push_glob("refs/remotes/*");
    let _ = revwalk.push_glob("refs/tags/*");
    let _ = revwalk.push_head();

    let mut commits = Vec::new();
    for oid in revwalk {
        let oid = oid?;
        let commit = repo.find_commit(oid)?;
        commits.push(commit_info(&commit, refs));
    }
    Ok(commits)
}

fn commit_info(commit: &Commit, refs: &HashMap<Oid, Vec<RefLabel>>) -> CommitInfo {
    let id = commit.id().to_string();
    let short_id = id.chars().take(8).collect();
    let message = commit.message().unwrap_or("").to_string();
    let summary = commit
        .summary()
        .map(str::to_string)
        .unwrap_or_else(|| message.lines().next().unwrap_or("").to_string());
    let author = commit.author();
    let committer = commit.committer();
    let time = author.when();

    CommitInfo {
        short_id,
        summary,
        message,
        author_name: author.name().unwrap_or("").to_string(),
        author_email: author.email().unwrap_or("").to_string(),
        committer_name: committer.name().unwrap_or("").to_string(),
        committer_email: committer.email().unwrap_or("").to_string(),
        time_seconds: time.seconds(),
        time_offset_minutes: time.offset_minutes(),
        parents: commit.parent_ids().map(|p| p.to_string()).collect(),
        refs: refs.get(&commit.id()).cloned().unwrap_or_default(),
        id,
    }
}

fn status_from_delta(delta: Delta) -> ChangeStatus {
    match delta {
        Delta::Added => ChangeStatus::Added,
        Delta::Deleted => ChangeStatus::Deleted,
        Delta::Modified => ChangeStatus::Modified,
        Delta::Renamed => ChangeStatus::Renamed,
        Delta::Copied => ChangeStatus::Copied,
        Delta::Typechange => ChangeStatus::TypeChange,
        Delta::Untracked => ChangeStatus::Untracked,
        _ => ChangeStatus::Other,
    }
}

/// Drive libgit2's patch printer and translate each emitted line into a typed
/// [`DiffLine`]. Content/hunk/file-header lines keep their text; +/-/context
/// lines get their origin character prepended so the monospace view reads like
/// a real unified diff even before color is applied.
fn render_diff(diff: git2::Diff) -> Diff {
    let mut lines = Vec::new();
    let _ = diff.print(DiffFormat::Patch, |_delta, _hunk, line| {
        let content = String::from_utf8_lossy(line.content());
        let content = content.trim_end_matches('\n');
        match line.origin_value() {
            DiffLineType::FileHeader => {
                push_multiline(&mut lines, DiffLineKind::FileHeader, content)
            }
            DiffLineType::HunkHeader => {
                push_multiline(&mut lines, DiffLineKind::HunkHeader, content)
            }
            DiffLineType::Context => {
                lines.push(DiffLine::new(DiffLineKind::Context, format!(" {content}")))
            }
            DiffLineType::Addition => {
                lines.push(DiffLine::new(DiffLineKind::Addition, format!("+{content}")))
            }
            DiffLineType::Deletion => {
                lines.push(DiffLine::new(DiffLineKind::Deletion, format!("-{content}")))
            }
            DiffLineType::ContextEOFNL | DiffLineType::AddEOFNL | DiffLineType::DeleteEOFNL => {
                lines.push(DiffLine::new(DiffLineKind::Meta, content.to_string()))
            }
            _ => push_multiline(&mut lines, DiffLineKind::Meta, content),
        }
        true
    });
    Diff { lines }
}

fn push_multiline(out: &mut Vec<DiffLine>, kind: DiffLineKind, content: &str) {
    for line in content.split('\n') {
        out.push(DiffLine::new(kind, line.to_string()));
    }
}