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 --- src/git.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src/git.rs') 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); -- cgit v1.2.3