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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
use std::io::IsTerminal;
use std::path::PathBuf;
use std::process::Command;

use anyhow::{anyhow, Result};
use git2::build::{CheckoutBuilder, RepoBuilder};
use git2::{Cred, FetchOptions, RemoteCallbacks, Repository as GitRepository};
use inquire::{Confirm, Select, Text};
use serde::Serialize;

use crate::cli::{
    Cli, OutputFormat, RepoCloneArgs, RepoCommand, RepoCreateArgs,
    RepoDeleteArgs, RepoInfoArgs, RepoListArgs, RepoRefArgs,
};

pub async fn run(cli: &Cli, cmd: RepoCommand) -> Result<()> {
    match cmd {
        RepoCommand::List(args) => list(cli, args).await,
        RepoCommand::Create(args) => create(args).await,
        RepoCommand::Clone(args) => clone(args).await,
        RepoCommand::Info(args) => info(cli, args).await,
        RepoCommand::Delete(args) => delete(args).await,
        RepoCommand::Star(args) => star(args).await,
        RepoCommand::Unstar(args) => unstar(args).await,
    }
}

async fn list(cli: &Cli, args: RepoListArgs) -> Result<()> {
    let session = crate::util::load_session_with_refresh().await?;
    let auth = crate::ops::auth::PdsAuth::from_session(&session)?;

    // Use the PDS to list repo records for the user
    let pds = session
        .pds
        .clone()
        .or_else(|| std::env::var("TANGLED_PDS_BASE").ok())
        .unwrap_or_else(|| "https://bsky.social".into());
    // Default to the logged-in user handle if --user is not provided
    let effective_user =
        args.user.as_deref().unwrap_or(session.handle.as_str());
    let repos = crate::ops::repo::list_repos(
        &pds,
        Some(effective_user),
        args.knot.as_deref(),
        args.starred,
        &auth,
    )
    .await?;

    match cli.format {
        OutputFormat::Json | OutputFormat::Yaml => {
            crate::util::print_serialized(cli.format, &repos)?;
        }
        OutputFormat::Table => {
            crate::util::print_table(
                ["NAME", "KNOT", "VISIBILITY"],
                repos.into_iter().map(|repo| {
                    [
                        repo.name,
                        repo.knot.unwrap_or_default(),
                        if repo.private { "private" } else { "public" }
                            .to_string(),
                    ]
                }),
            );
        }
    }

    Ok(())
}

async fn create(mut args: RepoCreateArgs) -> Result<()> {
    let interactive = args.name.is_none() && std::io::stdin().is_terminal();
    let mode = if interactive {
        prompt_create_mode()?
    } else {
        RepoCreateMode::Scratch
    };
    let local_repo = if mode == RepoCreateMode::ExistingLocal {
        Some(GitRepository::discover(".")?)
    } else {
        None
    };

    let default_name = local_repo.as_ref().and_then(default_repo_name);
    let name = resolve_create_name(args.name.take(), default_name.as_deref())?;
    let description =
        resolve_create_description(args.description.take(), interactive)?;
    let default_branch = local_repo.as_ref().and_then(current_branch_name);

    let session = crate::util::load_session_with_refresh().await?;
    let auth = crate::ops::auth::PdsAuth::from_session(&session)?;

    // Determine PDS base and target knot hostname
    let pds = session
        .pds
        .clone()
        .or_else(|| std::env::var("TANGLED_PDS_BASE").ok())
        .unwrap_or_else(|| "https://bsky.social".into());
    let knot = args
        .knot
        .or_else(|| std::env::var("TANGLED_DEFAULT_KNOT").ok())
        .unwrap_or_else(|| crate::ops::DEFAULT_KNOT_HOST.to_string());
    let knot = knot
        .trim_end_matches('/')
        .trim_start_matches("https://")
        .trim_start_matches("http://")
        .to_string();
    let opts = crate::ops::types::CreateRepoOptions {
        did: &session.did,
        name: &name,
        knot: &knot,
        description: description.as_deref(),
        default_branch: default_branch.as_deref(),
        source: None,
        pds_base: &pds,
        auth: &auth,
    };
    crate::ops::repo::create_repo(&knot, opts).await?;

    println!("Created repo '{}' (knot: {})", name, knot);

    if let Some(repo) = local_repo.as_ref() {
        maybe_push_existing_repo(repo, &knot, &session.handle, &name).await?;
    }

    Ok(())
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum RepoCreateMode {
    Scratch,
    ExistingLocal,
}

fn prompt_create_mode() -> Result<RepoCreateMode> {
    let scratch = "Create a new repository on Tangled from scratch";
    let existing = "Push an existing local repository to Tangled";
    let selected =
        Select::new("What would you like to do?", vec![scratch, existing])
            .prompt()?;
    Ok(if selected == existing {
        RepoCreateMode::ExistingLocal
    } else {
        RepoCreateMode::Scratch
    })
}

fn resolve_create_name(
    provided: Option<String>,
    default: Option<&str>,
) -> Result<String> {
    let name = match provided {
        Some(name) => name,
        None if std::io::stdin().is_terminal() => {
            let prompt = Text::new("Repository name");
            match default {
                Some(default) if !default.is_empty() => {
                    prompt.with_default(default).prompt()?
                }
                _ => prompt.prompt()?,
            }
        }
        None => return Err(anyhow!("repository name is required")),
    };
    let name = name.trim().to_string();
    if name.is_empty() {
        return Err(anyhow!("repository name cannot be empty"));
    }
    Ok(name)
}

fn resolve_create_description(
    provided: Option<String>,
    interactive: bool,
) -> Result<Option<String>> {
    if provided.is_some() || !interactive {
        return Ok(provided);
    }
    let description = Text::new("Description (optional)").prompt()?;
    Ok((!description.trim().is_empty()).then_some(description))
}

fn default_repo_name(repo: &GitRepository) -> Option<String> {
    repo.workdir()
        .and_then(|path| path.file_name())
        .and_then(|name| name.to_str())
        .map(str::to_string)
}

fn current_branch_name(repo: &GitRepository) -> Option<String> {
    repo.head()
        .ok()
        .and_then(|head| head.shorthand().map(str::to_string))
}

async fn maybe_push_existing_repo(
    repo: &GitRepository,
    knot: &str,
    handle: &str,
    name: &str,
) -> Result<()> {
    let branch = current_branch_name(repo).ok_or_else(|| {
        anyhow!("cannot push local repository while HEAD is detached")
    })?;
    let remote_name = if repo.find_remote("origin").is_ok() {
        "tangled"
    } else {
        "origin"
    };
    let remote_url = ssh_remote_url(knot, handle, name);

    if !Confirm::new("Add a git remote and push the current branch?")
        .with_default(true)
        .prompt()?
    {
        println!("To push this repository later:");
        println!("  git remote add {} {}", remote_name, remote_url);
        println!("  git push -u {} {}", remote_name, branch);
        return Ok(());
    }

    match repo.find_remote(remote_name) {
        Ok(remote) => {
            let existing = remote.url().unwrap_or_default();
            if existing != remote_url {
                return Err(anyhow!(
                    "remote '{}' already exists with URL {}; expected {}",
                    remote_name,
                    existing,
                    remote_url
                ));
            }
        }
        Err(_) => {
            run_git_with_spinner(
                "Adding git remote...",
                vec!["remote", "add", remote_name, remote_url.as_str()],
            )
            .await?;
        }
    }
    run_git_with_spinner(
        "Pushing current branch...",
        vec!["push", "-u", remote_name, branch.as_str()],
    )
    .await?;
    println!("Pushed '{}' to remote '{}'.", branch, remote_name);
    Ok(())
}

fn ssh_remote_url(knot: &str, handle: &str, name: &str) -> String {
    let host = if knot == crate::ops::DEFAULT_KNOT_HOST {
        "tangled.org"
    } else {
        knot
    };
    format!("git@{}:{}/{}", host, handle.trim_start_matches('@'), name)
}

async fn run_git_with_spinner(message: &str, args: Vec<&str>) -> Result<()> {
    let owned = args.into_iter().map(str::to_string).collect::<Vec<_>>();
    let display = owned.join(" ");
    crate::progress::with_spinner(message.to_string(), async move {
        tokio::task::spawn_blocking(move || {
            let status = Command::new("git").args(&owned).status()?;
            if status.success() {
                Ok(())
            } else {
                Err(anyhow!("git {} failed with {}", display, status))
            }
        })
        .await?
    })
    .await
}

async fn clone(args: RepoCloneArgs) -> Result<()> {
    let session = crate::util::load_session_with_refresh().await?;
    let auth = crate::ops::auth::PdsAuth::from_session(&session)?;

    let (owner, name) = parse_repo_ref(&args.repo, &session.handle);
    let pds = session
        .pds
        .clone()
        .or_else(|| std::env::var("TANGLED_PDS_BASE").ok())
        .unwrap_or_else(|| "https://bsky.social".into());
    let info =
        crate::ops::repo::get_repo_info(&pds, owner, &name, &auth).await?;

    let remote = if args.https {
        let owner_path = if owner.starts_with('@') {
            owner.to_string()
        } else {
            format!("@{}", owner)
        };
        format!("https://tangled.org/{}/{}", owner_path, name)
    } else {
        let knot = if info.knot == "knot1.tangled.sh" {
            "tangled.org".to_string()
        } else {
            info.knot.clone()
        };
        format!("git@{}:{}/{}", knot, owner.trim_start_matches('@'), name)
    };

    let target = PathBuf::from(&name);
    println!("Cloning {} -> {:?}", remote, target);

    let pb = crate::progress::progress_bar("Cloning repository...");

    let mut callbacks = RemoteCallbacks::new();
    callbacks.credentials(|_url, username_from_url, _allowed| {
        if let Some(user) = username_from_url {
            Cred::ssh_key_from_agent(user)
        } else {
            Cred::default()
        }
    });
    let fetch_pb = pb.clone();
    callbacks.transfer_progress(move |stats| {
        let total = stats.total_objects() as u64;
        if total > 0 {
            fetch_pb.set_length(total);
            fetch_pb.set_position(stats.received_objects() as u64);
            fetch_pb.set_message("Receiving objects...");
        }
        true
    });

    let mut fetch_opts = FetchOptions::new();
    fetch_opts.remote_callbacks(callbacks);
    if let Some(d) = args.depth {
        fetch_opts.depth(d as i32);
    }

    let checkout_pb = pb.clone();
    let mut checkout = CheckoutBuilder::new();
    checkout.progress(move |_path, completed, total| {
        if total > 0 {
            checkout_pb.set_length(total as u64);
            checkout_pb.set_position(completed as u64);
            checkout_pb.set_message("Checking out files...");
        }
    });

    let mut builder = RepoBuilder::new();
    builder.fetch_options(fetch_opts);
    builder.with_checkout(checkout);
    let result = builder.clone(&remote, &target);
    pb.finish_and_clear();

    match result {
        Ok(_) => Ok(()),
        Err(e) => {
            println!("Failed to clone via libgit2: {}", e);
            println!(
                "Hint: try: git clone{} {}",
                args.depth
                    .map(|d| format!(" --depth {}", d))
                    .unwrap_or_default(),
                remote
            );
            Err(anyhow!(e.to_string()))
        }
    }
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct RepoInfoOutput {
    name: String,
    owner_did: String,
    rkey: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    repo_did: Option<String>,
    knot: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    spindle: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    source: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    default_branch: Option<crate::ops::types::DefaultBranch>,
    #[serde(skip_serializing_if = "Option::is_none")]
    languages: Option<crate::ops::types::Languages>,
}

async fn info(cli: &Cli, args: RepoInfoArgs) -> Result<()> {
    let session = crate::util::load_session_with_refresh().await?;
    let auth = crate::ops::auth::PdsAuth::from_session(&session)?;
    let (owner, name) = parse_repo_ref(&args.repo, &session.handle);
    let pds = session
        .pds
        .clone()
        .or_else(|| std::env::var("TANGLED_PDS_BASE").ok())
        .unwrap_or_else(|| "https://bsky.social".into());
    let info =
        crate::ops::repo::get_repo_info(&pds, owner, &name, &auth).await?;

    let (default_branch, languages) = if args.stats {
        let default_branch = crate::ops::repo::get_default_branch(
            &info.knot, &info.did, &info.name,
        )
        .await
        .ok();
        let languages =
            crate::ops::repo::get_languages(&info.knot, &info.did, &info.name)
                .await
                .ok();
        (default_branch, languages)
    } else {
        (None, None)
    };

    if matches!(cli.format, OutputFormat::Json | OutputFormat::Yaml) {
        let output = RepoInfoOutput {
            name: info.name,
            owner_did: info.did,
            rkey: info.rkey,
            repo_did: info.repo_did,
            knot: info.knot,
            spindle: info.spindle,
            description: info.description,
            source: info.source,
            default_branch,
            languages,
        };
        return crate::util::print_serialized(cli.format, &output);
    }

    println!("NAME:        {}", info.name);
    println!("OWNER DID:   {}", info.did);
    println!("KNOT:        {}", info.knot);
    if let Some(spindle) = info.spindle.as_deref().filter(|s| !s.is_empty()) {
        println!("SPINDLE:     {}", spindle);
    }
    if let Some(desc) = info.description.as_deref().filter(|s| !s.is_empty()) {
        println!("DESCRIPTION: {}", desc);
    }

    if let Some(def) = default_branch {
        println!(
            "DEFAULT BRANCH: {} ({})",
            def.name,
            def.short_hash.unwrap_or(def.hash)
        );
        if let Some(msg) = def.message.filter(|message| !message.is_empty()) {
            println!("LAST COMMIT:   {}", msg);
        }
    }
    if let Some(langs) = languages.filter(|langs| !langs.languages.is_empty()) {
        println!("LANGUAGES:");
        for language in langs.languages.iter().take(6) {
            println!("  - {} ({}%)", language.name, language.percentage);
        }
    }

    if args.contributors {
        println!("Contributors: not implemented yet");
    }
    Ok(())
}

async fn delete(args: RepoDeleteArgs) -> Result<()> {
    let session = crate::util::load_session_with_refresh().await?;
    let auth = crate::ops::auth::PdsAuth::from_session(&session)?;
    let (owner, name) = parse_repo_ref(&args.repo, &session.handle);
    let pds = session
        .pds
        .clone()
        .or_else(|| std::env::var("TANGLED_PDS_BASE").ok())
        .unwrap_or_else(|| "https://bsky.social".into());
    let record =
        crate::ops::repo::get_repo_info(&pds, owner, &name, &auth).await?;
    crate::ops::repo::delete_repo(
        &record.knot,
        &record.did,
        &name,
        &pds,
        &auth,
    )
    .await?;
    println!("Deleted repo '{}'", name);
    Ok(())
}

async fn star(args: RepoRefArgs) -> Result<()> {
    let session = crate::util::load_session_with_refresh().await?;
    let auth = crate::ops::auth::PdsAuth::from_session(&session)?;
    let (owner, name) = parse_repo_ref(&args.repo, &session.handle);
    let pds = session
        .pds
        .clone()
        .or_else(|| std::env::var("TANGLED_PDS_BASE").ok())
        .unwrap_or_else(|| "https://bsky.social".into());
    let info =
        crate::ops::repo::get_repo_info(&pds, owner, &name, &auth).await?;
    let subject = format!("at://{}/sh.tangled.repo/{}", info.did, info.rkey);
    crate::ops::repo::star_repo(&pds, &auth, &subject, &session.did).await?;
    println!("Starred {}/{}", owner, name);
    Ok(())
}

async fn unstar(args: RepoRefArgs) -> Result<()> {
    let session = crate::util::load_session_with_refresh().await?;
    let auth = crate::ops::auth::PdsAuth::from_session(&session)?;
    let (owner, name) = parse_repo_ref(&args.repo, &session.handle);
    let pds = session
        .pds
        .clone()
        .or_else(|| std::env::var("TANGLED_PDS_BASE").ok())
        .unwrap_or_else(|| "https://bsky.social".into());
    let info =
        crate::ops::repo::get_repo_info(&pds, owner, &name, &auth).await?;
    let subject = format!("at://{}/sh.tangled.repo/{}", info.did, info.rkey);
    crate::ops::repo::unstar_repo(&pds, &auth, &subject, &session.did).await?;
    println!("Unstarred {}/{}", owner, name);
    Ok(())
}

fn parse_repo_ref<'a>(
    spec: &'a str,
    default_owner: &'a str,
) -> (&'a str, String) {
    if let Some((owner, name)) = spec.split_once('/') {
        (owner, name.to_string())
    } else {
        (default_owner, spec.to_string())
    }
}