defaultRemote returns the remote that relative submodule URLs are resolved against, mirroring canonical Git's repo_default_remote (remote.c) and resolve_relative_url (builtin/submodule--helper.c): 1. if HEAD is on a branch with branch. .remote configured, use that remote; 2. else if exactly on
(r *Repository)
| 194 | // finds the branch but with an empty Remote does not short-circuit |
| 195 | // rule (2). Returns an error when the chosen remote is not configured. |
| 196 | func defaultRemote(r *Repository) (*config.RemoteConfig, error) { |
| 197 | cfg, err := r.Config() |
| 198 | if err != nil { |
| 199 | return nil, err |
| 200 | } |
| 201 | |
| 202 | if ref, err := r.Reference(plumbing.HEAD, false); err == nil && |
| 203 | ref.Type() == plumbing.SymbolicReference && |
| 204 | ref.Target().IsBranch() { |
| 205 | if b, ok := cfg.Branches[ref.Target().Short()]; ok && b.Remote != "" { |
| 206 | return lookupRemote(cfg, b.Remote) |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if len(cfg.Remotes) == 1 { |
| 211 | for name := range cfg.Remotes { |
| 212 | return lookupRemote(cfg, name) |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return lookupRemote(cfg, DefaultRemoteName) |
| 217 | } |
| 218 | |
| 219 | func lookupRemote(cfg *config.Config, name string) (*config.RemoteConfig, error) { |
| 220 | rc, ok := cfg.Remotes[name] |
searching dependent graphs…