tangled-cli 0.1.0

CLI for interacting with Tangled, an AT Protocol-based git collaboration platform
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
use anyhow::{anyhow, Result};
use git2::Repository;
use serde::Serialize;
use tabled::builder::Builder;
use tabled::settings::Style;
use tangled_config::session::{Session, SessionManager};

use crate::cli::OutputFormat;

/// Serialize a value in the requested machine-readable output format.
pub fn print_serialized<T: Serialize + ?Sized>(
    format: OutputFormat,
    value: &T,
) -> Result<()> {
    print!("{}", render_serialized(format, value)?);
    Ok(())
}

fn render_serialized<T: Serialize + ?Sized>(
    format: OutputFormat,
    value: &T,
) -> Result<String> {
    match format {
        OutputFormat::Json => {
            Ok(format!("{}\n", serde_json::to_string_pretty(value)?))
        }
        OutputFormat::Yaml => Ok(serde_yaml::to_string(value)?),
        OutputFormat::Table => {
            Err(anyhow!("table output requires an explicit table layout"))
        }
    }
}

/// Print a compact, borderless table with aligned columns.
///
/// List output stays one row per record even when a value unexpectedly
/// contains tabs or newlines.
pub fn print_table<const C: usize>(
    headers: [&str; C],
    rows: impl IntoIterator<Item = [String; C]>,
) {
    println!("{}", render_table(headers, rows));
}

fn render_table<const C: usize>(
    headers: [&str; C],
    rows: impl IntoIterator<Item = [String; C]>,
) -> String {
    let mut builder = Builder::default();
    builder.push_record(headers);
    for row in rows {
        builder.push_record(row.map(|cell| single_line(&cell)));
    }

    let mut table = builder.build();
    table.with(Style::blank());

    // Style::blank retains one cell-padding space around the table. Remove
    // only the outer padding while preserving the alignment between columns.
    table
        .to_string()
        .lines()
        .map(str::trim)
        .collect::<Vec<_>>()
        .join("\n")
}

fn single_line(value: &str) -> String {
    value.replace(['\t', '\r', '\n'], " ")
}

/// Load session and automatically refresh if expired
pub async fn load_session() -> Result<Session> {
    let mgr = SessionManager::default();
    let session = mgr
        .load()?
        .ok_or_else(|| anyhow!("Please login first: tang auth login"))?;

    Ok(session)
}

/// Refresh the session using the refresh token
pub async fn refresh_session(session: &Session) -> Result<Session> {
    let mut new_session = if session.oauth.is_some() {
        crate::ops::oauth::refresh(session).await?
    } else {
        let pds = session
            .pds
            .clone()
            .unwrap_or_else(|| "https://bsky.social".to_string());
        crate::ops::session::refresh_session(&pds, &session.refresh_jwt).await?
    };

    // Preserve PDS from old session
    new_session.pds = session.pds.clone();

    // Save the refreshed session
    let mgr = SessionManager::default();
    mgr.save(&new_session)?;

    Ok(new_session)
}

/// Load session with automatic refresh on ExpiredToken
pub async fn load_session_with_refresh() -> Result<Session> {
    let session = load_session().await?;

    // Proactively refresh stale sessions: OAuth access tokens near expiry,
    // app-password JWTs older than 30 minutes.
    let stale = match &session.oauth {
        Some(oauth) => chrono::Utc::now().timestamp() > oauth.expires_at - 120,
        None => {
            chrono::Utc::now()
                .signed_duration_since(session.created_at)
                .num_minutes()
                > 30
        }
    };

    if stale {
        // Session is old, proactively refresh
        match refresh_session(&session).await {
            Ok(new_session) => return Ok(new_session),
            Err(_) => {
                // If refresh fails, try with the old session anyway
                // It might still work
            }
        }
    }

    Ok(session)
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitRepoContext {
    pub owner: String,
    pub name: String,
    pub host: String,
    pub remote_name: String,
    pub remote_url: String,
    pub current_branch: Option<String>,
    pub default_branch: Option<String>,
}

impl GitRepoContext {
    pub fn repo_spec(&self) -> String {
        format!("{}/{}", self.owner, self.name)
    }
}

pub fn current_git_repo_context() -> Result<GitRepoContext> {
    let repo = Repository::discover(".")?;
    let current_branch = current_branch(&repo);
    let (remote_name, remote) =
        select_tangled_remote(&repo, current_branch.as_deref())?;
    let remote_url = remote
        .url()
        .ok_or_else(|| {
            anyhow!("could not infer repository: remote has no URL")
        })?
        .to_string();
    let (owner, name, host) = parse_tangled_remote_url(&remote_url)
        .ok_or_else(|| {
            anyhow!("could not parse Tangled remote URL: {}", remote_url)
        })?;

    Ok(GitRepoContext {
        owner,
        name,
        host,
        remote_name,
        remote_url,
        current_branch,
        default_branch: default_branch(&repo),
    })
}

pub fn parse_tangled_remote_url(url: &str) -> Option<(String, String, String)> {
    if let Some(rest) = url.strip_prefix("git@") {
        let mut parts = rest.splitn(2, ':');
        let host = normalize_host(parts.next()?);
        let path = parts.next()?;
        if !is_tangled_host(&host) {
            return None;
        }
        return parse_remote_path(path)
            .map(|(owner, name)| (owner, name, host));
    }

    if url.starts_with("http://")
        || url.starts_with("https://")
        || url.starts_with("ssh://")
    {
        let parsed = url::Url::parse(url).ok()?;
        let host = normalize_host(parsed.host_str()?);
        if !is_tangled_host(&host) {
            return None;
        }
        return parse_remote_path(parsed.path())
            .map(|(owner, name)| (owner, name, host));
    }

    None
}

fn parse_remote_path(path: &str) -> Option<(String, String)> {
    let mut segs = path.trim_matches('/').trim_end_matches(".git").split('/');
    let owner = segs.next()?.trim_start_matches('@');
    let name = segs.next()?;
    if owner.is_empty() || name.is_empty() || segs.next().is_some() {
        return None;
    }
    Some((owner.to_string(), name.to_string()))
}

fn normalize_host(host: &str) -> String {
    host.trim_end_matches('/').to_ascii_lowercase()
}

fn is_tangled_host(host: &str) -> bool {
    host == "tangled.org" || host.ends_with(".tangled.sh")
}

fn select_tangled_remote<'repo>(
    repo: &'repo Repository,
    current_branch: Option<&str>,
) -> Result<(String, git2::Remote<'repo>)> {
    if let Some(branch) = current_branch {
        if let Ok(local_branch) =
            repo.find_branch(branch, git2::BranchType::Local)
        {
            if let Ok(upstream) = local_branch.upstream() {
                if let Some(remote_name) = upstream_remote_name(repo, &upstream)
                {
                    let remote = repo.find_remote(&remote_name)?;
                    if remote.url().and_then(parse_tangled_remote_url).is_some()
                    {
                        return Ok((remote_name, remote));
                    }
                }
            }
        }
    }

    if let Ok(remote) = repo.find_remote("origin") {
        if remote.url().and_then(parse_tangled_remote_url).is_some() {
            return Ok(("origin".to_string(), remote));
        }
    }

    let mut tangled = vec![];
    let remotes = repo.remotes().map_err(|_| {
        anyhow!("could not infer repository: no git remotes configured")
    })?;
    for name in remotes.iter().flatten() {
        let remote = repo.find_remote(name)?;
        if remote.url().and_then(parse_tangled_remote_url).is_some() {
            tangled.push(name.to_string());
        }
    }

    match tangled.as_slice() {
        [] => Err(anyhow!(
            "could not infer repository: no Tangled git remote found; pass --repo"
        )),
        [name] => Ok((name.clone(), repo.find_remote(name)?)),
        names => Err(anyhow!(
            "could not infer repository: multiple Tangled remotes found ({}); pass --repo",
            names.join(", ")
        )),
    }
}

fn upstream_remote_name(
    repo: &Repository,
    upstream: &git2::Branch<'_>,
) -> Option<String> {
    let upstream_ref = upstream.get().name()?;
    let remotes = repo.remotes().ok()?;
    for remote in remotes.iter().flatten() {
        let prefix = format!("refs/remotes/{}/", remote);
        if upstream_ref.starts_with(&prefix) {
            return Some(remote.to_string());
        }
    }
    None
}

fn current_branch(repo: &Repository) -> Option<String> {
    repo.head()
        .ok()
        .and_then(|head| head.shorthand().map(ToOwned::to_owned))
}

fn default_branch(repo: &Repository) -> Option<String> {
    repo.find_reference("refs/remotes/origin/HEAD")
        .ok()
        .and_then(|reference| {
            reference.symbolic_target().map(ToOwned::to_owned)
        })
        .and_then(|target| {
            target
                .strip_prefix("refs/remotes/origin/")
                .map(ToOwned::to_owned)
        })
        .or_else(|| {
            ["main", "master"]
                .into_iter()
                .find(|branch| {
                    repo.find_reference(&format!(
                        "refs/remotes/origin/{}",
                        branch
                    ))
                    .is_ok()
                })
                .map(ToOwned::to_owned)
        })
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::{parse_tangled_remote_url, render_serialized, render_table};
    use crate::cli::OutputFormat;

    #[test]
    fn renders_json_and_yaml_output() {
        let value = json!({"name": "demo", "private": false});

        let json = render_serialized(OutputFormat::Json, &value).unwrap();
        let yaml = render_serialized(OutputFormat::Yaml, &value).unwrap();

        assert_eq!(json, "{\n  \"name\": \"demo\",\n  \"private\": false\n}\n");
        assert_eq!(yaml, "name: demo\nprivate: false\n");
    }

    #[test]
    fn table_output_aligns_columns_and_keeps_records_on_one_line() {
        let output = render_table(
            ["NAME", "VALUE"],
            [
                ["short".to_string(), "one".to_string()],
                ["longer name".to_string(), "two\nlines".to_string()],
            ],
        );
        let lines = output.lines().collect::<Vec<_>>();
        let value_column = lines[0].find("VALUE").unwrap();

        assert_eq!(lines.len(), 3);
        assert!(!output.contains(['\t', '\r']));
        assert_eq!(lines[1].find("one"), Some(value_column));
        assert_eq!(lines[2].find("two lines"), Some(value_column));
    }

    #[test]
    fn parses_tangled_https_remotes() {
        assert_eq!(
            parse_tangled_remote_url(
                "https://tangled.org/@alice.example/demo.git"
            ),
            Some((
                "alice.example".to_string(),
                "demo".to_string(),
                "tangled.org".to_string()
            ))
        );
        assert_eq!(
            parse_tangled_remote_url("https://tangled.org/alice.example/demo"),
            Some((
                "alice.example".to_string(),
                "demo".to_string(),
                "tangled.org".to_string()
            ))
        );
    }

    #[test]
    fn parses_tangled_ssh_remotes() {
        assert_eq!(
            parse_tangled_remote_url(
                "git@knot1.tangled.sh:alice.example/demo.git"
            ),
            Some((
                "alice.example".to_string(),
                "demo".to_string(),
                "knot1.tangled.sh".to_string()
            ))
        );
    }

    #[test]
    fn parses_tangled_ssh_url_remotes() {
        assert_eq!(
            parse_tangled_remote_url(
                "ssh://git@knot1.tangled.sh/alice.example/demo.git"
            ),
            Some((
                "alice.example".to_string(),
                "demo".to_string(),
                "knot1.tangled.sh".to_string()
            ))
        );
    }

    #[test]
    fn rejects_non_tangled_and_extra_path_remotes() {
        assert_eq!(
            parse_tangled_remote_url("git@github.com:alice/demo.git"),
            None
        );
        assert_eq!(
            parse_tangled_remote_url("https://tangled.org/alice/demo/extra"),
            None
        );
    }
}