|
| 1 | +use anyhow::bail; |
1 | 2 | use anyhow::Result; |
2 | | -use anyhow::{bail, Ok}; |
3 | 3 | use console::style; |
4 | 4 | use regex::Regex; |
5 | | -use std::fmt::Debug; |
6 | | -use std::{ops::Rem, str::FromStr}; |
| 5 | +use std::str::FromStr; |
7 | 6 |
|
8 | 7 | use rustygit::{types::BranchName, Repository}; |
9 | 8 |
|
@@ -75,20 +74,68 @@ impl RepoExtenstions for Repository { |
75 | 74 | } |
76 | 75 | } |
77 | 76 |
|
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 |
79 | 79 | 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 | + |
92 | 139 | Ok(()) |
93 | 140 | } |
94 | 141 |
|
|
0 commit comments