Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ cherry-pick and an undo all stop here instead of being thrown away.
- [x] A terminal under the graph: a shell per tab, in the repository's
folder and as the profile in use, and the graph keeps up with what
is typed into it
- [x] The window keeps up by itself with what an editor, a terminal or an
agent does to the repository in front, with no button to press
- [ ] Three-way editor with line-by-line picking
- [ ] A screen for the submodules themselves, bisect, LFS, signing

Expand Down Expand Up @@ -178,6 +180,7 @@ crates/
prefs_view.rs the preferences: one column of rows, and no Save button
terminal.rs a shell per tab: the pseudo-terminal, the keys, the colours
terminal_view.rs the grid painted, the mouse heard, and the bar above it
watch.rs looking at the repository in front, off the painting thread
chooser.rs asking the desktop for its own folder dialog
graph_view.rs painting the graph and the references column
board_view.rs the branches laid out in columns
Expand Down Expand Up @@ -387,8 +390,8 @@ goes out with are the profile's, read when the shell starts, because a
commit that went out under another client's name for having been typed
instead of clicked is the mistake profiles exist to stop. On a checkout on
another machine the shell is on that machine, reached with `ssh` and the
key the checkout was opened with. **The graph keeps up.** Nothing here watches the disk, so the
window looks when the shell goes quiet: at the branches and HEAD every
key the checkout was opened with. **The graph keeps up** sooner than the rest of the window does: it
looks when the shell goes quiet, without waiting for the next look: at the branches and HEAD every
time, at the working tree after a line has been sent in, and it reads
everything again only when one of them has actually moved -- `git commit`
in the panel puts a commit in the graph, and `ls` does not take away the
Expand All @@ -405,6 +408,31 @@ among them, which come out as boxes here as they do in a commit message.
Run on Linux, the shell on another machine against a server in a
container; read out of the code and not seen on macOS or Windows.

**Keeping up:** a repository is not only changed from here. An editor
saves a file, a terminal makes a commit, an agent does both for an hour,
and a window still showing what was true when it last read has to be told
to look by somebody who knows it is stale. GitKraken is told by the
system: it has every folder of the working tree watched, waits two seconds
for the events to stop, and reads again the part they were about -- which
comes with its three messages for when the watcher would not start, ran
out of watches or heard nothing. This looks instead. A thread that is not
the one that paints walks the repository in front the way `git status`
does, every two seconds while the window is the one in use and every five
while it is not, and at once when the window or a tab is come back to; a
walk that takes long is made less often, so that looking is never more
than a twentieth of the time. What it finds is two numbers, one for what
Git keeps and one for the working tree, and the window reads again only
the half that is not the one it is showing: a saved file is the list of
changes and nothing else, so the message being written stays where it is,
and a branch that moved is the whole history. A look that finds nothing
costs the window nothing, and what the window has just done itself is not
news to it. No dependency, no limit to run out of, and the same on all
three systems -- against two seconds' wait where a watcher would have
waited two seconds anyway. Only the repository in front; the others are
looked at when they are brought forward. Not a checkout on another
machine, where every look is a round trip on a connection the window is
using: its reload button is where it was. Run on Linux.

**The emulator:** the second dependency that was not part of the plan,
and the one there was never going to be a way round. What a shell writes
is forty years of escape sequences -- the second screen `vim` draws on,
Expand Down
2 changes: 1 addition & 1 deletion crates/gitrust-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ pub use settings::{
};
pub use stash::StashEntry;
pub use submodule::Sub;
pub use status::{ChangeKind, FileChange, WorkingState};
pub use status::{ChangeKind, FileChange, Pulse, WorkingState};
pub use worktree::Worktree;
pub use writer::Writer;
94 changes: 92 additions & 2 deletions crates/gitrust-core/src/repo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Opening the repository, reading commits, and working on the index.

use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
Expand All @@ -12,7 +14,7 @@ use git2::{
use crate::detail::{CommitDetail, CommitFile, RefEntry};
use crate::diff::{DiffLine, FileDiff, Hunk, HunkId};
use crate::model::{Commit, RefKind, RefLabel};
use crate::status::{staged_kind, unstaged_kind, ChangeKind, FileChange, WorkingState};
use crate::status::{staged_kind, unstaged_kind, ChangeKind, FileChange, Pulse, WorkingState};

pub struct Repo {
pub(crate) inner: Repository,
Expand Down Expand Up @@ -194,6 +196,36 @@ impl Repo {
/// The changed files, split between index and working tree. One file can
/// show up on both sides if it is half staged.
pub fn status(&self) -> Result<WorkingState> {
Ok(self.work()?.0)
}

/// The changed files, and the pulse they were read at.
///
/// For whoever shows them: the pulse kept beside the list is what a
/// later look is compared with, and it has to be of this reading and
/// not of one made a moment afterwards.
pub fn status_and_pulse(&self) -> Result<(WorkingState, Pulse)> {
let (state, tree) = self.work()?;
Ok((state, Pulse { git: self.git_pulse(), tree }))
}

/// Two numbers that stay the same for as long as the repository does.
///
/// What a window is compared with to know whether it is out of date,
/// without reading the history to find out. See [`Pulse`].
pub fn pulse(&self) -> Result<Pulse> {
Ok(self.status_and_pulse()?.1)
}

/// One walk through the working tree for both: the list, and a number
/// made out of it.
///
/// The number is more than the list. A file that was modified and is
/// modified again is the same line in the list and a different diff
/// behind it, so what goes in beside each path is when the file was
/// last written and how long it is, and for the index's side which
/// blob is staged.
fn work(&self) -> Result<(WorkingState, u64)> {
let mut options = StatusOptions::new();
options
.include_untracked(true)
Expand All @@ -204,24 +236,82 @@ impl Repo {

let statuses = self.inner.statuses(Some(&mut options))?;
let mut state = WorkingState::default();
let mut pulse = DefaultHasher::new();
let folder = self.inner.workdir();

for entry in statuses.iter() {
let Some(path) = entry.path() else {
continue;
};
let status = entry.status();
path.hash(&mut pulse);
status.bits().hash(&mut pulse);

if let Some(kind) = staged_kind(status) {
state.staged.push(FileChange { path: path.to_string(), kind, staged: true });
if let Some(staged) = entry.head_to_index() {
staged.new_file().id().as_bytes().hash(&mut pulse);
}
}
if let Some(kind) = unstaged_kind(status) {
state.unstaged.push(FileChange { path: path.to_string(), kind, staged: false });
// A file that cannot be asked -- it has just gone -- says
// nothing, and the next look says the rest.
if let Some(about) = folder.and_then(|folder| std::fs::symlink_metadata(folder.join(path)).ok()) {
about.len().hash(&mut pulse);
about.modified().ok().hash(&mut pulse);
}
}
}

state.staged.sort_by(|a, b| a.path.cmp(&b.path));
state.unstaged.sort_by(|a, b| a.path.cmp(&b.path));
Ok(state)
Ok((state, pulse.finish()))
}

/// A number made of everything Git itself keeps: where HEAD is, every
/// reference and what it points at, the stashes, and what is half-done.
///
/// The references are put in order first. `git pack-refs` moves them
/// from one file to another and changes nothing, and it should not look
/// like something happened.
///
/// Cheap beside the other half, which walks the working tree, and so
/// it can be asked for on its own by whoever only needs to know
/// whether the history has to be read again.
pub fn git_pulse(&self) -> u64 {
let mut pulse = DefaultHasher::new();
self.head_name().hash(&mut pulse);
self.head_id().map(|oid| oid.as_bytes().to_vec()).hash(&mut pulse);
(self.inner.state() as u32).hash(&mut pulse);

let mut references: Vec<(Vec<u8>, Vec<u8>)> = self
.inner
.references()
.map(|references| {
references
.flatten()
.map(|reference| {
let target = match reference.target() {
Some(oid) => oid.as_bytes().to_vec(),
None => reference.symbolic_target_bytes().unwrap_or_default().to_vec(),
};
(reference.name_bytes().to_vec(), target)
})
.collect()
})
.unwrap_or_default();
references.sort();
references.hash(&mut pulse);

// Dropping a stash from the middle leaves `refs/stash` where it
// was. How many there are is in its log.
self.inner
.reflog("refs/stash")
.map(|log| log.len())
.unwrap_or(0)
.hash(&mut pulse);
pulse.finish()
}

/// A file's diff. With `staged` it compares HEAD against the index; without
Expand Down
20 changes: 20 additions & 0 deletions crates/gitrust-core/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ impl WorkingState {
}
}

/// Two numbers that stay the same for as long as a repository does.
///
/// Nothing here is told when a file changes, so the window finds out by
/// looking, and what it compares is this: a number for what Git keeps and
/// a number for the working tree. They mean nothing in themselves and are
/// not kept from one run to the next -- two of them are only ever asked
/// whether they are equal.
///
/// Two, because they are answered differently. A branch that has moved is
/// the history read again. A file that was saved is the list of changes
/// read again and nothing else, and somebody halfway through a commit
/// message should not lose their place for having saved a file.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Pulse {
/// HEAD, the references, the stashes and what is half-done.
pub git: u64,
/// What has changed in the working tree and the index, and when.
pub tree: u64,
}

/// A change recorded in the index against HEAD.
pub(crate) fn staged_kind(status: Status) -> Option<ChangeKind> {
if status.contains(Status::INDEX_NEW) {
Expand Down
53 changes: 53 additions & 0 deletions crates/gitrust-core/tests/working_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,56 @@ fn discarding_a_hunk_of_a_new_file_says_to_discard_it_whole() {
.to_string();
assert!(err.contains("discard it whole"), "{err}");
}

/// What the window compares to know it is out of date: it moves when the
/// repository does, in the half that moved, and not otherwise.
#[test]
fn the_pulse_moves_with_the_repository_and_only_then() {
let temp = TempRepo::new("pulse");
temp.write("a.txt", "one\n");
let repo = temp.open();
repo.stage("a.txt").unwrap();
repo.commit("first").unwrap();

let clean = repo.pulse().unwrap();
// Looking changes nothing, and neither does another program's look.
assert_eq!(repo.pulse().unwrap(), clean);
assert_eq!(temp.open().pulse().unwrap(), clean);
let (state, read_at) = repo.status_and_pulse().unwrap();
assert!(state.is_clean());
assert_eq!(read_at, clean);

// A file saved by something else: the working tree's half and no other.
temp.write("a.txt", "one\ntwo\n");
let edited = temp.open().pulse().unwrap();
assert_eq!(edited.git, clean.git);
assert_ne!(edited.tree, clean.tree);

// Saved again: the same line in the list of changes, another diff
// behind it, and the pulse knows the difference.
temp.write("a.txt", "one\ntwo\nthree\n");
let again = repo.pulse().unwrap();
assert_eq!(repo.status().unwrap().unstaged.len(), 1);
assert_ne!(again.tree, edited.tree);

// Staged: still the working tree's half.
repo.stage("a.txt").unwrap();
let staged = repo.pulse().unwrap();
assert_eq!(staged.git, clean.git);
assert_ne!(staged.tree, again.tree);

// Committed from somewhere else: Git's half, and the tree is as clean
// as it was to begin with.
temp.open().commit("second").unwrap();
let committed = repo.pulse().unwrap();
assert_ne!(committed.git, clean.git);
assert_eq!(committed.tree, clean.tree);

// A branch made by another program is a reference that was not there.
let other = git2::Repository::open(temp.path()).unwrap();
let head = other.head().unwrap().peel_to_commit().unwrap();
other.branch("topic", &head, false).unwrap();
let branched = repo.pulse().unwrap();
assert_ne!(branched.git, committed.git);
assert_eq!(branched.tree, committed.tree);
}
Loading
Loading