Skip to content

Commit 2b7d7f4

Browse files
author
Stephan Dilly
authored
Set tracking branch on push (#452)
set upstream on each push (if not already defined). closes #275
1 parent 44ba5a8 commit 2b7d7f4

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

asyncgit/src/sync/branch.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
error::{Error, Result},
55
sync::{utils, CommitId},
66
};
7-
use git2::BranchType;
7+
use git2::{BranchType, Repository};
88
use scopetime::scope_time;
99
use utils::get_head_repo;
1010

@@ -87,6 +87,25 @@ pub struct BranchCompare {
8787
pub behind: usize,
8888
}
8989

90+
///
91+
pub(crate) fn branch_set_upstream(
92+
repo: &Repository,
93+
branch_name: &str,
94+
) -> Result<()> {
95+
scope_time!("branch_set_upstream");
96+
97+
let mut branch =
98+
repo.find_branch(branch_name, BranchType::Local)?;
99+
100+
if branch.upstream().is_err() {
101+
//TODO: what about other remote names
102+
let upstream_name = format!("origin/{}", branch_name);
103+
branch.set_upstream(Some(upstream_name.as_str()))?;
104+
}
105+
106+
Ok(())
107+
}
108+
90109
///
91110
pub fn branch_compare_upstream(
92111
repo_path: &str,
@@ -97,6 +116,7 @@ pub fn branch_compare_upstream(
97116
let repo = utils::repo(repo_path)?;
98117

99118
let branch = repo.find_branch(branch, BranchType::Local)?;
119+
100120
let upstream = branch.upstream()?;
101121

102122
let branch_commit =

asyncgit/src/sync/remotes.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//!
22
3-
use super::CommitId;
3+
use super::{branch::branch_set_upstream, CommitId};
44
use crate::{
55
error::Result, sync::cred::BasicAuthCredential, sync::utils,
66
};
@@ -90,7 +90,7 @@ pub fn push(
9090
basic_credential: Option<BasicAuthCredential>,
9191
progress_sender: Sender<ProgressNotification>,
9292
) -> Result<()> {
93-
scope_time!("push_origin");
93+
scope_time!("push");
9494

9595
let repo = utils::repo(repo_path)?;
9696
let mut remote = repo.find_remote(remote)?;
@@ -103,7 +103,11 @@ pub fn push(
103103
));
104104
options.packbuilder_parallelism(0);
105105

106-
remote.push(&[branch], Some(&mut options))?;
106+
let branch_name = format!("refs/heads/{}", branch);
107+
108+
remote.push(&[branch_name.as_str()], Some(&mut options))?;
109+
110+
branch_set_upstream(&repo, branch)?;
107111

108112
Ok(())
109113
}

src/tabs/status.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,6 @@ impl Status {
377377

378378
fn push(&self) {
379379
if let Some(branch) = self.git_branch_name.last() {
380-
let branch = format!("refs/heads/{}", branch);
381-
382380
self.queue
383381
.borrow_mut()
384382
.push_back(InternalEvent::Push(branch));

0 commit comments

Comments
 (0)