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
use std::{
path::{Path, PathBuf},
sync::atomic::{AtomicBool, Ordering},
};
use anyhow::bail;
use git::{odb::FindExt, worktree::index::checkout, Progress};
use git_repository as git;
use crate::{
index,
index::{parse_file, Options},
};
pub fn checkout_exclusive(
index_path: impl AsRef<Path>,
dest_directory: impl AsRef<Path>,
repo: Option<PathBuf>,
mut err: impl std::io::Write,
mut progress: impl Progress,
should_interrupt: &AtomicBool,
index::checkout_exclusive::Options {
index: Options { object_hash, .. },
empty_files,
keep_going,
thread_limit,
}: index::checkout_exclusive::Options,
) -> anyhow::Result<()> {
let repo = repo.map(git_repository::discover).transpose()?;
let dest_directory = dest_directory.as_ref();
if dest_directory.exists() {
bail!(
"Refusing to checkout index into existing directory '{}' - remove it and try again",
dest_directory.display()
)
}
std::fs::create_dir_all(dest_directory)?;
let mut index = parse_file(index_path, object_hash)?;
let mut num_skipped = 0;
let maybe_symlink_mode = if !empty_files && repo.is_some() {
git::index::entry::Mode::DIR
} else {
git::index::entry::Mode::SYMLINK
};
for entry in index.entries_mut().iter_mut().filter(|e| {
e.mode
.contains(maybe_symlink_mode | git::index::entry::Mode::DIR | git::index::entry::Mode::COMMIT)
}) {
entry.flags.insert(git::index::entry::Flags::SKIP_WORKTREE);
num_skipped += 1;
}
if num_skipped > 0 {
progress.info(format!("Skipping {} DIR/SYMLINK/COMMIT entries", num_skipped));
}
let opts = git::worktree::index::checkout::Options {
fs: git::worktree::fs::Capabilities::probe(dest_directory),
destination_is_initially_empty: true,
overwrite_existing: false,
keep_going,
thread_limit,
..Default::default()
};
let mut files = progress.add_child("checkout");
let mut bytes = progress.add_child("writing");
let entries_for_checkout = index.entries().len() - num_skipped;
files.init(Some(entries_for_checkout), git::progress::count("files"));
bytes.init(None, git::progress::bytes());
let start = std::time::Instant::now();
let no_repo = repo.is_none();
let checkout::Outcome {
errors,
collisions,
files_updated,
bytes_written,
} = match repo {
Some(repo) => git::worktree::index::checkout(
&mut index,
dest_directory,
{
let objects = repo.objects.into_arc()?;
move |oid, buf| {
objects.find_blob(oid, buf).ok();
if empty_files {
objects.find_blob(oid, buf)?;
buf.clear();
Ok(git::objs::BlobRef { data: buf })
} else {
objects.find_blob(oid, buf)
}
}
},
&mut files,
&mut bytes,
should_interrupt,
opts,
),
None => git::worktree::index::checkout(
&mut index,
dest_directory,
|_, buf| {
buf.clear();
Ok(git::objs::BlobRef { data: buf })
},
&mut files,
&mut bytes,
should_interrupt,
opts,
),
}?;
files.show_throughput(start);
bytes.show_throughput(start);
progress.done(format!(
"Created {} {} files{} ({})",
files_updated,
no_repo.then_some("empty").unwrap_or_default(),
should_interrupt
.load(Ordering::Relaxed)
.then(|| {
format!(
" of {}",
entries_for_checkout.saturating_sub(errors.len() + collisions.len())
)
})
.unwrap_or_default(),
git::progress::bytes()
.unwrap()
.display(bytes_written as usize, None, None)
));
if !(collisions.is_empty() && errors.is_empty()) {
let mut messages = Vec::new();
if !errors.is_empty() {
messages.push(format!("kept going through {} errors(s)", errors.len()));
for record in errors {
writeln!(err, "{}: {}", record.path, record.error).ok();
}
}
if !collisions.is_empty() {
messages.push(format!("encountered {} collision(s)", collisions.len()));
for col in collisions {
writeln!(err, "{}: collision ({:?})", col.path, col.error_kind).ok();
}
}
bail!(
"One or more errors occurred - checkout is incomplete: {}",
messages.join(", ")
);
}
Ok(())
}