Skip to content

Commit

Permalink
Create a function to return the git-link itself
Browse files Browse the repository at this point in the history
`git-link` is incredibly useful, but it doesn't return the URL it
creates. This introduces a new function which does so, allowing more
tooling to make use of this great feature.

Wrapped up in this I did some refactoring to drop the use of `setq`
internally to the function, as that defies the purely functional
nature of the call. This _shouldn't_ break anything.
  • Loading branch information
futuro committed Mar 16, 2022
1 parent 0996164 commit 68d8420
Showing 1 changed file with 55 additions and 39 deletions.
94 changes: 55 additions & 39 deletions git-link.el
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ return (FILENAME . REVISION) otherwise nil."

(defvar magit-buffer-file-name)

(defun git-link--relative-filename ()
(let* ((filename (buffer-file-name (buffer-base-buffer)))
(defun git-link--relative-filename (&optional filename)
(let* ((filename (or filename (buffer-file-name (buffer-base-buffer))))
(dir (git-link--repo-root)))

(when (null filename)
Expand Down Expand Up @@ -666,6 +666,55 @@ return (FILENAME . REVISION) otherwise nil."
(git-link--remote)))

;;;###autoload
(defun git-link-url (file-or-buffer remote &optional start end use-branch-p)
"Create a URL for the FILE-OR-BUFFER's location at REMOTE.
When given START and END, include the current line number or
active region.
When given USE-BRANCH-P, force the use of the branch name in the
resultant url."
(let* ((filename (git-link--relative-filename
(if (bufferp file-or-buffer)
(buffer-file-name file-or-buffer)
file-or-buffer)))
(remote (or remote git-link-default-remote))
(remote-url (git-link--remote-url remote))
(remote-info (git-link--parse-remote remote-url))
(branch (git-link--branch))
(commit (git-link--commit))
(handler (git-link--handler git-link-remote-alist (car remote-info))))
(cond ((null remote-url)
(user-error "Remote `%s' not found" remote))
((null filename)
(user-error "Can't figure out what to link to"))
((null (car remote-info))
(user-error "Remote `%s' contains an unsupported URL" remote))
((not (functionp handler))
(user-error "No handler found for %s" (car remote-info)))
;; TODO: null ret val
(t
(let* ((vc-revision (git-link--parse-vc-revision filename))
(hostname (car remote-info))
(dirname (cadr remote-info))
(branch (if (and (not use-branch-p)
(or (git-link--using-git-timemachine)
(git-link--using-magit-blob-mode)
vc-revision
git-link-use-commit))
nil
(url-hexify-string branch)))
(filename (if vc-revision (car vc-revision) filename))
(commit (if vc-revision (car vc-revision) commit)))
(funcall handler
hostname
dirname
filename
branch
commit
start
end))))))

(defun git-link (remote start end)
"Create a URL representing the current buffer's location in its
GitHub/Bitbucket/GitLab/... repository at the current line number
Expand All @@ -688,43 +737,10 @@ Defaults to \"origin\"."
(list remote nil nil)
(list remote (car region) (cadr region))))))

(let (filename branch commit handler remote-info (remote-url (git-link--remote-url remote)))
(if (null remote-url)
(message "Remote `%s' not found" remote)

(setq remote-info (git-link--parse-remote remote-url)
filename (git-link--relative-filename)
branch (git-link--branch)
commit (git-link--commit)
handler (git-link--handler git-link-remote-alist (car remote-info)))

(cond ((null filename)
(message "Can't figure out what to link to"))
((null (car remote-info))
(message "Remote `%s' contains an unsupported URL" remote))
((not (functionp handler))
(message "No handler found for %s" (car remote-info)))
;; TODO: null ret val
(t
(let ((vc-revison (git-link--parse-vc-revision filename)))
(when vc-revison
(setq filename (car vc-revison)
commit (cdr vc-revison)))

(git-link--new
(funcall handler
(car remote-info)
(cadr remote-info)
filename
(if (or (git-link--using-git-timemachine)
(git-link--using-magit-blob-mode)
vc-revison
git-link-use-commit)
nil
(url-hexify-string branch))
commit
start
end))))))))
(condition-case err
(git-link--new (git-link-url buffer-file-name remote start end))
(user-error
(message (cadr err)))))

;;;###autoload
(defun git-link-commit (remote)
Expand Down

0 comments on commit 68d8420

Please sign in to comment.