Skip to content

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

Update README snapshot for `wt list` statuses

Maximilian Roosdde13b24cfb3
diff --git a/README.md b/README.md
index 3d8316716..33c0a1624 100644
--- a/README.md
+++ b/README.md
@@ -959,14 +959,13 @@ git config worktrunk.status.feature-x "💬"
```console
$ wt list
- Branch Status HEAD± main↕ Path Remote⇅ Commit Age Message
-@ main ^ ./repo b834638e 1d Initial commit
-+ clean-no-status _ ./repo.clean-no-status b834638e 1d Initial commit
-+ clean-with-status _ 💬 ./repo.clean-with-status b834638e 1d Initial commit
-+ dirty-no-status ! +1 -1 ./repo.dirty-no-status b834638e 1d Initial commit
-+ dirty-with-status ?_ 🤖 ./repo.dirty-with-status b834638e 1d Initial commit
-
-⚪ Showing 5 worktrees, 1 with changes
+ Branch Status HEAD± main↕ Path Remote⇅ Commit Age Message
+@ main ^ ./repo b834638e 1d Initial commit
++ feature-api ↑ 🤖 ↑1 ./repo.feature-api 9606cd0f 1d Add REST API endpoints
++ review-ui ? ↑ 💬 ↑1 ./repo.review-ui afd3b353 1d Add dashboard component
++ wip-docs ?_ ./repo.wip-docs b834638e 1d Initial commit
+
+⚪ Showing 4 worktrees, 2 ahead
```
<!-- END AUTO-GENERATED -->
@@ -984,22 +983,6 @@ When using Claude:
- Changes to `💬` when Claude needs input (waiting for permission or idle)
- Clears the status completely when the session ends
-<!-- ⚠️ AUTO-GENERATED from tests/snapshots/integration__integration_tests__list__with_user_status.snap — edit source to update -->
-
-```console
-$ wt list
- Branch Status HEAD± main↕ Path Remote⇅ Commit Age Message
-@ main ^ ./repo b834638e 1d Initial commit
-+ clean-no-status _ ./repo.clean-no-status b834638e 1d Initial commit
-+ clean-with-status _ 💬 ./repo.clean-with-status b834638e 1d Initial commit
-+ dirty-no-status ! +1 -1 ./repo.dirty-no-status b834638e 1d Initial commit
-+ dirty-with-status ?_ 🤖 ./repo.dirty-with-status b834638e 1d Initial commit
-
-⚪ Showing 5 worktrees, 1 with changes
-```
-
-<!-- END AUTO-GENERATED -->
-
**How it works:**
- Status is stored as `worktrunk.status.<branch>` in `.git/config`
diff --git a/tests/integration_tests/list.rs b/tests/integration_tests/list.rs
index cb55f54a0..4082b52d0 100644
--- a/tests/integration_tests/list.rs
+++ b/tests/integration_tests/list.rs
@@ -761,37 +761,57 @@ fn test_list_with_user_status() {
let mut repo = TestRepo::new();
repo.commit_with_age("Initial commit", DAY);
- // Worktree with user status only (no git changes)
- repo.add_worktree("clean-with-status");
-
- // Set user status (emoji only, branch-keyed)
+ // Branch ahead of main with commits and user status 🤖
+ let feature_wt = repo.add_worktree("feature-api");
+ std::fs::write(feature_wt.join("api.rs"), "// API implementation").unwrap();
let mut cmd = Command::new("git");
repo.configure_git_cmd(&mut cmd);
- cmd.args(["config", "worktrunk.status.clean-with-status", "💬"])
- .current_dir(repo.root_path())
+ cmd.args(["add", "."])
+ .current_dir(&feature_wt)
.output()
.unwrap();
-
- // Worktree with both git status and user status
- let dirty_wt = repo.add_worktree("dirty-with-status");
-
- // Set user status (emoji only, branch-keyed)
let mut cmd = Command::new("git");
repo.configure_git_cmd(&mut cmd);
- cmd.args(["config", "worktrunk.status.dirty-with-status", "🤖"])
+ cmd.args(["commit", "-m", "Add REST API endpoints"])
+ .current_dir(&feature_wt)
+ .output()
+ .unwrap();
+ // Set user status
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["config", "worktrunk.status.feature-api", "🤖"])
.current_dir(repo.root_path())
.output()
.unwrap();
+ // Branch with uncommitted changes and user status 💬
+ let review_wt = repo.add_worktree("review-ui");
+ std::fs::write(review_wt.join("component.tsx"), "// UI component").unwrap();
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "."])
+ .current_dir(&review_wt)
+ .output()
+ .unwrap();
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["commit", "-m", "Add dashboard component"])
+ .current_dir(&review_wt)
+ .output()
+ .unwrap();
// Add uncommitted changes
- std::fs::write(dirty_wt.join("new.txt"), "content").unwrap();
-
- // Worktree with git status only (no user status)
- let dirty_no_status_wt = repo.add_worktree("dirty-no-status");
- std::fs::write(dirty_no_status_wt.join("file.txt"), "content").unwrap();
+ std::fs::write(review_wt.join("styles.css"), "/* pending styles */").unwrap();
+ // Set user status
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["config", "worktrunk.status.review-ui", "💬"])
+ .current_dir(repo.root_path())
+ .output()
+ .unwrap();
- // Worktree with neither (control)
- repo.add_worktree("clean-no-status");
+ // Branch with uncommitted changes only (no user status)
+ let wip_wt = repo.add_worktree("wip-docs");
+ std::fs::write(wip_wt.join("README.md"), "# Documentation").unwrap();
snapshot_list("with_user_status", &repo);
}
diff --git a/tests/integration_tests/readme_sync.rs b/tests/integration_tests/readme_sync.rs
index 55ec1212d..c5ab43ee9 100644
--- a/tests/integration_tests/readme_sync.rs
+++ b/tests/integration_tests/readme_sync.rs
@@ -14,10 +14,9 @@ use std::sync::LazyLock;
/// Regex to find README snapshot markers
/// Format: <!-- ⚠️ AUTO-GENERATED from path.snap — edit source to update -->
-/// Captures: (1) snapshot path, (2) command line (e.g., "$ wt merge"), (3) content
static SNAPSHOT_MARKER_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
- r"(?s)<!-- ⚠️ AUTO-GENERATED from ([^\s]+\.snap) — edit source to update -->\n+```\w*\n(\$ [^\n]+)\n(.*?)```\n+<!-- END AUTO-GENERATED -->",
+ r"(?s)<!-- ⚠️ AUTO-GENERATED from ([^\s]+\.snap) — edit source to update -->\n+```\w*\n(.*?)```\n+<!-- END AUTO-GENERATED -->",
)
.unwrap()
});
@@ -157,11 +156,66 @@ fn combine_stdout_stderr(stdout: &str, stderr: &str) -> String {
result.join("\n")
}
-/// Parse content from an insta snapshot file
-fn parse_snapshot(path: &Path) -> Result<String, String> {
- let content = fs::read_to_string(path)
+/// Regex to extract program and args from YAML front matter
+static YAML_ARGS_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
+ Regex::new(r"(?s)program:\s*(\S+).*?args:\s*\n((?:\s*-\s*[^\n]+\n?)*)").unwrap()
+});
+
+/// Parse command line from snapshot YAML front matter
+fn parse_command_from_snapshot(content: &str) -> Option<String> {
+ if !content.starts_with("---") {
+ return None;
+ }
+
+ let parts: Vec<&str> = content.splitn(3, "---").collect();
+ if parts.len() < 2 {
+ return None;
+ }
+
+ let yaml = parts[1];
+ YAML_ARGS_PATTERN.captures(yaml).map(|cap| {
+ let program = cap.get(1).unwrap().as_str();
+ let args_block = cap.get(2).unwrap().as_str();
+ let args: Vec<&str> = args_block
+ .lines()
+ .filter_map(|line| {
+ let trimmed = line.trim();
+ if trimmed.starts_with("- ") {
+ Some(trimmed.trim_start_matches("- ").trim_matches('"'))
+ } else {
+ None
+ }
+ })
+ .collect();
+ format!("$ {} {}", program, args.join(" "))
+ })
+}
+
+/// Parse content from an insta snapshot file, optionally including command line
+/// Prefers the README command if provided, falls back to YAML program/args.
+fn parse_snapshot_with_command(
+ path: &Path,
+ readme_command: Option<&str>,
+) -> Result<String, String> {
+ let raw = fs::read_to_string(path)
.map_err(|e| format!("Failed to read {}: {}", path.display(), e))?;
+ // Prefer README command (user-facing), fall back to YAML (test implementation detail)
+ let command = readme_command
+ .map(String::from)
+ .or_else(|| parse_command_from_snapshot(&raw));
+ let content = parse_snapshot_content(&raw)?;
+
+ Ok(match command {
+ Some(cmd) => format!("{}\n{}", cmd, content),
+ None => content,
+ })
+}
+
+/// Parse content from snapshot file content
+fn parse_snapshot_content(content: &str) -> Result<String, String> {
+ let content = content.to_string();
+
// Remove YAML front matter
let content = if content.starts_with("---") {
let parts: Vec<&str> = content.splitn(3, "---").collect();
@@ -314,10 +368,11 @@ fn get_help_output(command: &str, project_root: &Path) -> Result<String, String>
}
/// Update a section in the README content, returning (new content, updated count, total count)
+/// The replacement function receives (id, current_content) to allow preserving existing values.
fn update_readme_section(
content: &str,
pattern: &Regex,
- get_replacement: impl Fn(&str) -> Result<String, String>,
+ get_replacement: impl Fn(&str, &str) -> Result<String, String>,
wrapper: (&str, &str),
) -> Result<(String, usize, usize), Vec<String>> {
let mut result = content.to_string();
@@ -339,7 +394,7 @@ fn update_readme_section(
// Process in reverse order to preserve positions
for (start, end, id, current) in matches.into_iter().rev() {
- let expected = match get_replacement(&id) {
+ let expected = match get_replacement(&id, &current) {
Ok(content) => content,
Err(e) => {
errors.push(format!("❌ {}: {}", id, e));
@@ -359,8 +414,9 @@ fn update_readme_section(
)
} else {
// With wrapper (snapshot sections)
+ // wrapper.0 includes trailing \n, so no extra newline between wrapper and content
format!(
- "<!-- ⚠️ AUTO-GENERATED from {} — edit source to update -->\n\n{}\n{}\n{}\n\n<!-- END AUTO-GENERATED -->",
+ "<!-- ⚠️ AUTO-GENERATED from {} — edit source to update -->\n\n{}{}\n{}\n\n<!-- END AUTO-GENERATED -->",
id, wrapper.0, expected, wrapper.1
)
};
@@ -480,45 +536,29 @@ fn test_readme_examples_are_in_sync() {
let mut updated_content = readme_content.clone();
let mut total_updated = 0;
- // Update snapshot markers (preserving command lines)
- let matches: Vec<_> = SNAPSHOT_MARKER_PATTERN
- .captures_iter(&updated_content.clone())
- .map(|cap| {
- let full_match = cap.get(0).unwrap();
- let snap_path = cap.get(1).unwrap().as_str().to_string();
- let command_line = cap.get(2).unwrap().as_str().to_string();
- let current = normalize_readme_content(cap.get(3).unwrap().as_str());
- (
- full_match.start(),
- full_match.end(),
- snap_path,
- command_line,
- current,
- )
- })
- .collect();
-
- checked += matches.len();
-
- // Process in reverse order to preserve positions
- for (start, end, snap_path, command_line, current) in matches.into_iter().rev() {
- let full_path = project_root.join(&snap_path);
- let expected = match parse_snapshot(&full_path) {
- Ok(content) => normalize_for_readme(&content),
- Err(e) => {
- errors.push(format!("❌ {}: {}", snap_path, e));
- continue;
- }
- };
-
- if current != expected {
- let replacement = format!(
- "<!-- ⚠️ AUTO-GENERATED from {} — edit source to update -->\n\n```console\n{}\n{}\n```\n\n<!-- END AUTO-GENERATED -->",
- snap_path, command_line, expected
- );
- updated_content.replace_range(start..end, &replacement);
- total_updated += 1;
+ // Update snapshot markers (with command line from YAML, or preserved from README)
+ let project_root_for_snapshots = project_root.to_path_buf();
+ match update_readme_section(
+ &updated_content,
+ &SNAPSHOT_MARKER_PATTERN,
+ |snap_path, current_content| {
+ // Extract existing command line from README if present (e.g., "$ wt switch --create fix-auth")
+ let existing_command = current_content
+ .lines()
+ .next()
+ .filter(|line| line.starts_with("$ "));
+ let full_path = project_root_for_snapshots.join(snap_path);
+ parse_snapshot_with_command(&full_path, existing_command)
+ .map(|content| normalize_for_readme(&content))
+ },
+ ("```console\n", "```"),
+ ) {
+ Ok((new_content, updated_count, total_count)) => {
+ updated_content = new_content;
+ total_updated += updated_count;
+ checked += total_count;
}
+ Err(errs) => errors.extend(errs),
}
// Update help markers (no wrapper - content is rendered markdown)
@@ -526,7 +566,7 @@ fn test_readme_examples_are_in_sync() {
match update_readme_section(
&updated_content,
&HELP_MARKER_PATTERN,
- |cmd| get_help_output(cmd, &project_root_clone),
+ |cmd, _current| get_help_output(cmd, &project_root_clone),
("", ""),
) {
Ok((new_content, updated_count, total_count)) => {
diff --git a/tests/snapshots/integration__integration_tests__list__with_user_status.snap b/tests/snapshots/integration__integration_tests__list__with_user_status.snap
index 839bcbcda..e37d6c0f1 100644
--- a/tests/snapshots/integration__integration_tests__list__with_user_status.snap
+++ b/tests/snapshots/integration__integration_tests__list__with_user_status.snap
@@ -23,11 +23,10 @@ exit_code: 0
----- stdout -----
----- stderr -----
- Branch Status HEAD± main↕ Path Remote⇅ Commit Age Message
-@ main ^ ./repo b834638e 1d Initial commit
-+ clean-no-status _ ./repo.clean-no-status b834638e 1d Initial commit
-+ clean-with-status _ 💬 ./repo.clean-with-status b834638e 1d Initial commit
-+ dirty-no-status ! +1 -1 ./repo.dirty-no-status b834638e 1d Initial commit
-+ dirty-with-status ?_ 🤖 ./repo.dirty-with-status b834638e 1d Initial commit
+ Branch Status HEAD± main↕ Path Remote⇅ Commit Age Message
+@ main ^ ./repo b834638e 1d Initial commit
++ feature-api ↑ 🤖 ↑1 ./repo.feature-api 9606cd0f 1d Add REST API endpoints
++ review-ui ? ↑ 💬 ↑1 ./repo.review-ui afd3b353 1d Add dashboard component
++ wip-docs ?_ ./repo.wip-docs b834638e 1d Initial commit
-⚪ Showing 5 worktrees, 1 with changes
+⚪ Showing 4 worktrees, 2 ahead