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
8 changes: 5 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ There are two crates.
first); cloning and creating a repository are the two functions next to it. Using `git` itself keeps hooks, credentials, signing and config
working without code of our own.
- `status.rs` and `diff.rs` parse `git status --porcelain=v2`, what commits changed (also to one
file, through its renames) and unified diffs, and build the patches for staging single lines.
file, through its renames), who last changed each line of a file, and unified diffs, and build
the patches for staging single lines.
- `conflict.rs` splits a conflicted file at the markers git wrote into it, and puts it back
together with ours, theirs or both for each conflict.
- `watch.rs` watches the working tree and the refs, and says when something changed that git
Expand All @@ -82,8 +83,9 @@ Its tests run against real repositories made with `git` in a temporary folder
in the meantime runs next. The tab on screen also reloads by itself when its watcher saw a
change.
- `start.rs` (open, clone and create), `ui.rs` (tabs and toolbar), `graph.rs`, `sidebar.rs`, `details.rs`,
`diff.rs`, `history.rs` (a file's history), `merge.rs` (a conflicted file) and `rebase.rs` (an
interactive rebase) draw the views, and `theme.rs` has the colours.
`diff.rs`, `history.rs` (a file's history), `blame.rs` (who last changed each line),
`merge.rs` (a conflicted file) and `rebase.rs` (an interactive rebase) draw the views, and
`theme.rs` has the colours.

The app's tests (`crates/app/src/tests.rs`) drive the whole window the way a user does, through
the accessibility tree with [egui_kittest](https://crates.io/crates/egui_kittest). That also keeps
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ GitCraft is young. This is what it does today; [ROADMAP.md](ROADMAP.md) has what
- **File history**: right-click a file, in a commit or in your changes, for every commit that
changed it, newest first, also from before it was renamed. Pick one to see its diff of the file,
with the commit beside it. Following a file through a large history happens in the background.
- **Blame**: right-click a file for who last changed each line, and in which commit, beside the
lines. From your changes it is the file as it is now, with the lines you have not committed yet,
and it keeps up as you edit; from a commit it is the file in that commit. Click a line to see
its commit and every other line it changed.
- **Staging**: stage or unstage files, whole hunks or single lines, then commit or amend.
- **Discard**: throw away the changes to a file, a hunk or single lines, after you confirm.
- **Branches and tags**: create, check out, merge, rebase, delete, from the sidebar or a right click.
Expand Down
12 changes: 6 additions & 6 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ drive it the way a user does, and keeps every benchmark as fast as before.
another name or URL, or remove it
- [x] Search the graph by message, author or SHA, and jump to any branch or tag by its name
- [x] File history: the commits that changed a file, followed through renames, each with its diff
- [x] Blame: who last changed each line of a file, and in which commit

## Next

1. **Blame**: who last changed each line of a file, and in which commit.
2. **Better diffs**: side by side, word-level changes, syntax colouring, images.
3. **Keyboard**: shortcuts for everything, and a command palette.
4. **Settings**: light and dark theme, font size, date format, default pull mode.
5. **Graph polish**: resizable columns, compact lanes, author avatars (optional, never by default
1. **Better diffs**: side by side, word-level changes, syntax colouring, images.
2. **Keyboard**: shortcuts for everything, and a command palette.
3. **Settings**: light and dark theme, font size, date format, default pull mode.
4. **Graph polish**: resizable columns, compact lanes, author avatars (optional, never by default
over the network).
6. **Packaging**: installers with the icon for Windows (MSI), macOS (.app in a .dmg) and Linux
5. **Packaging**: installers with the icon for Windows (MSI), macOS (.app in a .dmg) and Linux
(AppImage, .deb, Flatpak), signed where the platform expects it.

## After 1.0
Expand Down
180 changes: 131 additions & 49 deletions crates/app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ pub enum Action {
},
/// Show the diff of the file in a commit of its history, by row, and the commit beside it.
Visit(usize),
/// Show who last changed each line of a file instead of the graph.
Blame {
/// The commit whose file it is, or `None` for the file in the working tree.
at: Option<String>,
/// The file.
path: String,
},
/// Back to the graph.
CloseFile,
/// Pick or unpick a line of the open diff for staging.
Expand Down Expand Up @@ -222,6 +229,39 @@ impl Merge {
/// Each commit that changed a file, newest first, with what it did to it.
type Changes = Vec<(ObjectId, FileChange)>;

/// Each line of a file, with the commit that last changed it.
type Lines = Vec<(ObjectId, String)>;

/// Something being read in the background for the centre, if it is: what was read, or what went
/// wrong.
type Reading<T> = Option<Receiver<Result<T, String>>>;

/// Runs `read` on a thread of its own, and wakes the window once it is done.
fn read_in_background<T: Send + 'static>(
ctx: &egui::Context,
read: impl FnOnce() -> Result<T> + Send + 'static,
) -> Receiver<Result<T, String>> {
let (send, reading) = mpsc::channel();
let ctx = ctx.clone();
std::thread::spawn(move || {
// Sending fails only when what it was read for was closed in the meantime.
let _ = send.send(read().map_err(|error| error.to_string()));
ctx.request_repaint();
});
reading
}

/// What was read in the background, once it is there.
fn take<T>(reading: &mut Reading<T>) -> Option<Result<T, String>> {
let read = match reading.as_ref()?.try_recv() {
Err(TryRecvError::Empty) => return None,
Ok(read) => read,
Err(TryRecvError::Disconnected) => Err("Reading stopped unexpectedly".to_owned()),
};
*reading = None;
Some(read)
}

/// The history of a file in the centre: the commits that changed it, above the diff of the one
/// picked. It is read in the background, because following a file through a large history takes
/// a while.
Expand All @@ -234,7 +274,7 @@ pub struct FileHistory {
/// Each commit that changed the file.
pub commits: Changes,
/// The history being read, if it is.
pub reading: Option<Receiver<Result<Changes, String>>>,
pub reading: Reading<Changes>,
}

impl FileHistory {
Expand All @@ -246,14 +286,31 @@ impl FileHistory {

/// Reads the history, in the background.
fn read(&mut self, ctx: &egui::Context, git: &Git) {
let (send, reading) = mpsc::channel();
let (git, ctx, at, path) = (git.clone(), ctx.clone(), self.at.clone(), self.path.clone());
std::thread::spawn(move || {
// Sending fails only when the history was closed in the meantime.
let _ = send.send(status::file_history(&git, &at, &path).map_err(|error| error.to_string()));
ctx.request_repaint();
});
self.reading = Some(reading);
let (git, at, path) = (git.clone(), self.at.clone(), self.path.clone());
self.reading = Some(read_in_background(ctx, move || status::file_history(&git, &at, &path)));
}
}

/// Who last changed each line of a file, in the centre. It is read in the background, because
/// git goes back through the history of the file for it.
#[derive(Debug)]
pub struct Blame {
/// The file.
pub path: String,
/// The commit whose file it is, or `None` for the file in the working tree, which is read
/// again after every change.
pub at: Option<String>,
/// Each line, with the commit that last changed it; the null id when it is not committed.
pub lines: Lines,
/// The blame being read, if it is.
pub reading: Reading<Lines>,
}

impl Blame {
/// Reads the blame, in the background.
fn read(&mut self, ctx: &egui::Context, git: &Git) {
let (git, at, path) = (git.clone(), self.at.clone(), self.path.clone());
self.reading = Some(read_in_background(ctx, move || status::blame(&git, at.as_deref(), &path)));
}
}

Expand Down Expand Up @@ -385,6 +442,8 @@ pub struct Workspace {
pub plan: Option<Plan>,
/// The history of a file in the centre, if any.
pub history: Option<FileHistory>,
/// Who last changed each line of a file, in the centre, if anyone.
pub blame: Option<Blame>,
/// The commit summary being written.
pub summary: String,
/// The rest of the commit message.
Expand Down Expand Up @@ -427,6 +486,7 @@ impl Workspace {
merge: None,
plan: None,
history: None,
blame: None,
summary: String::new(),
description: String::new(),
amend: false,
Expand Down Expand Up @@ -509,19 +569,23 @@ impl Workspace {
}
Action::Next(down) => self.reveal_row(ctx, self.search.next(self.selected_commit(), down)),
Action::Open(source) => {
self.history = None;
(self.history, self.blame) = (None, None);
self.open(source);
}
Action::History { at, path } => {
let mut history = FileHistory { path, at, commits: Vec::new(), reading: None };
self.close_centre();
let history = self.history.insert(FileHistory { path, at, commits: Vec::new(), reading: None });
history.read(ctx, &self.repo.git);
(self.open_file, self.merge, self.plan, self.history) = (None, None, None, Some(history));
}
Action::Visit(row) => self.visit(ctx, row),
Action::Blame { at, path } => {
self.close_centre();
self.blame.insert(Blame { path, at, lines: Vec::new(), reading: None }).read(ctx, &self.repo.git);
}
Action::Resolve(path) => match Conflicts::read(&self.repo.git, &path) {
Ok(conflicts) => {
(self.merge, self.open_file, self.plan, self.history) =
(Some(Merge::new(path, conflicts)), None, None, None);
self.close_centre();
self.merge = Some(Merge::new(path, conflicts));
}
Err(error) => self.error = Some(error.to_string()),
},
Expand All @@ -543,28 +607,12 @@ impl Workspace {
self.apply(ctx, Action::Shift(from, to));
}
}
Action::Step(row, step) => {
let Some(plan) = &mut self.plan else { return };
let id = plan.steps[row].1;
// A new message starts from the one it has.
if step == Step::Reword && !plan.messages.contains_key(&id) {
match self.commits.get(&self.repo, id) {
Ok(commit) => {
plan.messages.insert(id, commit.message.trim_end().to_owned());
}
Err(error) => {
self.error = Some(error.to_string());
return;
}
}
}
plan.steps[row].0 = step;
}
Action::Step(row, step) => self.step(row, step),
Action::Shift(from, to) => self.plan.iter_mut().for_each(|plan| {
let step = plan.steps.remove(from);
plan.steps.insert(to, step);
}),
Action::CloseFile => (self.open_file, self.merge, self.plan, self.history) = (None, None, None, None),
Action::CloseFile => self.close_centre(),
Action::ToggleLine(hunk, line) => {
if let Some(file) = &mut self.open_file
&& !file.lines.remove(&(hunk, line))
Expand All @@ -590,6 +638,30 @@ impl Workspace {
}
}

/// Sets what the rebase does with the commit in `row` of the plan.
fn step(&mut self, row: usize, step: Step) {
let Some(plan) = &mut self.plan else { return };
let id = plan.steps[row].1;
// A new message starts from the one it has.
if step == Step::Reword && !plan.messages.contains_key(&id) {
match self.commits.get(&self.repo, id) {
Ok(commit) => {
plan.messages.insert(id, commit.message.trim_end().to_owned());
}
Err(error) => {
self.error = Some(error.to_string());
return;
}
}
}
plan.steps[row].0 = step;
}

/// Back to the graph in the centre.
fn close_centre(&mut self) {
(self.open_file, self.merge, self.plan, self.history, self.blame) = (None, None, None, None, None);
}

/// Shows the diff of a file in the centre.
fn open(&mut self, source: Source) {
match diff::read(&self.repo.git, &source) {
Expand All @@ -610,22 +682,28 @@ impl Workspace {
self.open(source);
}

/// Takes in the history of the file in the centre once it is read, and shows the diff of its
/// newest commit when none is open.
fn take_history(&mut self, ctx: &egui::Context) {
let Some(history) = &mut self.history else { return };
let read = match history.reading.as_ref().map(Receiver::try_recv) {
None | Some(Err(TryRecvError::Empty)) => return,
Some(Ok(read)) => read,
Some(Err(TryRecvError::Disconnected)) => Err("Reading the history stopped unexpectedly".to_owned()),
};
history.reading = None;
match read {
Ok(commits) => history.commits = commits,
Err(error) => self.error = Some(error),
/// Takes in the history or the blame of the file in the centre once it is read. A history
/// shows the diff of its newest commit when none is open; a blame that failed, as when the
/// file is gone, closes.
fn take_reading(&mut self, ctx: &egui::Context) {
if let Some(history) = &mut self.history
&& let Some(read) = take(&mut history.reading)
{
match read {
Ok(commits) => history.commits = commits,
Err(error) => self.error = Some(error),
}
if self.open_file.is_none() && !history.commits.is_empty() {
self.visit(ctx, 0);
}
}
if self.open_file.is_none() && !history.commits.is_empty() {
self.visit(ctx, 0);
if let Some(blame) = &mut self.blame
&& let Some(read) = take(&mut blame.reading)
{
match read {
Ok(lines) => blame.lines = lines,
Err(error) => (self.blame, self.error) = (None, Some(error)),
}
}
}

Expand All @@ -634,7 +712,7 @@ impl Workspace {
match self.repo.git.rebase_plan(&onto) {
Ok(listed) if listed.is_empty() => self.error = Some(format!("Nothing to rebase onto {onto:.7}")),
Ok(listed) => {
(self.open_file, self.merge, self.history) = (None, None, None);
self.close_centre();
return Some(self.plan.insert(Plan::new(onto, listed)));
}
Err(error) => self.error = Some(error.to_string()),
Expand Down Expand Up @@ -748,7 +826,7 @@ impl Workspace {

/// Takes in the result of the background job, once it is there, and starts the next.
pub fn poll(&mut self, ctx: &egui::Context) {
self.take_history(ctx);
self.take_reading(ctx);
let Some(job) = &self.job else { return };
let (failed, snapshot, watch) = match job.outcome.try_recv() {
Err(TryRecvError::Empty) => return,
Expand Down Expand Up @@ -782,6 +860,10 @@ impl Workspace {
history.read(ctx, &self.repo.git);
}
}
// Any change may be to the file in the working tree, or to the commits behind it.
if let Some(blame) = self.blame.as_mut().filter(|blame| blame.at.is_none()) {
blame.read(ctx, &self.repo.git);
}
self.snapshot = snapshot;
}
Some(Err(error)) => self.error = Some(error.to_string()),
Expand Down
Loading
Loading