Skip to content

Commit 798b317

Browse files
committed
Watcher: Watch for the HEAD SHA
Include the current HEAD commit SHA with GitActivity events emitted by the watcher and propagate it through the API and frontend plumbing. - Change GitActivity from a single ProjectId variant to a struct with project_id and head_sha so consumers can know the exact HEAD commit. - When logs/HEAD changes, read the repository HEAD and peel to the commit to obtain the SHA, then emit GitActivity with head_sha. - Surface head SHA to the frontend: include headSha in the ChangeForFrontend payload for git activity. - Add gix and gitbutler-oxidize workspace deps used to access repository information. - Expose head_sha API command and related response types; extend the existing operating_mode command to return head (shorthand) along with operating mode. - Wire the new head_sha command into the server RPC dispatch and tauri command list. This allows the UI and other consumers to reliably identify the current commit when git activity updates occur.
1 parent a15aa66 commit 798b317

File tree

9 files changed

+74
-8
lines changed

9 files changed

+74
-8
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/but-api/src/commands/modes.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,50 @@ use tracing::instrument;
1212

1313
use crate::error::Error;
1414

15+
#[derive(Debug, serde::Serialize)]
16+
#[serde(rename_all = "camelCase")]
17+
pub struct HeadAndMode {
18+
pub head: Option<String>,
19+
pub operating_mode: Option<OperatingMode>,
20+
}
21+
1522
#[api_cmd]
1623
#[cfg_attr(feature = "tauri", tauri::command(async))]
1724
#[instrument(err(Debug))]
18-
pub fn operating_mode(project_id: ProjectId) -> Result<OperatingMode, Error> {
25+
pub fn operating_mode(project_id: ProjectId) -> Result<HeadAndMode, Error> {
1926
let project = gitbutler_project::get(project_id)?;
2027
let ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
21-
Ok(gitbutler_operating_modes::operating_mode(&ctx))
28+
let head = match ctx.repo().head() {
29+
Ok(head_ref) => head_ref.shorthand().map(|s| s.to_string()),
30+
Err(_) => None,
31+
};
32+
33+
Ok(HeadAndMode {
34+
head,
35+
operating_mode: Some(gitbutler_operating_modes::operating_mode(&ctx)),
36+
})
37+
}
38+
39+
#[derive(Debug, serde::Serialize)]
40+
#[serde(rename_all = "camelCase")]
41+
pub struct HeadSha {
42+
head_sha: String,
43+
}
44+
45+
#[api_cmd]
46+
#[cfg_attr(feature = "tauri", tauri::command(async))]
47+
#[instrument(err(Debug))]
48+
pub fn head_sha(project_id: ProjectId) -> Result<HeadSha, Error> {
49+
let project = gitbutler_project::get(project_id)?;
50+
let ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
51+
let head_ref = ctx.repo().head().context("failed to get head")?;
52+
let head_sha = head_ref
53+
.peel_to_commit()
54+
.context("failed to get head commit")?
55+
.id()
56+
.to_string();
57+
58+
Ok(HeadSha { head_sha })
2259
}
2360

2461
#[api_cmd]

crates/but-server/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ async fn handle_command(
310310
}
311311
// Operating modes commands
312312
"operating_mode" => modes::operating_mode_cmd(request.params),
313+
"head_sha" => modes::head_sha_cmd(request.params),
313314
"enter_edit_mode" => modes::enter_edit_mode_cmd(request.params),
314315
"abort_edit_and_return_to_workspace" => {
315316
modes::abort_edit_and_return_to_workspace_cmd(request.params)

crates/but-server/src/projects.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,14 @@ impl ActiveProjects {
6565
name: format!("project://{project_id}/git/head"),
6666
payload: serde_json::json!({ "head": head, "operatingMode": operating_mode }),
6767
},
68-
Change::GitActivity(project_id) => FrontendEvent {
68+
Change::GitActivity {
69+
project_id,
70+
head_sha,
71+
} => FrontendEvent {
6972
name: format!("project://{project_id}/git/activity"),
70-
payload: serde_json::json!({}),
73+
payload: serde_json::json!({
74+
"headSha": head_sha,
75+
}),
7176
},
7277
Change::WorktreeChanges {
7378
project_id,

crates/gitbutler-tauri/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ fn main() -> anyhow::Result<()> {
298298
remotes::list_remotes,
299299
remotes::add_remote,
300300
modes::operating_mode,
301+
modes::head_sha,
301302
modes::enter_edit_mode,
302303
modes::save_edit_and_return_to_workspace,
303304
modes::abort_edit_and_return_to_workspace,

crates/gitbutler-tauri/src/window.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@ pub(crate) mod state {
4242
payload: serde_json::json!({ "head": head, "operatingMode": operating_mode }),
4343
project_id,
4444
},
45-
Change::GitActivity(project_id) => ChangeForFrontend {
45+
Change::GitActivity {
46+
project_id,
47+
head_sha,
48+
} => ChangeForFrontend {
4649
name: format!("project://{project_id}/git/activity"),
47-
payload: serde_json::json!({}),
50+
payload: serde_json::json!({
51+
"headSha": head_sha,
52+
}),
4853
project_id,
4954
},
5055
Change::WorktreeChanges {

crates/gitbutler-watcher/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ but-settings.workspace = true
2424
but-hunk-assignment.workspace = true
2525
but-hunk-dependency.workspace = true
2626
serde-error = "0.1.3"
27+
gix.workspace = true
28+
gitbutler-oxidize.workspace = true
2729

2830
[lints.clippy]
2931
all = "deny"

crates/gitbutler-watcher/src/events.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ pub enum Change {
1111
head: String,
1212
operating_mode: OperatingMode,
1313
},
14-
GitActivity(ProjectId),
14+
GitActivity {
15+
project_id: ProjectId,
16+
head_sha: String,
17+
},
1518
WorktreeChanges {
1619
project_id: ProjectId,
1720
changes: but_hunk_assignment::WorktreeChanges,

crates/gitbutler-watcher/src/handler.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,17 @@ impl Handler {
140140
self.emit_app_event(Change::GitFetch(ctx.project().id))?;
141141
}
142142
"logs/HEAD" => {
143-
self.emit_app_event(Change::GitActivity(ctx.project().id))?;
143+
let repo = ctx.repo();
144+
let head_ref = repo.head().context("failed to get head")?;
145+
let head_sha = head_ref
146+
.peel_to_commit()
147+
.context("failed to get head commit")?
148+
.id()
149+
.to_string();
150+
self.emit_app_event(Change::GitActivity {
151+
project_id: ctx.project().id,
152+
head_sha,
153+
})?;
144154
}
145155
"index" => {
146156
let _ = self.emit_worktree_changes(ctx);

0 commit comments

Comments
 (0)