-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathfunction.rs
More file actions
172 lines (149 loc) · 5.85 KB
/
function.rs
File metadata and controls
172 lines (149 loc) · 5.85 KB
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
use std::io::{self, Read, Write};
use gix_hash::ObjectId;
use bstr::ByteSlice;
use crate::serve::{
upload_pack::{parse_haves, parse_wants, write_ack, write_nak, AckStatus, Error},
write_capabilities_v2, write_v1, write_v2_ls_refs, RefAdvertisement,
};
use crate::transport::{
packetline::{
blocking_io::encode::{data_to_write, delim_to_write, flush_to_write},
PacketLineRef,
},
server::blocking_io::connection::Connection,
};
use crate::Command;
/// Serve a V1 upload-pack session.
pub fn serve_upload_pack_v1<R: Read, W: Write>(
connection: &mut Connection<R, W>,
refs: &[RefAdvertisement<'_>],
has_object: impl Fn(&gix_hash::oid) -> bool,
generate_pack: impl FnOnce(&[ObjectId], &[ObjectId], &mut dyn Write) -> io::Result<()>,
capabilities: &[&str],
) -> Result<(), Error> {
write_v1(&mut connection.writer, refs, capabilities)?;
let wants = parse_wants(&mut connection.line_provider)?;
if wants.wants.is_empty() {
return Ok(());
}
connection.line_provider.reset();
let mut common = Vec::new();
loop {
let haves = parse_haves(&mut connection.line_provider)?;
let mut found_common = false;
for oid in &haves.haves {
if has_object(oid) {
write_ack(&mut connection.writer, oid, AckStatus::Common)?;
common.push(*oid);
found_common = true;
}
}
if haves.done {
break;
}
if !found_common {
write_nak(&mut connection.writer)?;
}
connection.line_provider.reset();
}
if let Some(last) = common.last() {
write_ack(&mut connection.writer, last, AckStatus::Final)?;
} else {
write_nak(&mut connection.writer)?;
}
let want_ids: Vec<ObjectId> = wants.wants.iter().map(|w| w.id).collect();
generate_pack(&want_ids, &common, &mut connection.writer)?;
Ok(())
}
/// Serve a V2 upload-pack session.
pub fn serve_upload_pack_v2<R: Read, W: Write>(
connection: &mut Connection<R, W>,
refs: &[RefAdvertisement<'_>],
has_object: impl Fn(&gix_hash::oid) -> bool,
generate_pack: impl FnOnce(&[ObjectId], &[ObjectId], &mut dyn Write) -> io::Result<()>,
capabilities: &[(&str, Option<&str>)],
) -> Result<(), Error> {
write_capabilities_v2(&mut connection.writer, capabilities)?;
loop {
connection.line_provider.reset();
let line = match connection.line_provider.read_line() {
Some(Ok(line)) => line?,
Some(Err(e)) if e.kind() == io::ErrorKind::UnexpectedEof => break,
Some(Err(e)) => return Err(e.into()),
None => break, // connection closed
};
let command = match line {
PacketLineRef::Data(data) => parse_command(data)?,
_ => break,
};
match command {
Command::LsRefs => {
while let Some(line) = connection.line_provider.read_line() {
let _ = line??;
}
write_v2_ls_refs(&mut connection.writer, refs)?;
}
Command::Fetch => {
// V2 sends all arguments in one flush-terminated group.
let mut want_ids = Vec::new();
let mut have_ids = Vec::new();
let mut done = false;
while let Some(line) = connection.line_provider.read_line() {
let line = line??;
match line {
PacketLineRef::Data(data) => {
let data = data.trim();
if let Some(hex) = data.strip_prefix(b"want ") {
let id =
ObjectId::from_hex(hex).map_err(|_| Error::UnexpectedLine { line: hex.into() })?;
want_ids.push(id);
} else if let Some(hex) = data.strip_prefix(b"have ") {
let id =
ObjectId::from_hex(hex).map_err(|_| Error::UnexpectedLine { line: hex.into() })?;
have_ids.push(id);
} else if data == b"done" {
done = true;
}
// Skip unknown lines (capabilities like thin-pack, ofs-delta).
}
PacketLineRef::Delimiter => {}
_ => break,
}
}
data_to_write(b"acknowledgments\n", &mut connection.writer)?;
let mut common = Vec::new();
for oid in &have_ids {
if has_object(oid) {
write_ack(&mut connection.writer, oid, AckStatus::Common)?;
common.push(*oid);
}
}
if common.is_empty() {
write_nak(&mut connection.writer)?;
}
if !done {
flush_to_write(&mut connection.writer)?;
continue;
}
data_to_write(b"ready\n", &mut connection.writer)?;
delim_to_write(&mut connection.writer)?;
data_to_write(b"packfile\n", &mut connection.writer)?;
generate_pack(&want_ids, &common, &mut connection.writer)?;
flush_to_write(&mut connection.writer)?;
break;
}
}
}
Ok(())
}
fn parse_command(data: &[u8]) -> Result<Command, Error> {
let name = data
.trim()
.strip_prefix(b"command=")
.ok_or_else(|| Error::UnexpectedLine { line: data.into() })?;
match name {
b"ls-refs" => Ok(Command::LsRefs),
b"fetch" => Ok(Command::Fetch),
_ => Err(Error::UnexpectedLine { line: data.into() }),
}
}