aboutsummaryrefslogtreecommitdiffstats
path: root/.emacs.d/modules/et-vc.el
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2026-04-01 11:49:47 -0400
committerEugene Triguba <hello@eugenetriguba.com>2026-04-01 11:50:20 -0400
commit7aa80639f92f1c8359b5311014e2fc22831eba86 (patch)
tree188efe1520d7ef6daf208d8de50388c1a3a98238 /.emacs.d/modules/et-vc.el
parentUpdate nvim config (diff)
downloaddotfiles-7aa80639f92f1c8359b5311014e2fc22831eba86.zip
Update emacs config
Diffstat (limited to '.emacs.d/modules/et-vc.el')
-rw-r--r--.emacs.d/modules/et-vc.el83
1 files changed, 83 insertions, 0 deletions
diff --git a/.emacs.d/modules/et-vc.el b/.emacs.d/modules/et-vc.el
new file mode 100644
index 0000000..107ebf9
--- /dev/null
+++ b/.emacs.d/modules/et-vc.el
@@ -0,0 +1,83 @@
+;;; -*- Lexical-binding:t -*-
+(require 'vc-git)
+
+(global-set-key (kbd "C-c gu")
+ 'et-generate-remote-git-url)
+
+(defun et-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."
+ (interactive)
+
+ (if (not buffer-file-name)
+ (message "Not visiting a file.")
+ (let* ((git-remote-url-cmd "git config --get remote.origin.url")
+ (remote-url (string-trim
+ (shell-command-to-string git-remote-url-cmd)))
+ (git-head-sha-cmd "git rev-parse HEAD")
+ (head-sha (string-trim (shell-command-to-string git-head-sha-cmd)))
+ (relative-file-path (file-relative-name buffer-file-name
+ (vc-git-root buffer-file-name)))
+ (line-num (line-number-at-pos)))
+
+ (if (et--is-supported-provider-p remote-url)
+ (progn
+ (kill-new (et--generate-remote-git-url remote-url head-sha relative-file-path line-num))
+ (message "Copied remote git URL"))
+ (message (format "%s is not a github.com url." 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.
+
+ Supported providers:
+ - github
+ - sourcehut
+
+ Otherwise, if the url corresponds to an unsupported provider,
+ nil is returned."
+ (cond
+ ((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)))
+ (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)))))
+
+
+(defun et--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)
+ head-sha
+ relative-file-path
+ line-num))
+
+(defun et--extract-repo-path (base-url remote-url)
+ (if (et--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)
+ (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)