Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: git-link-blame #70

Closed
wants to merge 2 commits into from
Closed
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
30 changes: 30 additions & 0 deletions git-link.el
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,21 @@ As an example, \"gitlab\" will match with both \"gitlab.com\" and
:type '(alist :key-type string :value-type (group function))
:group 'git-link)

(defcustom git-link-blame-matchers
'(("git.sr.ht" nil)
("github" ("/blob/" "/blame/"))
("bitbucket" ("/src/" "/annotate/"))
("gitorious" nil)
("gitlab" ("/blob/" "/blame/"))
("visualstudio\\|azure" nil))
"Alist for `git-link-blame' fn to determine how to convert a
git-link URL to a git-blame URL. Each value can either be a tuple
of a regexp and its replacement, or a function that takes a
git-link URL string as a parameter and returns the converted
git-blame URL string."
:type '(alist :key-type string :value-type (group function))
:group 'git-link)

(defun git-link--exec(&rest args)
(ignore-errors
(with-temp-buffer
Expand Down Expand Up @@ -595,5 +610,20 @@ is non-nil also call `browse-url'."
(git-link--new (format "https://%s/%s" (car remote-info) (cadr remote-info)))
(error "Remote `%s' is unknown or contains an unsupported URL" remote))))

;;;###autoload
(defun git-link-blame ()
"Similar to `git-link`, with the difference of creating a git-blame link url"
(interactive)
(cl-flet ((git-link--new*
(git-link-url)
(let ((match (git-link--handler git-link-blame-matchers git-link-url)))
(cond ((functionp match) (funcall match git-link-url))
((car match) (replace-regexp-in-string (car match) (cadr match) git-link-url))
(t (message (format "No match exists in `git-link-blame-matchers` var for %s" git-link-url)))))))
(advice-add 'git-link--new :override #'git-link--new*) ; local override
(let ((link (call-interactively 'git-link)))
(advice-remove 'git-link--new #'git-link--new*)
(git-link--new link))))

(provide 'git-link)
;;; git-link.el ends here