Skip to content

Commit

Permalink
cleanup after init and better error for !auth config
Browse files Browse the repository at this point in the history
  • Loading branch information
gschoeni committed Aug 10, 2022
1 parent 1603b82 commit b1ea251
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 12 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,9 @@ To inspect any of the key value dbs below
COMMIT_HASH_3/
versions/ (copies of original files, versioned with commit ids)
// TODO: make subdirs based on first two chars of hash, which would mean we have ~16^2=256 top level dirs, then 256 in ///// each, which would spread out the data nicely. If you take logbase 256 that means we can have a billion examples /// split into the 4 levels easily
// (I think git does something somewhat similar?)
//
// ex) 59E029D4812AEBF0 -> 59/E0/29D4812AEBF0
// 72617025710EBB55 -> 72/61/7025710EBB55
// ex) 59E029D4812AEBF0 -> 59/E029D4812AEBF0
// 72617025710EBB55 -> 72/617025710EBB55
//
// TODO: use best lossless compression type based on file type, fall back to zlib or something for rest
// TODO: maybe create watcher program to catch and intercept on write? Is this possible?
Expand Down
20 changes: 17 additions & 3 deletions src/lib/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,28 @@ pub fn init(path: &Path) -> Result<LocalRepository, OxenError> {
return Err(OxenError::basic_str(err));
}

// Cleanup the .oxen dir if init fails
match p_init(path) {
Ok(result) => Ok(result),
Err(error) => {
std::fs::remove_dir_all(hidden_dir)?;
Err(error)
}
}
}

fn p_init(path: &Path) -> Result<LocalRepository, OxenError> {
let hidden_dir = util::fs::oxen_hidden_dir(path);

std::fs::create_dir_all(hidden_dir)?;
let config_path = util::fs::config_filepath(path);
let repo = LocalRepository::new(path)?;
repo.save(&config_path)?;

if let Ok(commit) = commit_with_no_files(&repo, constants::INITIAL_COMMIT_MSG) {
println!("Initial commit {}", commit.id);
}
let commit = commit_with_no_files(&repo, constants::INITIAL_COMMIT_MSG)?;
println!("Initial commit {}", commit.id);

// TODO: cleanup .oxen on failure

Ok(repo)
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/src/config/auth_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl AuthConfig {

pub fn default() -> Result<AuthConfig, OxenError> {
let err = String::from(
"AuthConfig::default() not configuration found, acquire an auth_config.toml file from your administrator.",
"~/.oxen/auth_config.toml not found, acquire the proper file from your administrator.",
);
if let Some(home_dir) = dirs::home_dir() {
let oxen_dir = util::fs::oxen_hidden_dir(&home_dir);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl OxenError {
}

pub fn head_not_found() -> OxenError {
OxenError::basic_str("Error: HEAD not found.")
OxenError::basic_str("Error: HEAD not found")
}

pub fn remote_not_set() -> OxenError {
Expand Down
10 changes: 8 additions & 2 deletions src/lib/src/index/commit_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ impl CommitWriter {
Ok(Some(parent_id)) => {
// We might be in a merge commit, in which case we would have multiple parents
if self.is_merge_commit() {
log::debug!("Create merge commit...");
self.create_merge_commit(message)
} else {
// We have one parent
log::debug!("Create commit with parent {:?}", parent_id);
Ok(NewCommit {
parent_ids: vec![parent_id],
message: String::from(message),
Expand All @@ -66,6 +68,7 @@ impl CommitWriter {
}
_ => {
// We are creating initial commit, no parents
log::debug!("Create initial commit...");
Ok(NewCommit {
parent_ids: vec![],
message: String::from(message),
Expand Down Expand Up @@ -136,9 +139,10 @@ impl CommitWriter {
// Create a commit object, that either points to parent or not
// must create this before anything else so that we know if it has parent or not.
let new_commit = self.create_commit_data(message)?;
let commit = self.gen_commit(&new_commit, status);
log::debug!("Created commit obj {:?}", new_commit);

log::debug!("COMMIT ID COMPUTED {} -> [{}]", commit.id, commit.message,);
let commit = self.gen_commit(&new_commit, status);
log::debug!("Commit Id computed {} -> [{}]", commit.id, commit.message,);

// Write entries
self.add_commit_from_status(&commit, status)?;
Expand All @@ -152,12 +156,14 @@ impl CommitWriter {
}

fn gen_commit(&self, commit_data: &NewCommit, status: &StagedData) -> Commit {
log::debug!("gen_commit from {} files", status.added_files.len());
let entries: Vec<StagedEntry> = status
.added_files
.iter()
.map(|(_, entry)| entry.clone())
.collect();
let id = util::hasher::compute_commit_hash(commit_data, &entries);
log::debug!("gen_commit id {}", id);
Commit::from_new_and_id(commit_data, id)
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/src/util/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn read_from_path(path: &Path) -> Result<String, OxenError> {
"util::fs::read_from_path could not open: {}",
path.display()
);
log::error!("{}", err);
Err(OxenError::basic_str(&err))
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/src/util/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ pub fn hash_buffer_128bit(buffer: &[u8]) -> u128 {

pub fn compute_commit_hash(commit_data: &NewCommit, entries: &[impl ContentHashable]) -> String {
let mut commit_hasher = xxhash_rust::xxh3::Xxh3::new();

log::debug!("Hashing {} entries", entries.len());
for entry in entries.iter() {
let hash = entry.content_hash();
let input = hash.as_bytes();
commit_hasher.update(input);
}

log::debug!("Hashing commit data {:?}", commit_data);
let commit_str = format!("{:?}", commit_data);
commit_hasher.update(commit_str.as_bytes());

Expand Down

0 comments on commit b1ea251

Please sign in to comment.