Skip to content

Commit 12d1a2b

Browse files
romtsnclaude
andcommitted
fix(code-mappings): Allow branch fallback without git remotes
When --repo is provided but --default-branch is not, the code no longer requires a git remote to be present. Branch inference gracefully falls back to 'main' when no git repo or remote is available. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b6e9157 commit 12d1a2b

File tree

1 file changed

+49
-24
lines changed

1 file changed

+49
-24
lines changed

src/commands/code_mappings/upload.rs

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,33 +67,46 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
6767
let (repo_name, default_branch) = match (explicit_repo, explicit_branch) {
6868
(Some(r), Some(b)) => (r.to_owned(), b.to_owned()),
6969
_ => {
70-
let git_repo = git2::Repository::open_from_env().map_err(|e| {
71-
anyhow::anyhow!(
72-
"Could not open git repository: {e}. \
73-
Use --repo and --default-branch to specify manually."
74-
)
75-
})?;
70+
let git_repo = git2::Repository::open_from_env();
71+
72+
// Resolve the best remote name when we have a git repo.
7673
// Prefer explicit config (SENTRY_VCS_REMOTE / ini), then inspect
7774
// the repo for the best remote (upstream > origin > first).
78-
let config = Config::current();
79-
let configured_remote = config.get_cached_vcs_remote();
80-
let remote_name = if vcs::git_repo_remote_url(&git_repo, &configured_remote).is_ok() {
81-
debug!("Using configured VCS remote: {configured_remote}");
82-
configured_remote
83-
} else if let Some(best) = vcs::find_best_remote(&git_repo)? {
84-
debug!("Configured remote '{configured_remote}' not found, using: {best}");
85-
best
86-
} else {
87-
bail!(
88-
"No remotes found in the git repository. \
89-
Use --repo and --default-branch to specify manually."
90-
);
91-
};
75+
let remote_name = git_repo.as_ref().ok().and_then(|repo| {
76+
let config = Config::current();
77+
let configured_remote = config.get_cached_vcs_remote();
78+
if vcs::git_repo_remote_url(repo, &configured_remote).is_ok() {
79+
debug!("Using configured VCS remote: {configured_remote}");
80+
Some(configured_remote)
81+
} else {
82+
match vcs::find_best_remote(repo) {
83+
Ok(Some(best)) => {
84+
debug!(
85+
"Configured remote '{configured_remote}' not found, using: {best}"
86+
);
87+
Some(best)
88+
}
89+
_ => None,
90+
}
91+
}
92+
});
9293

9394
let repo_name = match explicit_repo {
9495
Some(r) => r.to_owned(),
9596
None => {
96-
let remote_url = vcs::git_repo_remote_url(&git_repo, &remote_name)?;
97+
let git_repo = git_repo.as_ref().map_err(|e| {
98+
anyhow::anyhow!(
99+
"Could not open git repository: {e}. \
100+
Use --repo to specify manually."
101+
)
102+
})?;
103+
let remote_name = remote_name.as_deref().ok_or_else(|| {
104+
anyhow::anyhow!(
105+
"No remotes found in the git repository. \
106+
Use --repo to specify manually."
107+
)
108+
})?;
109+
let remote_url = vcs::git_repo_remote_url(git_repo, remote_name)?;
97110
debug!("Found remote '{remote_name}': {remote_url}");
98111
let inferred = vcs::get_repo_from_remote(&remote_url);
99112
if inferred.is_empty() {
@@ -107,9 +120,21 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
107120
let default_branch = match explicit_branch {
108121
Some(b) => b.to_owned(),
109122
None => {
110-
let inferred =
111-
vcs::git_repo_base_ref(&git_repo, &remote_name).unwrap_or_else(|e| {
112-
debug!("Could not infer default branch, falling back to 'main': {e}");
123+
let inferred = git_repo
124+
.as_ref()
125+
.ok()
126+
.and_then(|repo| {
127+
remote_name.as_deref().and_then(|name| {
128+
vcs::git_repo_base_ref(repo, name)
129+
.map(Some)
130+
.unwrap_or_else(|e| {
131+
debug!("Could not infer default branch from remote: {e}");
132+
None
133+
})
134+
})
135+
})
136+
.unwrap_or_else(|| {
137+
debug!("No git repo or remote available, falling back to 'main'");
113138
"main".to_owned()
114139
});
115140
println!("Inferred default branch: {inferred}");

0 commit comments

Comments
 (0)