Skip to content

Commit

Permalink
add checkout to commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hateofhades committed Dec 26, 2024
1 parent 4bdf048 commit fa10513
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src-tauri/src/git/git_commit.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use super::{
git_commit_author::{GitCommitAuthor, GitCommitAuthorType},
git_files::GitFilesRequired,
git_folders::{GitFolders, GIT_FOLDER},
git_project::GitProject,
git_tree::GitTree,
object::{GitObject, Header},
};
use crate::errors::git_object_error::{CommitError, GitObjectError};
use core::fmt;
use serde::{Deserialize, Serialize};
use std::{
fs::{self, OpenOptions},
io::Write,
path::{Path, PathBuf},
};

pub enum CommitPrefix {
Tree,
Expand Down Expand Up @@ -149,6 +157,35 @@ impl GitCommit {

Ok(history)
}

/**
* Checkout all the files in a commit
*/
pub fn checkout(&self, project: &GitProject) -> Result<(), GitObjectError> {
let files =
GitTree::from_hash(project, &self.get_tree_hash())?.get_object_blobs(project, None);

let _ = files.iter().for_each(|file| {
let path = PathBuf::from(project.get_directory()).join(&file.0);
let file_in_fs = OpenOptions::new().write(true).create(true).open(path);

Check warning on line 170 in src-tauri/src/git/git_commit.rs

View check run for this annotation

Codecov / codecov/patch

src-tauri/src/git/git_commit.rs#L164-L170

Added lines #L164 - L170 were not covered by tests

if let Ok(mut file_in_fs) = file_in_fs {
let _ = file_in_fs.write_all(file.1.data());
}
});

let head_path = PathBuf::from(project.get_directory())
.join(GIT_FOLDER)
.join(GitFilesRequired::HEAD.as_ref());
OpenOptions::new()
.write(true)
.open(head_path)
.map_err(|_| GitObjectError::InvalidCommitFile(CommitError::InvalidContent))?
.write_all(self.get_hash().as_bytes())
.map_err(|_| GitObjectError::InvalidHash)?;

Check warning on line 185 in src-tauri/src/git/git_commit.rs

View check run for this annotation

Codecov / codecov/patch

src-tauri/src/git/git_commit.rs#L172-L185

Added lines #L172 - L185 were not covered by tests

Ok(())
}

Check warning on line 188 in src-tauri/src/git/git_commit.rs

View check run for this annotation

Codecov / codecov/patch

src-tauri/src/git/git_commit.rs#L187-L188

Added lines #L187 - L188 were not covered by tests
}

impl GitObject for GitCommit {
Expand Down

0 comments on commit fa10513

Please # to comment.