summaryrefslogtreecommitdiff
path: root/lisp/init-git.el
blob: 001c1193bfa2c5a584451ef87b427fb0cf0504f0 (plain)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
(require 'git-annex)

(setq vc-follow-symlinks t)

(setq git-annex-commit nil)

(define-prefix-command 'km/git-map)
(global-set-key (kbd "C-c g") 'km/git-map)


;;; Magit

(add-to-list 'load-path "~/src/emacs/magit/lisp/")
(require 'magit)

(add-to-list 'load-path "~/src/emacs/orgit/")
(require 'orgit)

(setq magit-restore-window-configuration t
      magit-revert-buffers t
      magit-delete-by-moving-to-trash nil
      magit-log-show-margin nil)

(add-hook 'magit-find-file-hook 'view-mode)
;; http://whattheemacsd.com/setup-magit.el-01.html
(add-hook 'magit-status-mode-hook 'delete-other-windows)
(after 'magit
  (remove-hook 'magit-refs-sections-hook 'magit-insert-tags))

(after 'git-commit
  (add-hook 'git-commit-setup-hook
            (lambda ()
              (add-hook 'with-editor-pre-finish-hook
                        'git-commit-save-message nil t)))
  (add-hook 'git-commit-setup-hook 'git-commit-turn-on-flyspell))

(defun km/magit-auto-commit ()
  "Commit all changes with \"auto\" commit message.
This can be useful for non-source code repos (e.g., Org mode note
files) or commiting incomplete changes that will be extended into
a proper commit."
  (interactive)
  (magit-run-git "commit" "--all" "--message=auto"))

(defun km/magit-show-commit-at-point (&optional choose-project)
  "Show commit point.
If there is no current project or if the prefix argument
CHOOSE-PROJECT is non-nil, prompt for the project name."
  (interactive "P")
  (if (save-excursion (skip-chars-backward "A-z0-9")
                      (looking-at "\\b[A-z0-9]\\{4,40\\}\\b"))
      (let* ((hash (match-string-no-properties 0))
             (project
              (and (or choose-project
                       (not (projectile-project-p)))
                   (completing-read "Project: "
                                    (projectile-relevant-known-projects))))
             (default-directory (or project default-directory)))
        (magit-show-commit hash))
    (user-error "No hash found at point")))

(defun km/magit-commit-extend-all ()
  "Run `magit-commit-extend' with '--all' flag.
This can easily be done from the popup menu but is put into a
function for calling from outside Magit buffers."
  (interactive)
  (magit-commit-extend '("--all")))

(defun km/magit-ff-merge-upstream ()
  "Perform fast-forward merge of upstream branch.
\n(git merge --no-edit --ff-only <upstream>)"
  (interactive)
  (--if-let (magit-get-tracked-branch)
      (magit-merge it '("--ff-only"))
    (user-error "No upstream branch")))

(defun km/magit-stage-file-intent (file)
  "Stage FILE but not its content.
With a prefix argument or when there is no file at point, ask for
the file to be staged.  Otherwise, stage the file at point
without requiring confirmation.
\n(git add -N FILE)"
  ;; Modified from `magit-stage-file'.
  (interactive
   (let* ((atpoint (magit-section-when (file)))
          (current (magit-file-relative-name))
          (choices (magit-untracked-files))
          (default (car (member (or atpoint current) choices))))
     (list (if (or current-prefix-arg (not default))
               (magit-completing-read "Stage file" choices
                                      nil t nil nil default)
             default))))
  (magit-run-git "add" "-N" file))

(defun km/magit-push-all ()
  "Push all branches."
  (interactive)
  (let ((remote (magit-read-remote "Remote")))
    (magit-run-git-async "push" "-v" remote "--all")))

(defun km/magit-push-head (remote &optional args)
  "Push current branch to same name on remote.
\n(git push [ARGS] REMOTE HEAD)"
  (interactive (list (magit-read-remote "Remote") (magit-push-arguments)))
  (magit-run-git-async "push" "-v" args remote "HEAD"))

(defun km/magit-checkout-local-tracking (remote-branch)
  "Create and checkout a local tracking branch for REMOTE-BRANCH.
\n(git checkout -t REMOTE-BRANCH\)"
  (interactive
   (list (magit-completing-read "Remote branch"
                                (magit-list-remote-branch-names))))
  (magit-run-git "checkout" "-t" remote-branch))

(defun km/magit-delete-previous-branch (&optional force)
  "Delete previous branch.
\n(git branch -d @{-1})"
  (interactive "P")
  (magit-run-git "branch" (if force "-D" "-d") "@{-1}"))

(defun km/magit-checkout-previous-branch ()
  "Checkout previous branch.
\n(git checkout -)"
  (interactive)
  (magit-run-git "checkout" "-"))

(defun km/magit-list-recent-refs (n &optional remote)
  "List N recent refs.
If REMOTE is non-nil, limit to remote refs."
  (magit-git-lines
   "for-each-ref" "--sort=-committerdate" "--format=%(refname:short)"
   (format "--count=%s" n)
   (if remote "refs/remotes" "refs")))

(defun km/magit-checkout-recent-ref (n)
  "Checkout branch from N recent refs.
Refs are sorted by committer date."
  (interactive (list (or (and current-prefix-arg
                              (prefix-numeric-value current-prefix-arg))
                         5)))
  (magit-run-git "checkout"
                 (magit-completing-read
                  "Ref" (km/magit-list-recent-refs n))))

(defun km/magit-checkout-track-recent-ref (n)
  "Create and checkout a local tracking branch.
Listed refs are limited to N most recent, sorted by committer
date."
  (interactive (list (or (and current-prefix-arg
                              (prefix-numeric-value current-prefix-arg))
                         5)))
  (magit-run-git "checkout" "-t"
                 (magit-completing-read
                  "Ref" (km/magit-list-recent-refs n 'remote))))

(defun km/magit-checkout-master ()
  "Checkout master branch.
\n(git checkout master)"
  (interactive)
  (magit-run-git "checkout" "master"))

(defun km/magit-branch-and-checkout-from-current (branch)
  "Create and checkout BRANCH at current branch.
This is equivalent to running `magit-branch-and-checkout' with
START-POINT set to the current branch.
\n(git checkout -b BRANCH)"
  (interactive (list (magit-read-string "Branch name")))
  (magit-run-git "checkout" "-b" branch))

(defun km/magit-backup-branch ()
  "Create a backup branch for the current branch.
\n(git branch b/<current-branch>)"
  (interactive)
  (--if-let (magit-get-current-branch)
      (magit-run-git "branch" (concat "b/" it))
    (user-error "No current branch")))

(defun km/magit-mode-bury-all-windows (&optional kill-buffer)
  "Run `magit-mode-quit-window' until no longer in Magit buffer."
  (interactive "P")
  (while (derived-mode-p 'magit-mode)
    (magit-mode-bury-buffer kill-buffer)))

(defun km/magit-log-select-guess-fixup-commit (&optional ntop)
  "Guess commit from fixup/squash commmits.
Consider NTOP commits (default is 5) when searching for 'fixup!'
and 'squash!' titles."
  (interactive (list (or (and current-prefix-arg
                              (prefix-numeric-value current-prefix-arg))
                         5)))
  (let (ntop-end msgs commit-pts)
    (save-excursion
      ;; Get limit for fixup/squash search.
      (goto-char (point-min))
      (setq ntop-end (line-end-position (1+ ntop)))
      ;; Get fixup and squash messages.
      (while (re-search-forward "[a-z0-9]+ \\(fixup!\\|squash!\\) \\(.+\\)"
                                ntop-end t)
        (push (match-string-no-properties 2) msgs))
      (when (not msgs)
        (user-error "No fixup or squash commits found"))
      ;; Find earliest commit.
      (goto-char (point-min))
      (dolist (msg msgs)
        (when (re-search-forward (concat "[a-z0-9]+ " msg "\n") nil t)
          (push (match-beginning 0) commit-pts))))
    (if commit-pts
        (goto-char (apply #'max commit-pts))
      (message "No matching commits found"))))

(defun km/magit-checkout-file (rev file)
  "Checkout FILE from revision REV.
\n(git checkout REV -- file)"
  (interactive
   (let ((rev (magit-read-branch-or-commit "Revision")))
     (list rev (magit-read-file-from-rev rev "File"))))
  (magit-with-toplevel
    (magit-run-git "checkout" rev "--" file)))

(defun km/magit-pin-file (&optional other-rev)
  "Pin this file to the current revision.

Visit the current file and current revision with
`magit-find-file'.  Position point as in the original buffer.
This may not correspond to same content if text before point has
changed since the current commit.

If OTHER-REV is non-nil, prompt for another revision instead of
the current.  In this case, keep point at the beginning of the
buffer."
  (interactive "P")
  (let ((pos (point))
        (rev (if other-rev
                 (magit-read-branch-or-commit "Find file from revision")
               (or (magit-get-current-branch)
                   (magit-rev-parse "HEAD"))))
        (fname (and (or buffer-file-name
                        (user-error "Buffer not visiting file"))
                    (file-relative-name buffer-file-name
                                        (magit-toplevel)))))
    (magit-find-file rev fname)
    (unless other-rev (goto-char pos))))

(defun km/git-rebase-show-commit ()
  "Show the commit on the current line if any.
Unlike `git-rebase-show-commit', display (but don't switch to)
the commit buffer. And no dinging."
  (interactive)
  (save-excursion
    (goto-char (line-beginning-position))
    (--if-let (and (looking-at git-rebase-line)
                   (match-string 2))
        (magit-show-commit it t))))

(defun km/magit-insert-staged-file (&optional no-directory)
  "Select staged file to insert.
This is useful for referring to file names in commit messages.
By default, the path for the file name is relative to the top
directory of the repository.  Remove the directory component from
the file name if NO-DIRECTORY is non-nil."
  (interactive "P")
  (let* ((default-directory (magit-toplevel))
         (files (magit-staged-files))
         (file (if (= 1 (length files))
                   (car files)
                 (completing-read "Staged file: " (magit-staged-files)
                                  nil t))))
    (insert (if no-directory (file-name-nondirectory file) file))))

(define-key ctl-x-4-map "g" 'magit-find-file-other-window)
(define-key km/file-map "g" 'magit-find-file)

(key-chord-define-global "jg" 'magit-status)

(after 'magit
  ;; Remove `magit-add-change-log-entry-other-window', which overrides
  ;; my binding for `km/zsh-ansi-term-other-window'.
  (define-key magit-mode-map (kbd "C-x 4 a") nil)
  (define-key magit-mode-map "N" 'km/magit-stage-file-intent)
  (define-key magit-mode-map "Q" 'km/magit-mode-bury-all-windows)

  ;; `magit-diff-visit-file-worktree' is also on C-RET.
  (define-key magit-file-section-map (kbd "C-j") 'magit-diff-visit-file-worktree)
  (define-key magit-hunk-section-map (kbd "C-j") 'magit-diff-visit-file-worktree)

  (define-key magit-log-mode-map "j" 'km/avy-goto-subword-1)
  (define-key magit-refs-mode-map "j" 'km/avy-goto-subword-1)
  (define-key magit-cherry-mode-map "j" 'km/avy-goto-subword-1)

  (define-key km/git-map "." 'km/magit-show-commit-at-point)
  (define-key km/git-map "e" 'km/magit-commit-extend-all)
  (define-key km/git-map "f" 'km/magit-checkout-file)
  (define-key km/git-map "l" 'magit-log-buffer-file)
  (define-key km/git-map "p" 'km/magit-pin-file)
  (define-key km/git-map "s" 'km/magit-insert-staged-file)
  (define-key km/git-map "u" 'km/magit-auto-commit))

(after 'magit-log
  (define-key magit-log-select-mode-map "."
    'km/magit-log-select-guess-fixup-commit))

(after 'git-rebase
  (define-key git-rebase-mode-map "\s" 'km/git-rebase-show-commit))


;;; Magit popups

(setq magit-popup-show-help-echo nil
      magit-popup-show-common-commands nil
      magit-popup-use-prefix-argument 'default)

(after 'magit
  (setq magit-branch-arguments
        (delete "--track" magit-branch-arguments))

  (define-key magit-popup-mode-map (kbd "SPC <t>") 'magit-invoke-popup-switch)
  (define-key magit-popup-mode-map (kbd "SPC SPC <t>") 'magit-invoke-popup-option)

  (setq magit-patch-popup
        (plist-put magit-patch-popup :use-prefix 'popup))

  (magit-define-popup-action 'magit-commit-popup
    ?u "Auto commit" 'km/magit-auto-commit)

  (magit-define-popup-action 'magit-push-popup
    ?a "Push all" 'km/magit-push-all)
  (magit-define-popup-action 'magit-push-popup
    ?h "Push HEAD" 'km/magit-push-head)

  (magit-define-popup-action 'magit-merge-popup
    ?u "Merge upstream" 'km/magit-ff-merge-upstream)

  (magit-define-popup-action 'magit-branch-popup
    ?c "Create & checkout from current"
    'km/magit-branch-and-checkout-from-current)
  (magit-define-popup-action 'magit-branch-popup
    ?C "Create" 'magit-branch)
  (magit-define-popup-action 'magit-branch-popup
    ?l "Delete previous branch"
    'km/magit-delete-previous-branch)
  (magit-define-popup-action 'magit-branch-popup
    ?m "Checkout master" 'km/magit-checkout-master)
  (magit-define-popup-action 'magit-branch-popup
    ?n "Checkou recent ref" 'km/magit-checkout-recent-ref)
  (magit-define-popup-action 'magit-branch-popup
    ?N "Track recent ref" 'km/magit-checkout-track-recent-ref)
  (magit-define-popup-action 'magit-branch-popup
    ?p "Checkout previous" 'km/magit-checkout-previous-branch)
  (magit-define-popup-action 'magit-branch-popup
    ?s "Backup current branch" 'km/magit-backup-branch)
  (magit-define-popup-action 'magit-branch-popup
    ?t "Local tracking" 'km/magit-checkout-local-tracking)

  (defadvice magit-merge-editmsg (around km/magit-merge-editmsg-no-ff activate)
    "Set '--no-ff' flag when running `magit-merge-editmsg'."
    (let ((args '("--no-ff")))
      ad-do-it)))


;;; Magit Annex

(add-to-list 'load-path "~/src/emacs/magit-annex/")
(require 'magit-annex-autoloads)

(setq magit-annex-unused-open-function #'org-open-file)

(after 'magit-annex
  (setq magit-annex-all-action-arguments
        (delete "--auto" magit-annex-all-action-arguments)))


;;; Other git

(defun km/git-shorten-hash-at-point (&optional n)
  "Shorten hash at point to N characters.
N defaults to 10 and can be controlled by a numeric prefix
argument."
  (interactive (list (or (and current-prefix-arg
                              (prefix-numeric-value current-prefix-arg))
                         10)))
  (cond
   ((< n 4)
    (user-error "Hash must be at least 4 characters"))
   ((>= n 40)
    (user-error "Full hashes are 40 characters"))
   ((> n 30)
    (message "That doesn't seem incredibly useful, but OK")))
  (let ((offset (- (skip-chars-backward "A-z0-9"))))
    (if (looking-at "\\b[A-z0-9]\\{5,40\\}\\b")
        (let ((hash-len (- (match-end 0) (match-beginning 0)))
              (hash (match-string 0)))
          (when (< hash-len n)
            (user-error "Desired hash length is greater than current"))
          (replace-match (substring hash 0 n) 'fixedcase)
          (when (< offset n)
            (skip-chars-backward "A-z0-9")
            (goto-char (+ (point) offset))))
      (goto-char (+ (point) offset))
      (user-error "No hash found at point"))))

(define-key km/git-map "n" 'km/git-shorten-hash-at-point)

(provide 'init-git)