Skip to content
New issue

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

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

Already on GitHub? # to your account

Add Checkout to Commit #234

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
25 changes: 25 additions & 0 deletions src-tauri/src/git/git_branch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
use std::{fs::OpenOptions, io::Write, path::PathBuf};

use serde::{Deserialize, Serialize};

use crate::errors::git_object_error::{CommitError, GitObjectError};

use super::{
git_commit::GitCommit, git_files::GitFilesRequired, git_folders::GIT_FOLDER,
git_project::GitProject, object::GitObject,
};

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct GitBranch {
Expand All @@ -11,6 +20,22 @@
pub fn new(name: String, commit: String) -> GitBranch {
GitBranch { name, commit }
}

pub fn checkout(&self, project: &GitProject) -> Result<(), GitObjectError> {
GitCommit::from_hash(project, &self.commit)?.checkout(project)?;

Check warning on line 25 in src-tauri/src/git/git_branch.rs

View check run for this annotation

Codecov / codecov/patch

src-tauri/src/git/git_branch.rs#L24-L25

Added lines #L24 - L25 were not covered by tests

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.name.as_bytes())
.map_err(|_| GitObjectError::InvalidHash)?;

Check warning on line 35 in src-tauri/src/git/git_branch.rs

View check run for this annotation

Codecov / codecov/patch

src-tauri/src/git/git_branch.rs#L27-L35

Added lines #L27 - L35 were not covered by tests

Ok(())
}

Check warning on line 38 in src-tauri/src/git/git_branch.rs

View check run for this annotation

Codecov / codecov/patch

src-tauri/src/git/git_branch.rs#L37-L38

Added lines #L37 - L38 were not covered by tests
}

#[cfg(test)]
Expand Down
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,15 @@
use super::{
git_commit_author::{GitCommitAuthor, GitCommitAuthorType},
git_files::GitFilesRequired,
git_folders::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::OpenOptions, io::Write, path::PathBuf};

pub enum CommitPrefix {
Tree,
Expand Down Expand Up @@ -149,6 +153,39 @@

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);

files.iter().for_each(|file| {
let path = PathBuf::from(project.get_directory()).join(&file.0);
let file_in_fs = OpenOptions::new()
.write(true)
.truncate(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#L160-L170

Added lines #L160 - 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
17 changes: 17 additions & 0 deletions src-tauri/src/git/project_folder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{
git_branch::GitBranch,
git_commit::{GitCommit, GitCommitWithHash},
git_folders::GitBranchType,
git_project::GitProject,
Expand Down Expand Up @@ -83,6 +84,22 @@
.map_err(|_| GitError::InvalidHistory)
}

#[tauri::command]
pub fn checkout_branch(project: GitProject, branch: GitBranch) -> Result<(), GitError> {
branch
.checkout(&project)
.map_err(|_| GitError::NoLocalBranches)
}

Check warning on line 92 in src-tauri/src/git/project_folder.rs

View check run for this annotation

Codecov / codecov/patch

src-tauri/src/git/project_folder.rs#L88-L92

Added lines #L88 - L92 were not covered by tests

#[tauri::command]
pub fn checkout_commit(project: GitProject, hash: &str) -> Result<(), GitError> {
let commit = GitCommit::from_hash(&project, hash).map_err(|_| GitError::InvalidHistory)?;

Check warning on line 96 in src-tauri/src/git/project_folder.rs

View check run for this annotation

Codecov / codecov/patch

src-tauri/src/git/project_folder.rs#L95-L96

Added lines #L95 - L96 were not covered by tests

commit
.checkout(&project)
.map_err(|_| GitError::InvalidHistory)
}

Check warning on line 101 in src-tauri/src/git/project_folder.rs

View check run for this annotation

Codecov / codecov/patch

src-tauri/src/git/project_folder.rs#L98-L101

Added lines #L98 - L101 were not covered by tests

#[cfg(test)]
mod tests {
use std::{
Expand Down
Loading