Skip to content

Commit

Permalink
add commands
Browse files Browse the repository at this point in the history
  • Loading branch information
hateofhades committed Dec 26, 2024
1 parent ac25be4 commit 6d93136
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
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 @@ impl GitBranch {
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
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 @@ pub fn get_commit_history(
.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

0 comments on commit 6d93136

Please # to comment.