History

stage 1 of modularization

Sebastian Thiel committed 8 years ago

Commit 1e1262d1 parent bafe8885 files · +429 −425Browse files

Unified · Split

5 files
  1. lib/git-odb/src/loose.rs +0 −207
  2. lib/git-odb/src/loose/mod.rs +214 −0
  3. lib/git-odb/src/object.rs +0 −218
  4. lib/git-odb/src/object/mod.rs +27 −0
  5. lib/git-odb/src/object/parsed.rs +188 −0
lib/git-odb/src/loose.rs+0−207
@@ -1,207 +0,0 @@
1−use object::{Id, Kind};
2−
3−use walkdir::WalkDir;
4−use failure::{Error, ResultExt};
5−use hex::{FromHex, ToHex};
6−use smallvec::SmallVec;
7−use std::{fs::File, io::{Cursor, Read}, path::PathBuf};
8−use std::os::unix::fs::MetadataExt;
9−use zlib;
10−use object::parsed;
11−
12−const HEADER_READ_COMPRESSED_BYTES: usize = 512;
13−const HEADER_READ_UNCOMPRESSED_BYTES: usize = 1024;
14−
15−pub struct Db {
16− pub path: PathBuf,
17−}
18−
19−pub struct Object {
20− pub kind: Kind,
21− pub size: usize,
22− decompressed_data: SmallVec<[u8; HEADER_READ_UNCOMPRESSED_BYTES]>,
23− compressed_data: SmallVec<[u8; HEADER_READ_COMPRESSED_BYTES]>,
24− header_size: usize,
25− path: Option<PathBuf>,
26− is_decompressed: bool,
27−}
28−
29−impl Object {
30− pub fn parsed(&mut self) -> Result<parsed::Object, Error> {
31− Ok(match self.kind {
32− Kind::Tag | Kind::Commit | Kind::Tree => {
33− if !self.is_decompressed {
34− let total_size = self.header_size + self.size;
35− let cap = self.decompressed_data.capacity();
36− if cap < total_size {
37− self.decompressed_data.reserve_exact(total_size - cap);
38− }
39− unsafe {
40− debug_assert!(self.decompressed_data.capacity() >= total_size);
41− self.decompressed_data.set_len(total_size);
42− }
43− let mut cursor = Cursor::new(&mut self.decompressed_data[..]);
44− // TODO Performance opportunity
45− // here we do a lot of additional work, which could be saved if we
46− // could re-use the previous state. This doesn't work for some reason.
47− let mut deflate = zlib::Inflate::default();
48− deflate.all_till_done(&self.compressed_data[..], &mut cursor)?;
49− self.is_decompressed = deflate.is_done;
50− debug_assert!(deflate.is_done);
51− self.compressed_data = Default::default();
52− }
53− let bytes = &self.decompressed_data[self.header_size..];
54− match self.kind {
55− Kind::Tag => parsed::Object::Tag(parsed::Tag::from_bytes(bytes)?),
56− _ => unimplemented!(),
57− }
58− }
59− Kind::Blob => unimplemented!(),
60− })
61− }
62−}
63−
64−impl Db {
65− pub fn iter(&self) -> impl Iterator<Item = Result<Id, Error>> {
66− use std::path::Component::Normal;
67− WalkDir::new(&self.path)
68− .min_depth(2)
69− .max_depth(3)
70− .follow_links(false)
71− .into_iter()
72− .filter_map(|e| {
73− let mut is_valid_path = false;
74− let e = e.map_err(Error::from).map(|e| {
75− let p = e.path();
76− let (c1, c2) = p.components()
77− .fold((None, None), |(_c1, c2), cn| (c2, Some(cn)));
78− if let (Some(Normal(c1)), Some(Normal(c2))) = (c1, c2) {
79− if c1.len() == 2 && c2.len() == 38 {
80− if let (Some(c1), Some(c2)) = (c1.to_str(), c2.to_str()) {
81− let mut buf = [0u8; 40];
82− {
83− let (first_byte, rest) = buf.split_at_mut(2);
84− first_byte.copy_from_slice(c1.as_bytes());
85− rest.copy_from_slice(c2.as_bytes());
86− }
87− if let Ok(b) = <[u8; 20]>::from_hex(&buf[..]) {
88− is_valid_path = true;
89− return b;
90− }
91− }
92− }
93− }
94− [0u8; 20]
95− });
96− if is_valid_path {
97− Some(e)
98− } else {
99− None
100− }
101− })
102− }
103−
104− pub fn find(&self, id: &Id) -> Result<Object, Error> {
105− let path = sha1_path(id, self.path.clone());
106−
107− let mut deflate = zlib::Inflate::default();
108− let mut decompressed = [0; HEADER_READ_UNCOMPRESSED_BYTES];
109− let mut compressed = [0; HEADER_READ_COMPRESSED_BYTES];
110− let ((_status, _consumed_in, consumed_out), bytes_read, mut input_stream) = {
111− let mut istream = File::open(&path)?;
112− let bytes_read = istream.read(&mut compressed[..])?;
113− let mut out = Cursor::new(&mut decompressed[..]);
114−
115− (
116− deflate
117− .once(&compressed[..bytes_read], &mut out)
118− .with_context(|_| {
119− format!("ZIP inflating failed while reading '{}'", path.display())
120− })?,
121− bytes_read,
122− istream,
123− )
124− };
125−
126− let (kind, size, header_size) =
127− parse::header(&decompressed[..consumed_out]).with_context(|_| {
128− format!(
129− "Invalid header layout at '{}', expected '<type> <size>'",
130− path.display()
131− )
132− })?;
133−
134− let decompressed = SmallVec::from_buf(decompressed);
135− let mut compressed = SmallVec::from_buf(compressed);
136−
137− let path = match kind {
138− Kind::Tag | Kind::Commit | Kind::Tree => {
139− let fsize = input_stream.metadata()?.size();
140− assert!(fsize <= ::std::usize::MAX as u64);
141− let fsize = fsize as usize;
142− if bytes_read == fsize {
143− None
144− } else {
145− let cap = compressed.capacity();
146− if cap < fsize {
147− compressed.reserve_exact(fsize - cap);
148− debug_assert!(fsize == compressed.capacity());
149− }
150− unsafe {
151− compressed.set_len(fsize);
152− }
153− input_stream.read_exact(&mut compressed[bytes_read..])?;
154− None
155− }
156− }
157− Kind::Blob => Some(path), // we will open the file again when needed. Maybe we can load small sized objects anyway
158− };
159−
160− Ok(Object {
161− kind,
162− size,
163− decompressed_data: decompressed,
164− compressed_data: compressed,
165− header_size,
166− path,
167− is_decompressed: deflate.is_done,
168− })
169− }
170−}
171−
172−pub mod parse {
173− use failure::{err_msg, Error};
174−
175− use object;
176− use std::str;
177−
178− pub fn header(input: &[u8]) -> Result<(object::Kind, usize, usize), Error> {
179− let header_end = input
180− .iter()
181− .position(|&b| b == 0)
182− .ok_or_else(|| err_msg("Invalid header, did not find 0 byte"))?;
183− let header = &input[..header_end];
184− let mut split = header.split(|&b| b == b' ');
185− match (split.next(), split.next()) {
186− (Some(kind), Some(size)) => Ok((
187− object::Kind::from_bytes(kind)?,
188− str::from_utf8(size)?.parse()?,
189− header_end + 1, // account for 0 byte
190− )),
191− _ => bail!("expected '<type> <size>'"),
192− }
193− }
194−}
195−
196−fn sha1_path(id: &[u8; 20], mut root: PathBuf) -> PathBuf {
197− let mut buf = String::with_capacity(40);
198− id.write_hex(&mut buf)
199− .expect("no failure as everything is preset by now");
200− root.push(&buf[..2]);
201− root.push(&buf[2..]);
202− root
203−}
204−
205−pub fn at(path: impl Into<PathBuf>) -> Db {
206− Db { path: path.into() }
207−}
lib/git-odb/src/loose/mod.rs+214−0
@@ -0,0 +1,214 @@
1+const HEADER_READ_COMPRESSED_BYTES: usize = 512;
2+const HEADER_READ_UNCOMPRESSED_BYTES: usize = 1024;
3+
4+mod object {
5+ use object::{parsed, Kind};
6+ use failure::Error;
7+ use smallvec::SmallVec;
8+ use std::{io::Cursor, path::PathBuf};
9+ use zlib;
10+ use loose::{HEADER_READ_COMPRESSED_BYTES, HEADER_READ_UNCOMPRESSED_BYTES};
11+
12+ pub struct Object {
13+ pub kind: Kind,
14+ pub size: usize,
15+ decompressed_data: SmallVec<[u8; HEADER_READ_UNCOMPRESSED_BYTES]>,
16+ compressed_data: SmallVec<[u8; HEADER_READ_COMPRESSED_BYTES]>,
17+ header_size: usize,
18+ _path: Option<PathBuf>,
19+ is_decompressed: bool,
20+ }
21+
22+ impl Object {
23+ pub fn parsed(&mut self) -> Result<parsed::Object, Error> {
24+ Ok(match self.kind {
25+ Kind::Tag | Kind::Commit | Kind::Tree => {
26+ if !self.is_decompressed {
27+ let total_size = self.header_size + self.size;
28+ let cap = self.decompressed_data.capacity();
29+ if cap < total_size {
30+ self.decompressed_data.reserve_exact(total_size - cap);
31+ }
32+ unsafe {
33+ debug_assert!(self.decompressed_data.capacity() >= total_size);
34+ self.decompressed_data.set_len(total_size);
35+ }
36+ let mut cursor = Cursor::new(&mut self.decompressed_data[..]);
37+ // TODO Performance opportunity
38+ // here we do a lot of additional work, which could be saved if we
39+ // could re-use the previous state. This doesn't work for some reason.
40+ let mut deflate = zlib::Inflate::default();
41+ deflate.all_till_done(&self.compressed_data[..], &mut cursor)?;
42+ self.is_decompressed = deflate.is_done;
43+ debug_assert!(deflate.is_done);
44+ self.compressed_data = Default::default();
45+ }
46+ let bytes = &self.decompressed_data[self.header_size..];
47+ match self.kind {
48+ Kind::Tag => parsed::Object::Tag(parsed::Tag::from_bytes(bytes)?),
49+ _ => unimplemented!(),
50+ }
51+ }
52+ Kind::Blob => unimplemented!(),
53+ })
54+ }
55+ }
56+}
57+
58+mod db {
59+ use object::{Id, Kind};
60+
61+ use walkdir::WalkDir;
62+ use failure::{err_msg, Error, ResultExt};
63+ use hex::{FromHex, ToHex};
64+ use smallvec::SmallVec;
65+ use std::{str, fs::File, io::{Cursor, Read}, path::PathBuf};
66+ use std::os::unix::fs::MetadataExt;
67+ use zlib;
68+ use loose::{Object, HEADER_READ_COMPRESSED_BYTES, HEADER_READ_UNCOMPRESSED_BYTES};
69+
70+ pub struct Db {
71+ pub path: PathBuf,
72+ }
73+
74+ impl Db {
75+ pub fn iter(&self) -> impl Iterator<Item = Result<Id, Error>> {
76+ use std::path::Component::Normal;
77+ WalkDir::new(&self.path)
78+ .min_depth(2)
79+ .max_depth(3)
80+ .follow_links(false)
81+ .into_iter()
82+ .filter_map(|e| {
83+ let mut is_valid_path = false;
84+ let e = e.map_err(Error::from).map(|e| {
85+ let p = e.path();
86+ let (c1, c2) = p.components()
87+ .fold((None, None), |(_c1, c2), cn| (c2, Some(cn)));
88+ if let (Some(Normal(c1)), Some(Normal(c2))) = (c1, c2) {
89+ if c1.len() == 2 && c2.len() == 38 {
90+ if let (Some(c1), Some(c2)) = (c1.to_str(), c2.to_str()) {
91+ let mut buf = [0u8; 40];
92+ {
93+ let (first_byte, rest) = buf.split_at_mut(2);
94+ first_byte.copy_from_slice(c1.as_bytes());
95+ rest.copy_from_slice(c2.as_bytes());
96+ }
97+ if let Ok(b) = <[u8; 20]>::from_hex(&buf[..]) {
98+ is_valid_path = true;
99+ return b;
100+ }
101+ }
102+ }
103+ }
104+ [0u8; 20]
105+ });
106+ if is_valid_path {
107+ Some(e)
108+ } else {
109+ None
110+ }
111+ })
112+ }
113+
114+ pub fn find(&self, id: &Id) -> Result<Object, Error> {
115+ let path = sha1_path(id, self.path.clone());
116+
117+ let mut deflate = zlib::Inflate::default();
118+ let mut decompressed = [0; HEADER_READ_UNCOMPRESSED_BYTES];
119+ let mut compressed = [0; HEADER_READ_COMPRESSED_BYTES];
120+ let ((_status, _consumed_in, consumed_out), bytes_read, mut input_stream) = {
121+ let mut istream = File::open(&path)?;
122+ let bytes_read = istream.read(&mut compressed[..])?;
123+ let mut out = Cursor::new(&mut decompressed[..]);
124+
125+ (
126+ deflate
127+ .once(&compressed[..bytes_read], &mut out)
128+ .with_context(|_| {
129+ format!("ZIP inflating failed while reading '{}'", path.display())
130+ })?,
131+ bytes_read,
132+ istream,
133+ )
134+ };
135+
136+ let (kind, size, header_size) = parse_header(&decompressed[..consumed_out])
137+ .with_context(|_| {
138+ format!(
139+ "Invalid header layout at '{}', expected '<type> <size>'",
140+ path.display()
141+ )
142+ })?;
143+
144+ let decompressed = SmallVec::from_buf(decompressed);
145+ let mut compressed = SmallVec::from_buf(compressed);
146+
147+ let path = match kind {
148+ Kind::Tag | Kind::Commit | Kind::Tree => {
149+ let fsize = input_stream.metadata()?.size();
150+ assert!(fsize <= ::std::usize::MAX as u64);
151+ let fsize = fsize as usize;
152+ if bytes_read == fsize {
153+ None
154+ } else {
155+ let cap = compressed.capacity();
156+ if cap < fsize {
157+ compressed.reserve_exact(fsize - cap);
158+ debug_assert!(fsize == compressed.capacity());
159+ }
160+ unsafe {
161+ compressed.set_len(fsize);
162+ }
163+ input_stream.read_exact(&mut compressed[bytes_read..])?;
164+ None
165+ }
166+ }
167+ Kind::Blob => Some(path), // we will open the file again when needed. Maybe we can load small sized objects anyway
168+ };
169+
170+ Ok(Object {
171+ kind,
172+ size,
173+ decompressed_data: decompressed,
174+ compressed_data: compressed,
175+ header_size,
176+ _path: path,
177+ is_decompressed: deflate.is_done,
178+ })
179+ }
180+ }
181+
182+ pub fn parse_header(input: &[u8]) -> Result<(Kind, usize, usize), Error> {
183+ let header_end = input
184+ .iter()
185+ .position(|&b| b == 0)
186+ .ok_or_else(|| err_msg("Invalid header, did not find 0 byte"))?;
187+ let header = &input[..header_end];
188+ let mut split = header.split(|&b| b == b' ');
189+ match (split.next(), split.next()) {
190+ (Some(kind), Some(size)) => Ok((
191+ Kind::from_bytes(kind)?,
192+ str::from_utf8(size)?.parse()?,
193+ header_end + 1, // account for 0 byte
194+ )),
195+ _ => bail!("expected '<type> <size>'"),
196+ }
197+ }
198+
199+ fn sha1_path(id: &[u8; 20], mut root: PathBuf) -> PathBuf {
200+ let mut buf = String::with_capacity(40);
201+ id.write_hex(&mut buf)
202+ .expect("no failure as everything is preset by now");
203+ root.push(&buf[..2]);
204+ root.push(&buf[2..]);
205+ root
206+ }
207+
208+ pub fn at(path: impl Into<PathBuf>) -> Db {
209+ Db { path: path.into() }
210+ }
211+}
212+
213+pub use self::db::*;
214+pub use self::object::*;
lib/git-odb/src/object.rs+0−218
@@ -1,218 +0,0 @@
1−use std::str;
2−
3−use failure::Error;
4−
5−pub type Id = [u8; 20];
6−
7−#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd)]
8−pub enum Kind {
9− Tag,
10− Commit,
11− Tree,
12− Blob,
13−}
14−
15−impl Kind {
16− pub fn from_bytes(s: &[u8]) -> Result<Kind, Error> {
17− Ok(match s {
18− b"tag" => Kind::Tag,
19− b"commit" => Kind::Commit,
20− b"tree" => Kind::Tree,
21− b"blob" => Kind::Blob,
22− _ => bail!("Unknown object kind: {:?}", str::from_utf8(s)),
23− })
24− }
25−}
26−
27−pub mod parsed {
28− use failure::{err_msg, Error, ResultExt};
29− use object::{Id, Kind};
30− use std::str;
31− use hex::FromHex;
32− use {Sign, Time};
33−
34− const PGP_SIGNATURE_BEGIN: &'static [u8] = b"-----BEGIN PGP SIGNATURE-----";
35− const PGP_SIGNATURE_END: &'static [u8] = b"-----END PGP SIGNATURE-----";
36−
37− #[derive(PartialEq, Eq, Debug, Hash)]
38− pub enum Object<'data> {
39− Tag(Tag<'data>),
40− }
41−
42− impl<'data> Object<'data> {
43− pub fn kind(&self) -> Kind {
44− match self {
45− Object::Tag(_) => Kind::Tag,
46− }
47− }
48− }
49−
50− #[derive(PartialEq, Eq, Debug, Hash)]
51− pub struct Signature<'data> {
52− pub name: &'data [u8],
53− pub email: &'data [u8],
54− pub time: Time,
55− }
56−
57− #[derive(PartialEq, Eq, Debug, Hash)]
58− pub struct Tag<'data> {
59− pub target_raw: &'data [u8],
60− pub name_raw: &'data [u8],
61− pub target_kind: Kind,
62− pub message: Option<&'data [u8]>,
63− pub pgp_signature: Option<&'data [u8]>,
64− pub signature: Signature<'data>,
65− }
66−
67− fn split2_at_space(
68− d: &[u8],
69− v: impl FnOnce(&[u8], &[u8]) -> bool,
70− ) -> Result<(&[u8], &[u8]), Error> {
71− let mut t = d.splitn(2, |&b| b == b' ');
72− Ok(match (t.next(), t.next()) {
73− (Some(t1), Some(t2)) => {
74− if !v(t1, t2) {
75− bail!("Tokens in {:?} are invalid", str::from_utf8(d))
76− }
77− (t1, t2)
78− }
79− _ => bail!(
80− "didnt find two tokens separated by space in {:?}'",
81− str::from_utf8(d)
82− ),
83− })
84− }
85−
86− fn parse_timezone_offset(d: &str) -> Result<(i32, Sign), Error> {
87− let db = d.as_bytes();
88− if d.len() < 5 || !(db[0] == b'+' || db[0] == b'-') {
89− bail!("Invalid timezone offset: '{}'", d);
90− }
91− let sign = if db[0] == b'-' {
92− Sign::Minus
93− } else {
94− Sign::Plus
95− };
96− let hours = str::from_utf8(&db[..3])
97− .expect("valid utf8")
98− .parse::<i32>()?;
99− let minutes = str::from_utf8(&db[3..])
100− .expect("valid utf8")
101− .parse::<i32>()?;
102− Ok((hours * 3600 + minutes * 60, sign))
103− }
104−
105− fn parse_signature(d: &[u8]) -> Result<Signature, Error> {
106− const ONE_SPACE: usize = 1;
107− let email_begin = d.iter()
108− .position(|&b| b == b'<')
109− .ok_or_else(|| err_msg("Could not find beginning of email marked by '<'"))
110− .and_then(|pos| {
111− if pos == 0 {
112− Err(err_msg("Email found in place of author name"))
113− } else {
114− Ok(pos)
115− }
116− })?;
117− let email_end = email_begin
118− + d.iter()
119− .skip(email_begin)
120− .position(|&b| b == b'>')
121− .ok_or_else(|| err_msg("Could not find end of email marked by '>'"))
122− .and_then(|pos| {
123− if pos >= d.len() - 1 - ONE_SPACE {
124− Err(err_msg("There is no time after email"))
125− } else {
126− Ok(pos)
127− }
128− })?;
129− let (time_in_seconds, tzofz) = split2_at_space(&d[email_end + ONE_SPACE + 1..], |_, _| {
130− true
131− }).and_then(|(t1, t2)| {
132− str::from_utf8(t1)
133− .map_err(Into::into)
134− .and_then(|t1| str::from_utf8(t2).map_err(Into::into).map(|t2| (t1, t2)))
135− })
136− .with_context(|_| "Could not convert time to utf8, even though it should be ascii")?;
137− let (offset, sign) = parse_timezone_offset(tzofz)
138− .with_context(|_| format!("Timezone parsing failed for '{}'", tzofz))?;
139−
140− Ok(Signature {
141− name: &d[..email_begin - ONE_SPACE],
142− email: &d[email_begin + 1..email_end],
143− time: Time {
144− time: time_in_seconds
145− .parse::<u32>()
146− .with_context(|_| format!("Could not parse '{}' to seconds", time_in_seconds))?,
147− offset,
148− sign,
149− },
150− })
151− }
152−
153− fn parse_message<'data>(
154− d: &'data [u8],
155− mut lines: impl Iterator<Item = &'data [u8]>,
156− ) -> Result<(Option<&'data [u8]>, Option<&'data [u8]>), Error> {
157− Ok(match lines.next() {
158− Some(l) if l.len() == 0 => {
159− let msg_begin = (l.as_ptr().wrapping_offset_from(d.as_ptr()) + 1) as usize;
160− if msg_begin >= d.len() {
161− bail!("Message separator was not followed by message")
162− }
163− let mut msg_end = d.len();
164− let mut pgp_signature = None;
165− if let Some(pgp_begin_line) = lines.find(|l| l.starts_with(PGP_SIGNATURE_BEGIN)) {
166− match lines.find(|l| l.starts_with(PGP_SIGNATURE_END)) {
167− None => bail!("Didn't find end of signature marker"),
168− Some(_) => {
169− msg_end =
170− pgp_begin_line.as_ptr().wrapping_offset_from(d.as_ptr()) as usize;
171− pgp_signature = Some(&d[msg_end..])
172− }
173− }
174− }
175− (Some(&d[msg_begin..msg_end]), pgp_signature)
176− }
177− Some(l) => bail!(
178− "Expected empty newline to separate message, got {:?}",
179− str::from_utf8(l),
180− ),
181− None => (None, None),
182− })
183− }
184−
185− impl<'data> Tag<'data> {
186− pub fn target(&self) -> Id {
187− <[u8; 20]>::from_hex(self.target_raw).expect("prior validation")
188− }
189− pub fn from_bytes(d: &'data [u8]) -> Result<Tag<'data>, Error> {
190− let mut lines = d.split(|&b| b == b'\n');
191− let (target, target_kind, name, signature) =
192− match (lines.next(), lines.next(), lines.next(), lines.next()) {
193− (Some(target), Some(kind), Some(name), Some(tagger)) => {
194− let (_, target) = split2_at_space(target, |f, v| {
195− f == b"object" && v.len() == 40 && <[u8; 20]>::from_hex(v).is_ok()
196− })?;
197− let kind = split2_at_space(kind, |f, _v| f == b"type")
198− .and_then(|(_, kind)| Kind::from_bytes(kind))?;
199− let (_, name) = split2_at_space(name, |f, _v| f == b"tag")?;
200− let (_, tagger) = split2_at_space(tagger, |f, _v| f == b"tagger")?;
201− (target, kind, name, parse_signature(tagger)?)
202− }
203− _ => bail!("Expected four lines: target, type, tag and tagger"),
204− };
205−
206− let (message, pgp_signature) = parse_message(d, &mut lines)?;
207−
208− Ok(Tag {
209− target_raw: target,
210− name_raw: name,
211− target_kind,
212− message,
213− signature,
214− pgp_signature,
215− })
216− }
217− }
218−}
lib/git-odb/src/object/mod.rs+27−0
@@ -0,0 +1,27 @@
1+use std::str;
2+
3+use failure::Error;
4+
5+pub type Id = [u8; 20];
6+
7+#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd)]
8+pub enum Kind {
9+ Tag,
10+ Commit,
11+ Tree,
12+ Blob,
13+}
14+
15+impl Kind {
16+ pub fn from_bytes(s: &[u8]) -> Result<Kind, Error> {
17+ Ok(match s {
18+ b"tag" => Kind::Tag,
19+ b"commit" => Kind::Commit,
20+ b"tree" => Kind::Tree,
21+ b"blob" => Kind::Blob,
22+ _ => bail!("Unknown object kind: {:?}", str::from_utf8(s)),
23+ })
24+ }
25+}
26+
27+pub mod parsed;
lib/git-odb/src/object/parsed.rs+188−0
@@ -0,0 +1,188 @@
1+use failure::{err_msg, Error, ResultExt};
2+use object::{Id, Kind};
3+use std::str;
4+use hex::FromHex;
5+use {Sign, Time};
6+
7+const PGP_SIGNATURE_BEGIN: &'static [u8] = b"-----BEGIN PGP SIGNATURE-----";
8+const PGP_SIGNATURE_END: &'static [u8] = b"-----END PGP SIGNATURE-----";
9+
10+#[derive(PartialEq, Eq, Debug, Hash)]
11+pub enum Object<'data> {
12+ Tag(Tag<'data>),
13+}
14+
15+impl<'data> Object<'data> {
16+ pub fn kind(&self) -> Kind {
17+ match self {
18+ Object::Tag(_) => Kind::Tag,
19+ }
20+ }
21+}
22+
23+#[derive(PartialEq, Eq, Debug, Hash)]
24+pub struct Signature<'data> {
25+ pub name: &'data [u8],
26+ pub email: &'data [u8],
27+ pub time: Time,
28+}
29+
30+#[derive(PartialEq, Eq, Debug, Hash)]
31+pub struct Tag<'data> {
32+ pub target_raw: &'data [u8],
33+ pub name_raw: &'data [u8],
34+ pub target_kind: Kind,
35+ pub message: Option<&'data [u8]>,
36+ pub pgp_signature: Option<&'data [u8]>,
37+ pub signature: Signature<'data>,
38+}
39+
40+fn split2_at_space(
41+ d: &[u8],
42+ v: impl FnOnce(&[u8], &[u8]) -> bool,
43+) -> Result<(&[u8], &[u8]), Error> {
44+ let mut t = d.splitn(2, |&b| b == b' ');
45+ Ok(match (t.next(), t.next()) {
46+ (Some(t1), Some(t2)) => {
47+ if !v(t1, t2) {
48+ bail!("Tokens in {:?} are invalid", str::from_utf8(d))
49+ }
50+ (t1, t2)
51+ }
52+ _ => bail!(
53+ "didnt find two tokens separated by space in {:?}'",
54+ str::from_utf8(d)
55+ ),
56+ })
57+}
58+
59+fn parse_timezone_offset(d: &str) -> Result<(i32, Sign), Error> {
60+ let db = d.as_bytes();
61+ if d.len() < 5 || !(db[0] == b'+' || db[0] == b'-') {
62+ bail!("Invalid timezone offset: '{}'", d);
63+ }
64+ let sign = if db[0] == b'-' {
65+ Sign::Minus
66+ } else {
67+ Sign::Plus
68+ };
69+ let hours = str::from_utf8(&db[..3])
70+ .expect("valid utf8")
71+ .parse::<i32>()?;
72+ let minutes = str::from_utf8(&db[3..])
73+ .expect("valid utf8")
74+ .parse::<i32>()?;
75+ Ok((hours * 3600 + minutes * 60, sign))
76+}
77+
78+fn parse_signature(d: &[u8]) -> Result<Signature, Error> {
79+ const ONE_SPACE: usize = 1;
80+ let email_begin = d.iter()
81+ .position(|&b| b == b'<')
82+ .ok_or_else(|| err_msg("Could not find beginning of email marked by '<'"))
83+ .and_then(|pos| {
84+ if pos == 0 {
85+ Err(err_msg("Email found in place of author name"))
86+ } else {
87+ Ok(pos)
88+ }
89+ })?;
90+ let email_end = email_begin
91+ + d.iter()
92+ .skip(email_begin)
93+ .position(|&b| b == b'>')
94+ .ok_or_else(|| err_msg("Could not find end of email marked by '>'"))
95+ .and_then(|pos| {
96+ if pos >= d.len() - 1 - ONE_SPACE {
97+ Err(err_msg("There is no time after email"))
98+ } else {
99+ Ok(pos)
100+ }
101+ })?;
102+ let (time_in_seconds, tzofz) = split2_at_space(&d[email_end + ONE_SPACE + 1..], |_, _| true)
103+ .and_then(|(t1, t2)| {
104+ str::from_utf8(t1)
105+ .map_err(Into::into)
106+ .and_then(|t1| str::from_utf8(t2).map_err(Into::into).map(|t2| (t1, t2)))
107+ })
108+ .with_context(|_| "Could not convert time to utf8, even though it should be ascii")?;
109+ let (offset, sign) = parse_timezone_offset(tzofz)
110+ .with_context(|_| format!("Timezone parsing failed for '{}'", tzofz))?;
111+
112+ Ok(Signature {
113+ name: &d[..email_begin - ONE_SPACE],
114+ email: &d[email_begin + 1..email_end],
115+ time: Time {
116+ time: time_in_seconds
117+ .parse::<u32>()
118+ .with_context(|_| format!("Could not parse '{}' to seconds", time_in_seconds))?,
119+ offset,
120+ sign,
121+ },
122+ })
123+}
124+
125+fn parse_message<'data>(
126+ d: &'data [u8],
127+ mut lines: impl Iterator<Item = &'data [u8]>,
128+) -> Result<(Option<&'data [u8]>, Option<&'data [u8]>), Error> {
129+ Ok(match lines.next() {
130+ Some(l) if l.len() == 0 => {
131+ let msg_begin = (l.as_ptr().wrapping_offset_from(d.as_ptr()) + 1) as usize;
132+ if msg_begin >= d.len() {
133+ bail!("Message separator was not followed by message")
134+ }
135+ let mut msg_end = d.len();
136+ let mut pgp_signature = None;
137+ if let Some(pgp_begin_line) = lines.find(|l| l.starts_with(PGP_SIGNATURE_BEGIN)) {
138+ match lines.find(|l| l.starts_with(PGP_SIGNATURE_END)) {
139+ None => bail!("Didn't find end of signature marker"),
140+ Some(_) => {
141+ msg_end = pgp_begin_line.as_ptr().wrapping_offset_from(d.as_ptr()) as usize;
142+ pgp_signature = Some(&d[msg_end..])
143+ }
144+ }
145+ }
146+ (Some(&d[msg_begin..msg_end]), pgp_signature)
147+ }
148+ Some(l) => bail!(
149+ "Expected empty newline to separate message, got {:?}",
150+ str::from_utf8(l),
151+ ),
152+ None => (None, None),
153+ })
154+}
155+
156+impl<'data> Tag<'data> {
157+ pub fn target(&self) -> Id {
158+ <[u8; 20]>::from_hex(self.target_raw).expect("prior validation")
159+ }
160+ pub fn from_bytes(d: &'data [u8]) -> Result<Tag<'data>, Error> {
161+ let mut lines = d.split(|&b| b == b'\n');
162+ let (target, target_kind, name, signature) =
163+ match (lines.next(), lines.next(), lines.next(), lines.next()) {
164+ (Some(target), Some(kind), Some(name), Some(tagger)) => {
165+ let (_, target) = split2_at_space(target, |f, v| {
166+ f == b"object" && v.len() == 40 && <[u8; 20]>::from_hex(v).is_ok()
167+ })?;
168+ let kind = split2_at_space(kind, |f, _v| f == b"type")
169+ .and_then(|(_, kind)| Kind::from_bytes(kind))?;
170+ let (_, name) = split2_at_space(name, |f, _v| f == b"tag")?;
171+ let (_, tagger) = split2_at_space(tagger, |f, _v| f == b"tagger")?;
172+ (target, kind, name, parse_signature(tagger)?)
173+ }
174+ _ => bail!("Expected four lines: target, type, tag and tagger"),
175+ };
176+
177+ let (message, pgp_signature) = parse_message(d, &mut lines)?;
178+
179+ Ok(Tag {
180+ target_raw: target,
181+ name_raw: name,
182+ target_kind,
183+ message,
184+ signature,
185+ pgp_signature,
186+ })
187+ }
188+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t