Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Remove legacy marker format & add standalone hook tests (#223)

Maximilian Roosfda66b0f1fd5
diff --git a/src/commands/config.rs b/src/commands/config.rs
index 3a8c9b93b..c172c4f69 100644
--- a/src/commands/config.rs
+++ b/src/commands/config.rs
@@ -1091,11 +1091,7 @@ fn handle_state_show_table(repo: &Repository) -> anyhow::Result<()> {
let mut table = String::from("| Branch | Marker | Age |\n");
table.push_str("|--------|--------|-----|\n");
for entry in markers {
- let age = if entry.set_at == 0 {
- "?".to_string() // Legacy marker without timestamp
- } else {
- format_relative_time_short(entry.set_at as i64)
- };
+ let age = format_relative_time_short(entry.set_at as i64);
table.push_str(&format!(
"| {} | {} | {} |\n",
entry.branch, entry.marker, age
@@ -1223,32 +1219,26 @@ fn get_all_markers(repo: &Repository) -> Vec<MarkerEntry> {
let mut markers = Vec::new();
for line in output.lines() {
- // Format: "worktrunk.marker.escaped_branch json_or_plain_value"
- if let Some((key, value)) = line.split_once(' ')
- && let Some(escaped_branch) = key.strip_prefix("worktrunk.marker.")
- {
- let branch = worktrunk::git::unescape_branch_from_config(escaped_branch);
- // Try to parse as JSON, fall back to legacy plain-text
- if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(value) {
- // Skip if "marker" field is missing (corrupted data)
- let Some(marker) = parsed.get("marker").and_then(|v| v.as_str()) else {
- continue;
- };
- let set_at = parsed.get("set_at").and_then(|v| v.as_u64()).unwrap_or(0);
- markers.push(MarkerEntry {
- branch,
- marker: marker.to_string(),
- set_at,
- });
- } else {
- // Legacy plain-text marker (no timestamp)
- markers.push(MarkerEntry {
- branch,
- marker: value.to_string(),
- set_at: 0, // Unknown age
- });
- }
- }
+ // Format: "worktrunk.marker.escaped_branch json_value"
+ let Some((key, value)) = line.split_once(' ') else {
+ continue;
+ };
+ let Some(escaped_branch) = key.strip_prefix("worktrunk.marker.") else {
+ continue;
+ };
+ let Ok(parsed) = serde_json::from_str::<serde_json::Value>(value) else {
+ continue; // Skip invalid JSON
+ };
+ let Some(marker) = parsed.get("marker").and_then(|v| v.as_str()) else {
+ continue; // Skip if "marker" field is missing
+ };
+ let set_at = parsed.get("set_at").and_then(|v| v.as_u64()).unwrap_or(0);
+ let branch = worktrunk::git::unescape_branch_from_config(escaped_branch);
+ markers.push(MarkerEntry {
+ branch,
+ marker: marker.to_string(),
+ set_at,
+ });
}
// Sort by age (most recent first), then by branch name for ties
diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rs
index ef3aea461..244adc7c6 100644
--- a/src/git/repository/mod.rs
+++ b/src/git/repository/mod.rs
@@ -280,7 +280,6 @@ impl Repository {
/// Read a user-defined marker from `worktrunk.marker.<branch>` in git config.
///
/// Markers are stored as JSON: `{"marker": "text", "set_at": unix_timestamp}`.
- /// Falls back to plain text for legacy markers without timestamps.
pub fn branch_keyed_marker(&self, branch: &str) -> Option<String> {
let escaped = super::escape_branch_for_config(branch);
let config_key = format!("worktrunk.marker.{}", escaped);
@@ -290,15 +289,11 @@ impl Repository {
.map(|output| output.trim().to_string())
.filter(|s| !s.is_empty())?;
- // Try to parse as JSON, fall back to raw string for legacy markers
- if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(&raw) {
- parsed
- .get("marker")
- .and_then(|v| v.as_str())
- .map(|s| s.to_string())
- } else {
- Some(raw) // Legacy plain-text marker
- }
+ let parsed: serde_json::Value = serde_json::from_str(&raw).ok()?;
+ parsed
+ .get("marker")
+ .and_then(|v| v.as_str())
+ .map(|s| s.to_string())
}
/// Read user-defined branch-keyed marker.
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 48215e5f5..0591132d4 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -1553,10 +1553,12 @@ exit /b 1
///
/// This handles the branch name escaping required for git config keys,
/// which only allow alphanumeric, `-`, and `.` characters.
+ /// Markers are stored as JSON with a timestamp.
pub fn set_marker(&self, branch: &str, marker: &str) {
let escaped = worktrunk::git::escape_branch_for_config(branch);
let config_key = format!("worktrunk.marker.{}", escaped);
- self.git_command(&["config", &config_key, marker])
+ let json_value = format!(r#"{{"marker":"{}","set_at":{}}}"#, marker, TEST_EPOCH);
+ self.git_command(&["config", &config_key, &json_value])
.output()
.unwrap();
}
diff --git a/tests/integration_tests/config_state.rs b/tests/integration_tests/config_state.rs
index 67f8159d7..de519be48 100644
--- a/tests/integration_tests/config_state.rs
+++ b/tests/integration_tests/config_state.rs
@@ -258,10 +258,8 @@ fn test_state_clear_ci_status_branch_not_cached(repo: TestRepo) {
#[rstest]
fn test_state_get_marker(repo: TestRepo) {
- // Set a marker first
- repo.git_command(&["config", "worktrunk.marker.main", "🚧"])
- .status()
- .unwrap();
+ // Set a marker first (using JSON format)
+ repo.set_marker("main", "🚧");
let output = wt_state_cmd(&repo, "marker", "get", &[]).output().unwrap();
assert!(output.status.success());
@@ -281,10 +279,8 @@ fn test_state_get_marker_empty(repo: TestRepo) {
fn test_state_get_marker_specific_branch(repo: TestRepo) {
repo.git_command(&["branch", "feature"]).status().unwrap();
- // Set a marker for feature branch
- repo.git_command(&["config", "worktrunk.marker.feature", "πŸ”§"])
- .status()
- .unwrap();
+ // Set a marker for feature branch (using JSON format)
+ repo.set_marker("feature", "πŸ”§");
let output = wt_state_cmd(&repo, "marker", "get", &["--branch", "feature"])
.output()
@@ -326,10 +322,8 @@ fn test_state_set_marker_branch_specific(repo: TestRepo) {
#[rstest]
fn test_state_clear_marker_branch_default(repo: TestRepo) {
- // Set a marker first
- repo.git_command(&["config", "worktrunk.marker.main", "🚧"])
- .status()
- .unwrap();
+ // Set a marker first (using JSON format)
+ repo.set_marker("main", "🚧");
let output = wt_state_cmd(&repo, "marker", "clear", &[])
.output()
@@ -347,10 +341,8 @@ fn test_state_clear_marker_branch_default(repo: TestRepo) {
#[rstest]
fn test_state_clear_marker_branch_specific(repo: TestRepo) {
- // Set a marker first
- repo.git_command(&["config", "worktrunk.marker.feature", "πŸ”§"])
- .status()
- .unwrap();
+ // Set a marker first (using JSON format)
+ repo.set_marker("feature", "πŸ”§");
let output = wt_state_cmd(&repo, "marker", "clear", &["--branch", "feature"])
.output()
@@ -368,16 +360,10 @@ fn test_state_clear_marker_branch_specific(repo: TestRepo) {
#[rstest]
fn test_state_clear_marker_all(repo: TestRepo) {
- // Set multiple markers
- repo.git_command(&["config", "worktrunk.marker.main", "🚧"])
- .status()
- .unwrap();
- repo.git_command(&["config", "worktrunk.marker.feature", "πŸ”§"])
- .status()
- .unwrap();
- repo.git_command(&["config", "worktrunk.marker.bugfix", "πŸ›"])
- .status()
- .unwrap();
+ // Set multiple markers (using JSON format)
+ repo.set_marker("main", "🚧");
+ repo.set_marker("feature", "πŸ”§");
+ repo.set_marker("bugfix", "πŸ›");
let output = wt_state_cmd(&repo, "marker", "clear", &["--all"])
.output()
@@ -502,10 +488,8 @@ fn test_state_clear_all_comprehensive(repo: TestRepo) {
.status()
.unwrap();
- // Marker
- repo.git_command(&["config", "worktrunk.marker.main", "🚧"])
- .status()
- .unwrap();
+ // Marker (using JSON format)
+ repo.set_marker("main", "🚧");
// CI cache
repo.git_command(&[
diff --git a/tests/integration_tests/user_hooks.rs b/tests/integration_tests/user_hooks.rs
index 16ddc67b5..55a715777 100644
--- a/tests/integration_tests/user_hooks.rs
+++ b/tests/integration_tests/user_hooks.rs
@@ -680,3 +680,95 @@ approved-commands = ["echo 'PROJECT_POST_START' > project_bg.txt"]
"Project post-start should have run"
);
}
+
+// ============================================================================
+// Standalone Hook Execution Tests (wt hook <type>)
+// ============================================================================
+
+/// Test `wt hook post-create` standalone execution
+#[rstest]
+fn test_standalone_hook_post_create(repo: TestRepo) {
+ // Write project config with post-create hook
+ repo.write_project_config(r#"post-create = "echo 'STANDALONE_POST_CREATE' > hook_ran.txt""#);
+
+ let mut cmd = crate::common::wt_command();
+ cmd.current_dir(repo.root_path());
+ cmd.env("WORKTRUNK_CONFIG_PATH", repo.test_config_path());
+ cmd.args(["hook", "post-create", "--force"]);
+
+ let output = cmd.output().unwrap();
+ assert!(
+ output.status.success(),
+ "wt hook post-create should succeed"
+ );
+
+ // Hook should have run
+ let marker = repo.root_path().join("hook_ran.txt");
+ assert!(marker.exists(), "post-create hook should have run");
+ let content = fs::read_to_string(&marker).unwrap();
+ assert!(content.contains("STANDALONE_POST_CREATE"));
+}
+
+/// Test `wt hook post-start` standalone execution
+#[rstest]
+fn test_standalone_hook_post_start(repo: TestRepo) {
+ // Write project config with post-start hook
+ repo.write_project_config(r#"post-start = "echo 'STANDALONE_POST_START' > hook_ran.txt""#);
+
+ let mut cmd = crate::common::wt_command();
+ cmd.current_dir(repo.root_path());
+ cmd.env("WORKTRUNK_CONFIG_PATH", repo.test_config_path());
+ cmd.args(["hook", "post-start", "--force"]);
+
+ let output = cmd.output().unwrap();
+ assert!(output.status.success(), "wt hook post-start should succeed");
+
+ // Hook should have run
+ let marker = repo.root_path().join("hook_ran.txt");
+ assert!(marker.exists(), "post-start hook should have run");
+ let content = fs::read_to_string(&marker).unwrap();
+ assert!(content.contains("STANDALONE_POST_START"));
+}
+
+/// Test `wt hook pre-commit` standalone execution
+#[rstest]
+fn test_standalone_hook_pre_commit(repo: TestRepo) {
+ // Write project config with pre-commit hook
+ repo.write_project_config(r#"pre-commit = "echo 'STANDALONE_PRE_COMMIT' > hook_ran.txt""#);
+
+ let mut cmd = crate::common::wt_command();
+ cmd.current_dir(repo.root_path());
+ cmd.env("WORKTRUNK_CONFIG_PATH", repo.test_config_path());
+ cmd.args(["hook", "pre-commit", "--force"]);
+
+ let output = cmd.output().unwrap();
+ assert!(output.status.success(), "wt hook pre-commit should succeed");
+
+ // Hook should have run
+ let marker = repo.root_path().join("hook_ran.txt");
+ assert!(marker.exists(), "pre-commit hook should have run");
+ let content = fs::read_to_string(&marker).unwrap();
+ assert!(content.contains("STANDALONE_PRE_COMMIT"));
+}
+
+/// Test `wt hook post-create` fails when no hooks configured
+#[rstest]
+fn test_standalone_hook_no_hooks_configured(repo: TestRepo) {
+ // No project config, no user config with hooks
+ let mut cmd = crate::common::wt_command();
+ cmd.current_dir(repo.root_path());
+ cmd.env("WORKTRUNK_CONFIG_PATH", repo.test_config_path());
+ cmd.args(["hook", "post-create", "--force"]);
+
+ let output = cmd.output().unwrap();
+ assert!(
+ !output.status.success(),
+ "wt hook should fail when no hooks configured"
+ );
+
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(
+ stderr.contains("No post-create hook configured"),
+ "Error should mention no hook configured, got: {stderr}"
+ );
+}