MCPcopy Create free account
hub / github.com/ChrisFeldmeier/OpenCodeRust / list_worktrees

Function list_worktrees

crates/opencode-server/src/worktree.rs:44–97  ·  view source on GitHub ↗
(repo_path: &Path)

Source from the content-addressed store, hash-verified

42}
43
44pub fn list_worktrees(repo_path: &Path) -> Result<Vec<WorktreeInfo>, WorktreeError> {
45 if !is_git_repo(repo_path) {
46 return Err(WorktreeError::NotGitRepo);
47 }
48
49 let output = run_git(&["worktree", "list", "--porcelain"], repo_path)?;
50
51 let mut worktrees = Vec::new();
52 let mut current_path: Option<String> = None;
53 let mut current_branch: Option<String> = None;
54 let mut current_head: Option<String> = None;
55
56 for line in output.lines() {
57 if line.starts_with("worktree ") {
58 if let (Some(path), Some(branch), Some(head)) =
59 (&current_path, &current_branch, &current_head)
60 {
61 worktrees.push(WorktreeInfo {
62 path: path.clone(),
63 branch: branch.clone(),
64 head: head.clone(),
65 });
66 }
67 current_path = Some(line["worktree ".len()..].to_string());
68 current_branch = None;
69 current_head = None;
70 } else if line.starts_with("HEAD ") {
71 current_head = Some(line["HEAD ".len()..].to_string());
72 } else if line.starts_with("branch ") {
73 let branch_full = &line["branch ".len()..];
74 let branch = branch_full
75 .strip_prefix("refs/heads/")
76 .unwrap_or(branch_full);
77 current_branch = Some(branch.to_string());
78 }
79 }
80
81 if let (Some(path), Some(branch), Some(head)) = (&current_path, &current_branch, &current_head)
82 {
83 worktrees.push(WorktreeInfo {
84 path: path.clone(),
85 branch: branch.clone(),
86 head: head.clone(),
87 });
88 } else if let (Some(path), Some(head)) = (&current_path, &current_head) {
89 worktrees.push(WorktreeInfo {
90 path: path.clone(),
91 branch: "HEAD".to_string(),
92 head: head.clone(),
93 });
94 }
95
96 Ok(worktrees)
97}
98
99pub fn create_worktree(
100 repo_path: &Path,

Callers

nothing calls this directly

Calls 4

is_git_repoFunction · 0.85
run_gitFunction · 0.85
newFunction · 0.85
cloneMethod · 0.45

Tested by

no test coverage detected