Skip to content

Commit 3523908

Browse files
committed
Address review comments
1 parent 1a0e73b commit 3523908

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

src/commands/build/upload.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
114114

115115
let cached_remote = config.get_cached_vcs_remote();
116116
// Try to open the git repository and find the remote, but handle errors gracefully.
117-
let (vcs_provider, head_repo_name, head_ref) = {
117+
let (vcs_provider, head_repo_name, head_ref, base_ref) = {
118118
// Try to open the repo and get the remote URL, but don't fail if not in a repo.
119119
let repo = git2::Repository::open_from_env().ok();
120120
let repo_ref = repo.as_ref();
@@ -168,33 +168,36 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
168168
.map(Cow::Owned)
169169
});
170170

171-
(vcs_provider, head_repo_name, head_ref)
171+
let base_ref = matches
172+
.get_one("base_ref")
173+
.map(String::as_str)
174+
.map(Cow::Borrowed)
175+
.or_else(|| {
176+
// Try to get the base ref from the VCS if not provided
177+
// This attempts to find the merge-base with the remote tracking branch
178+
repo_ref
179+
.and_then(|r| match git_repo_base_ref(r, &cached_remote) {
180+
Ok(Some(base_ref_name)) => {
181+
debug!("Found base branch reference: {}", base_ref_name);
182+
Some(base_ref_name)
183+
}
184+
Ok(None) => {
185+
debug!("No base branch reference found (no local branch points to merge-base)");
186+
None
187+
}
188+
Err(e) => {
189+
debug!("Error getting base branch reference: {}", e);
190+
None
191+
}
192+
})
193+
.map(Cow::Owned)
194+
});
195+
196+
(vcs_provider, head_repo_name, head_ref, base_ref)
172197
};
173198

174199
let base_repo_name = matches.get_one("base_repo_name").map(String::as_str);
175200
let base_sha = matches.get_one("base_sha").map(String::as_str);
176-
let base_ref = matches
177-
.get_one("base_ref")
178-
.map(String::as_str)
179-
.map(Cow::Borrowed)
180-
.or_else(|| {
181-
// Try to get the base ref from the VCS if not provided
182-
// This attempts to find the merge-base with the remote tracking branch
183-
if let Ok(repo) = git2::Repository::open_from_env() {
184-
match git_repo_base_ref(&repo, &cached_remote) {
185-
Ok(base_ref_name) => {
186-
debug!("Found base branch reference: {}", base_ref_name);
187-
Some(Cow::Owned(base_ref_name))
188-
}
189-
Err(e) => {
190-
debug!("No base branch reference found: {}", e);
191-
None
192-
}
193-
}
194-
} else {
195-
None
196-
}
197-
});
198201
let pr_number = matches.get_one::<u32>("pr_number");
199202

200203
let build_configuration = matches.get_one("build_configuration").map(String::as_str);

src/utils/vcs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ pub fn git_repo_head_ref(repo: &git2::Repository) -> Result<String> {
249249
}
250250
}
251251

252-
pub fn git_repo_base_ref(repo: &git2::Repository, remote_name: &str) -> Result<String> {
252+
pub fn git_repo_base_ref(repo: &git2::Repository, remote_name: &str) -> Result<Option<String>> {
253253
// Get the current HEAD commit
254254
let head_commit = repo.head()?.peel_to_commit()?;
255255

@@ -277,7 +277,7 @@ fn find_merge_base_ref(
277277
repo: &git2::Repository,
278278
head_commit: &git2::Commit,
279279
remote_ref: &git2::Reference,
280-
) -> Result<String> {
280+
) -> Result<Option<String>> {
281281
let remote_commit = remote_ref.peel_to_commit()?;
282282
let merge_base_oid = repo.merge_base(head_commit.id(), remote_commit.id())?;
283283

@@ -287,14 +287,14 @@ fn find_merge_base_ref(
287287
if let Ok(branch_commit) = branch.get().peel_to_commit() {
288288
if branch_commit.id() == merge_base_oid {
289289
if let Some(branch_name) = branch.name()? {
290-
return Ok(branch_name.to_owned());
290+
return Ok(Some(branch_name.to_owned()));
291291
}
292292
}
293293
}
294294
}
295295

296-
// If no branch name found, return the commit SHA
297-
Ok(merge_base_oid.to_string())
296+
// If no branch name found, return None (only return branch names)
297+
Ok(None)
298298
}
299299

300300
fn find_reference_url(repo: &str, repos: &[Repo]) -> Result<Option<String>> {

0 commit comments

Comments
 (0)