summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2021-08-23 19:39:43 -0700
committerFranck Cuny <franck@fcuny.net>2021-08-23 19:39:43 -0700
commit76921742edfedbe0e01d468f298e901cf808bb0d (patch)
tree6f789358a57069b1f3ecc50d213db1dc2179c27e
parentemacs: use the package `forge' (diff)
downloademacs.d-76921742edfedbe0e01d468f298e901cf808bb0d.tar.gz
emacs: git links to sourcegraph for work repo
This article [1] shows how to use sourcegraph with the package
`git-link'. However, it requires to modify the configuration of the
repository to add a new remote named 'git-link'. This is inconvenient,
and this can be automated.

This change adds a function to automatically generate the remote URL
when the remote origin of a repo is set to twitter.biz.

We also add the configuration for our own gitea instance.

[1] https://sideshowcoder.com/2020/07/02/opening-sourcegraph-from-emacs/
-rw-r--r--emacs/custom/fcuny-git.el44
1 files changed, 26 insertions, 18 deletions
diff --git a/emacs/custom/fcuny-git.el b/emacs/custom/fcuny-git.el
index 340da9c..b85a9ea 100644
--- a/emacs/custom/fcuny-git.el
+++ b/emacs/custom/fcuny-git.el
@@ -84,34 +84,42 @@
   :config
   (add-to-list 'forge-alist '("git.fcuny.net:222" "git.fcuny.net/api/v1" "git.fcuny.net" forge-gitea-repository)))
 
-;; from https://sideshowcoder.com/2020/07/02/opening-sourcegraph-from-emacs/
-;; in a repo, add the following in .git/config:
-;;
-;; [git-link]
-;;        remote = mysourcegraph.sourcegraph
-;; [remote "mysourcegraph.sourcegraph"]
-;;        url = https://sourcegraph.twitter.biz/gitpuppet.twitter.biz/puppet-twitter
-;;
 (use-package git-link
   :ensure t
+  :bind (("C-c g l" . git-link)
+         ("C-c g a" . git-link-commit))
   :config
-  (defun git-link-sourcegraph (hostname dirname filename _branch commit start end)
-    (let ((line-or-range (if end (format "%s-%s" start end) start)))
+  (defvar fcuny/map-git-remote-to-sg
+    '(("git.twitter.biz" "sourcegraph.twitter.biz/twitter.biz")
+      ("gitpuppet.twitter.biz" "sourcegraph.twitter.biz/gitpuppet.twitter.biz")))
+
+  (defun fcuny/get-sg-remote-from-hostname (hostname)
+    (nth 1 (assoc hostname fcuny/map-git-remote-to-sg)))
+
+  (defun fcuny/git-link-work-sourcegraph (hostname dirname filename _branch commit start end)
+    (let ((line-or-range (if end (format "%s-%s" start end) start))
+          (sg-base-url (fcuny/get-sg-remote-from-hostname hostname)))
       (format "https://%s/%s@%s/-/blob/%s#L%s"
-              hostname
+              sg-base-url
               dirname
               commit
               filename
               line-or-range)))
 
-  (defun git-link-commit-sourcegraph (hostname dirname commit)
-    (format "https://%s/%s/-/commit/%s"
-            hostname
-            dirname
-            commit))
+  (defun fcuny/git-link-commit-work-sourcegraph (hostname dirname commit)
+    (let (sg-base-url (fcuny/get-sg-remote-from-hostname hostname))
+      (format "https://%s/%s/-/commit/%s"
+              sg-base-url
+              dirname
+              commit)))
+
+  ;; for work related repositories, open them in our instance of sourcegraph
+  (add-to-list 'git-link-remote-alist '("twitter" fcuny/git-link-work-sourcegraph))
+  (add-to-list 'git-link-commit-remote-alist '("twitter" fcuny/git-link-commit-work-sourcegraph))
 
-  (add-to-list 'git-link-remote-alist '("sourcegraph" git-link-sourcegraph))
-  (add-to-list 'git-link-commit-remote-alist '("sourcegraph" git-link-commit-sourcegraph))
+  ;; for personal code I use gitea, which is similar to codeberg
+  (add-to-list 'git-link-remote-alist '("git\\.fcuny\\.net" git-link-codeberg))
+  (add-to-list 'git-link-commit-remote-alist '("git\\.fcuny\\.net" git-link-commit-codeberg))
 
   (setq git-link-open-in-browser 't))