-
-
Notifications
You must be signed in to change notification settings - Fork 263
/
fetch.rs
220 lines (208 loc) · 8.03 KB
/
fetch.rs
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
use gix::bstr::BString;
use crate::OutputFormat;
pub struct Options {
pub format: OutputFormat,
pub dry_run: bool,
pub remote: Option<String>,
/// If non-empty, override all ref-specs otherwise configured in the remote
pub ref_specs: Vec<BString>,
pub shallow: gix::remote::fetch::Shallow,
pub handshake_info: bool,
}
pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 1..=3;
pub(crate) mod function {
use anyhow::bail;
use gix::{prelude::ObjectIdExt, refspec::match_group::validate::Fix, remote::fetch::Status};
use super::Options;
use crate::OutputFormat;
pub fn fetch<P>(
repo: gix::Repository,
mut progress: P,
mut out: impl std::io::Write,
err: impl std::io::Write,
Options {
format,
dry_run,
remote,
handshake_info,
shallow,
ref_specs,
}: Options,
) -> anyhow::Result<()>
where
P: gix::Progress,
P::SubProgress: 'static,
{
if format != OutputFormat::Human {
bail!("JSON output isn't yet supported for fetching.");
}
let mut remote = crate::repository::remote::by_name_or_url(&repo, remote.as_deref())?;
if !ref_specs.is_empty() {
remote.replace_refspecs(ref_specs.iter(), gix::remote::Direction::Fetch)?;
remote = remote.with_fetch_tags(gix::remote::fetch::Tags::None);
}
let res: gix::remote::fetch::Outcome = remote
.connect(gix::remote::Direction::Fetch)?
.prepare_fetch(&mut progress, Default::default())?
.with_dry_run(dry_run)
.with_shallow(shallow)
.receive(&mut progress, &gix::interrupt::IS_INTERRUPTED)?;
if handshake_info {
writeln!(out, "Handshake Information")?;
writeln!(out, "\t{:?}", res.ref_map.handshake)?;
}
let ref_specs = remote.refspecs(gix::remote::Direction::Fetch);
match res.status {
Status::NoPackReceived { update_refs } => {
print_updates(&repo, 1, update_refs, ref_specs, res.ref_map, &mut out, err)
}
Status::DryRun {
update_refs,
negotiation_rounds,
} => print_updates(
&repo,
negotiation_rounds,
update_refs,
ref_specs,
res.ref_map,
&mut out,
err,
),
Status::Change {
update_refs,
write_pack_bundle,
negotiation_rounds,
} => {
print_updates(
&repo,
negotiation_rounds,
update_refs,
ref_specs,
res.ref_map,
&mut out,
err,
)?;
if let Some(data_path) = write_pack_bundle.data_path {
writeln!(out, "pack file: \"{}\"", data_path.display()).ok();
}
if let Some(index_path) = write_pack_bundle.index_path {
writeln!(out, "index file: \"{}\"", index_path.display()).ok();
}
Ok(())
}
}?;
if dry_run {
writeln!(out, "DRY-RUN: No ref was updated and no pack was received.").ok();
}
Ok(())
}
pub(crate) fn print_updates(
repo: &gix::Repository,
negotiation_rounds: usize,
update_refs: gix::remote::fetch::refs::update::Outcome,
refspecs: &[gix::refspec::RefSpec],
mut map: gix::remote::fetch::RefMap,
mut out: impl std::io::Write,
mut err: impl std::io::Write,
) -> anyhow::Result<()> {
let mut last_spec_index = gix::remote::fetch::SpecIndex::ExplicitInRemote(usize::MAX);
let mut updates = update_refs
.iter_mapping_updates(&map.mappings, refspecs, &map.extra_refspecs)
.filter_map(|(update, mapping, spec, edit)| spec.map(|spec| (update, mapping, spec, edit)))
.collect::<Vec<_>>();
updates.sort_by_key(|t| t.2);
let mut skipped_due_to_implicit_tag = None;
fn consume_skipped_tags(skipped: &mut Option<usize>, out: &mut impl std::io::Write) -> std::io::Result<()> {
if let Some(skipped) = skipped.take() {
if skipped != 0 {
writeln!(
out,
"\tskipped {skipped} tags known to the remote without bearing on this commit-graph. Use `gix remote ref-map` to list them."
)?;
}
}
Ok(())
}
for (update, mapping, spec, edit) in updates {
if mapping.spec_index != last_spec_index {
last_spec_index = mapping.spec_index;
consume_skipped_tags(&mut skipped_due_to_implicit_tag, &mut out)?;
spec.to_ref().write_to(&mut out)?;
let is_implicit = mapping.spec_index.implicit_index().is_some();
if is_implicit {
write!(&mut out, " (implicit")?;
if spec.to_ref()
== gix::remote::fetch::Tags::Included
.to_refspec()
.expect("always yields refspec")
{
skipped_due_to_implicit_tag = Some(0);
write!(&mut out, ", due to auto-tag")?;
}
write!(&mut out, ")")?;
}
writeln!(out)?;
}
if let Some(num_skipped) = skipped_due_to_implicit_tag.as_mut() {
if matches!(update.mode, gix::remote::fetch::refs::update::Mode::NoChangeNeeded) {
*num_skipped += 1;
continue;
}
}
write!(out, "\t")?;
match &mapping.remote {
gix::remote::fetch::Source::ObjectId(id) => {
write!(out, "{}", id.attach(repo).shorten_or_id())?;
}
gix::remote::fetch::Source::Ref(r) => {
crate::repository::remote::refs::print_ref(&mut out, r)?;
}
};
match edit {
Some(edit) => {
writeln!(out, " -> {} [{}]", edit.name, update.mode)
}
None => writeln!(out, " [{}]", update.mode),
}?;
}
consume_skipped_tags(&mut skipped_due_to_implicit_tag, &mut out)?;
if !map.fixes.is_empty() {
writeln!(
err,
"The following destination refs were removed as they didn't start with 'ref/'"
)?;
map.fixes.sort_by(|l, r| match (l, r) {
(
Fix::MappingWithPartialDestinationRemoved { spec: l, .. },
Fix::MappingWithPartialDestinationRemoved { spec: r, .. },
) => l.cmp(r),
});
let mut prev_spec = None;
for fix in &map.fixes {
match fix {
Fix::MappingWithPartialDestinationRemoved { name, spec } => {
if prev_spec.map_or(true, |prev_spec| prev_spec != spec) {
prev_spec = spec.into();
spec.to_ref().write_to(&mut err)?;
writeln!(err)?;
}
writeln!(err, "\t{name}")?;
}
}
}
}
if map.remote_refs.len() - map.mappings.len() != 0 {
writeln!(
err,
"server sent {} tips, {} were filtered due to {} refspec(s).",
map.remote_refs.len(),
map.remote_refs.len() - map.mappings.len(),
refspecs.len()
)?;
}
if negotiation_rounds != 1 {
writeln!(err, "needed {negotiation_rounds} rounds of pack-negotiation")?;
}
Ok(())
}
}