grit-lib 0.5.21

Core library for the grit Git implementation
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
//! Fast object transfer between on-disk repositories (local / `file://` remotes).
//!
//! When the source stores objects in packfiles, linking or copying those packs
//! avoids rebuilding a pack in memory (Git's `--local` fetch/clone path).

use std::collections::HashSet;
use std::fs;
use std::path::Path;

use crate::error::{Error, Result};
use crate::fs_publish::hard_link_or_copy;
use crate::objects::ObjectId;
use crate::odb::Odb;
use crate::pack;

fn pack_entry_name_ok(name: &str) -> bool {
    if name.starts_with("tmp_") || name.starts_with(".") {
        return false;
    }
    name.ends_with(".pack")
        || name.ends_with(".idx")
        || name.ends_with(".rev")
        || name.ends_with(".bitmap")
        || name.ends_with(".promisor")
        || name.ends_with(".keep")
        || name.ends_with(".mtimes")
}

/// Link or copy pack-related files from `remote_objects/pack/` into `local_objects/pack/`.
///
/// Skips files that already exist in the destination. Every `.idx` is linked after
/// the other files, so a reader never finds an index without its pack. Safe to run
/// from several processes into one destination at once. Returns the number of files
/// this call found missing and installed (racing writers may each count a file).
pub(crate) fn link_remote_pack_files(
    remote_objects: &Path,
    local_objects: &Path,
    progress: &mut dyn crate::progress::ProgressSink,
    cancel: &dyn crate::progress::Cancel,
    total_hint: usize,
) -> Result<usize> {
    let remote_pack = remote_objects.join("pack");
    if !remote_pack.is_dir() {
        return Ok(0);
    }
    let local_pack = local_objects.join("pack");
    fs::create_dir_all(&local_pack).map_err(Error::Io)?;
    let mut names = Vec::new();
    for entry in fs::read_dir(&remote_pack).map_err(Error::Io)? {
        let name = entry.map_err(Error::Io)?.file_name();
        if pack_entry_name_ok(&name.to_string_lossy()) {
            names.push(name);
        }
    }
    names.sort_by_key(|name| name.to_string_lossy().ends_with(".idx"));
    let mut linked = 0usize;
    for name in names {
        crate::progress::poll_cancel(progress, cancel)?;
        let dst = local_pack.join(&name);
        if dst.exists() {
            continue;
        }
        hard_link_or_copy(&remote_pack.join(&name), &dst)?;
        linked += 1;
        if total_hint > 0 {
            progress.inc(1);
        }
    }
    Ok(linked)
}

fn link_loose_objects(
    remote_objects: &Path,
    local_objects: &Path,
    progress: &mut dyn crate::progress::ProgressSink,
    cancel: &dyn crate::progress::Cancel,
) -> Result<usize> {
    let mut copied = 0usize;
    for entry in fs::read_dir(remote_objects).map_err(Error::Io)? {
        crate::progress::poll_cancel(progress, cancel)?;
        let entry = entry.map_err(Error::Io)?;
        let name = entry.file_name();
        let name_str = name.to_string_lossy();
        if name_str.len() != 2 || !name_str.chars().all(|c| c.is_ascii_hexdigit()) {
            continue;
        }
        let local_shard = local_objects.join(&name);
        fs::create_dir_all(&local_shard).map_err(Error::Io)?;
        let Ok(shard_entries) = fs::read_dir(entry.path()) else {
            continue;
        };
        for obj in shard_entries.flatten() {
            if copied.is_multiple_of(64) {
                crate::progress::poll_cancel(progress, cancel)?;
            }
            let dst = local_shard.join(obj.file_name());
            if dst.exists() {
                continue;
            }
            hard_link_or_copy(&obj.path(), &dst)?;
            copied += 1;
            progress.inc(1);
        }
    }
    Ok(copied)
}

/// After pack/loose linking, refresh pack caches so [`Odb::exists`] sees new objects.
pub(crate) fn refresh_local_odb_after_link(local_odb: &Odb) {
    local_odb.invalidate_packs();
    pack::clear_pack_cache();
}

/// Whether `objects_dir` stores every object in packfiles (no loose objects, no alternates).
///
/// Used before the initial local-clone pack-link fast path: when true, linking packs for tip
/// oids alone is sufficient because the pack closure is the whole object store.
pub(crate) fn source_objects_fully_packed(objects_dir: &Path) -> Result<bool> {
    if !pack::read_alternates_recursive(objects_dir)?.is_empty() {
        return Ok(false);
    }
    for entry in fs::read_dir(objects_dir).map_err(Error::Io)? {
        let entry = entry.map_err(Error::Io)?;
        let name = entry.file_name();
        let name_str = name.to_string_lossy();
        if name_str == "info" || name_str == "pack" {
            continue;
        }
        if name_str.len() != 2 || !name_str.chars().all(|c| c.is_ascii_hexdigit()) {
            continue;
        }
        let Ok(mut shard) = fs::read_dir(entry.path()) else {
            continue;
        };
        if shard.next().is_some() {
            return Ok(false);
        }
    }
    Ok(true)
}

/// Try to satisfy `needed` by linking the remote's on-disk objects into `local`.
///
/// Returns `true` when every oid in `needed` is present in `local_odb` afterward.
pub(crate) fn try_satisfy_via_object_link(
    remote_objects: &Path,
    local_objects: &Path,
    local_odb: &Odb,
    needed: &[ObjectId],
    link_loose: bool,
    progress: &mut dyn crate::progress::ProgressSink,
    cancel: &dyn crate::progress::Cancel,
) -> Result<bool> {
    if needed.is_empty() {
        return Ok(true);
    }
    crate::progress::poll_cancel(progress, cancel)?;
    let linked_packs = link_remote_pack_files(
        remote_objects,
        local_objects,
        progress,
        cancel,
        needed.len(),
    )?;
    let linked_loose = if link_loose {
        link_loose_objects(remote_objects, local_objects, progress, cancel)?
    } else {
        0
    };
    if linked_packs == 0 && linked_loose == 0 {
        return Ok(false);
    }
    refresh_local_odb_after_link(local_odb);
    if linked_packs > 0 && needed.iter().all(|oid| local_odb.exists(oid)) {
        return Ok(true);
    }
    if linked_loose > 0 && needed.iter().all(|oid| local_odb.exists(oid)) {
        return Ok(true);
    }
    Ok(false)
}

/// Hard-link or copy loose object files for `oids` from `source_objects` into `dest_objects`.
///
/// Skips oids that are not stored loose at the source. Returns how many files were linked.
pub(crate) fn link_loose_objects_for_oids(
    source_objects: &Path,
    dest_objects: &Path,
    oids: &[ObjectId],
    progress: &mut dyn crate::progress::ProgressSink,
    cancel: &dyn crate::progress::Cancel,
) -> Result<usize> {
    let mut linked = 0usize;
    for (i, oid) in oids.iter().enumerate() {
        if i.is_multiple_of(64) {
            crate::progress::poll_cancel(progress, cancel)?;
        }
        let hex = oid.to_hex();
        if hex.len() < 2 {
            continue;
        }
        let (shard, rest) = hex.split_at(2);
        let src = source_objects.join(shard).join(rest);
        if !src.is_file() {
            continue;
        }
        let dst_shard = dest_objects.join(shard);
        fs::create_dir_all(&dst_shard).map_err(Error::Io)?;
        let dst = dst_shard.join(rest);
        if dst.exists() {
            continue;
        }
        hard_link_or_copy(&src, &dst)?;
        linked += 1;
        progress.inc(1);
    }
    Ok(linked)
}

/// Oids newly visible in `local_odb` after linking (subset of `needed`).
pub(crate) fn linked_object_set(local_odb: &Odb, needed: &[ObjectId]) -> HashSet<ObjectId> {
    needed
        .iter()
        .copied()
        .filter(|oid| local_odb.exists(oid))
        .collect()
}

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

    fn git(dir: &Path, args: &[&str]) {
        let out = Command::new("git")
            .env("GIT_AUTHOR_NAME", "Test")
            .env("GIT_AUTHOR_EMAIL", "test@example.com")
            .env("GIT_COMMITTER_NAME", "Test")
            .env("GIT_COMMITTER_EMAIL", "test@example.com")
            .current_dir(dir)
            .args(args)
            .env("GIT_CONFIG_GLOBAL", "/dev/null")
            .env("GIT_CONFIG_SYSTEM", "/dev/null")
            .output()
            .expect("git");
        assert!(
            out.status.success(),
            "git: {}",
            String::from_utf8_lossy(&out.stderr)
        );
    }

    #[test]
    fn source_objects_fully_packed_repacked_repo() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let src = tmp.path().join("src");
        fs::create_dir_all(&src).unwrap();
        git(&src, &["init", "-q", "-b", "main"]);
        fs::write(src.join("f"), b"payload\n").unwrap();
        git(&src, &["add", "f"]);
        git(&src, &["commit", "-qm", "c"]);
        git(&src, &["repack", "-a", "-d", "-f"]);
        let objects = src.join(".git/objects");
        assert!(source_objects_fully_packed(&objects).expect("scan"));
    }

    #[test]
    fn source_objects_fully_packed_false_when_loose_blob_remains() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let src = tmp.path().join("src");
        fs::create_dir_all(&src).unwrap();
        git(&src, &["init", "-q", "-b", "main"]);
        fs::write(src.join("a"), b"one\n").unwrap();
        git(&src, &["add", "a"]);
        git(&src, &["commit", "-qm", "one"]);
        let head = Command::new("git")
            .current_dir(&src)
            .args(["rev-parse", "HEAD"])
            .output()
            .unwrap();
        let tree = Command::new("git")
            .current_dir(&src)
            .args(["rev-parse", "HEAD^{tree}"])
            .output()
            .unwrap();
        let head_hex = String::from_utf8_lossy(&head.stdout);
        let tree_hex = String::from_utf8_lossy(&tree.stdout);
        let input = format!("{}\n{}\n", head_hex.trim(), tree_hex.trim());
        let mut pack = Command::new("git")
            .current_dir(&src)
            .args(["pack-objects", "-q", ".git/objects/pack/pack"])
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .env("GIT_CONFIG_GLOBAL", "/dev/null")
            .env("GIT_CONFIG_SYSTEM", "/dev/null")
            .spawn()
            .expect("pack-objects");
        if let Some(mut stdin) = pack.stdin.take() {
            use std::io::Write;
            stdin.write_all(input.as_bytes()).unwrap();
        }
        assert!(pack.wait_with_output().unwrap().status.success());
        git(&src, &["prune-packed"]);
        let objects = src.join(".git/objects");
        assert!(
            !source_objects_fully_packed(&objects).expect("scan"),
            "mixed pack+loose must not qualify for tip-only fast path"
        );
    }

    #[test]
    fn link_remote_pack_files_hardlinks_repacked_repo() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let src = tmp.path().join("src");
        fs::create_dir_all(&src).unwrap();
        git(&src, &["init", "-q", "-b", "main"]);
        fs::write(src.join("f"), b"payload\n").unwrap();
        git(&src, &["add", "f"]);
        git(&src, &["commit", "-qm", "c"]);
        git(&src, &["repack", "-a", "-d", "-f"]);

        let dst_objects = tmp.path().join("dst/objects");
        fs::create_dir_all(&dst_objects).unwrap();
        let linked = link_remote_pack_files(
            &src.join(".git/objects"),
            &dst_objects,
            &mut crate::progress::NullProgress,
            &crate::progress::NeverCancel,
            0,
        )
        .expect("link");
        assert!(
            linked >= 2,
            "expected at least .pack and .idx, got {linked}"
        );

        let local_odb = Odb::new(&dst_objects);
        refresh_local_odb_after_link(&local_odb);
        let rev = Command::new("git")
            .current_dir(&src)
            .args(["rev-parse", "HEAD"])
            .output()
            .unwrap();
        let tip = ObjectId::from_hex(String::from_utf8_lossy(&rev.stdout).trim()).unwrap();
        assert!(local_odb.exists(&tip));
    }

    #[cfg(unix)]
    #[test]
    fn parallel_link_remote_pack_files_into_one_repo_all_succeed() {
        use std::os::unix::fs::PermissionsExt;

        for source_mode in [0o444, 0o644] {
            let tmp = tempfile::tempdir().expect("tempdir");
            let src = tmp.path().join("src");
            fs::create_dir_all(&src).unwrap();
            git(&src, &["init", "-q", "-b", "main"]);
            for i in 0..20 {
                fs::write(src.join(format!("f{i}")), format!("payload {i}\n")).unwrap();
            }
            git(&src, &["add", "."]);
            git(&src, &["commit", "-qm", "c"]);
            git(&src, &["repack", "-a", "-d", "-f"]);
            let src_objects = src.join(".git/objects");
            let src_pack_dir = src_objects.join("pack");
            let mut before = Vec::new();
            for entry in fs::read_dir(&src_pack_dir).unwrap().flatten() {
                fs::set_permissions(entry.path(), fs::Permissions::from_mode(source_mode)).unwrap();
                before.push((entry.path(), fs::read(entry.path()).unwrap()));
            }

            let dst = tmp.path().join("dst");
            fs::create_dir_all(&dst).unwrap();
            git(&dst, &["init", "-q", "-b", "main"]);
            let dst_objects = std::sync::Arc::new(dst.join(".git/objects"));
            let src_objects = std::sync::Arc::new(src_objects);
            let barrier = std::sync::Arc::new(std::sync::Barrier::new(6));
            let handles: Vec<_> = (0..6)
                .map(|_| {
                    let (s, d, b) = (src_objects.clone(), dst_objects.clone(), barrier.clone());
                    std::thread::spawn(move || {
                        b.wait();
                        link_remote_pack_files(
                            &s,
                            &d,
                            &mut crate::progress::NullProgress,
                            &crate::progress::NeverCancel,
                            0,
                        )
                    })
                })
                .collect();
            for h in handles {
                h.join().unwrap().expect("parallel link succeeds");
            }

            for (path, bytes) in &before {
                assert_eq!(&fs::read(path).unwrap(), bytes, "source changed: {path:?}");
            }
            git(&src, &["fsck", "--strict"]);
            let tip = Command::new("git")
                .current_dir(&src)
                .args(["rev-parse", "HEAD"])
                .output()
                .unwrap();
            git(
                &dst,
                &[
                    "update-ref",
                    "refs/heads/main",
                    String::from_utf8_lossy(&tip.stdout).trim(),
                ],
            );
            git(&dst, &["fsck", "--strict"]);
        }
    }

    #[test]
    fn hard_link_rejects_symlink_in_pack_dir() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let src_pack = tmp.path().join("src/pack");
        fs::create_dir_all(&src_pack).unwrap();
        let evil = src_pack.join("evil.keep");
        #[cfg(unix)]
        {
            use std::os::unix::fs::symlink;
            symlink("elsewhere", &evil).unwrap();
        }
        #[cfg(not(unix))]
        {
            return;
        }
        let dst_pack = tmp.path().join("dst/pack");
        fs::create_dir_all(&dst_pack).unwrap();
        let dst = dst_pack.join("evil.keep");
        let err = hard_link_or_copy(&evil, &dst).unwrap_err();
        assert!(
            matches!(err, Error::PathError(_)),
            "expected PathError, got {err:?}"
        );
    }
}