summary refs log tree commit diff
path: root/emacs
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2022-05-31 08:51:31 -0700
committerFranck Cuny <franck@fcuny.net>2022-05-31 08:51:31 -0700
commit5698c869a9efefce161946c891a85d0ef8bbc71c (patch)
tree99285d6e4843b5a3b34a3d14364aa90f21c6ddf3 /emacs
parentref(text): replace dabbrev with hippie-expand (diff)
downloademacs.d-5698c869a9efefce161946c891a85d0ef8bbc71c.tar.gz
fix(git-extra): use cs.fcuny.xyz for my own repositories
I run an instance of sourcegraph on cs.fcuny.xyz for some of my
repositories. When using `git-link' on my own code, I want to go there.

I refactor some of the code to format the base URL using the hostname,
and clean up the directory name in case the repository is coming from a
gerrit server (there's a `a/' prefix to the name of the repository that
I need to drop).
Diffstat (limited to '')
-rw-r--r--emacs/elisp/my-git-extra.el42
1 files changed, 25 insertions, 17 deletions
diff --git a/emacs/elisp/my-git-extra.el b/emacs/elisp/my-git-extra.el
index 0b6420b..30fff78 100644
--- a/emacs/elisp/my-git-extra.el
+++ b/emacs/elisp/my-git-extra.el
@@ -12,31 +12,39 @@
          (target-dir (concat  "~/workspace/" repo-name)))
     (magit-clone-regular url target-dir nil)))
 
-(defun my/get-sg-remote-from-hostname (hostname)
-  "Create a sourcegraph URL from HOSTNAME."
-  (format "sourcegraph.rbx.com/%s" hostname))
+(defun my/sg-instance (hostname)
+  "Return the base URL for a sourcegraph instance based on HOSTNAME."
+  (cond ((string-match "cl\.fcuny\.net" hostname) (format "cs.fcuny.xyz/%s" hostname))
+        (t (format "sourcegraph.rbx.com/%s" hostname))))
 
-(defun my/git-link-work-sourcegraph (hostname dirname filename _branch commit start end)
+(defun my/git-link-sourcegraph (hostname dirname filename _branch commit start end)
   "Create a link to sourcegraph given a HOSTNAME DIRNAME FILENAME _BRANCH COMMIT START and END."
-  ;; Use the default branch of the repository instead of the
-  ;; current one (we might be on a feature branch that is not
-  ;; available on the remote).
-  (let ((sg-base-url (my/get-sg-remote-from-hostname hostname))
-        (main-branch (magit-main-branch)))
+  (let ((sg-base-url (my/sg-instance hostname))
+        ;; Use the default branch of the repository instead of the
+        ;; current one (we might be on a feature branch that is not
+        ;; available on the remote).
+        (main-branch (magit-main-branch))
+        ;; repositories cloned with gerrit have a "a/" prefix which we
+        ;; need to remove, as it's not part of the repository name in
+        ;; sourcegraph.
+        (dirname (replace-regexp-in-string "a\/" "" dirname)))
     (git-link-sourcegraph sg-base-url dirname filename main-branch commit start end)))
 
-(defun my/git-link-commit-work-sourcegraph (hostname dirname commit)
+(defun my/git-link-commit-sourcegraph (hostname dirname commit)
   "Create the link to sourcegraph given a HOSTNAME DIRNAME and COMMIT."
-  (let ((sg-base-url (my/get-sg-remote-from-hostname hostname)))
+  (let ((sg-base-url (my/sg-instance hostname))
+        (dirname (replace-regexp-in-string "a\/" "" dirname)))
     (git-link-commit-sourcegraph sg-base-url dirname commit)))
 
 ;; for work related repositories, open them in our instance of sourcegraph
-(add-to-list 'git-link-remote-alist '("github\\.rbx\\.com" my/git-link-work-sourcegraph))
-(add-to-list 'git-link-commit-remote-alist '("github\\.rbx\\.com" my/git-link-commit-work-sourcegraph))
-
-;; for personal code I use gitea, which is similar to codeberg
-(add-to-list 'git-link-remote-alist '("git\\.my\\.net" git-link-codeberg))
-(add-to-list 'git-link-commit-remote-alist '("git\\.my\\.net" git-link-commit-codeberg))
+(add-to-list 'git-link-remote-alist '("github\\.rbx\\.com" my/git-link-sourcegraph))
+(add-to-list 'git-link-commit-remote-alist '("github\\.rbx\\.com" my/git-link-work-sourcegraph))
+
+;; for personal code I use cgit and gerrit
+(add-to-list 'git-link-remote-alist '("git\\.fcuny\\.net" my/git-link-sourcegraph))
+(add-to-list 'git-link-remote-alist '("cl\\.fcuny\\.net" my/git-link-sourcegraph))
+(add-to-list 'git-link-commit-remote-alist '("git\\.fcuny\\.net" my/git-link-commit-sourcegraph))
+(add-to-list 'git-link-commit-remote-alist '("cl\\.fcuny\\.net" my/git-link-commit-sourcegraph))
 
 (provide 'my-git-extra)
 ;;; my-git-extra.el ends here