Skip to content

Commit

Permalink
Commit and push to stored branch name
Browse files Browse the repository at this point in the history
Before this change eureka would store the branch name on setup but
wouldn't commit nor push to the branch. This commit fixes that.
  • Loading branch information
simeg committed Nov 7, 2020
1 parent a8cac6d commit 93b88cd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
23 changes: 20 additions & 3 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ impl Git {
Self { repo }
}

pub fn checkout_branch(&self, branch_name: &str) -> Result<(), git2::Error> {
let repo = &self.repo;

let commit = repo
.head()
.map(|head| head.target())
.and_then(|oid| repo.find_commit(oid.unwrap()))?;

// Create new branch if it doesn't exist and swallow error
let _branch = repo.branch(branch_name, &commit, false);

let refname = format!("refs/heads/{}", branch_name);
let obj = repo.revparse_single(&*refname)?;

repo.checkout_tree(&obj, None)?;

repo.set_head(&*refname)
}

pub fn add(&self) -> Result<(), git2::Error> {
let mut index = self.repo.index()?;

Expand All @@ -41,13 +60,11 @@ impl Git {
)
}

pub fn push(&self) -> Result<(), git2::Error> {
pub fn push(&self, branch_name: &str) -> Result<(), git2::Error> {
let mut remote = self.repo.find_remote("origin").unwrap();

remote.connect_auth(Direction::Push, Some(self.get_callbacks()), None)?;

let branch_name = "master";

let mut options = PushOptions::default();
options.remote_callbacks(self.get_callbacks());
remote.push(
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,20 @@ where

self.printer
.println("Adding and committing your new idea..");
let branch_name = self
.fh
.config_read(Branch)
.unwrap_or_else(|_| panic!("Branch config is missing (should never end up here"));
git.checkout_branch(&*branch_name)
.expect("Something went wrong checking out branch");
git.add()
.and_then(|_| git.commit(commit_subject))
.expect("Something went wrong adding or committing");
self.printer.println("Added and committed!");

self.printer.println("Pushing your new idea..");
git.push().expect("Something went wrong pushing");
git.push(&*branch_name)
.expect("Something went wrong pushing");
self.printer.println("Pushed!");
}

Expand Down

0 comments on commit 93b88cd

Please sign in to comment.