From 972319c1811e1a9c4f491a03ba92ea5a624cca5e Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Mon, 10 Apr 2023 21:31:02 +0200 Subject: Update the main branch of a repo --- readme.org | 2 +- src/git.rs | 25 ++++++++++++++++++++++++- src/lib.rs | 7 ++++++- src/parser.rs | 9 +++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/readme.org b/readme.org index 9dc3eb8..a63a3d9 100644 --- a/readme.org +++ b/readme.org @@ -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 diff --git a/src/git.rs b/src/git.rs index 4b1ec0d..1159e7f 100644 --- a/src/git.rs +++ b/src/git.rs @@ -69,9 +69,14 @@ pub fn init( repo_name: &str, group: &Option, description: &Option, + branch: &str, ) -> Result { 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); diff --git a/src/lib.rs b/src/lib.rs index 196078c..134ba97 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,12 +33,17 @@ pub fn run_command(user_input: String) -> Result, 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), @@ -25,12 +26,20 @@ pub struct SetDescriptionArgs { pub description: String, } +#[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, #[arg(long)] pub description: Option, + #[arg(long, default_value = "main")] + pub branch: String, pub repo_name: String, } -- cgit v1.2.3