thoughts-tool 0.12.0

Flexible thought management using filesystem mounts for git repositories
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Canonical repository identity normalization.
//!
//! This module provides `RepoIdentity` as the single source of truth for repository identity,
//! enabling consistent URL normalization across SSH, HTTPS, and various git hosting formats.

use anyhow::Result;
use anyhow::bail;

/// Maximum allowed subgroup nesting depth (GitLab supports up to 20 levels).
const MAX_SUBGROUP_DEPTH: usize = 20;

/// Canonical repository identity extracted from a git URL.
///
/// This struct normalizes various URL formats (SSH, HTTPS, with/without .git suffix)
/// into a consistent identity that can be used for deduplication and matching.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RepoIdentity {
    /// Host name (lowercased), e.g., "github.com"
    pub host: String,
    /// Organization path (may contain multiple segments for GitLab subgroups), e.g., "org" or "group/subgroup"
    pub org_path: String,
    /// Repository name (no .git suffix, no trailing slash)
    pub repo: String,
}

/// Canonical key for identity-based lookups and deduplication.
///
/// All fields are lowercased for case-insensitive matching.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RepoIdentityKey {
    pub host: String,
    pub org_path: String,
    pub repo: String,
}

impl RepoIdentity {
    /// Parse a git URL into a `RepoIdentity`.
    ///
    /// Supported formats:
    /// - SSH scp-like: `git@github.com:org/repo.git`
    /// - SSH with port: `ssh://git@host:2222/org/repo.git`
    /// - HTTPS: `https://github.com/org/repo` or `https://github.com/org/repo.git`
    /// - GitLab subgroups: `https://gitlab.com/a/b/c/repo.git`
    /// - Azure DevOps: `https://dev.azure.com/org/proj/_git/repo`
    ///
    /// # Errors
    /// Returns an error if the URL cannot be parsed or has invalid structure.
    pub fn parse(url: &str) -> Result<Self> {
        let url = url.trim();

        // Determine URL type and extract host + path
        let (host, path) = if url.starts_with("git@") {
            // SSH scp-like: git@host:path
            parse_scp_url(url)?
        } else if url.starts_with("ssh://") {
            // SSH with scheme: ssh://[user@]host[:port]/path
            parse_ssh_scheme_url(url)?
        } else if url.starts_with("https://") || url.starts_with("http://") {
            // HTTPS/HTTP: scheme://[user@]host[:port]/path
            parse_https_url(url)?
        } else {
            bail!("Unsupported URL format: {url}");
        };

        // Normalize path: remove trailing slashes and .git suffix
        let path = path
            .trim_end_matches('/')
            .trim_end_matches(".git")
            .trim_end_matches('/');

        // Split path into segments and validate
        let segments: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();

        if segments.is_empty() {
            bail!("URL has no path segments: {url}");
        }

        // Check for invalid segments
        for seg in &segments {
            if *seg == "." || *seg == ".." {
                bail!("Invalid path segment '{seg}' in URL: {url}");
            }
        }

        if segments.len() > MAX_SUBGROUP_DEPTH + 1 {
            bail!(
                "Path has too many segments ({}, max {}): {}",
                segments.len(),
                MAX_SUBGROUP_DEPTH + 1,
                url
            );
        }

        // Handle Azure DevOps special case: org/proj/_git/repo
        let (org_path, repo) = if let Some(git_idx) = segments.iter().position(|s| *s == "_git") {
            if git_idx + 1 >= segments.len() {
                bail!("Azure DevOps URL missing repo after _git: {url}");
            }
            let org_segments = &segments[..git_idx];
            let repo = segments[git_idx + 1];
            (org_segments.join("/"), repo.to_string())
        } else if segments.len() == 1 {
            // Single segment: treat as repo with empty org (unusual but valid for some hosts)
            (String::new(), segments[0].to_string())
        } else {
            // Standard case: all but last segment is org_path, last is repo
            let org_segments = &segments[..segments.len() - 1];
            let repo = segments[segments.len() - 1];
            (org_segments.join("/"), repo.to_string())
        };

        Ok(Self {
            host: host.to_lowercase(),
            org_path,
            repo,
        })
    }

    /// Get the canonical key for identity-based lookups.
    ///
    /// All fields are lowercased for case-insensitive matching.
    pub fn canonical_key(&self) -> RepoIdentityKey {
        RepoIdentityKey {
            host: self.host.to_lowercase(),
            org_path: self.org_path.to_lowercase(),
            repo: self.repo.to_lowercase(),
        }
    }
}

/// Parse SSH scp-like URL: `git@host:path` or `user@host:path`
fn parse_scp_url(url: &str) -> Result<(String, String)> {
    // Format: [user@]host:path
    let without_user = url.find('@').map_or(url, |i| &url[i + 1..]);

    let colon_pos = without_user
        .find(':')
        .ok_or_else(|| anyhow::anyhow!("Invalid scp-like URL (missing colon): {url}"))?;

    let host = &without_user[..colon_pos];
    let path = &without_user[colon_pos + 1..];

    if host.is_empty() {
        bail!("Empty host in URL: {url}");
    }

    Ok((host.to_string(), path.to_string()))
}

/// Parse SSH scheme URL: `ssh://[user@]host[:port]/path`
fn parse_ssh_scheme_url(url: &str) -> Result<(String, String)> {
    let without_scheme = url
        .strip_prefix("ssh://")
        .ok_or_else(|| anyhow::anyhow!("Not an SSH URL: {url}"))?;

    // Strip userinfo if present
    let without_user = without_scheme
        .find('@')
        .map_or(without_scheme, |i| &without_scheme[i + 1..]);

    // Find the first slash (separates host[:port] from path)
    let slash_pos = without_user
        .find('/')
        .ok_or_else(|| anyhow::anyhow!("SSH URL missing path: {url}"))?;

    let host_port = &without_user[..slash_pos];
    let path = &without_user[slash_pos + 1..];

    // Extract host (strip port if present)
    let host = host_port
        .split(':')
        .next()
        .ok_or_else(|| anyhow::anyhow!("Empty host in URL: {url}"))?;

    if host.is_empty() {
        bail!("Empty host in URL: {url}");
    }

    Ok((host.to_string(), path.to_string()))
}

/// Parse HTTPS/HTTP URL: `scheme://[user@]host[:port]/path`
fn parse_https_url(url: &str) -> Result<(String, String)> {
    let scheme_end = url
        .find("://")
        .ok_or_else(|| anyhow::anyhow!("Invalid URL (missing ://): {url}"))?;

    let without_scheme = &url[scheme_end + 3..];

    // Strip userinfo if present
    let without_user = without_scheme
        .find('@')
        .map_or(without_scheme, |i| &without_scheme[i + 1..]);

    // Find the first slash (separates host[:port] from path)
    let slash_pos = without_user
        .find('/')
        .ok_or_else(|| anyhow::anyhow!("URL missing path: {url}"))?;

    let host_port = &without_user[..slash_pos];
    let path = &without_user[slash_pos + 1..];

    // Extract host (strip port if present)
    let host = host_port
        .split(':')
        .next()
        .ok_or_else(|| anyhow::anyhow!("Empty host in URL: {url}"))?;

    if host.is_empty() {
        bail!("Empty host in URL: {url}");
    }

    Ok((host.to_string(), path.to_string()))
}

/// Split `url` into (`base_url`, `optional_subpath`) using a last-colon heuristic.
///
/// Treats it as `URL:subpath` only if the base portion parses as a valid `RepoIdentity`.
/// This avoids confusing `host:port` for a subpath delimiter.
///
/// # Examples
/// ```ignore
/// // No subpath
/// parse_url_and_subpath("git@github.com:org/repo.git")
///   => ("git@github.com:org/repo.git", None)
///
/// // With subpath
/// parse_url_and_subpath("git@github.com:org/repo.git:docs/api")
///   => ("git@github.com:org/repo.git", Some("docs/api"))
///
/// // SSH with port (port is NOT a subpath)
/// parse_url_and_subpath("ssh://git@host:2222/org/repo.git")
///   => ("ssh://git@host:2222/org/repo.git", None)
///
/// // SSH with port AND subpath
/// parse_url_and_subpath("ssh://git@host:2222/org/repo.git:docs/api")
///   => ("ssh://git@host:2222/org/repo.git", Some("docs/api"))
/// ```
pub fn parse_url_and_subpath(url: &str) -> (String, Option<String>) {
    // Strategy: find the rightmost colon and check if the left side parses as a valid URL.
    // If it does, the right side is a subpath. If not, there's no subpath.

    // Handle scheme-based URLs: ssh://, https://, http://
    // For these, we need to be careful about host:port patterns

    let url = url.trim();

    // Try splitting from the right
    if let Some(colon_pos) = url.rfind(':') {
        let potential_base = &url[..colon_pos];
        let potential_subpath = &url[colon_pos + 1..];

        // Don't split if subpath is empty
        if potential_subpath.is_empty() {
            return (url.to_string(), None);
        }

        // Don't split if subpath looks like a port (all digits)
        if potential_subpath.chars().all(|c| c.is_ascii_digit()) {
            return (url.to_string(), None);
        }

        // Don't split if potential_base is empty or just a scheme
        if potential_base.is_empty() || potential_base.ends_with("//") {
            return (url.to_string(), None);
        }

        // Try parsing the base as a RepoIdentity
        if RepoIdentity::parse(potential_base).is_ok() {
            return (
                potential_base.to_string(),
                Some(potential_subpath.to_string()),
            );
        }
    }

    (url.to_string(), None)
}

#[cfg(test)]
mod tests {
    use super::*;

    // ===== RepoIdentity::parse tests =====

    #[test]
    fn test_parse_ssh_scp_basic() {
        let id = RepoIdentity::parse("git@github.com:org/repo.git").unwrap();
        assert_eq!(id.host, "github.com");
        assert_eq!(id.org_path, "org");
        assert_eq!(id.repo, "repo");
    }

    #[test]
    fn test_parse_ssh_scp_no_git_suffix() {
        let id = RepoIdentity::parse("git@github.com:org/repo").unwrap();
        assert_eq!(id.host, "github.com");
        assert_eq!(id.org_path, "org");
        assert_eq!(id.repo, "repo");
    }

    #[test]
    fn test_parse_https_basic() {
        let id = RepoIdentity::parse("https://github.com/org/repo").unwrap();
        assert_eq!(id.host, "github.com");
        assert_eq!(id.org_path, "org");
        assert_eq!(id.repo, "repo");
    }

    #[test]
    fn test_parse_https_with_git_suffix() {
        let id = RepoIdentity::parse("https://github.com/org/repo.git").unwrap();
        assert_eq!(id.host, "github.com");
        assert_eq!(id.org_path, "org");
        assert_eq!(id.repo, "repo");
    }

    #[test]
    fn test_parse_https_trailing_slash() {
        let id = RepoIdentity::parse("https://github.com/org/repo/").unwrap();
        assert_eq!(id.host, "github.com");
        assert_eq!(id.org_path, "org");
        assert_eq!(id.repo, "repo");
    }

    #[test]
    fn test_parse_ssh_with_port() {
        let id = RepoIdentity::parse("ssh://git@host.example.com:2222/org/repo.git").unwrap();
        assert_eq!(id.host, "host.example.com");
        assert_eq!(id.org_path, "org");
        assert_eq!(id.repo, "repo");
    }

    #[test]
    fn test_parse_gitlab_subgroups() {
        let id = RepoIdentity::parse("https://gitlab.com/group/subgroup/team/repo.git").unwrap();
        assert_eq!(id.host, "gitlab.com");
        assert_eq!(id.org_path, "group/subgroup/team");
        assert_eq!(id.repo, "repo");
    }

    #[test]
    fn test_parse_gitlab_deep_subgroups() {
        let id = RepoIdentity::parse("https://gitlab.com/a/b/c/d/e/repo.git").unwrap();
        assert_eq!(id.host, "gitlab.com");
        assert_eq!(id.org_path, "a/b/c/d/e");
        assert_eq!(id.repo, "repo");
    }

    #[test]
    fn test_parse_azure_devops() {
        let id = RepoIdentity::parse("https://dev.azure.com/myorg/myproj/_git/myrepo").unwrap();
        assert_eq!(id.host, "dev.azure.com");
        assert_eq!(id.org_path, "myorg/myproj");
        assert_eq!(id.repo, "myrepo");
    }

    #[test]
    fn test_parse_host_case_normalized() {
        let id = RepoIdentity::parse("https://GitHub.COM/Org/Repo").unwrap();
        assert_eq!(id.host, "github.com");
        // org_path and repo preserve case
        assert_eq!(id.org_path, "Org");
        assert_eq!(id.repo, "Repo");
    }

    #[test]
    fn test_parse_http_scheme() {
        let id = RepoIdentity::parse("http://github.com/org/repo").unwrap();
        assert_eq!(id.host, "github.com");
        assert_eq!(id.org_path, "org");
        assert_eq!(id.repo, "repo");
    }

    #[test]
    fn test_parse_rejects_invalid_segments() {
        assert!(RepoIdentity::parse("https://github.com/../repo").is_err());
        assert!(RepoIdentity::parse("https://github.com/./repo").is_err());
    }

    #[test]
    fn test_parse_rejects_unsupported_scheme() {
        assert!(RepoIdentity::parse("ftp://github.com/org/repo").is_err());
        assert!(RepoIdentity::parse("org/repo").is_err());
    }

    // ===== canonical_key tests =====

    #[test]
    fn test_canonical_key_equality_across_schemes() {
        let ssh = RepoIdentity::parse("git@github.com:User/Repo.git").unwrap();
        let https = RepoIdentity::parse("https://github.com/user/repo").unwrap();

        assert_eq!(ssh.canonical_key(), https.canonical_key());
    }

    #[test]
    fn test_canonical_key_different_repos() {
        let a = RepoIdentity::parse("git@github.com:org/repo-a.git").unwrap();
        let b = RepoIdentity::parse("git@github.com:org/repo-b.git").unwrap();

        assert_ne!(a.canonical_key(), b.canonical_key());
    }

    #[test]
    fn test_canonical_key_different_orgs() {
        let a = RepoIdentity::parse("git@github.com:alice/utils.git").unwrap();
        let b = RepoIdentity::parse("git@github.com:bob/utils.git").unwrap();

        assert_ne!(a.canonical_key(), b.canonical_key());
    }

    // ===== parse_url_and_subpath tests =====

    #[test]
    fn test_subpath_none_basic() {
        let (url, sub) = parse_url_and_subpath("git@github.com:user/repo.git");
        assert_eq!(url, "git@github.com:user/repo.git");
        assert_eq!(sub, None);
    }

    #[test]
    fn test_subpath_present() {
        let (url, sub) = parse_url_and_subpath("git@github.com:user/repo.git:docs/api");
        assert_eq!(url, "git@github.com:user/repo.git");
        assert_eq!(sub, Some("docs/api".to_string()));
    }

    #[test]
    fn test_subpath_https_none() {
        let (url, sub) = parse_url_and_subpath("https://github.com/user/repo");
        assert_eq!(url, "https://github.com/user/repo");
        assert_eq!(sub, None);
    }

    #[test]
    fn test_subpath_ssh_port_not_confused() {
        // Port should NOT be treated as subpath
        let (url, sub) = parse_url_and_subpath("ssh://git@host:2222/org/repo.git");
        assert_eq!(url, "ssh://git@host:2222/org/repo.git");
        assert_eq!(sub, None);
    }

    #[test]
    fn test_subpath_ssh_port_with_actual_subpath() {
        let (url, sub) = parse_url_and_subpath("ssh://git@host:2222/org/repo.git:docs/api");
        assert_eq!(url, "ssh://git@host:2222/org/repo.git");
        assert_eq!(sub, Some("docs/api".to_string()));
    }

    #[test]
    fn test_subpath_empty_subpath_ignored() {
        let (url, sub) = parse_url_and_subpath("git@github.com:user/repo.git:");
        assert_eq!(url, "git@github.com:user/repo.git:");
        assert_eq!(sub, None);
    }
}