sley-remote 0.2.0

Callable fetch, push, clone, and ls-remote orchestration over the sley transport and object stack.
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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! `git-remote` — callable fetch / push / clone / ls-remote orchestration.
//!
//! This crate lifts the network-transport orchestration out of the `git-cli`
//! monolith so it can be driven as a library (the way a downstream consumer such
//! as heddle needs). The wire codecs ([`sley_protocol`]), the pack encoder
//! ([`sley_pack`]), pack building ([`sley_odb`]) and ref/commit plumbing already
//! live in their own crates; `git-remote` is the glue that sequences them into
//! `fetch`/`push`/`clone`/`ls-remote`, with the CLI-specific concerns (argument
//! parsing, stdout/stderr formatting, exit codes, repository discovery from
//! process-global state) kept out via the seams below:
//!
//! * [`CredentialProvider`] — how authenticated remotes obtain credentials. The
//!   caller injects one (e.g. a credential-helper-backed impl, an interactive
//!   prompt, or [`NoCredentials`] for unauthenticated/public access).
//! * [`ProgressSink`] — where human-facing progress/summary lines go. The
//!   orchestration returns structured outcomes and emits progress through this
//!   sink instead of printing, so the caller controls presentation.
//!
//! The lift proceeds in stages (see `docs/git-remote-extraction.md`); this is
//! the scaffold (stage A).

use std::path::Path;

use sley_config::GitConfig;
use sley_core::{ObjectFormat, Result};
use sley_transport::GitCredential;

mod credentials;
pub use credentials::{
    CredentialHelperProvider, credential_fill, credential_request_for_url, credential_store,
    http_credential_host, http_protocol_name, http_url_credential,
};

#[cfg(feature = "http")]
mod http;
#[cfg(feature = "http")]
pub use http::{
    HttpFetchPackRequest, HttpServiceAdvertisements, http_advertised_refs,
    http_authorization_headers, http_check_status, http_protocol_v2_fetch_response,
    http_send_with_auth, http_service_advertisements, http_upload_pack_advertisements,
    http_upload_pack_fetch_response, http_upload_pack_shallow_fetch_response,
    http_validate_content_type, install_fetch_pack_via_http_protocol_v2_fetch,
    install_fetch_pack_via_http_upload_pack, new_http_client, remote_url_is_http,
};

mod ssh;
pub use ssh::{
    SshFetchPackRequest, SshTransportOptions, install_fetch_pack_via_ssh_upload_pack, ssh_program,
    ssh_transport_options_from_config, ssh_upload_pack_advertisements,
    ssh_upload_pack_advertisements_with_options, ssh_upload_pack_fetch_response,
    ssh_upload_pack_shallow_fetch_response,
};

mod git;
pub use git::{
    GitFetchPackRequest, git_upload_pack_advertisements,
    git_upload_pack_advertisements_with_protocol, install_fetch_pack_via_git_upload_pack,
};

mod local;
pub use local::{
    INFINITE_DEPTH, LocalDeepenPlan, attach_receive_pack_capabilities,
    attach_upload_pack_capabilities, compute_local_deepen, compute_local_deepen_by_rev_list,
    install_fetch_pack_via_local_upload_pack, local_fetch_advertisements, local_have_oids,
    receive_pack_features, receive_pack_into_local_repository,
    receive_pack_request_uses_push_options, serve_upload_pack_v2, serve_upload_pack_v2_with_config,
    upload_pack_features, upload_pack_from_local_repository, upload_pack_request_uses_sideband,
    upload_pack_sideband_response,
};

mod fetch;
pub use fetch::{
    FetchOptions, FetchOutcome, FetchRequest, FetchServices, FetchSource, PruneRefsInput,
    PrunedRef,
    append_reachable_auto_follow_tags, apply_configured_fetch_prune_option,
    apply_configured_remote_tag_option, fetch, fetch_head_source_description,
    fetch_refspec_excludes, fetch_refspecs_for_source, mark_tag_refspec_updates_not_for_merge,
    order_bundle_fetch_all_tags_updates, prune_refs_from_advertisements,
    retain_missing_auto_follow_tags, write_default_fetch_head, write_fetch_head,
    write_fetch_head_records,
};

mod pack;
pub use pack::{
    PushPackRequest, build_push_packfile, build_receive_pack_body,
    remote_advertisement_tips_known_to_local,
};

mod push;
pub use push::{
    PushAction, PushActionPlan, PushActionRequest, PushCommand, PushDestination, PushOptions,
    PushOutcome, PushPlan, PushRefStatus, PushReportRef, PushReportRequest, PushRequest,
    PushServices, PushStatusReport, execute_push_action_plan, execute_push_plan,
    local_push_source_refs, normalize_push_refname, normalize_push_refspec, plan_push,
    plan_push_actions, push, push_actions, push_local_with_report, reject_non_fast_forward_pushes,
    validate_receive_pack_report,
};

mod ls_remote;
pub use ls_remote::{LsRemoteFilter, LsRemoteRecord, LsRemoteSource, ls_remote};

mod clone;
pub use clone::{CloneOptions, CloneOutcome, CloneRequest, CloneServices, CloneSource, clone};

mod bundle;
pub use bundle::{FetchBundleRequest, fetch_bundle};

mod shallow;
pub use shallow::{apply_shallow_info, read_shallow, write_shallow};

mod capabilities;
pub use capabilities::{
    BUNDLE_FETCH_SUPPORTED, HTTP_PROTOCOL_V2_FETCH, RemoteTransportKind, SSH_CLONE_SUPPORTED,
    THIN_PACK_PUSH_SUPPORTED, TransportCapabilities,
};

mod protocol;
pub use protocol::{
    TransportPolicyError, check_transport_allowed, is_transport_allowed,
    transport_scheme_for_remote, transport_scheme_for_url,
};

mod resolve;
pub use resolve::{
    fetch_source_for_url, fetch_url, push_destination_for_url, push_url, resolve_fetch_source,
    resolve_push_destination, transport_kind_for_url,
};

/// The object format of the repository whose common `$GIT_DIR` is `common_git_dir`.
///
/// Reads `common_git_dir/config`'s `extensions.objectFormat`, defaulting to
/// SHA-1 when the config is absent or unreadable (matching git). `common_git_dir`
/// must already be the common git dir; this does no worktree resolution.
pub fn object_format_for_git_dir(common_git_dir: &Path) -> Result<ObjectFormat> {
    let Ok(config) = GitConfig::read(common_git_dir.join("config")) else {
        return Ok(ObjectFormat::Sha1);
    };
    config.repository_object_format()
}

/// Supplies credentials for an authenticated remote, mirroring git's credential
/// protocol: [`fill`](CredentialProvider::fill) is handed a partial
/// [`GitCredential`] describing the request (protocol/host/path) and returns a
/// completed credential, or `None` to proceed unauthenticated.
///
/// [`approve`](CredentialProvider::approve) / [`reject`](CredentialProvider::reject)
/// let a backing store remember or forget a credential after the request
/// succeeds or fails; the default no-ops suit providers without a store.
pub trait CredentialProvider {
    /// Complete `request` into a usable credential, or return `None` to attempt
    /// the request without authentication.
    fn fill(&mut self, request: GitCredential) -> Result<Option<GitCredential>>;

    /// Record `credential` as having worked (e.g. store it). Default: no-op.
    fn approve(&mut self, _credential: &GitCredential) -> Result<()> {
        Ok(())
    }

    /// Record `credential` as having failed (e.g. erase it). Default: no-op.
    fn reject(&mut self, _credential: &GitCredential) -> Result<()> {
        Ok(())
    }
}

/// A [`CredentialProvider`] that never supplies credentials, so every request is
/// attempted unauthenticated. This is what an embedder targeting public remotes
/// (e.g. heddle) uses to suppress prompts.
#[derive(Debug, Default, Clone, Copy)]
pub struct NoCredentials;

impl CredentialProvider for NoCredentials {
    fn fill(&mut self, _request: GitCredential) -> Result<Option<GitCredential>> {
        Ok(None)
    }
}

/// Receives human-facing progress and summary events from an operation (the
/// "To <remote>" push summary, prune notices, "Cloning into…", etc.). The
/// orchestration returns structured outcomes regardless; this is purely for
/// presentation, so the default implementations discard everything.
pub trait ProgressSink {
    /// A free-form progress or summary line.
    fn message(&mut self, _message: &str) {}
}

/// A [`ProgressSink`] that discards every event.
#[derive(Debug, Default, Clone, Copy)]
pub struct SilentProgress;

impl ProgressSink for SilentProgress {}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::{Path, PathBuf};
    use std::sync::atomic::{AtomicU64, Ordering};

    use sley_config::{ConfigEntry, ConfigSection};
    use sley_formats::RepositoryLayout;
    use sley_object::{Commit, EncodedObject, ObjectType, Tree};
    use sley_odb::{FileObjectDatabase, ObjectWriter};
    use sley_refs::{FileRefStore, RefTarget, RefUpdate};
    use sley_transport::{RemoteUrl, parse_remote_url};

    #[test]
    fn no_credentials_never_fills() {
        let mut provider = NoCredentials;
        let request = GitCredential::default();
        assert!(
            provider
                .fill(request)
                .expect("test operation should succeed")
                .is_none()
        );
    }

    #[test]
    fn silent_progress_accepts_messages() {
        let mut progress = SilentProgress;
        progress.message("Cloning into 'x'...");
    }

    static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);

    fn live_env(name: &str) -> Option<String> {
        match std::env::var(name) {
            Ok(value) if !value.is_empty() => Some(value),
            _ => None,
        }
    }

    fn live_repo(name: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "sley-remote-live-{name}-{}-{}",
            std::process::id(),
            TEMP_COUNTER.fetch_add(1, Ordering::Relaxed)
        ));
        let _ = fs::remove_dir_all(&dir);
        RepositoryLayout::init_at(&dir, ObjectFormat::Sha1, false)
            .expect("live test repository should initialize");
        dir.join(".git")
    }

    fn remote_config(url: &str) -> GitConfig {
        GitConfig {
            sections: vec![ConfigSection::new(
                "remote",
                Some("origin".into()),
                vec![
                    ConfigEntry::new("url", Some(url.into())),
                    ConfigEntry::new("fetch", Some("+refs/heads/*:refs/remotes/origin/*".into())),
                ],
            )],
            ..GitConfig::default()
        }
    }

    fn fetch_options(depth: Option<u32>) -> FetchOptions {
        FetchOptions {
            quiet: true,
            auto_follow_tags: false,
            fetch_all_tags: false,
            prune: false,
            prune_tags: false,
            dry_run: false,
            append: false,
            write_fetch_head: true,
            tag_option_explicit: true,
            prune_option_explicit: true,
            prune_tags_option_explicit: true,
            refmap: None,
            depth,
            merge_srcs: Vec::new(),
            filter: None,
            refetch: false,
            cloning: false,
            record_promisor_refs: true,
            update_shallow: false,
            deepen_relative: false,
            update_head_ok: false,
            deepen_since: None,
            deepen_not: Vec::new(),
            ssh_options: None,
        }
    }

    fn write_live_commit(git_dir: &Path, branch: &str) {
        let format = ObjectFormat::Sha1;
        let db = FileObjectDatabase::from_git_dir(git_dir, format);
        let tree = db
            .write_object(EncodedObject::new(
                ObjectType::Tree,
                Tree { entries: vec![] }.write(),
            ))
            .expect("live commit tree should write");
        let timestamp = 1 + TEMP_COUNTER.fetch_add(1, Ordering::Relaxed);
        let identity =
            format!("Sley Remote Live <sley@example.invalid> {timestamp} +0000").into_bytes();
        let oid = db
            .write_object(EncodedObject::new(
                ObjectType::Commit,
                Commit {
                    tree,
                    parents: Vec::new(),
                    author: identity.clone(),
                    committer: identity,
                    encoding: None,
                    message: format!("sley remote live {branch}\n").into_bytes(),
                }
                .write(),
            ))
            .expect("live commit should write");
        let store = FileRefStore::new(git_dir, format);
        let mut tx = store.transaction();
        tx.update(RefUpdate {
            name: format!("refs/heads/{branch}"),
            expected: None,
            new: RefTarget::Direct(oid),
            reflog: None,
        });
        tx.update(RefUpdate {
            name: "HEAD".into(),
            expected: None,
            new: RefTarget::Symbolic(format!("refs/heads/{branch}")),
            reflog: None,
        });
        tx.commit().expect("live refs should update");
    }

    struct EnvCredentials {
        username: String,
        password: String,
    }

    impl CredentialProvider for EnvCredentials {
        fn fill(&mut self, mut request: GitCredential) -> Result<Option<GitCredential>> {
            request.username = Some(self.username.clone());
            request.password = Some(self.password.clone());
            Ok(Some(request))
        }
    }

    fn live_fetch(
        url_var: &str,
        branch_var: &str,
        source: FetchSource,
        credentials: &mut dyn CredentialProvider,
        depth: Option<u32>,
    ) {
        let Some(url) = live_env(url_var) else {
            return;
        };
        let branch = live_env(branch_var).unwrap_or_else(|| "main".into());
        let local = live_repo(url_var);
        let refspec = format!("refs/heads/{branch}:refs/remotes/origin/{branch}");
        let config = remote_config(&url);
        let options = fetch_options(depth);
        let mut progress = SilentProgress;

        let outcome = fetch(
            FetchRequest {
                git_dir: &local,
                format: ObjectFormat::Sha1,
                config: &config,
                remote_name: "origin",
                source: &source,
                refspecs: &[refspec],
                options: &options,
            },
            FetchServices {
                credentials,
                progress: &mut progress,
            },
        )
        .expect("live fetch should succeed");

        assert!(!outcome.ref_updates.is_empty());
        if depth.is_some() {
            assert!(
                local.join("shallow").exists(),
                "shallow fetch should write .git/shallow"
            );
        }
    }

    fn live_push(
        url_var: &str,
        branch_prefix_var: &str,
        destination: PushDestination,
        credentials: &mut dyn CredentialProvider,
    ) {
        let Some(_) = live_env(url_var) else {
            return;
        };
        let branch_prefix =
            live_env(branch_prefix_var).unwrap_or_else(|| "sley-remote-live".into());
        let branch = format!(
            "{branch_prefix}-{}-{}",
            std::process::id(),
            TEMP_COUNTER.fetch_add(1, Ordering::Relaxed)
        );
        let local = live_repo(url_var);
        write_live_commit(&local, &branch);
        let refspec = format!("refs/heads/{branch}:refs/heads/{branch}");
        let options = PushOptions {
            quiet: true,
            force: false,
        };
        let mut progress = SilentProgress;

        let outcome = push(
            PushRequest {
                git_dir: &local,
                common_git_dir: &local,
                format: ObjectFormat::Sha1,
                config: &GitConfig::default(),
                remote: "origin",
                destination: &destination,
                refspecs: &[refspec],
                options: &options,
            },
            PushServices {
                credentials,
                progress: &mut progress,
            },
        )
        .expect("live push should succeed");

        assert_eq!(outcome.commands.len(), 1);
    }

    #[test]
    fn live_github_https_public_fetch() {
        let Some(url) = live_env("SLEY_REMOTE_LIVE_GITHUB_HTTPS_PUBLIC_URL") else {
            return;
        };
        let remote = parse_remote_url(&url).expect("live HTTPS URL should parse");
        let mut credentials = NoCredentials;
        live_fetch(
            "SLEY_REMOTE_LIVE_GITHUB_HTTPS_PUBLIC_URL",
            "SLEY_REMOTE_LIVE_GITHUB_HTTPS_PUBLIC_BRANCH",
            FetchSource::Http(remote),
            &mut credentials,
            None,
        );
    }

    #[test]
    fn live_private_https_auth_fetch_uses_credential_provider() {
        let (Some(url), Some(username), Some(password)) = (
            live_env("SLEY_REMOTE_LIVE_PRIVATE_HTTPS_URL"),
            live_env("SLEY_REMOTE_LIVE_PRIVATE_HTTPS_USERNAME"),
            live_env("SLEY_REMOTE_LIVE_PRIVATE_HTTPS_PASSWORD"),
        ) else {
            return;
        };
        let remote = parse_remote_url(&url).expect("live private HTTPS URL should parse");
        let mut credentials = EnvCredentials { username, password };
        live_fetch(
            "SLEY_REMOTE_LIVE_PRIVATE_HTTPS_URL",
            "SLEY_REMOTE_LIVE_PRIVATE_HTTPS_BRANCH",
            FetchSource::Http(remote),
            &mut credentials,
            None,
        );
    }

    #[test]
    fn live_https_push() {
        let Some(url) = live_env("SLEY_REMOTE_LIVE_HTTPS_PUSH_URL") else {
            return;
        };
        let remote = parse_remote_url(&url).expect("live HTTPS push URL should parse");
        let mut no_credentials;
        let mut env_credentials;
        let credentials: &mut dyn CredentialProvider = match (
            live_env("SLEY_REMOTE_LIVE_HTTPS_PUSH_USERNAME"),
            live_env("SLEY_REMOTE_LIVE_HTTPS_PUSH_PASSWORD"),
        ) {
            (Some(username), Some(password)) => {
                env_credentials = EnvCredentials { username, password };
                &mut env_credentials
            }
            _ => {
                no_credentials = NoCredentials;
                &mut no_credentials
            }
        };
        live_push(
            "SLEY_REMOTE_LIVE_HTTPS_PUSH_URL",
            "SLEY_REMOTE_LIVE_HTTPS_PUSH_BRANCH_PREFIX",
            PushDestination::Http(remote),
            credentials,
        );
    }

    #[test]
    fn live_ssh_fetch() {
        let Some(url) = live_env("SLEY_REMOTE_LIVE_SSH_FETCH_URL") else {
            return;
        };
        let remote = parse_remote_url(&url).expect("live SSH fetch URL should parse");
        let mut credentials = NoCredentials;
        live_fetch(
            "SLEY_REMOTE_LIVE_SSH_FETCH_URL",
            "SLEY_REMOTE_LIVE_SSH_FETCH_BRANCH",
            FetchSource::Ssh(remote),
            &mut credentials,
            None,
        );
    }

    #[test]
    fn live_ssh_push() {
        let Some(url) = live_env("SLEY_REMOTE_LIVE_SSH_PUSH_URL") else {
            return;
        };
        let remote = parse_remote_url(&url).expect("live SSH push URL should parse");
        let mut credentials = NoCredentials;
        live_push(
            "SLEY_REMOTE_LIVE_SSH_PUSH_URL",
            "SLEY_REMOTE_LIVE_SSH_PUSH_BRANCH_PREFIX",
            PushDestination::Ssh(remote),
            &mut credentials,
        );
    }

    #[test]
    fn live_shallow_https_fetch_and_clone() {
        let Some(url) = live_env("SLEY_REMOTE_LIVE_SHALLOW_HTTPS_URL") else {
            return;
        };
        let branch =
            live_env("SLEY_REMOTE_LIVE_SHALLOW_HTTPS_BRANCH").unwrap_or_else(|| "main".into());
        let remote = parse_remote_url(&url).expect("live shallow HTTPS URL should parse");
        let mut credentials = NoCredentials;
        live_fetch(
            "SLEY_REMOTE_LIVE_SHALLOW_HTTPS_URL",
            "SLEY_REMOTE_LIVE_SHALLOW_HTTPS_BRANCH",
            FetchSource::Http(remote.clone()),
            &mut credentials,
            Some(1),
        );

        let destination = std::env::temp_dir().join(format!(
            "sley-remote-live-clone-{}-{}",
            std::process::id(),
            TEMP_COUNTER.fetch_add(1, Ordering::Relaxed)
        ));
        let _ = fs::remove_dir_all(&destination);
        let config = remote_config(&url);
        let mut configure = |_git_dir: &Path| Ok(config.clone());
        let mut configure_branch = |_git_dir: &Path, _branch: &str| Ok(config.clone());
        let options = CloneOptions {
            origin: "origin",
            checkout_branch: &branch,
            remote_head_branch: &branch,
            single_branch: true,
            depth: Some(1),
            deepen_since: None,
            deepen_not: Vec::new(),
            committer: b"Sley Remote Live <sley@example.invalid> 1 +0000".to_vec(),
            detached_head: None,
            checkout: true,
            filter: None,
            // The live test clones a specific branch via --single-branch, so the
            // branch was explicitly requested (a missing remote tip is a hard error).
            branch_explicit: true,
            ref_storage: sley_formats::RefStorageFormat::Files,
            ssh_options: None,
        };
        let mut clone_credentials = NoCredentials;
        let mut progress = SilentProgress;

        let outcome = clone(
            CloneRequest {
                destination: &destination,
                git_dir_override: None,
                core_worktree: None,
                format: ObjectFormat::Sha1,
                source: &CloneSource::Http(RemoteUrl { ..remote }),
                options: &options,
            },
            CloneServices {
                configure: &mut configure,
                configure_branch: &mut configure_branch,
                credentials: &mut clone_credentials,
                progress: &mut progress,
            },
        )
        .expect("live shallow HTTPS clone should succeed");

        assert!(outcome.git_dir.join("shallow").exists());
    }
}