Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions git-link.el
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@

;;; Change Log:

;; 2024-11-03 - v0.9.3
;; * Add support for linking to a different branch with git-link-diffrent-branch (@dotemacs)
;;
;; 2024-06-29 - v0.9.2
;; * Add git-link-add-to-kill-ring to not add to kill ring (thanks Michael Hauser-Raspe)
;; * Add prefix arg to open in browser when calling git-link-homepage (thanks Sibi Prabakaran)
Expand Down Expand Up @@ -852,6 +855,24 @@ shown via annotate in bitbucket."
"annotate"
"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.

(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))
(magit-list-local-branches)))
default-remote-branch-name)))
(setq current-prefix-arg nil)
(call-interactively 'git-link)
(setq git-link-default-branch git-link-current-branch-setting)))

;;;###autoload
(defun git-link (remote start end)
"Create a URL representing the current buffer's location in its
Expand Down