diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2026-04-02 19:02:19 -0400 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2026-04-02 19:02:19 -0400 |
| commit | fdcf582c498c89f3363bbb9ed1f89f9e3dde287c (patch) | |
| tree | c96db5dfa84ed487b23a921b75e22aa1dd5b53fc /.emacs.d/modules/et-vc.el | |
| parent | Add magit and fix typo in et-keys to C-g instead of M-g (diff) | |
| download | dotfiles-fdcf582c498c89f3363bbb9ed1f89f9e3dde287c.zip | |
Adjust emacs config
Diffstat (limited to '.emacs.d/modules/et-vc.el')
| -rw-r--r-- | .emacs.d/modules/et-vc.el | 101 |
1 files changed, 61 insertions, 40 deletions
diff --git a/.emacs.d/modules/et-vc.el b/.emacs.d/modules/et-vc.el index ea8e797..559e14e 100644 --- a/.emacs.d/modules/et-vc.el +++ b/.emacs.d/modules/et-vc.el @@ -1,17 +1,26 @@ -;;; -*- Lexical-binding:t -*- +;;; et-vc --- Version control configuration -*- lexical-binding:t -*- +;; +;;; Commentary: +;; +;;; Code: (require 'vc-git) (use-package magit) (global-set-key (kbd "C-c gu") - 'et-generate-remote-git-url) + 'et-vc-generate-remote-git-url) -(defun et-generate-remote-git-url () +(defun et-vc-generate-remote-git-url () "Generate a remote URL for the current buffer file and line number. - * The remote URL can be HTTPS or SSH. - * The git hosting provider is determined via the git remote origin url. - * Only Github and SourceHut are supported as a hosting provider." +* The remote URL can be HTTPS or SSH. +* The git hosting provider is determined via the git remote `origin` url. +* Only Github and SourceHut are supported as a hosting provider. + +For example, if the point is on line 12 of example.el on head SHA +abc123, and the git remote origin url is set to a github URL, then a +URL such https://github.com/myuser/myrepo/blob/abc123/example.el#L12 +would be added to the kill ring." (interactive) (if (not buffer-file-name) @@ -22,61 +31,73 @@ (vc-git-root buffer-file-name))) (line-num (line-number-at-pos))) - (if (et--is-supported-provider-p remote-url) + (if (et-vc--is-supported-provider-p remote-url) (progn - (kill-new (et--generate-remote-git-url remote-url head-sha relative-file-path line-num)) + (kill-new (et-vc--generate-remote-git-url remote-url head-sha relative-file-path line-num)) (message "Copied remote git URL")) (message (format "%s is not a supported git hosting provider." remote-url)))))) -(defun et--is-supported-provider-p (url) - "Check whether url is a supported git hosting provider for URL generation." - (not (null (et--get-git-hosting-provider url)))) - -(defun et--get-git-hosting-provider (url) - "Get the git hosting provider for the given url. +(defun et-vc--is-supported-provider-p (url) + "Check whether URL is a supported git hosting provider for URL generation." + (not (null (et-vc--get-git-hosting-provider url)))) - Supported providers: - - github - - sourcehut +(defun et-vc--get-git-hosting-provider (url) + "Get the git hosting provider for the given URL. - Otherwise, if the url corresponds to an unsupported provider, - nil is returned." +The supported providers are github and sourcehut. If the URL +corresponds to an unsupported provider, null is returned." (cond - ((string-match-p "github\\.com" url) "github") - ((string-match-p "git\\.sr\\.ht" url) "sourcehut"))) + ((string-match-p "github\\.com" url) 'github) + ((string-match-p "git\\.sr\\.ht" url) 'sourcehut))) -(defun et--generate-remote-git-url (remote-url head-sha relative-file-path line-num) - (let ((provider (et--get-git-hosting-provider remote-url))) +(defun et-vc--generate-remote-git-url (remote-url head-sha relative-file-path line-num) + (let ((provider (et-vc--get-git-hosting-provider remote-url))) (cond - ((string-match-p "github" provider) - (et--gh-generate-remote-git-url remote-url head-sha relative-file-path line-num)) - ((string-match-p "sourcehut" provider) - (et--sh-generate-remote-git-url remote-url head-sha relative-file-path line-num))))) + ((eq 'github provider) + (et-vc--gh-generate-remote-git-url remote-url head-sha relative-file-path line-num)) + ((eq 'sourcehut provider) + (et-vc--sh-generate-remote-git-url remote-url head-sha relative-file-path line-num))))) - -(defun et--gh-generate-remote-git-url (remote-url head-sha relative-file-path line-num) +(defun et-vc--gh-generate-remote-git-url (remote-url head-sha relative-file-path line-num) (format "https://github.com/%s/blob/%s/%s#L%d" - (et--extract-repo-path "https://github.com" remote-url) + (et-vc--extract-repo-path "https://github.com" remote-url) + head-sha + relative-file-path + line-num)) + +(defun et-vc--sh-generate-remote-git-url (remote-url head-sha relative-file-path line-num) + (format "https://git.sr.ht/%s/tree/%s/item/%s#L%d" + (et-vc--extract-repo-path "https://git.sr.ht" remote-url) head-sha relative-file-path line-num)) -(defun et--extract-repo-path (base-url remote-url) - (if (et--git-ssh-url-p remote-url) +(defun et-vc--extract-repo-path (base-url remote-url) + "Extract the repository path from the REMOTE-URL. + +BASE-URL is used to determine where the repository part of the +REMOTE-URL starts if the REMOTE-URL is a http URL. + +Example: + (et-vc--extract-repo-path + \"https://github.com\" + \"https://github.com/user/repo\") + => \"user/repo\" + + (et-vc--extract-repo-path + \"https://git.sr.ht\" + \"git@git.sr.ht:~eugenetriguba/repo\") + => \"~eugenetriguba/repo\"" + (if (et-vc--git-ssh-url-p remote-url) (substring remote-url (+ 1 (string-match ":" remote-url))) (replace-regexp-in-string (format "%s/\\(.*?\\)\\(.git\\)?$" base-url) "\\1" remote-url))) -(defun et--git-ssh-url-p (url) +(defun et-vc--git-ssh-url-p (url) + "Check whether URL is a SSH-based Git origin URL." (string-prefix-p "git@" url)) -(defun et--sh-generate-remote-git-url (remote-url head-sha relative-file-path line-num) - (format "https://git.sr.ht/%s/tree/%s/item/%s#L%d" - (et--extract-repo-path "https://git.sr.ht" remote-url) - head-sha - relative-file-path - line-num)) - (provide 'et-vc) +;;; et-vc.el ends here |