@@ -66,33 +66,46 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
6666 let ( repo_name, default_branch) = match ( explicit_repo, explicit_branch) {
6767 ( Some ( r) , Some ( b) ) => ( r. to_owned ( ) , b. to_owned ( ) ) ,
6868 _ => {
69- let git_repo = git2:: Repository :: open_from_env ( ) . map_err ( |e| {
70- anyhow:: anyhow!(
71- "Could not open git repository: {e}. \
72- Use --repo and --default-branch to specify manually."
73- )
74- } ) ?;
69+ let git_repo = git2:: Repository :: open_from_env ( ) ;
70+
71+ // Resolve the best remote name when we have a git repo.
7572 // Prefer explicit config (SENTRY_VCS_REMOTE / ini), then inspect
7673 // the repo for the best remote (upstream > origin > first).
77- let config = Config :: current ( ) ;
78- let configured_remote = config. get_cached_vcs_remote ( ) ;
79- let remote_name = if vcs:: git_repo_remote_url ( & git_repo, & configured_remote) . is_ok ( ) {
80- debug ! ( "Using configured VCS remote: {configured_remote}" ) ;
81- configured_remote
82- } else if let Some ( best) = vcs:: find_best_remote ( & git_repo) ? {
83- debug ! ( "Configured remote '{configured_remote}' not found, using: {best}" ) ;
84- best
85- } else {
86- bail ! (
87- "No remotes found in the git repository. \
88- Use --repo and --default-branch to specify manually."
89- ) ;
90- } ;
74+ let remote_name = git_repo. as_ref ( ) . ok ( ) . and_then ( |repo| {
75+ let config = Config :: current ( ) ;
76+ let configured_remote = config. get_cached_vcs_remote ( ) ;
77+ if vcs:: git_repo_remote_url ( repo, & configured_remote) . is_ok ( ) {
78+ debug ! ( "Using configured VCS remote: {configured_remote}" ) ;
79+ Some ( configured_remote)
80+ } else {
81+ match vcs:: find_best_remote ( repo) {
82+ Ok ( Some ( best) ) => {
83+ debug ! (
84+ "Configured remote '{configured_remote}' not found, using: {best}"
85+ ) ;
86+ Some ( best)
87+ }
88+ _ => None ,
89+ }
90+ }
91+ } ) ;
9192
9293 let repo_name = match explicit_repo {
9394 Some ( r) => r. to_owned ( ) ,
9495 None => {
95- let remote_url = vcs:: git_repo_remote_url ( & git_repo, & remote_name) ?;
96+ let git_repo = git_repo. as_ref ( ) . map_err ( |e| {
97+ anyhow:: anyhow!(
98+ "Could not open git repository: {e}. \
99+ Use --repo to specify manually."
100+ )
101+ } ) ?;
102+ let remote_name = remote_name. as_deref ( ) . ok_or_else ( || {
103+ anyhow:: anyhow!(
104+ "No remotes found in the git repository. \
105+ Use --repo to specify manually."
106+ )
107+ } ) ?;
108+ let remote_url = vcs:: git_repo_remote_url ( git_repo, remote_name) ?;
96109 debug ! ( "Found remote '{remote_name}': {remote_url}" ) ;
97110 let inferred = vcs:: get_repo_from_remote ( & remote_url) ;
98111 if inferred. is_empty ( ) {
@@ -106,9 +119,21 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
106119 let default_branch = match explicit_branch {
107120 Some ( b) => b. to_owned ( ) ,
108121 None => {
109- let inferred =
110- vcs:: git_repo_base_ref ( & git_repo, & remote_name) . unwrap_or_else ( |e| {
111- debug ! ( "Could not infer default branch, falling back to 'main': {e}" ) ;
122+ let inferred = git_repo
123+ . as_ref ( )
124+ . ok ( )
125+ . and_then ( |repo| {
126+ remote_name. as_deref ( ) . and_then ( |name| {
127+ vcs:: git_repo_base_ref ( repo, name)
128+ . map ( Some )
129+ . unwrap_or_else ( |e| {
130+ debug ! ( "Could not infer default branch from remote: {e}" ) ;
131+ None
132+ } )
133+ } )
134+ } )
135+ . unwrap_or_else ( || {
136+ debug ! ( "No git repo or remote available, falling back to 'main'" ) ;
112137 "main" . to_owned ( )
113138 } ) ;
114139 println ! ( "Inferred default branch: {inferred}" ) ;
0 commit comments