summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2023-04-10 21:31:02 +0200
committerJustin Wernick <justin@worthe-it.co.za>2023-04-10 21:31:02 +0200
commit972319c1811e1a9c4f491a03ba92ea5a624cca5e (patch)
tree225e09bb5af58481c214f2422ad9b8f1527fa2db
parentcc5fd57780595d3ec73dc4e0e884d4099e319878 (diff)
Update the main branch of a repo
-rw-r--r--readme.org2
-rw-r--r--src/git.rs25
-rw-r--r--src/lib.rs7
-rw-r--r--src/parser.rs9
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<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);
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<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,
}