Skip to content

cleanup and init a bare repository #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 28 additions & 10 deletions git-config/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use nom::{
bytes::complete::{escaped, tag, take_till, take_while},
character::{
complete::{char, none_of, one_of},
is_newline, is_space,
is_space,
},
combinator::{map, opt},
error::{Error as NomError, ErrorKind},
Expand Down Expand Up @@ -834,9 +834,7 @@ impl<'a> Parser<'a> {
/// frontmatter
#[inline]
pub fn take_frontmatter(&mut self) -> Vec<Event<'a>> {
let mut to_return = vec![];
std::mem::swap(&mut self.frontmatter, &mut to_return);
to_return
std::mem::take(&mut self.frontmatter)
}

/// Returns the parsed sections from the parser. Consider
Expand Down Expand Up @@ -948,7 +946,7 @@ pub fn parse_from_bytes(input: &[u8]) -> Result<Parser, Error> {
let (i, frontmatter) = many0(alt((
map(comment, Event::Comment),
map(take_spaces, |whitespace| Event::Whitespace(Cow::Borrowed(whitespace))),
map(take_newline, |(newline, counter)| {
map(take_newlines, |(newline, counter)| {
newlines += counter;
Event::Newline(Cow::Borrowed(newline))
}),
Expand Down Expand Up @@ -1017,7 +1015,7 @@ pub fn parse_from_bytes_owned(input: &[u8]) -> Result<Parser<'static>, Error<'st
let (i, frontmatter) = many0(alt((
map(comment, Event::Comment),
map(take_spaces, |whitespace| Event::Whitespace(Cow::Borrowed(whitespace))),
map(take_newline, |(newline, counter)| {
map(take_newlines, |(newline, counter)| {
newlines += counter;
Event::Newline(Cow::Borrowed(newline))
}),
Expand Down Expand Up @@ -1096,7 +1094,7 @@ fn section<'a, 'b>(i: &'a [u8], node: &'b mut ParserNode) -> IResult<&'a [u8], (
}
}

if let Ok((new_i, (v, new_newlines))) = take_newline(i) {
if let Ok((new_i, (v, new_newlines))) = take_newlines(i) {
if old_i != new_i {
i = new_i;
newlines += new_newlines;
Expand Down Expand Up @@ -1367,10 +1365,30 @@ fn take_spaces(i: &[u8]) -> IResult<&[u8], &str> {
}
}

fn take_newline(i: &[u8]) -> IResult<&[u8], (&str, usize)> {
fn take_newlines(i: &[u8]) -> IResult<&[u8], (&str, usize)> {
let mut counter = 0;
let (i, v) = take_while(|c| (c as char).is_ascii() && is_newline(c))(i)?;
counter += v.len();
let mut consumed_bytes = 0;
let mut next_must_be_newline = false;
for b in i.iter() {
if !(*b as char).is_ascii() {
break;
};
if *b == b'\r' {
if next_must_be_newline {
break;
}
next_must_be_newline = true;
continue;
};
if *b == b'\n' {
counter += 1;
consumed_bytes += if next_must_be_newline { 2 } else { 1 };
next_must_be_newline = false;
} else {
break;
}
}
let (v, i) = i.split_at(consumed_bytes);
if v.is_empty() {
Err(nom::Err::Error(NomError {
input: i,
Expand Down
14 changes: 14 additions & 0 deletions git-config/tests/fixtures/repo-config.crlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; hello
# world

[core]
repositoryformatversion = 0
bare = true
[other]
repositoryformatversion = 0

bare = true

# before section with newline
[foo]
repositoryformatversion = 0
16 changes: 11 additions & 5 deletions git-config/tests/integration_tests/file_integeration_test.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use std::{borrow::Cow, convert::TryFrom};
use std::{borrow::Cow, convert::TryFrom, path::Path};

use git_config::{file::GitConfig, values::*};

#[test]
fn parse_config_with_windows_line_endings_successfully() -> crate::Result {
GitConfig::open(Path::new("tests").join("fixtures").join("repo-config.crlf"))?;
Ok(())
}

/// Asserts we can cast into all variants of our type
#[test]
fn get_value_for_all_provided_values() -> Result<(), Box<dyn std::error::Error>> {
fn get_value_for_all_provided_values() -> crate::Result {
let config = r#"
[core]
bool-explicit = false
Expand Down Expand Up @@ -72,7 +78,7 @@ fn get_value_for_all_provided_values() -> Result<(), Box<dyn std::error::Error>>
/// There was a regression where lookup would fail because we only checked the
/// last section entry for any given section and subsection
#[test]
fn get_value_looks_up_all_sections_before_failing() -> Result<(), Box<dyn std::error::Error>> {
fn get_value_looks_up_all_sections_before_failing() -> crate::Result {
let config = r#"
[core]
bool-explicit = false
Expand All @@ -98,7 +104,7 @@ fn get_value_looks_up_all_sections_before_failing() -> Result<(), Box<dyn std::e
}

#[test]
fn section_names_are_case_insensitive() -> Result<(), Box<dyn std::error::Error>> {
fn section_names_are_case_insensitive() -> crate::Result {
let config = "[core] bool-implicit";
let file = GitConfig::try_from(config)?;
assert!(file.value::<Boolean>("core", None, "bool-implicit").is_ok());
Expand All @@ -111,7 +117,7 @@ fn section_names_are_case_insensitive() -> Result<(), Box<dyn std::error::Error>
}

#[test]
fn value_names_are_case_insensitive() -> Result<(), Box<dyn std::error::Error>> {
fn value_names_are_case_insensitive() -> crate::Result {
let config = "[core]
a = true
A = false";
Expand Down
2 changes: 2 additions & 0 deletions git-config/tests/integration_tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
// TL;DR single mod makes integration tests faster to compile, test, and with
// less build artifacts.

type Result = std::result::Result<(), Box<dyn std::error::Error>>;

mod file_integeration_test;
mod parser_integration_tests;
15 changes: 12 additions & 3 deletions git-config/tests/integration_tests/parser_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,18 @@ fn newline_events_are_merged() {
#[test]
fn error() {
let input = "[core] a=b\n 4a=3";
println!("{}", parse_from_str(input).unwrap_err());
assert_eq!(
parse_from_str(input).unwrap_err().to_string(),
"Got an unexpected token on line 2 while trying to parse a config name: '4a=3'"
);
let input = "[core] a=b\n =3";
println!("{}", parse_from_str(input).unwrap_err());
assert_eq!(
parse_from_str(input).unwrap_err().to_string(),
"Got an unexpected token on line 2 while trying to parse a config name: '=3'"
);
let input = "[core";
println!("{}", parse_from_str(input).unwrap_err());
assert_eq!(
parse_from_str(input).unwrap_err().to_string(),
"Got an unexpected token on line 1 while trying to parse a section header: '[core'"
);
}
3 changes: 2 additions & 1 deletion git-pack/src/bundle/init.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::Bundle;
use std::{
convert::TryFrom,
path::{Path, PathBuf},
};

use crate::Bundle;

/// Returned by [`Bundle::at()`]
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]
Expand Down
3 changes: 2 additions & 1 deletion git-pack/src/bundle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ pub mod write;
mod verify {
use std::sync::{atomic::AtomicBool, Arc};

use crate::Bundle;
use git_features::progress::Progress;

use crate::Bundle;

impl Bundle {
/// Similar to [`crate::index::File::verify_integrity()`] but more convenient to call as the presence of the
/// pack file is a given.
Expand Down
3 changes: 1 addition & 2 deletions git-pack/src/cache/delta/from_offsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use std::{

use git_features::progress::{self, Progress};

use crate::cache::delta::Tree;
use crate::index::access::PackOffset;
use crate::{cache::delta::Tree, index::access::PackOffset};

/// Returned by [`Tree::from_offsets_in_pack()`]
#[derive(thiserror::Error, Debug)]
Expand Down
6 changes: 4 additions & 2 deletions git-pack/src/cache/delta/traverse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ use git_features::{
progress::{self, Progress},
};

use crate::cache::delta::{Item, Tree};
use crate::data::EntryRange;
use crate::{
cache::delta::{Item, Tree},
data::EntryRange,
};

mod resolve;

Expand Down
6 changes: 4 additions & 2 deletions git-pack/src/cache/delta/traverse/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use git_features::{
zlib,
};

use crate::cache::delta::traverse::{Context, Error};
use crate::data::EntryRange;
use crate::{
cache::delta::traverse::{Context, Error},
data::EntryRange,
};

pub(crate) fn deltas<T, F, P, MBFN, S, E>(
nodes: crate::cache::delta::Chunk<'_, T>,
Expand Down
3 changes: 2 additions & 1 deletion git-pack/src/data/entry/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::data::Entry;
use git_hash::SIZE_OF_SHA1_DIGEST as SHA1_SIZE;

use crate::data::Entry;

const _TYPE_EXT1: u8 = 0;
const COMMIT: u8 = 1;
const TREE: u8 = 2;
Expand Down
3 changes: 2 additions & 1 deletion git-pack/src/data/object.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Contains a borrowed Object bound to a buffer holding its decompressed data.

use crate::data::Object;
use git_object::{BlobRef, CommitRef, CommitRefIter, ObjectRef, TagRef, TagRefIter, TreeRef, TreeRefIter};

use crate::data::Object;

impl<'a> Object<'a> {
/// Constructs a new data object from `kind` and `data`.
pub fn new(kind: git_object::Kind, data: &'a [u8]) -> Object<'a> {
Expand Down
4 changes: 2 additions & 2 deletions git-pack/src/data/output/count/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::data;
use crate::data::output::Count;
use git_hash::ObjectId;

use crate::{data, data::output::Count};

/// Specifies how the pack location was handled during counting
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
Expand Down
7 changes: 4 additions & 3 deletions git-pack/src/index/traverse/indexed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use std::{

use git_features::{parallel, progress::Progress};

use crate::cache::delta::traverse::Context;
use crate::index::{self, util::index_entries_sorted_by_offset_ascending};

use super::{Error, SafetyCheck};
use crate::{
cache::delta::traverse::Context,
index::{self, util::index_entries_sorted_by_offset_ascending},
};

/// Traversal with index
impl index::File {
Expand Down
6 changes: 4 additions & 2 deletions git-pack/src/index/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use std::{convert::TryInto, io, sync::atomic::AtomicBool};
pub use error::Error;
use git_features::progress::{self, Progress};

use crate::cache::delta::{traverse::Context, Tree};
use crate::loose;
use crate::{
cache::delta::{traverse::Context, Tree},
loose,
};

mod encode;
mod error;
Expand Down
15 changes: 15 additions & 0 deletions git-repository/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
### v0.9.0 (2021-08-??)

#### New

- `init()`
- `init_bare()`
- `Repository::init(Kind)`
- `open()`
- `Repository::open()`

#### Breaking
- **renames / moves / Signature Changes**
- `path::Path` to `Path`
- `init::repository(dir)` -> `path::create::into(dir, **Kind**)`

### v0.8.1 (2021-08-28)

- Introduce `EasyArcExclusive` type, now available thanks to `parking_lot` 0.11.2
Expand Down
4 changes: 3 additions & 1 deletion git-repository/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ git-tempfile = { version ="^1.0.0", path = "../git-tempfile" }
git-lock = { version ="^1.0.0", path = "../git-lock" }
git-validate = { version = "^0.5.0", path = "../git-validate" }

git-config = { version ="^0.1.0", path = "../git-config" }
git-odb = { version ="^0.21.0", path = "../git-odb" }
git-hash = { version = "^0.5.0", path = "../git-hash" }
git-object = { version ="^0.13.0", path = "../git-object" }
Expand All @@ -52,10 +53,11 @@ git-diff = { version ="^0.9.0", path = "../git-diff", optional = true }
git-features = { version = "^0.16.0", path = "../git-features", features = ["progress"] }

signal-hook = { version = "0.3.9", default-features = false }
quick-error = "2.0.0"
thiserror = "1.0.26"
parking_lot = { version = "0.11.2", features = ["arc_lock"] }

[dev-dependencies]
git-testtools = { path = "../tests/tools" }
signal-hook = { version = "0.3.9", default-features = false }
anyhow = "1"
tempfile = "3.2.0"
1 change: 1 addition & 0 deletions git-repository/src/assets/baseline-init/config
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[core]
repositoryformatversion = 0
bare = {bare-value}
19 changes: 6 additions & 13 deletions git-repository/src/easy/borrow.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
#![allow(missing_docs)]
pub mod state {
use quick_error::quick_error;
quick_error! {
#[derive(Debug)]
pub enum Error {
Borrow(err: std::cell::BorrowError) {
display("A state member could not be borrowed")
from()
}
BorrowMut(err: std::cell::BorrowMutError) {
display("A state member could not be mutably borrowed")
from()
}
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("A state member could not be borrowed")]
Borrow(#[from] std::cell::BorrowError),
#[error("A state member could not be mutably borrowed")]
BorrowMut(#[from] std::cell::BorrowMutError),
}

pub type Result<T> = std::result::Result<T, Error>;
Expand Down
Loading