Skip to main content

opencode_sdk/http/
worktree.rs

1//! Worktree API for OpenCode.
2//!
3//! Experimental endpoints for git worktree management.
4
5use crate::error::Result;
6use crate::http::HttpClient;
7use reqwest::Method;
8use serde::{Deserialize, Serialize};
9
10/// Worktree API client.
11#[derive(Clone)]
12pub struct WorktreeApi {
13    http: HttpClient,
14}
15
16impl WorktreeApi {
17    /// Create a new Worktree API client.
18    pub fn new(http: HttpClient) -> Self {
19        Self { http }
20    }
21
22    /// Create a worktree (experimental).
23    ///
24    /// # Errors
25    ///
26    /// Returns an error if the request fails.
27    pub async fn create(&self, req: &CreateWorktreeRequest) -> Result<Worktree> {
28        let body = serde_json::to_value(req)?;
29        self.http
30            .request_json(Method::POST, "/experimental/worktree", Some(body))
31            .await
32    }
33
34    /// List worktrees (experimental).
35    ///
36    /// # Errors
37    ///
38    /// Returns an error if the request fails.
39    pub async fn list(&self) -> Result<Vec<Worktree>> {
40        self.http
41            .request_json(Method::GET, "/experimental/worktree", None)
42            .await
43    }
44}
45
46/// Request to create a worktree.
47#[derive(Debug, Clone, Serialize, Deserialize)]
48#[serde(rename_all = "camelCase")]
49pub struct CreateWorktreeRequest {
50    /// Branch name.
51    pub branch: String,
52    /// Path for the worktree.
53    #[serde(default, skip_serializing_if = "Option::is_none")]
54    pub path: Option<String>,
55}
56
57/// A git worktree.
58#[derive(Debug, Clone, Serialize, Deserialize)]
59#[serde(rename_all = "camelCase")]
60pub struct Worktree {
61    /// Worktree path.
62    pub path: String,
63    /// Branch name.
64    #[serde(default, skip_serializing_if = "Option::is_none")]
65    pub branch: Option<String>,
66    /// Whether this is the main worktree.
67    #[serde(default)]
68    pub is_main: bool,
69}