git-link icon indicating copy to clipboard operation
git-link copied to clipboard

`git-link--remote`: fallback to existing remote rather than `origin`

Open mooseyboots opened this issue 10 months ago • 2 comments

if git-link--remote fails to find a remote, it tries to fallback to origin.

i have repos with no origin but that do have remotes. in such cases doesn't it make sense to fallback to an actually existing remote, even just the first result of git-link--remotes, rather than a hardcoded option?

e.g.

(defun git-link--remote ()
  (let* ((branch (git-link--current-branch))
	 (remote (or (git-link--get-config "git-link.remote")
		     git-link-default-remote
		     (git-link--branch-remote branch)
                     (car (git-link--remotes))))) ;; Fallback to first remote if all else fails

    ;; Git defaults to "." if the branch has no remote.
    ;; If the branch has no remote we try master's, which may be set.
    (if (or (null remote)
	    (and (string= remote ".")
		 (not (string= branch "master"))))
	(setq remote (git-link--branch-remote "master")))

    (if (or (null remote) (string= remote "."))
	"origin"
      remote)))

i just started using git-link, so not sure if this would make sense in the library/for other users.

mooseyboots avatar Mar 08 '25 12:03 mooseyboots

I think origin was chosen because it's a git default. Maybe this has changed?

Currently a default remote can be set via git-link-default-remote or the git-link.remote git config value. Does this work for you?

The only downside I can think to the first result of git-link--remotes is: is that any better than origin? I guess it could be "better" because it exists per git config but who knows if that's what the user wanted to link to. In both cases it's a guess. Seems to boil down to: is it better to guess using git's default or better to guess using a (random?) origin?

sshaw avatar Mar 08 '25 16:03 sshaw

my thought was that if guessing, it's better to return a remote that exists, rather than a default that may not exist.

i do have a remote, but the checks fail, and so git-link fails with error "Remote origin not found". my thought is why not try a little harder before simply failing.

you could also only fall back to first existing remote after say confirming that the fallback, "origin" doesn't exist. another fall back could be to use magit-read-remote, if origin doesn't exist.

mooseyboots avatar Mar 08 '25 16:03 mooseyboots