Skip to main content

worktree_io/issue/
paths.rs

1use std::path::PathBuf;
2
3use crate::config::Config;
4
5use super::IssueRef;
6
7impl IssueRef {
8    /// Path to the worktree checkout.
9    ///
10    /// For `Local`: `~/worktrees/local/{project_name}/issue-{display_number}`
11    /// For others:  `~/worktrees/github/{owner}/{repo}/issue-N`
12    #[must_use]
13    pub fn temp_path(&self) -> PathBuf {
14        self.bare_clone_path().join(self.workspace_dir_name())
15    }
16
17    /// Path to the bare clone (or the local repo itself for `Local`).
18    ///
19    /// # Panics
20    ///
21    /// Panics if the home directory cannot be determined.
22    #[must_use]
23    pub fn bare_clone_path(&self) -> PathBuf {
24        let temp = Config::load().map(|c| c.workspace.temp).unwrap_or(false);
25        self.bare_clone_path_rooted(temp)
26    }
27
28    pub(crate) fn bare_clone_path_rooted(&self, temp: bool) -> PathBuf {
29        let base = if temp {
30            std::env::temp_dir()
31        } else {
32            dirs::home_dir().expect("could not determine home directory")
33        }
34        .join("worktrees");
35        match self {
36            Self::GitHub { owner, repo, .. }
37            | Self::Linear { owner, repo, .. }
38            | Self::Jira { owner, repo, .. }
39            | Self::Adhoc { owner, repo, .. } => base.join("github").join(owner).join(repo),
40            Self::GitLab { owner, repo, .. } => base.join("gitlab").join(owner).join(repo),
41            Self::AzureDevOps {
42                org, project, repo, ..
43            } => base.join("azuredevops").join(org).join(project).join(repo),
44            Self::Local { project_path, .. } => {
45                let name = project_path
46                    .file_name()
47                    .unwrap_or_default()
48                    .to_string_lossy();
49                base.join("local").join(name.as_ref())
50            }
51        }
52    }
53}
54
55#[cfg(test)]
56#[path = "paths_tests.rs"]
57mod paths_tests;