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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ targets = [
"x86_64-unknown-linux-gnu",
"aarch64-unknown-linux-gnu",
]

[workspace.metadata.dist.dependencies.homebrew]
difftastic = { stage = ["run"] }
14 changes: 14 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum Commands {
Switch(SwitchArgs),
/// Show status of the current branch in its stack
Status,
/// Show diff for the current branch's changes
Diff(DiffArgs),
/// Show log of all stacks with branches and commits
#[command(alias = "tree")]
Log(TreeArgs),
Expand Down Expand Up @@ -163,6 +165,18 @@ pub struct TreeArgs {
pub pr: bool,
}

// -- Diff --

#[derive(Args)]
pub struct DiffArgs {
/// Show diffstat summary instead of full diff
#[arg(long)]
pub stat: bool,
/// Use regular git diff instead of difftastic
#[arg(long)]
pub no_difftastic: bool,
}

// -- Config --

#[derive(Args)]
Expand Down
68 changes: 68 additions & 0 deletions src/commands/diff.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use anyhow::{bail, Result};
use std::process::Command;

use crate::context::Ctx;
use crate::ui;

pub fn run(ctx: &Ctx, stat: bool, no_difftastic: bool) -> Result<()> {
let current = ctx.git.current_branch()?;

let stack = match ctx.find_stack_for_branch(&current)? {
Some(s) => s,
None => bail!(
"Branch '{current}' is not in any stack.\n\
Use `gw adopt` to add it, or run `git diff` directly."
),
};

// parent_of returns base_branch for root branches
let parent = stack.parent_of(&current).unwrap();

let merge_base = ctx.git.merge_base(&parent, &current)?;

let use_difft = !no_difftastic && difft_available();

if stat {
// --stat always uses regular git diff (difftastic doesn't support --stat)
let status = Command::new("git")
.args(["diff", "--stat", &merge_base, "HEAD"])
.current_dir(ctx.git.repo_path())
.status()?;

if !status.success() {
bail!("git diff --stat failed");
}
} else {
let mut cmd = Command::new("git");
cmd.args(["diff", &merge_base, "HEAD"])
.current_dir(ctx.git.repo_path());

if use_difft {
cmd.env("GIT_EXTERNAL_DIFF", "difft");
}

let status = cmd.status()?;

if !status.success() && !use_difft {
bail!("git diff failed");
}
}

if !stat {
ui::info(&format!(
"Showing changes on '{}' since '{}'",
current, parent
));
}

Ok(())
}

fn difft_available() -> bool {
Command::new("difft")
.arg("--version")
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.is_ok_and(|s| s.success())
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod adopt;
pub mod branch;
pub mod completions;
pub mod config;
pub mod diff;
pub mod mcp_server;
pub mod mcp_setup;
pub mod push;
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ fn main() -> Result<()> {
Commands::Rebase(args) if args.cont || args.abort
) || matches!(&command, Commands::Log(_))
|| matches!(&command, Commands::Switch(_))
|| matches!(&command, Commands::Status);
|| matches!(&command, Commands::Status)
|| matches!(&command, Commands::Diff(_));

if !allowed {
let op = match prop_state.operation {
Expand All @@ -56,6 +57,7 @@ fn main() -> Result<()> {

match command {
Commands::Status => commands::status::run(&ctx),
Commands::Diff(args) => commands::diff::run(&ctx, args.stat, args.no_difftastic),
Commands::Stack(args) => commands::stack::run(args.command, &ctx),
Commands::Branch(args) => commands::branch::run(args.command, &ctx),
Commands::Adopt(args) => commands::adopt::run(args, &ctx),
Expand Down