Set tracking branch on push (#452)

set upstream on each push (if not already defined). closes #275
This commit is contained in:
Stephan Dilly 2020-12-20 16:48:47 +01:00 committed by GitHub
parent 44ba5a83c9
commit 2b7d7f467b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 6 deletions

View File

@ -4,7 +4,7 @@ use crate::{
error::{Error, Result},
sync::{utils, CommitId},
};
use git2::BranchType;
use git2::{BranchType, Repository};
use scopetime::scope_time;
use utils::get_head_repo;
@ -87,6 +87,25 @@ pub struct BranchCompare {
pub behind: usize,
}
///
pub(crate) fn branch_set_upstream(
repo: &Repository,
branch_name: &str,
) -> Result<()> {
scope_time!("branch_set_upstream");
let mut branch =
repo.find_branch(branch_name, BranchType::Local)?;
if branch.upstream().is_err() {
//TODO: what about other remote names
let upstream_name = format!("origin/{}", branch_name);
branch.set_upstream(Some(upstream_name.as_str()))?;
}
Ok(())
}
///
pub fn branch_compare_upstream(
repo_path: &str,
@ -97,6 +116,7 @@ pub fn branch_compare_upstream(
let repo = utils::repo(repo_path)?;
let branch = repo.find_branch(branch, BranchType::Local)?;
let upstream = branch.upstream()?;
let branch_commit =

View File

@ -1,6 +1,6 @@
//!
use super::CommitId;
use super::{branch::branch_set_upstream, CommitId};
use crate::{
error::Result, sync::cred::BasicAuthCredential, sync::utils,
};
@ -90,7 +90,7 @@ pub fn push(
basic_credential: Option<BasicAuthCredential>,
progress_sender: Sender<ProgressNotification>,
) -> Result<()> {
scope_time!("push_origin");
scope_time!("push");
let repo = utils::repo(repo_path)?;
let mut remote = repo.find_remote(remote)?;
@ -103,7 +103,11 @@ pub fn push(
));
options.packbuilder_parallelism(0);
remote.push(&[branch], Some(&mut options))?;
let branch_name = format!("refs/heads/{}", branch);
remote.push(&[branch_name.as_str()], Some(&mut options))?;
branch_set_upstream(&repo, branch)?;
Ok(())
}

View File

@ -377,8 +377,6 @@ impl Status {
fn push(&self) {
if let Some(branch) = self.git_branch_name.last() {
let branch = format!("refs/heads/{}", branch);
self.queue
.borrow_mut()
.push_back(InternalEvent::Push(branch));