about summary refs log tree commit diff stats
path: root/.emacs.d/lisp/init-git.el
blob: bfcfc22ba8402f7a314b05b4d37ee4c203a87324 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
;;; init-git.el --- VCS/Git Configuration File -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:

(use-package ediff
  :custom
  (ediff-setup-windows-plain 'ediff-setup-windows-plain))

(use-package diff-hl
  :config
  (global-diff-hl-mode +1)
  :hook
  (dired-mode . diff-hl-dired-mode)
  (magit-post-refresh . diff-hl-magit-post-refresh))

(use-package gitconfig)
(use-package git-modes)

(use-package git-timemachine
  :bind
  ("C-x v t" . git-timemachine-toggle))

(use-package vc
  :bind
  (("C-x v C-r" . my/vc-refresh-state)
   ("C-x v C-m" . my/update-git-master))
  :custom (vc-follow-symlinks nil)
  :config
  (defun my/vc-refresh-state ()
    (interactive)
    (when-let ((root-dir (vc-root-dir)))
      (dolist (buf (buffer-list))
        (when (and (not (buffer-modified-p buf))
                   (buffer-file-name buf)
                   (file-exists-p (buffer-file-name buf))
                   (file-in-directory-p (buffer-file-name buf) root-dir))
          (with-current-buffer buf
            (vc-refresh-state))))))

  ;; [alias]
  ;;   update-master = !git fetch origin master:master
  ;;   update-main = !git fetch origin main:main
  (defun my/update-git-master ()
    "Update git master or main branch."
    (interactive)
    (if-let ((root (vc-root-dir)))
        (let* ((branches (vc-git-branches))
               (main-p (member "main" branches))
               (master-p (member "master" branches))
               (current-branch (car branches))
               (on-master-p (member current-branch '("master" "main")))
               (command (if main-p "update-main" "update-master"))
               (buffer "*vc-update-master*"))
          (if on-master-p
              (vc-pull)
            ;; based on vc-git--pushpull
            (require 'vc-dispatcher)
            (apply #'vc-do-async-command buffer root vc-git-program command nil)
            (with-current-buffer buffer
              (vc-run-delayed
                (vc-compilation-mode 'git)
                (setq-local compile-command
                            (concat vc-git-program " " command))
                (setq-local compilation-directory root)
                (setq-local compilation-arguments
                            (list compile-command nil
                                  (lambda (_name-of-mode) buffer)
                                  nil))))
            (vc-set-async-update buffer)))
      (message "not a git repository"))))

(use-package magit
  :bind
  ("C-c g g" . magit-dispatch) ;; magit-file-dispatch is C-c M-g
  ("C-c g u" . my/magit-set-upstream)
  ("C-c g r" . my/magit-refresh-state)
  ("C-c g m" . my/magit-update-git-master)
  :config
  ;; Requires the following gitconfig:
  ;; [alias]
  ;;   upstream = !git push -u origin HEAD
  ;; TODO - this is useful after setting push remote, but is there a better way?
  (defun my/magit-set-upstream ()
    (interactive)
    (magit-shell-command-topdir "git upstream"))

  ;; update stale git info on the modeline (based on code removed from doom modeline)
  (defun my/magit-refresh-state ()
    "Update modeline git branch information."
    (interactive)
    (dolist (buf (buffer-list))
      (when (and (not (buffer-modified-p buf))
                 (buffer-file-name buf)
                 (file-exists-p (buffer-file-name buf))
                 (file-in-directory-p (buffer-file-name buf) (magit-toplevel)))
        (with-current-buffer buf
          (vc-refresh-state)))))

  ;; [alias]
  ;;   update-master = !git fetch origin master:master
  ;;   update-main = !git fetch origin main:main
  (defun my/magit-update-master ()
    "Update git master or main branch."
    (interactive)
    (if (magit-toplevel)
        (let* ((branches (vc-git-branches))
               (main-p (member "main" branches))
               (current-branch (car branches))
               (on-master-p (member current-branch '("master" "main")))
               (command (concat "git " (if main-p "update-main" "update-master"))))
          (if on-master-p
              (vc-pull)
            (magit-shell-command-topdir command)))
      (message "Not a git repository")))

  ;; difftastic code copied from https://tsdh.org/posts/2022-08-01-difftastic-diffing-with-magit.html
  (defun my/magit--with-difftastic (buffer command)
    "Run COMMAND with GIT_EXTERNAL_DIFF=difft then show result in BUFFER."
    (let ((process-environment
           (cons (concat "GIT_EXTERNAL_DIFF=difft --width="
                         (number-to-string (frame-width)))
                 process-environment)))
      ;; Clear the result buffer (we might regenerate a diff, e.g., for
      ;; the current changes in our working directory).
      (with-current-buffer buffer
        (setq buffer-read-only nil)
        (erase-buffer))
      ;; Now spawn a process calling the git COMMAND.
      (make-process
       :name (buffer-name buffer)
       :buffer buffer
       :command command
       ;; Don't query for running processes when emacs is quit.
       :noquery t
       ;; Show the result buffer once the process has finished.
       :sentinel (lambda (proc event)
                   (when (eq (process-status proc) 'exit)
                     (with-current-buffer (process-buffer proc)
                       (goto-char (point-min))
                       (ansi-color-apply-on-region (point-min) (point-max))
                       (setq buffer-read-only t)
                       (view-mode)
                       (end-of-line)
                       ;; difftastic diffs are usually 2-column side-by-side,
                       ;; so ensure our window is wide enough.
                       (let ((width (current-column)))
                         (while (zerop (forward-line 1))
                           (end-of-line)
                           (setq width (max (current-column) width)))
                         ;; Add column size of fringes
                         (setq width (+ width
                                        (fringe-columns 'left)
                                        (fringe-columns 'right)))
                         (goto-char (point-min))
                         (pop-to-buffer
                          (current-buffer)
                          `(;; If the buffer is that wide that splitting the frame in
                            ;; two side-by-side windows would result in less than
                            ;; 80 columns left, ensure it's shown at the bottom.
                            ,(when (> 80 (- (frame-width) width))
                               #'display-buffer-at-bottom)
                            (window-width
                             . ,(min width (frame-width))))))))))))

  (defun my/magit-show-with-difftastic (rev)
    "Show the result of \"git show REV\" with GIT_EXTERNAL_DIFF=difft."
    (interactive
     (list (or
            ;; If REV is given, just use it.
            (when (boundp 'rev) rev)
            ;; If not invoked with prefix arg, try to guess the REV from
            ;; point's position.
            (and (not current-prefix-arg)
                 (or (magit-thing-at-point 'git-revision t)
                     (magit-branch-or-commit-at-point)))
            ;; Otherwise, query the user.
            (magit-read-branch-or-commit "Revision"))))
    (if (not rev)
        (error "No revision specified")
      (my/magit--with-difftastic
       (get-buffer-create (concat "*git show difftastic " rev "*"))
       (list "git" "--no-pager" "show" "--ext-diff" rev))))

  (defun my/magit-diff-with-difftastic (arg)
    "Show the result of \"git diff ARG\" with GIT_EXTERNAL_DIFF=difft."
    (interactive
     (list (or
            ;; If RANGE is given, just use it.
            (when (boundp 'range) range)
            ;; If prefix arg is given, query the user.
            (and current-prefix-arg
                 (magit-diff-read-range-or-commit "Range"))
            ;; Otherwise, auto-guess based on position of point, e.g., based on
            ;; if we are in the Staged or Unstaged section.
            (pcase (magit-diff--dwim)
              ('unmerged (error "unmerged is not yet implemented"))
              ('unstaged nil)
              ('staged "--cached")
              (`(stash . ,value) (error "stash is not yet implemented"))
              (`(commit . ,value) (format "%s^..%s" value value))
              ((and range (pred stringp)) range)
              (_ (magit-diff-read-range-or-commit "Range/Commit"))))))
    (let ((name (concat "*git diff difftastic"
                        (if arg (concat " " arg) "")
                        "*")))
      (my/magit--with-difftastic
       (get-buffer-create name)
       `("git" "--no-pager" "diff" "--ext-diff" ,@(when arg (list arg))))))

  ;; Based on https://tsdh.org/posts/2022-08-01-difftastic-diffing-with-magit.html
  (transient-define-prefix my/magit-extra-commands ()
    "Extra magit commands."
    ["Extra commands"
     ("u" "Set upstream" my/magit-set-upstream)
     ("r" "Refresh state (update modeline)" my/magit-refresh-state)
     ("m" "Update master/main" my/magit-update-master)
     ("d" "Difftastic Diff (dwim)" my/magit-diff-with-difftastic)
     ("s" "Difftastic Show" my/magit-show-with-difftastic)])
  (transient-append-suffix 'magit-dispatch "!"
    '("#" "Extra Magit Cmds" my/magit-extra-commands))
  (define-key magit-status-mode-map (kbd "#") #'my/magit-extra-commands)

  :custom
  (magit-diff-refine-hunk 'all)
  (magit-diff-paint-whitespace-lines 'all)
  (magit-diff-refine-ignore-whitespace nil)
  (magit-diff-highlight-trailing t))

(use-package magit-todos)

(use-package forge
  :after magit)

(provide 'init-git)
;;; init-git.el ends here