forked from redguardtoo/emacs.d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit-git.el
215 lines (186 loc) · 8.17 KB
/
init-git.el
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
;; Solution 1: disable all vc backends
;; @see http://stackoverflow.com/questions/5748814/how-does-one-disable-vc-git-in-emacs
;; (setq vc-handled-backends ())
;; ;; Solution 2: if NO network mounted drive involved
(setq vc-handled-backends '(Git SVN Hg))
;; @see https://www.reddit.com/r/emacs/comments/4c0mi3/the_biggest_performance_improvement_to_emacs_ive/
;; open files faster but you can't check if file is version
;; controlled. other vcs functionality still works.
(remove-hook 'find-file-hooks 'vc-find-file-hook)
;; ;; Solution 3: setup vc-handled-backends per project
;; (setq vc-handled-backends ())
;; (defun my-setup-develop-environment ()
;; (interactive)
;; (cond
;; ((string-match-p (file-truename "~/.emacs.d") (file-name-directory (buffer-file-name))
;; (setq vc-handled-backends '(Git)))
;; (t (setq vc-handled-backends nil)))))
;; (add-hook 'java-mode-hook 'my-setup-develop-environment)
;; (add-hook 'emacs-lisp-mode-hook 'my-setup-develop-environment)
;; (add-hook 'org-mode-hook 'my-setup-develop-environment)
;; (add-hook 'js2-mode-hook 'my-setup-develop-environment)
;; (add-hook 'js-mode-hook 'my-setup-develop-environment)
;; (add-hook 'javascript-mode-hook 'my-setup-develop-environment)
;; (add-hook 'web-mode-hook 'my-setup-develop-environment)
;; (add-hook 'c++-mode-hook 'my-setup-develop-environment)
;; (add-hook 'c-mode-hook 'my-setup-develop-environment)
;; {{ git-gutter
(require 'git-gutter)
(defun git-gutter-reset-to-head-parent()
(interactive)
(let (parent (filename (buffer-file-name)))
(if (eq git-gutter:vcs-type 'svn)
(setq parent "PREV")
(setq parent (if filename (concat (shell-command-to-string (concat "git --no-pager log --oneline -n1 --pretty='format:%H' " filename)) "^") "HEAD^")))
(git-gutter:set-start-revision parent)
(message "git-gutter:set-start-revision HEAD^")))
(defun git-gutter-reset-to-default ()
(interactive)
(git-gutter:set-start-revision nil)
(message "git-gutter reset"))
;; If you enable global minor mode
(global-git-gutter-mode t)
;; nobody use bzr
;; people are forced use subversion or hg, so they take priority
(custom-set-variables '(git-gutter:handled-backends '(svn hg git)))
(git-gutter:linum-setup)
(global-set-key (kbd "C-x C-g") 'git-gutter:toggle)
(global-set-key (kbd "C-x v =") 'git-gutter:popup-hunk)
;; Stage current hunk
(global-set-key (kbd "C-x v s") 'git-gutter:stage-hunk)
;; Revert current hunk
(global-set-key (kbd "C-x v r") 'git-gutter:revert-hunk)
;; }}
;; {{ git-timemachine
(defun my-git-timemachine-show-selected-revision ()
"Show last (current) revision of file."
(interactive)
(let* ((collection (mapcar (lambda (rev)
;; re-shape list for the ivy-read
(cons (concat (substring-no-properties (nth 0 rev) 0 7) "|" (nth 5 rev) "|" (nth 6 rev)) rev))
(git-timemachine--revisions))))
(ivy-read "commits:"
collection
:action (lambda (rev)
;; compatible with ivy 8+ and later ivy version
(unless (string-match-p "^[a-z0-9]*$" (car rev))
(setq rev (cdr rev)))
(git-timemachine-show-revision rev)))))
(defun my-git-timemachine ()
"Open git snapshot with the selected version. Based on ivy-mode."
(interactive)
(unless (featurep 'git-timemachine)
(require 'git-timemachine))
(git-timemachine--start #'my-git-timemachine-show-selected-revision))
;; }}
;;----------------------------------------------------------------------------
;; git-svn conveniences
;;----------------------------------------------------------------------------
(eval-after-load 'compile
'(progn
(dolist (defn (list '(git-svn-updated "^\t[A-Z]\t\\(.*\\)$" 1 nil nil 0 1)
'(git-svn-needs-update "^\\(.*\\): needs update$" 1 nil nil 2 1)))
(add-to-list 'compilation-error-regexp-alist-alist defn))
(dolist (defn '(git-svn-updated git-svn-needs-update))
(add-to-list 'compilation-error-regexp-alist defn))))
(defvar git-svn--available-commands nil "Cached list of git svn subcommands")
(defun git-svn (dir)
"Run git svn"
(interactive "DSelect directory: ")
(unless git-svn--available-commands
(setq git-svn--available-commands
(string-all-matches "^ \\([a-z\\-]+\\) +" (shell-command-to-string "git svn help") 1)))
(let* ((default-directory (vc-git-root dir))
(compilation-buffer-name-function (lambda (major-mode-name) "*git-svn*")))
(compile (concat "git svn "
(ido-completing-read "git-svn command: " git-svn--available-commands nil t)))))
(defun git-get-current-file-relative-path ()
(replace-regexp-in-string (concat "^" (file-name-as-directory default-directory))
""
buffer-file-name))
(defun git-checkout-current-file ()
"git checkout urrent file"
(interactive)
(when (and (buffer-file-name)
(yes-or-no-p (format "git checkout %s?"
(file-name-nondirectory (buffer-file-name)))))
(let* ((filename (git-get-current-file-relative-path)))
(shell-command (concat "git checkout " filename))
(message "DONE! git checkout %s" filename))))
(defun git-add-current-file ()
"git add file of current buffer"
(interactive)
(let ((filename))
(when buffer-file-name
(setq filename (git-get-current-file-relative-path))
(shell-command (concat "git add " filename))
(message "DONE! git add %s" filename))))
;; {{ goto next/previous hunk
(defun my-goto-next-hunk (arg)
(interactive "p")
(if (memq major-mode '(diff-mode))
(diff-hunk-next)
(forward-line)
(if (re-search-forward "\\(^<<<<<<<\\|^=======\\|^>>>>>>>\\)" (point-max) t)
(goto-char (line-beginning-position))
(forward-line -1)
(git-gutter:next-hunk arg))))
(defun my-goto-previous-hunk (arg)
(interactive "p")
(if (memq major-mode '(diff-mode))
(diff-hunk-prev)
(forward-line -1)
(if (re-search-backward "\\(^>>>>>>>\\|^=======\\|^<<<<<<<\\)" (point-min) t)
(goto-char (line-beginning-position))
(forward-line -1)
(git-gutter:previous-hunk arg))))
;; }}
;; {{ git-gutter use ivy
(defun my-reshape-git-gutter (gutter)
"Re-shape gutter for `ivy-read'."
(let* ((linenum-start (aref gutter 3))
(linenum-end (aref gutter 4))
(target-line "")
(target-linenum 1)
(tmp-line "")
(max-line-length 0))
(save-excursion
(while (<= linenum-start linenum-end)
(goto-line linenum-start)
(setq tmp-line (replace-regexp-in-string "^[ \t]*" ""
(buffer-substring (line-beginning-position)
(line-end-position))))
(when (> (length tmp-line) max-line-length)
(setq target-linenum linenum-start)
(setq target-line tmp-line)
(setq max-line-length (length tmp-line)))
(setq linenum-start (1+ linenum-start))))
;; build (key . linenum-start)
(cons (format "%s %d: %s"
(if (eq 'deleted (aref gutter 1)) "-" "+")
target-linenum target-line)
target-linenum)))
(defun my-goto-git-gutter ()
(interactive)
(if git-gutter:diffinfos
(ivy-read "git-gutters:"
(mapcar 'my-reshape-git-gutter git-gutter:diffinfos)
:action (lambda (e)
(unless (numberp e) (setq e (cdr e)))
(goto-line e)))
(message "NO git-gutters!")))
;; }}
;; {{ git-messenger
;; show details to play `git blame' game
(setq git-messenger:show-detail t)
(add-hook 'git-messenger:after-popup-hook
(lambda (msg)
;; extract commit id and put into the kill ring
(when (string-match "\\(commit *: *\\)\\([0-9a-z]+\\)" msg)
;; get the first 7 characters as hash because that's `git log' use
(let ((hash (substring-no-properties (match-string 2 msg) 0 7)))
(copy-yank-str hash)
(message "commit hash %s => clipboard & kill-ring" hash)))))
(global-set-key (kbd "C-x v p") 'git-messenger:popup-message)
;; }}
(provide 'init-git)