dotfiles/emacs/.emacs.d/config/init-git.el

103 lines
3.9 KiB
EmacsLisp

;; -*- lexical-binding: t; -*-
;; Magit!
(use-package magit
:commands (magit-status
magit-file-dispatch
magit-dispatch
magit-name-local-branch)
:autoload (magit-git-lines
magit-toplevel)
:init
(leader-def-key "g" '(nil :which-key "git"))
:config
(add-hook 'magit-mode-hook #'hl-line-mode)
:general
(leader-map "gs" #'magit-status
"gg" #'magit-file-dispatch
"gd" #'magit-dispatch))
;; Not strictly git-related, but with-editor is the part of Magit that
;; lets Emacs be the $EDITOR for shell commands
(use-package with-editor
:hook ((eshell-mode . with-editor-export-editor)
(shell-mode . with-editor-export-editor)
(vterm-mode . (lambda ()
(with-editor-export-editor)
(vterm-clear))))
:commands (with-editor-async-shell-command
with-editor-shell-command))
;; git-link makes it easy to copy the GitHub URL of the current file/line
(use-package git-link
:commands (git-link git-link-commit)
:init
(defun git-link-browse ()
(interactive)
(let ((git-link-open-in-browser t))
(call-interactively 'git-link)))
(defun git-link-browse-commit ()
(interactive)
(let ((git-link-open-in-browser t))
(call-interactively 'git-link-commit)))
:config
(add-to-list 'git-link-remote-alist '("git.jeremydormitzer.com" git-link-bitbucket))
(add-to-list 'git-link-commit-remote-alist '("git.jeremydormitzer.com" git-link-commit-bitbucket))
(add-to-list 'git-link-remote-alist '("ghe.spotify.net" git-link-github))
(add-to-list 'git-link-commit-remote-alist '("ghe.spotify.net" git-link-commit-github))
:general
(leader-map "gy" #'git-link
"gl" #'git-link-browse
"gc" #'git-link-commit
"gb" #'git-link-browse-commit))
;; forge requires sqlite
(use-package sqlite3)
;; Interact with GitHub etc. from Magit
(use-package forge
:after (magit sqlite3)
:demand t
:autoload (forge-get-repository)
:config
(setq forge-add-default-bindings nil)
(add-to-list 'forge-alist '("git.jeremydormitzer.com"
"git.jeremydormitzer.com/api/v1"
"git.jeremydormitzer.com"
forge-gitea-repository))
;; forge-topic-at-point really should just return nil if the buffer is not readable
(advice-add 'forge-topic-at-point :around
(lambda (oldfn &rest args)
(ignore-errors (apply oldfn args))))
(defun forge-pullreq-diff (pullreq)
"Return the diff of the pull request visited or at point as a string."
(when pullreq
(let* ((base-ref (oref pullreq base-ref))
(head-ref (oref pullreq head-ref))
(base-remote (forge--get-remote base-ref))
(head-remote (forge--get-remote head-ref))
(default-directory (magit-toplevel))
(range (format "%s/%s..%s/%s" base-remote base-ref head-remote head-ref)))
(with-temp-buffer
(magit-git-insert "diff" range)
(buffer-string)))))
(defun forge-diff-for-pr ()
"Display a diff buffer for the pull request currently being created.
Does not work on already-created pull requests."
(interactive)
(let ((target forge--buffer-base-branch)
(source forge--buffer-head-branch))
(magit-diff-range (format "%s..%s" target source))))
(defun forge--submit-create-pullreq-after-advice (&rest args)
(when-let ((buf (magit-get-mode-buffer 'magit-diff-mode)))
(quit-window nil (get-buffer-window buf))))
(add-hook 'forge-create-pullreq-hook #'forge-diff-for-pr)
(advice-add 'forge--submit-create-pullreq :after #'forge--submit-create-pullreq-after-advice)
:custom
(forge-owned-accounts '((jdormit . (remote-name "jdormit"))))
:general
(normal magit-mode-map "yu" #'forge-copy-url-at-point-as-kill)
(magit-revision-mode-map [remap magit-browse-thing] #'forge-browse-dwim))
(provide 'init-git)