about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2021-08-24 19:21:46 -0700
committerFranck Cuny <franck@fcuny.net>2021-08-24 19:25:57 -0700
commit4f8e6e0a386b98cbb5662e8f5203e3c8224954d0 (patch)
tree30bdffb540be4766050fb6d7107e0ec440f559f8
parentblog: sort pages in reverse order (diff)
downloadfcuny.net-4f8e6e0a386b98cbb5662e8f5203e3c8224954d0.tar.gz
blog: git-link and sourcegraph
-rw-r--r--content/blog/git-link-and-sourcegraph.org40
1 files changed, 40 insertions, 0 deletions
diff --git a/content/blog/git-link-and-sourcegraph.org b/content/blog/git-link-and-sourcegraph.org
new file mode 100644
index 0000000..034f8da
--- /dev/null
+++ b/content/blog/git-link-and-sourcegraph.org
@@ -0,0 +1,40 @@
+#+TITLE: emacs' git-link and sourcegraph
+#+TAGS[]: emacs git
+#+DATE: <2021-08-24 Tue>
+
+I use [[https://sourcegraph.com/][sourcegraph]] for searching code, and I sometimes need to share a link to the source code I'm looking at in a buffer. For this, the package [[https://github.com/sshaw/git-link][=git-link=]] is great.
+
+To integrate sourcegraph and =git-link=, the [[https://github.com/sshaw/git-link#sourcegraph][documentation]] recommends adding a remote entry named =sourcegraph= in the repository, like this:
+
+#+begin_src sh
+git remote add sourcegraph https://sourcegraph.com/github.com/sshaw/copy-as-format
+#+end_src
+
+The next time you run =M-x git-link= in a buffer, it will use the URL associated with that remote. That's works great, except that now you need to add this for every repository. Instead, for my usage, I came up with the following solution:
+
+#+begin_src elisp
+(use-package git-link
+  :ensure t
+  :config
+  (defun fcuny/get-sg-remote-from-hostname (hostname)
+    (format "sourcegraph.<$domain>.<$tld>/%s" hostname))
+
+  (defun fcuny/git-link-work-sourcegraph (hostname dirname filename branch commit start end)
+    (let ((sg-base-url (fcuny/get-sg-remote-from-hostname hostname)))
+      (git-link-sourcegraph sg-base-url dirname filename branch commit start end)))
+
+  (defun fcuny/git-link-commit-work-sourcegraph (hostname dirname commit)
+    (let ((sg-base-url (fcuny/get-sg-remote-from-hostname hostname)))
+      (git-link-commit-sourcegraph sg-base-url dirname commit)))
+
+  (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))
+
+  (setq git-link-open-in-browser 't))
+#+end_src
+
+We use different domains to host various git repositories at work (e.g. =git.$work=, =gitfoo.$work=, etc). Each of them map to a different URI for sourcegraph (e.g. =sourcegraph.$work/gitfoo=).
+
+=git-link-commit-remote-alist= is an [[https://www.gnu.org/software/emacs/manual/html_node/elisp/Association-Lists.html][association list]] that takes a regular expression and a function. The custom function receives the hostname for the remote repository, which is then used to generate the URI for our sourcegraph instance. I then call =git-link-sourcegraph= replacing the hostname with the URI for sourcegraph.
+
+Now I can run =M-x git-link= in any repository where the host for the origin git repository matches =twitter= without having to setup the custom remote first.