Skip to content

Commit 3548b44

Browse files
committed
improve sync so it only pushes when actual changes, bump some deps
1 parent 09b9283 commit 3548b44

File tree

3 files changed

+74
-26
lines changed

3 files changed

+74
-26
lines changed

Cargo.lock

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "gstack"
3-
version = "0.0.4"
3+
version = "0.0.5"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -11,8 +11,8 @@ clap = { version = "4.5.11", features = ["derive"] }
1111
console = "0.15.8"
1212
dialoguer = "0.11.0"
1313
indicatif = "0.17.8"
14-
octocrab = "0.42.1"
15-
ron = "0.8.1"
14+
octocrab = "0.44.0"
15+
ron = "0.10.1"
1616
toml = "0.8.16"
1717
rustygit = "0.5.0"
1818
regex = "1.10.5"

src/repo_extensions.rs

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
use anyhow::bail;
12
use anyhow::Result;
2-
use anyhow::{bail, Ok};
33
use console::style;
44
use regex::Regex;
5-
use std::fmt::Debug;
6-
use std::{ops::Rem, str::FromStr};
5+
use std::str::FromStr;
76

87
use rustygit::{types::BranchName, Repository};
98

@@ -75,20 +74,68 @@ impl RepoExtenstions for Repository {
7574
}
7675
}
7776

78-
///Force push the curent branch to its associated remote, specifying the upstream branch
77+
///Force push the current branch to its associated remote, specifying the upstream branch,
78+
///but only if there are changes to push
7979
fn force_push_to_upstream(&self, upstream: &str, upstream_branch: &BranchName) -> Result<()> {
80-
let output = self.cmd_out(&[
81-
"push",
82-
"-u",
83-
upstream,
84-
upstream_branch.to_string().as_str(),
85-
"--force-with-lease",
86-
])?;
87-
println!(
88-
"Force pushed to upstream branch {} with output: {:?}",
89-
style(upstream_branch).green(),
90-
style(output.join(",")).white().on_black()
91-
);
80+
// Check if there are differences between local and remote branch
81+
let remote_ref = format!("{}/{}", upstream, upstream_branch);
82+
83+
// Get the commit hash of the local HEAD
84+
let local_commit = self.cmd_out(&["rev-parse", "HEAD"])?;
85+
let local_commit = local_commit.join("").trim().to_string();
86+
87+
// Try to get the commit hash of the remote branch
88+
let remote_commit_result = self.cmd_out(&["rev-parse", &remote_ref]);
89+
90+
// Determine if we need to push
91+
let need_to_push = match remote_commit_result {
92+
// Remote branch exists, check if it differs from local
93+
Ok(remote_commit) => {
94+
let remote_commit = remote_commit.join("").trim().to_string();
95+
96+
// Check if local and remote commits are different
97+
if local_commit != remote_commit {
98+
// Check if local is ahead or has diverged from remote
99+
let base_commit = self.cmd_out(&["merge-base", "HEAD", &remote_ref])?;
100+
let base_commit = base_commit.join("").trim().to_string();
101+
102+
// If different and valid ancestry, we should push
103+
true
104+
} else {
105+
// Commits are identical, no need to push
106+
println!(
107+
"No changes to push for branch {}: local and remote are at the same commit",
108+
style(upstream_branch).green()
109+
);
110+
false
111+
}
112+
}
113+
// Remote branch doesn't exist, we should push
114+
Err(_) => {
115+
println!(
116+
"Remote branch {} doesn't exist yet - will push",
117+
style(&remote_ref).yellow()
118+
);
119+
true
120+
}
121+
};
122+
123+
// Only push if needed
124+
if need_to_push {
125+
let output = self.cmd_out(&[
126+
"push",
127+
"-u",
128+
upstream,
129+
upstream_branch.to_string().as_str(),
130+
"--force-with-lease",
131+
])?;
132+
println!(
133+
"Force pushed to upstream branch {} with output: {:?}",
134+
style(upstream_branch).green(),
135+
style(output.join(",")).white().on_black()
136+
);
137+
}
138+
92139
Ok(())
93140
}
94141

0 commit comments

Comments
 (0)