Skip to content

Conversation

dmitrym0
Copy link

@dmitrym0 dmitrym0 commented Nov 4, 2024

#30 is a feature request to allow branch name to be specified instead of the current branch. This code was written by @dotemacs; I'm just creating the PR.

To test:

  1. C-u M-x git-link-different-branch

You should get a completing read to specify which branch you'd like to link to.

git-link.el Outdated
"Invoke `git-link', but with the `branch' name set to a different
branch than the one you're currently working on."
(interactive "P")
(let* ((default-remote-branch-name "main")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing to consider here is this main usage.

Some people might have master or something else.

@dmitrym0 dmitrym0 force-pushed the issue-30-allow-different-branch-to-be-selected branch from 3588bca to eaf7583 Compare September 23, 2025 16:21
"src")))

;;;###autoload
(defun git-link-different-branch (branch)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some outstanding questions form #30 related to this:

The assumption made in the current design is that using a single interactive function with prefix arguments is "typical emacs interactive usage" in that one will call the git-link function via a key binding: e.g., (global-set-key (kbd "C-c g l") 'git-link) and that key bindings are finite.

If it was a different function, what that have a keybinding too?

... "different branch" does feel more like an option to git-link. If we have a git-link-different-branch function shouldn't we also have git-link-different-remote and git-link-no-line-number functions? Public function namespace could continue to grow as more options are added.

I think prefix is better option, does is it not?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sshaw I'm open to implementing this in a way that fits your design philosophy for this tool. I read #30 but I'm not clear what your preferred approach would be. How would you integrate this functionality into git-link?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whilst a bit cumbersome I think doing it via prefix args and the existing git-link, git-link-commit functions are the way to go. At least with that route we don't need git-link-different-branch-commit, git-link-different-branch-homepage, etc... and I assume people are used to and like this approach? It is standard emacs usage. I suppose an argument —no pun intended— could be made that initial prefix arg should prompt for branch instead of remote? Not sure what is most common. Maybe we control that via config? Also see #138 and these comments for prefix-related discussion.

"Invoke `git-link', but with the `branch' name set to a different
branch than the one you're currently working on."
(interactive "P")
(let* ((default-remote-branch-name (magit-main-branch))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not assume the caller is using magit nor require it as part of this package.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So instead of (magit-main-branch), (car (vc-git-branches))?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this magit-less approach?

Using Emacs bundled vc-git instead.

(defun git-link-different-branch (branch)
  "Invoke `git-link', but with the `branch' name set to a different
branch than the one you're currently working on."
  (interactive "P")
  (let* ((default-remote-branch-name (car (vc-git-branches)))
         (git-link-current-branch-setting git-link-default-branch)
         (git-link-default-branch (if branch
                                      (completing-read
                                       (format "Instead of '%s' branch replace with branch: " (git-link--branch))
                                       (mapcar (lambda (branch)
                                                 (replace-regexp-in-string "^refs/heads/" "" branch))
                                               (vc-git-branches)))
                                    default-remote-branch-name)))
    (setq current-prefix-arg nil)
    (call-interactively 'git-link)
    (setq git-link-default-branch git-link-current-branch-setting)))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sshaw My mistake, I was under the impression that magit was already being used, I see it's not so. I'll refactor it.

@dotemacs (car (vc-git-branches)) looks to get us the current branch, but we want the "primary" branch instead. I found this magic incantation:

➜ git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'
master

Copy link
Owner

@sshaw sshaw Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd think git symbolic-ref would be the way to go (exec'd via git-link--exec), at least initially. There's could be value to retrieving it via other git-related packages because they have their own config, separate from git. E.g., Running git to get the branch says you have no upstream configured vs in magit, where it is configured, but, this can be a latter enhancement, if anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants