lux-lib 0.56.0

Library for the lux package manager for Lua
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
use crate::build::utils::recursive_copy_dir;
use crate::config::Config;
use crate::git::url::RemoteGitUrlParseError;
use crate::git::GitSource;
use crate::hash::HasIntegrity;
use crate::lockfile::RemotePackageSourceUrl;
use crate::lua_rockspec::RockSourceSpec;
use crate::package::PackageSpec;
use crate::rockspec::Rockspec;
use crate::{fs, operations};
use auth_git2::{GitAuthenticator, Prompter};
use bon::Builder;
use bytes::Bytes;
use git2::build::RepoBuilder;
use git2::{Direction, FetchOptions, RemoteCallbacks};
use miette::Diagnostic;
use remove_dir_all::remove_dir_all;
use ssri::Integrity;
use std::io;
use std::io::Cursor;
use std::io::Read;
use std::path::Path;
use std::path::PathBuf;
use thiserror::Error;
use tracing::span;

use super::DownloadSrcRockError;
use super::UnpackError;

/// A rocks package source fetcher, providing fine-grained control
/// over how a package should be fetched.
#[derive(Builder)]
#[builder(start_fn = new, finish_fn(name = _build, vis = ""))]
pub struct FetchSrc<'a, R: Rockspec> {
    #[builder(start_fn)]
    dest_dir: &'a Path,
    #[builder(start_fn)]
    rockspec: &'a R,
    #[builder(start_fn)]
    config: &'a Config,
    #[builder(setters(vis = "pub(crate)"))]
    source_url: Option<RemotePackageSourceUrl>,
}

#[derive(Debug)]
pub(crate) struct RemotePackageSourceMetadata {
    pub hash: Integrity,
    pub source_url: RemotePackageSourceUrl,
}

impl<R: Rockspec, State> FetchSrcBuilder<'_, R, State>
where
    State: fetch_src_builder::State + fetch_src_builder::IsComplete,
{
    /// Fetch and unpack the source into the `dest_dir`.
    pub async fn fetch(self) -> Result<(), FetchSrcError> {
        self.fetch_internal().await?;
        Ok(())
    }

    /// Fetch and unpack the source into the `dest_dir`,
    /// returning the source `Integrity`.
    pub(crate) async fn fetch_internal(self) -> Result<RemotePackageSourceMetadata, FetchSrcError> {
        let fetch = self._build();
        match do_fetch_src(&fetch).await {
            Err(err)
                if fetch
                    .source_url
                    .is_some_and(|url| matches!(url, RemotePackageSourceUrl::File { .. })) =>
            {
                // Don't fall back to downloading .src.rock archives if a local source was specified.
                Err(err)
            }
            Err(err) => match &fetch.rockspec.source().current_platform().source_spec {
                RockSourceSpec::Git(_) | RockSourceSpec::Url(_) => {
                    let package = PackageSpec::new(
                        fetch.rockspec.package().clone(),
                        fetch.rockspec.version().clone(),
                    );
                    let metadata = FetchSrcRock::new(&package, fetch.dest_dir, fetch.config)
                        .fetch()
                        .await?;
                    Ok(metadata)
                }
                RockSourceSpec::File(_) => Err(err),
            },
            Ok(metadata) => Ok(metadata),
        }
    }
}

#[derive(Error, Debug, Diagnostic)]
#[non_exhaustive]
pub enum FetchSrcError {
    #[error("failed to clone rock source:\n{0}")]
    #[diagnostic(help("check your network connection and verify the git URL is correct."))]
    GitClone(#[from] git2::Error),
    #[error("failed to parse git URL:\n{0}")]
    #[diagnostic(forward(0))]
    GitUrlParse(#[from] RemoteGitUrlParseError),
    #[error(transparent)]
    #[diagnostic(help("check your network connection."))]
    Request(#[from] reqwest::Error),
    #[error(transparent)]
    #[diagnostic(transparent)]
    Unpack(#[from] UnpackError),
    #[error(transparent)]
    #[diagnostic(transparent)]
    FetchSrcRock(#[from] FetchSrcRockError),
    #[error("unable to remove the '.git' directory:\n{0}")]
    #[diagnostic(help(
        "check that no process is using the directory and you have write permissions."
    ))]
    CleanGitDir(io::Error),
    #[error("unable to compute hash:\n{0}")]
    Hash(io::Error),
    #[error(transparent)]
    #[diagnostic(transparent)]
    Fs(#[from] fs::FsError),
}

/// A rocks package source fetcher, providing fine-grained control
/// over how a package should be fetched.
#[derive(Builder)]
#[builder(start_fn = new, finish_fn(name = _build, vis = ""))]
struct FetchSrcRock<'a> {
    #[builder(start_fn)]
    package: &'a PackageSpec,
    #[builder(start_fn)]
    dest_dir: &'a Path,
    #[builder(start_fn)]
    config: &'a Config,
}

impl<State> FetchSrcRockBuilder<'_, State>
where
    State: fetch_src_rock_builder::State + fetch_src_rock_builder::IsComplete,
{
    pub async fn fetch(self) -> Result<RemotePackageSourceMetadata, FetchSrcRockError> {
        do_fetch_src_rock(self._build()).await
    }
}

#[derive(Error, Debug, Diagnostic)]
#[non_exhaustive]
#[error(transparent)]
pub enum FetchSrcRockError {
    DownloadSrcRock(#[from] DownloadSrcRockError),
    Unpack(#[from] UnpackError),
    Io(#[from] io::Error),
}

/// A no-prompt implementer for auth_git2's prompter
#[derive(Copy, Clone, Debug)]
struct NullPrompter;

impl Prompter for NullPrompter {
    fn prompt_username_password(&mut self, _: &str, _: &git2::Config) -> Option<(String, String)> {
        None
    }

    fn prompt_password(&mut self, _: &str, _: &str, _: &git2::Config) -> Option<String> {
        None
    }

    fn prompt_ssh_key_passphrase(&mut self, _: &Path, _: &git2::Config) -> Option<String> {
        None
    }
}

async fn do_fetch_src<R: Rockspec>(
    fetch: &FetchSrc<'_, R>,
) -> Result<RemotePackageSourceMetadata, FetchSrcError> {
    let rockspec = fetch.rockspec;
    let rock_source = rockspec.source().current_platform();
    let dest_dir = fetch.dest_dir;
    let config = fetch.config;
    // prioritise lockfile source, if present
    let mut source_spec = match &fetch.source_url {
        Some(source_url) => match source_url {
            RemotePackageSourceUrl::Git { url, checkout_ref } => RockSourceSpec::Git(GitSource {
                url: url.parse()?,
                checkout_ref: Some(checkout_ref.clone()),
            }),
            RemotePackageSourceUrl::Url { url } => RockSourceSpec::Url(url.clone()),
            RemotePackageSourceUrl::File { path } => RockSourceSpec::File(path.clone()),
        },
        None => rock_source.source_spec.clone(),
    };
    let span = span!(
        tracing::Level::INFO,
        "Fetching source",
        location = source_spec.to_string(),
    );
    let _enter = span.enter();

    if let Some(vendor_dir) = config.vendor_dir() {
        source_spec = match source_spec {
            // could be a project directory (not vendored) or a local source
            // or a vendored dependency that we have already resolved
            RockSourceSpec::File(_) => source_spec,
            _ => {
                let pkg_vendor_dir =
                    vendor_dir.join(format!("{}@{}", rockspec.package(), rockspec.version()));
                RockSourceSpec::File(pkg_vendor_dir)
            }
        }
    }
    let metadata = match &source_spec {
        RockSourceSpec::Git(git) => {
            let url = git.url.to_string();
            tracing::debug!(message = format!("Cloning {url}").as_str());

            let resolved_ref = resolve_remote_ref(&url, git.checkout_ref.as_deref(), config);

            if let Some(oid) = resolved_ref {
                let cache_dir = source_cache_dir(config, &url).join(oid.to_string());
                if fs::sync::read_dir(&cache_dir).is_ok_and(|mut entries| entries.next().is_some())
                {
                    tracing::debug!("using cached git source");
                    recursive_copy_dir_no_ignore(&cache_dir, dest_dir).await?;
                    let hash = fetch.dest_dir.hash().await.map_err(FetchSrcError::Hash)?;
                    let checkout_ref = git.checkout_ref.clone().unwrap_or(oid.to_string());
                    return Ok(RemotePackageSourceMetadata {
                        hash,
                        source_url: RemotePackageSourceUrl::Git { url, checkout_ref },
                    });
                }
            }
            tracing::debug!("fetching git source");

            let checkout_ref = {
                let auth = if config.no_prompt() {
                    GitAuthenticator::default()
                        .try_password_prompt(0)
                        .prompt_ssh_key_password(false)
                        .set_prompter(NullPrompter)
                } else {
                    GitAuthenticator::default()
                };
                let git_config = git2::Config::open_default()?;
                let mut callbacks = RemoteCallbacks::new();
                callbacks.credentials(auth.credentials(&git_config));
                let mut fetch_options = FetchOptions::new();
                fetch_options.update_fetchhead(false);
                fetch_options.remote_callbacks(callbacks);
                if git.checkout_ref.is_none() {
                    fetch_options.depth(1);
                };
                let mut repo_builder = RepoBuilder::new();
                repo_builder.fetch_options(fetch_options);
                let repo = repo_builder.clone(&url, dest_dir)?;

                match &git.checkout_ref {
                    Some(checkout_ref) => {
                        let (object, _) = repo.revparse_ext(checkout_ref)?;
                        repo.checkout_tree(&object, None)?;
                        checkout_ref.clone()
                    }
                    None => {
                        let head = repo.head()?;
                        let commit = head.peel_to_commit()?;
                        commit.id().to_string()
                    }
                }
            };
            // The .git directory is not deterministic
            remove_dir_all(dest_dir.join(".git")).map_err(FetchSrcError::CleanGitDir)?;

            if let Some(oid) = resolved_ref {
                populate_source_cache(
                    dest_dir,
                    &source_cache_dir(config, &url).join(oid.to_string()),
                )
                .await;
            }

            let hash = fetch.dest_dir.hash().await.map_err(FetchSrcError::Hash)?;
            RemotePackageSourceMetadata {
                hash,
                source_url: RemotePackageSourceUrl::Git { url, checkout_ref },
            }
        }
        RockSourceSpec::Url(url) => {
            tracing::debug!(message = format!("📥 Downloading {url}").as_str());

            let cache_path = source_cache_dir(config, url.as_ref()).join("archive");
            let response = match fs::tokio::read(&cache_path).await {
                Ok(bytes) => {
                    tracing::debug!("using cached source archive");
                    Bytes::from(bytes)
                }
                Err(_) => {
                    tracing::debug!("fetching source archive");
                    // NOTE: We don't enforce HTTPS when fetching sources because some rockspecs
                    // have HTTP URLs in `source.url`.
                    let response = crate::reqwest::http_client(config)?
                        .get(url.clone())
                        .send()
                        .await?
                        .error_for_status()?
                        .bytes()
                        .await?;
                    write_source_cache_archive(&cache_path, &response).await;
                    response
                }
            };
            let hash = response.hash().await.map_err(FetchSrcError::Hash)?;
            let file_name = url
                .path_segments()
                .and_then(|mut segments| segments.next_back())
                .and_then(|name| {
                    if name.is_empty() {
                        None
                    } else {
                        Some(name.to_string())
                    }
                })
                .unwrap_or(url.to_string());
            let cursor = Cursor::new(response);
            let mime_type = infer::get(cursor.get_ref()).map(|file_type| file_type.mime_type());
            operations::unpack::unpack(
                mime_type,
                cursor,
                rock_source.unpack_dir.is_none(),
                file_name,
                dest_dir,
            )
            .await?;
            RemotePackageSourceMetadata {
                hash,
                source_url: RemotePackageSourceUrl::Url { url: url.clone() },
            }
        }
        RockSourceSpec::File(path) => {
            tracing::debug!(message = format!("📋 Copying {}", path.display()).as_str());

            let hash = if path.is_dir() {
                recursive_copy_dir(&path.to_path_buf(), dest_dir).await?;
                dest_dir.hash().await.map_err(FetchSrcError::Hash)?
            } else {
                let mut file = fs::sync::open(path)?;
                let mut buffer = Vec::new();
                file.read_to_end(&mut buffer)
                    .map_err(|source| fs::FsError::Read {
                        path: path.to_path_buf(),
                        source,
                    })?;
                let mime_type = infer::get(&buffer).map(|file_type| file_type.mime_type());
                let file_name = path
                    .file_name()
                    .map(|os_str| os_str.to_string_lossy())
                    .unwrap_or(path.to_string_lossy())
                    .to_string();
                operations::unpack::unpack(
                    mime_type,
                    file,
                    rock_source.unpack_dir.is_none(),
                    file_name,
                    dest_dir,
                )
                .await?;
                path.hash().await.map_err(FetchSrcError::Hash)?
            };
            RemotePackageSourceMetadata {
                hash,
                source_url: RemotePackageSourceUrl::File { path: path.clone() },
            }
        }
    };
    Ok(metadata)
}

/// Directory in which fetched sources are cached, keyed by source URL.
fn source_cache_dir(config: &Config, url: &str) -> PathBuf {
    config.cache_dir().join("sources").join(sanitize_url(url))
}

fn sanitize_url(url: &str) -> String {
    url.replace(&[':', '*', '?', '"', '<', '>', '|', '/', '\\'][..], "_")
}

/// Recursively copy a directory.
/// Unlike [`crate::build::utils::recursive_copy_dir`], this does not respect ignore files.
#[tracing::instrument(level = "trace")]
async fn recursive_copy_dir_no_ignore(src: &Path, dest: &Path) -> Result<(), fs::FsError> {
    let mut dirs: Vec<PathBuf> = vec![src.to_path_buf()];
    while let Some(dir) = dirs.pop() {
        for entry in fs::sync::read_dir(&dir)?.filter_map(Result::ok) {
            let entry_path = entry.path();
            let relative_path: PathBuf = pathdiff::diff_paths(&entry_path, src)
                .unwrap_or_else(|| unreachable!("diff_path with self"));
            let target = dest.join(relative_path);
            let file_type = entry.file_type().map_err(|source| fs::FsError::Read {
                path: entry_path.clone(),
                source,
            })?;
            if file_type.is_dir() {
                fs::tokio::create_dir_all(&target).await?;
                dirs.push(entry_path);
            } else if file_type.is_file() {
                if let Some(parent) = target.parent() {
                    fs::tokio::create_dir_all(parent).await?;
                }
                fs::tokio::copy(&entry_path, &target).await?;
            }
        }
    }
    Ok(())
}

/// Copy the fetched source into the cache, atomically (best-effort).
#[tracing::instrument(level = "trace")]
async fn populate_source_cache(dest_dir: &Path, cache_dir: &Path) {
    let Some(parent) = cache_dir.parent() else {
        return;
    };
    if fs::tokio::create_dir_all(parent).await.is_err() {
        return;
    }
    let temp = parent.join(format!(
        ".tmp-{}",
        cache_dir.file_name().unwrap_or_default().to_string_lossy()
    ));
    if recursive_copy_dir_no_ignore(dest_dir, &temp)
        .await
        .and_then(|_| fs::sync::rename(&temp, cache_dir))
        .is_err()
    {
        let _ = remove_dir_all(&temp);
        tracing::debug!("failed to populate the source cache");
    }
}

/// Write the downloaded archive to the cache, atomically (best-effort).
#[tracing::instrument(level = "trace")]
async fn write_source_cache_archive(path: &Path, contents: &Bytes) {
    let Some(parent) = path.parent() else {
        return;
    };
    if fs::tokio::create_dir_all(parent).await.is_err() {
        return;
    }
    let temp = parent.join(format!(
        ".tmp-{}",
        path.file_name().unwrap_or_default().to_string_lossy()
    ));
    if fs::tokio::write(&temp, contents).await.is_err()
        || fs::tokio::rename(&temp, path).await.is_err()
    {
        let _ = fs::tokio::remove_file(&temp).await;
        tracing::debug!("failed to write the source cache");
    }
}

/// Resolve a git `checkout_ref` to an immutable commit SHA without cloning,
/// using the remote's ref advertisement (equivalent to `git ls-remote`).
/// Returns `None` if the ref cannot be resolved.
#[tracing::instrument(level = "trace")]
fn resolve_remote_ref(url: &str, checkout_ref: Option<&str>, config: &Config) -> Option<git2::Oid> {
    if let Some(reference) = checkout_ref {
        if let Ok(oid) = git2::Oid::from_str(reference) {
            return Some(oid);
        }
    }
    let result: Result<git2::Oid, git2::Error> = (|| {
        let auth = if config.no_prompt() {
            GitAuthenticator::default()
                .try_password_prompt(0)
                .prompt_ssh_key_password(false)
                .set_prompter(NullPrompter)
        } else {
            GitAuthenticator::default()
        };
        let git_config = git2::Config::open_default()?;
        let mut callbacks = RemoteCallbacks::new();
        callbacks.credentials(auth.credentials(&git_config));
        let tempdir = fs::tempfile::tempdir().map_err(|err| {
            git2::Error::from_str(&format!("unable to create temporary directory: {err}"))
        })?;
        let repo = git2::Repository::init_bare(tempdir.path())?;
        let mut remote = repo.remote_anonymous(url)?;
        let connection = remote.connect_auth(Direction::Fetch, Some(callbacks), None)?;
        let refs = connection.list()?;
        let oid = match checkout_ref {
            Some(reference) => {
                let candidates = [
                    reference.to_string(),
                    format!("refs/heads/{reference}"),
                    format!("refs/tags/{reference}"),
                ];
                refs.iter()
                    .find(|head| candidates.iter().any(|c| c.as_str() == head.name()))
                    .map(|head| head.oid())
            }
            None => refs
                .iter()
                .find(|head| head.name() == "HEAD")
                .map(|head| head.oid()),
        };
        oid.ok_or_else(|| git2::Error::from_str("no matching ref advertised"))
    })();
    result.ok()
}

async fn do_fetch_src_rock(
    fetch: FetchSrcRock<'_>,
) -> Result<RemotePackageSourceMetadata, FetchSrcRockError> {
    let package = fetch.package;
    let span = span!(
        tracing::Level::INFO,
        "Fetching src.rock",
        package = package.to_string(),
    );
    let _enter = span.enter();

    let dest_dir = fetch.dest_dir;
    let config = fetch.config;
    let src_rock = operations::download_src_rock(package, config.server(), fetch.config).await?;
    let hash = src_rock.bytes.hash().await?;
    let cursor = Cursor::new(src_rock.bytes);
    let mime_type = infer::get(cursor.get_ref()).map(|file_type| file_type.mime_type());
    operations::unpack::unpack(mime_type, cursor, true, src_rock.file_name, dest_dir).await?;
    Ok(RemotePackageSourceMetadata {
        hash,
        source_url: RemotePackageSourceUrl::Url { url: src_rock.url },
    })
}

#[cfg(test)]
mod tests {
    use std::io::Write;

    use assert_fs::prelude::*;
    use httptest::{matchers::request, responders::status_code, Expectation, Server};
    use serial_test::serial;

    use crate::config::ConfigBuilder;
    use crate::lua_rockspec::RemoteLuaRockspec;

    use super::*;

    fn source_zip() -> Vec<u8> {
        let mut cursor = std::io::Cursor::new(Vec::new());
        let mut zip = zip::ZipWriter::new(&mut cursor);
        zip.start_file("test.lua", zip::write::SimpleFileOptions::default())
            .unwrap();
        zip.write_all(b"return 1").unwrap();
        zip.finish().unwrap();
        cursor.into_inner()
    }

    fn test_config(cache_dir: std::path::PathBuf) -> Config {
        ConfigBuilder::new()
            .unwrap()
            .cache_dir(Some(cache_dir))
            .no_progress(Some(true))
            .build()
            .unwrap()
    }

    #[tokio::test]
    #[serial]
    async fn fetch_url_source_hits_cache() {
        let cache_dir = assert_fs::TempDir::new().unwrap().to_path_buf();
        let server = Server::run();
        server.expect(
            // We only allow one call, so the second one has to hit the cache or fail
            Expectation::matching(request::path("/source.zip"))
                .times(1)
                .respond_with(status_code(200).body(source_zip())),
        );
        let url = server.url_str("/source.zip");
        let rockspec = RemoteLuaRockspec::new(&format!(
            r#"
rockspec_format = '3.0'
package = 'cached-package'
version = '1.0-1'
description = {{ summary = 'test package' }}
source = {{ url = '{url}', dir = 'source' }}
build = {{ type = 'builtin', modules = {{ ['test'] = 'source/test.lua' }} }}
"#
        ))
        .unwrap();
        let config = test_config(cache_dir);

        let dest_dir = assert_fs::TempDir::new().unwrap();
        let first = FetchSrc::new(dest_dir.path(), &rockspec, &config)
            .fetch_internal()
            .await
            .unwrap();

        let dest_dir = assert_fs::TempDir::new().unwrap();
        let second = FetchSrc::new(dest_dir.path(), &rockspec, &config)
            .fetch_internal()
            .await
            .unwrap();

        assert_eq!(first.hash, second.hash);
    }

    #[tokio::test]
    #[serial]
    async fn resolves_git_ref_to_oid_without_cloning() {
        let repo_dir = assert_fs::TempDir::new().unwrap();
        let bare_path = repo_dir.path().join("repo.git");
        let (commit_id, default_branch) = {
            let bare = git2::Repository::init_bare(&bare_path).unwrap();
            let signature = git2::Signature::now("test", "test@example.com").unwrap();
            let tree_id = {
                let mut builder = bare.treebuilder(None).unwrap();
                let blob = bare.blob(b"hello").unwrap();
                builder.insert("hello.txt", blob, 0o100644).unwrap();
                builder.write().unwrap()
            };
            let tree = bare.find_tree(tree_id).unwrap();
            let commit_id = bare
                .commit(Some("HEAD"), &signature, &signature, "init", &tree, &[])
                .unwrap();
            let head_ref = bare.find_reference("HEAD").unwrap();
            let default_branch = head_ref
                .symbolic_target()
                .unwrap()
                .unwrap()
                .strip_prefix("refs/heads/")
                .unwrap()
                .to_string();
            (commit_id, default_branch)
        };

        let url = format!("file://{}", bare_path.display());
        let config = test_config(assert_fs::TempDir::new().unwrap().to_path_buf());

        assert_eq!(
            resolve_remote_ref(&url, Some(&default_branch), &config).unwrap(),
            commit_id
        );
        assert_eq!(
            resolve_remote_ref(&url, Some(&commit_id.to_string()), &config).unwrap(),
            commit_id
        );
        assert_eq!(resolve_remote_ref(&url, None, &config).unwrap(), commit_id);
    }

    #[tokio::test]
    #[serial]
    async fn copy_dir_recursive_preserves_contents() {
        let src = assert_fs::TempDir::new().unwrap();
        src.child("sub").create_dir_all().unwrap();
        src.child("file.txt").write_str("hello").unwrap();
        src.child("sub/nested.txt").write_str("world").unwrap();
        let dest = assert_fs::TempDir::new().unwrap();
        let dest_path = dest.path().join("copy");
        recursive_copy_dir_no_ignore(src.path(), &dest_path)
            .await
            .unwrap();
        assert_eq!(
            fs::sync::read_to_string(dest_path.join("file.txt")).unwrap(),
            "hello"
        );
        assert_eq!(
            fs::sync::read_to_string(dest_path.join("sub/nested.txt")).unwrap(),
            "world"
        );
    }
}