diff options
-rw-r--r-- | readme.org | 2 | ||||
-rw-r--r-- | src/git.rs | 25 | ||||
-rw-r--r-- | src/lib.rs | 7 | ||||
-rw-r--r-- | src/parser.rs | 9 |
4 files changed, 40 insertions, 3 deletions
@@ -41,7 +41,7 @@ Pijul. - [X] change an existing repo - [X] Change ~git-init~ name to just be ~init~. Later, the ~git~ part will be an option which defaults to git. -- [ ] set the main branch of a repo +- [X] set the main branch of a repo - [ ] help docs on all the commands ** Post-MVP @@ -69,9 +69,14 @@ pub fn init( repo_name: &str, group: &Option<String>, description: &Option<String>, + branch: &str, ) -> Result<GitInitResult, ShackleError> { let mut init_opts = RepositoryInitOptions::new(); - init_opts.bare(true).mkdir(true).no_reinit(true); + init_opts + .bare(true) + .mkdir(true) + .no_reinit(true) + .initial_head(branch); let path = match group { Some(group) => { @@ -167,6 +172,24 @@ pub fn set_description(directory: &PathBuf, description: &str) -> Result<(), Sha } } +pub fn set_branch(directory: &PathBuf, branch: &str) -> Result<(), ShackleError> { + if !is_valid_git_repo_path(&directory)? { + return Err(ShackleError::InvalidDirectory); + } + + if let Ok(repo) = Repository::open_bare(&directory) { + repo.reference_symbolic( + "HEAD", + &format!("refs/heads/{branch}"), + true, + "shackle set-branch", + )?; + Ok(()) + } else { + Err(ShackleError::InvalidDirectory) + } +} + pub fn upload_pack(upload_pack_args: &GitUploadPackArgs) -> Result<(), ShackleError> { if !is_valid_git_repo_path(&upload_pack_args.directory)? { return Err(ShackleError::InvalidDirectory); @@ -33,12 +33,17 @@ pub fn run_command(user_input: String) -> Result<ControlFlow<(), ()>, ShackleErr git::set_description(&directory, &description)?; println!("Successfully updated description"); } + Ok(ShackleCommand::SetBranch(SetBranchArgs { directory, branch })) => { + git::set_branch(&directory, &branch)?; + println!("Successfully updated branch"); + } Ok(ShackleCommand::Init(InitArgs { repo_name, group, description, + branch, })) => { - let init_result = git::init(&repo_name, &group, &description)?; + let init_result = git::init(&repo_name, &group, &description, &branch)?; println!("Successfully created \"{}\"", init_result.path.display()); } Ok(ShackleCommand::GitUploadPack(upload_pack_args)) => { diff --git a/src/parser.rs b/src/parser.rs index 14b1a49..3a7d129 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -14,6 +14,7 @@ pub enum ShackleCommand { /// List all repositories available List, SetDescription(SetDescriptionArgs), + SetBranch(SetBranchArgs), Init(InitArgs), GitUploadPack(GitUploadPackArgs), GitReceivePack(GitReceivePackArgs), @@ -26,11 +27,19 @@ pub struct SetDescriptionArgs { } #[derive(Parser, Clone, Debug, PartialEq, Eq)] +pub struct SetBranchArgs { + pub directory: PathBuf, + pub branch: String, +} + +#[derive(Parser, Clone, Debug, PartialEq, Eq)] pub struct InitArgs { #[arg(long)] pub group: Option<String>, #[arg(long)] pub description: Option<String>, + #[arg(long, default_value = "main")] + pub branch: String, pub repo_name: String, } |