Skip to content

Commit 53f139c

Browse files
committed
Add default vcs base_ref parsing for mobile-app subcommand
1 parent 9f3bba8 commit 53f139c

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

src/commands/mobile_app/upload.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::utils::mobile_app::{
2424
use crate::utils::mobile_app::{is_aab_file, is_apk_file, is_zip_file, normalize_directory};
2525
use crate::utils::progress::ProgressBar;
2626
use crate::utils::vcs::{
27-
self, get_provider_from_remote, get_repo_from_remote, git_repo_remote_url,
27+
self, get_provider_from_remote, get_repo_from_remote, git_repo_head_ref, git_repo_remote_url,
2828
};
2929

3030
pub fn make_command(command: Command) -> Command {
@@ -107,10 +107,11 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
107107

108108
let cached_remote = config.get_cached_vcs_remote();
109109
// Try to open the git repository and find the remote, but handle errors gracefully.
110-
let (vcs_provider, head_repo_name) = {
110+
let (vcs_provider, head_repo_name, head_ref) = {
111111
// Try to open the repo and get the remote URL, but don't fail if not in a repo.
112112
let repo = git2::Repository::open_from_env().ok();
113-
let remote_url = repo.and_then(|repo| git_repo_remote_url(&repo, &cached_remote).ok());
113+
let repo_ref = repo.as_ref();
114+
let remote_url = repo_ref.and_then(|repo| git_repo_remote_url(repo, &cached_remote).ok());
114115

115116
let vcs_provider: Option<Cow<'_, str>> = matches
116117
.get_one("vcs_provider")
@@ -134,13 +135,22 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
134135
.map(Cow::Owned)
135136
});
136137

137-
(vcs_provider, head_repo_name)
138+
let head_ref: Option<Cow<'_, str>> = matches
139+
.get_one("head_ref")
140+
.map(String::as_str)
141+
.map(Cow::Borrowed)
142+
.or_else(|| {
143+
// Try to get the current ref from the VCS if not provided
144+
repo_ref
145+
.and_then(|r| git_repo_head_ref(r).ok())
146+
.map(Cow::Owned)
147+
});
148+
149+
(vcs_provider, head_repo_name, head_ref)
138150
};
139151

140152
let base_repo_name = matches.get_one("base_repo_name").map(String::as_str);
141-
142153
let base_sha = matches.get_one("base_sha").map(String::as_str);
143-
let head_ref = matches.get_one("head_ref").map(String::as_str);
144154
let base_ref = matches.get_one("base_ref").map(String::as_str);
145155
let pr_number = matches.get_one::<u32>("pr_number");
146156

@@ -207,7 +217,7 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
207217
vcs_provider: vcs_provider.as_deref(),
208218
head_repo_name: head_repo_name.as_deref(),
209219
base_repo_name,
210-
head_ref,
220+
head_ref: head_ref.as_deref(),
211221
base_ref,
212222
pr_number,
213223
};

src/utils/vcs.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ pub fn git_repo_remote_url(
234234
.ok_or_else(|| git2::Error::from_str("No remote URL found"))
235235
}
236236

237+
#[cfg(feature = "unstable-mobile-app")]
238+
pub fn git_repo_head_ref(repo: &git2::Repository) -> Result<String, git2::Error> {
239+
let head = repo.head()?;
240+
head.shorthand()
241+
.map(|s| s.to_owned())
242+
.ok_or_else(|| git2::Error::from_str("No HEAD reference found"))
243+
}
244+
237245
fn find_reference_url(repo: &str, repos: &[Repo]) -> Result<Option<String>> {
238246
let mut non_git = false;
239247
for configured_repo in repos {

0 commit comments

Comments
 (0)