/
magit-apply.el
354 lines (317 loc) · 14.3 KB
/
magit-apply.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
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
;;; magit-apply.el --- apply Git diffs
;; Copyright (C) 2010-2015 The Magit Project Developers
;;
;; For a full list of contributors, see the AUTHORS.md file
;; at the top-level directory of this distribution and at
;; https://raw.github.com/magit/magit/master/AUTHORS.md
;; Author: Jonas Bernoulli <jonas@bernoul.li>
;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
;; Magit is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; Magit is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
;; License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Magit. If not, see http://www.gnu.org/licenses.
;;; Code:
(require 'magit-core)
(require 'magit-diff)
(declare-function magit-maybe-backup 'magit-backup)
;; For `magit-discard-files'
(declare-function magit-checkout-stage 'magit)
(declare-function magit-checkout-read-stage 'magit)
(require 'dired)
;;; Options
(defcustom magit-delete-by-moving-to-trash t
"Whether Magit uses the system's trash can."
:package-version '(magit . "2.1.0")
:group 'magit
:type 'boolean)
;;; Commands
;;;; Apply
(defun magit-apply (&rest args)
"Apply the change at point.
With a prefix argument and if necessary, attempt a 3-way merge."
(interactive (and current-prefix-arg (list "--3way")))
(--when-let (magit-current-section)
(magit-maybe-backup)
(pcase (list (magit-diff-type) (magit-diff-scope))
(`(,(or `unstaged `staged) ,_)
(user-error "Change is already in the working tree"))
(`(,_ region) (magit-apply-region it args))
(`(,_ hunk) (magit-apply-hunk it args))
(`(,_ file) (magit-apply-diff it args)))))
(defun magit-apply-diff (section &rest args)
(magit-apply-patch (concat (magit-diff-file-header section)
(buffer-substring (magit-section-content section)
(magit-section-end section)))
args))
(defun magit-apply-hunk (section &rest args)
(when (string-match "^diff --cc" (magit-section-parent-value section))
(user-error "Cannot un-/stage resolution hunks. Stage the whole file"))
(magit-apply-patch (concat (magit-diff-file-header section)
(buffer-substring (magit-section-start section)
(magit-section-end section)))
args))
(defun magit-apply-region (section &rest args)
(magit-apply-patch (magit-diff-hunk-region-patch section args) args))
(defun magit-apply-patch (patch args)
(with-temp-buffer
(insert patch)
(magit-run-git-with-input nil
"apply" args
(unless (magit-diff-context-p) "--unidiff-zero")
"--ignore-space-change" "-"))
(magit-refresh))
;;;; Stage
(defun magit-stage ()
"Add the change at point to the staging area."
(interactive)
(--when-let (magit-current-section)
(magit-maybe-backup)
(pcase (list (magit-diff-type) (magit-diff-scope))
(`(untracked ,_) (magit-stage-untracked))
(`(unstaged region) (magit-apply-region it "--cached"))
(`(unstaged hunk) (magit-apply-hunk it "--cached"))
(`(unstaged file) (magit-run-git "add" "-u" "--" (magit-section-value it)))
(`(unstaged files) (magit-run-git "add" "-u" "--" (magit-region-values)))
(`(unstaged list) (magit-run-git "add" "-u" "."))
(`(staged ,_) (user-error "Already staged"))
(`(committed ,_) (user-error "Cannot stage committed changes"))
(`(undefined ,_) (user-error "Cannot stage this change")))))
;;;###autoload
(defun magit-stage-file (file)
"Stage all changes to FILE.
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."
(interactive
(let* ((atpoint (magit-section-when (file)))
(current (magit-file-relative-name))
(choices (nconc (magit-modified-files)
(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-maybe-backup)
(magit-run-git "add" file))
;;;###autoload
(defun magit-stage-modified (&optional all)
"Stage all changes to files modified in the worktree.
Stage all new content of tracked files and remove tracked files
that no longer exist in the working tree from the index also.
With a prefix argument also stage previously untracked (but not
ignored) files.
\('git add --update|--all .')."
(interactive (progn (unless (or (not (magit-anything-staged-p))
(magit-confirm 'stage-all-changes))
(user-error "Abort"))
(list current-prefix-arg)))
(magit-maybe-backup)
(magit-run-git "add" (if all "--all" "--update") "."))
(defun magit-stage-untracked ()
(let ((section (magit-current-section)) files repos)
(dolist (file (pcase (magit-diff-scope)
(`file (list (magit-section-value section)))
(`files (magit-region-values))
(`list (magit-untracked-files))))
(if (magit-git-repo-p file t)
(push file repos)
(push file files)))
(when files
(magit-run-git "add" "--" files))
(dolist (repo repos)
(save-excursion
(goto-char (magit-section-start repo))
(call-interactively 'magit-submodule-add)))))
;;;; Unstage
(defun magit-unstage ()
"Remove the change at point from the staging area."
(interactive)
(--when-let (magit-current-section)
(magit-maybe-backup)
(pcase (list (magit-diff-type) (magit-diff-scope))
(`(untracked ,_) (user-error "Cannot unstage untracked changes"))
(`(unstaged ,_) (user-error "Already unstaged"))
(`(staged region) (magit-apply-region it "--reverse" "--cached"))
(`(staged hunk) (magit-apply-hunk it "--reverse" "--cached"))
(`(staged file) (magit-unstage-1 (magit-section-value it)))
(`(staged files) (magit-unstage-1 (magit-region-values)))
(`(staged list) (when (or (and (not (magit-anything-unstaged-p))
(not (magit-untracked-files)))
(magit-confirm 'unstage-all-changes))
(magit-run-git "reset" "HEAD" "--")))
(`(committed ,_) (user-error "Cannot unstage committed changes"))
(`(undefined ,_) (user-error "Cannot unstage this change")))))
;;;###autoload
(defun magit-unstage-file (file)
"Unstage all changes to FILE.
With a prefix argument or when there is no file at point ask for
the file to be unstaged. Otherwise unstage the file at point
without requiring confirmation."
(interactive
(let* ((atpoint (magit-section-when (file)))
(current (magit-file-relative-name))
(choices (magit-staged-files))
(default (car (member (or atpoint current) choices))))
(list (if (or current-prefix-arg (not default))
(magit-completing-read "Unstage file" choices
nil t nil nil default)
default))))
(magit-maybe-backup)
(magit-unstage-1 file))
(defun magit-unstage-1 (args)
(if (magit-no-commit-p)
(magit-run-git "rm" "--cached" "--" args)
(magit-run-git "reset" "HEAD" "--" args)))
;;;; Discard
(defun magit-discard ()
"Remove the change at point."
(interactive)
(--when-let (magit-current-section)
(pcase (list (magit-diff-type) (magit-diff-scope))
(`(committed ,_) (user-error "Cannot discard committed changes"))
(`(undefined ,_) (user-error "Cannot discard this change"))
(`(,_ list) (magit-discard-files (magit-section-children it)))
(`(,_ files) (magit-discard-files (magit-region-sections)))
(`(,_ file) (magit-discard-files (list it)))
(_ (magit-discard-apply it)))))
(defun magit-discard-apply (section)
(magit-maybe-backup)
(let* ((type (magit-diff-type section))
(scope (magit-diff-scope section t))
(fn (pcase scope
(`region 'magit-apply-region)
(`hunk 'magit-apply-hunk)
(`file 'magit-apply-diff))))
(when (or (eq scope 'file)
(magit-confirm 'discard (format "Discard %s" scope)))
(if (eq type 'unstaged)
(funcall fn section "--reverse")
(if (magit-anything-unstaged-p
(if (eq scope 'file)
(magit-section-value section)
(magit-section-parent-value section)))
(progn
(let ((inhibit-magit-refresh t))
(funcall fn section "--reverse" "--cached")
(funcall fn section "--reverse"))
(magit-refresh))
(funcall fn section "--reverse" "--index"))))))
(defun magit-discard-files (sections)
(let ((auto-revert-verbose nil)
(inhibit-magit-refresh t)
(status (magit-file-status))
delete resurrect rename discard resolve)
(dolist (section sections)
(let ((file (magit-section-value section)))
(pcase (cons (pcase (magit-diff-type section)
(`staged ?X)
(`unstaged ?Y)
(`untracked ?Z))
(magit-file-status file status))
(`(?Z) (push file delete))
((or `(?Z ?? ??) `(?Z ?! ?!)) (push file delete))
((or `(?Z ?D ? ) `(,_ ?D ?D)) (push file delete))
((or `(,_ ?U ,_) `(,_ ,_ ?U)) (push file resolve))
(`(,_ ?A ?A) (push file resolve))
(`(?X ?M ,(or ? ?M ?D)) (push section discard))
(`(?Y ,_ ?M ) (push section discard))
(`(?X ?A ,(or ? ?M ?D)) (push file delete))
(`(?X ?C ,(or ? ?M ?D)) (push file delete))
(`(?X ?D ,(or ? ?M )) (push file resurrect))
(`(?Y ,_ ?D ) (push file resurrect))
(`(?X ?R ,(or ? ?M ?D)) (push section rename)))))
(when resolve
(let ((inhibit-magit-refresh t))
(dolist (file (nreverse resolve))
(magit-checkout-stage file (magit-checkout-read-stage file)))))
(magit-maybe-backup)
(magit-discard-files--resurrect (nreverse resurrect))
(magit-discard-files--delete (nreverse delete))
(magit-discard-files--rename (nreverse rename))
(magit-discard-files--discard (nreverse discard)))
(magit-refresh))
(defun magit-discard-files--resurrect (files)
(when (magit-confirm-files 'resurrect files)
(if (eq (magit-diff-type) 'staged)
(magit-call-git "reset" "--" files)
(magit-call-git "checkout" "--" files))))
(defun magit-discard-files--delete (files)
(when (if magit-delete-by-moving-to-trash
(magit-confirm-files 'trash files)
(magit-confirm-files 'delete files))
(let ((delete-by-moving-to-trash magit-delete-by-moving-to-trash)
(status (magit-file-status)))
(dolist (file files)
(if (memq (magit-diff-type) '(unstaged untracked))
(dired-delete-file file dired-recursive-deletes
magit-delete-by-moving-to-trash)
(pcase (nth 2 (assoc file status))
(? (delete-file file t)
(magit-call-git "rm" "--cached" "--" file))
(?M (let ((temp (magit-git-string "checkout-index" "--temp" file)))
(string-match
(format "\\(.+?\\)\t%s" (regexp-quote file)) temp)
(rename-file (match-string 1 temp)
(setq temp (concat file ".~{index}~")))
(delete-file temp t))
(magit-call-git "rm" "--cached" "--force" "--" file))
(?D (magit-call-git "checkout" "--" file)
(delete-file file t)
(magit-call-git "rm" "--cached" "--force" "--" file))))))))
(defun magit-discard-files--rename (sections)
(when (magit-confirm 'rename "Undo rename %s" "Undo %i renames"
(--map (format "%s -> %s"
(magit-section-source it)
(magit-section-value it))
sections))
(dolist (section sections)
(let ((file (magit-section-value section))
(orig (magit-section-source section)))
(if (file-exists-p file)
(magit-call-git "mv" file orig)
(magit-call-git "rm" "--cached" "--" file)
(magit-call-git "reset" "--" orig))))))
(defun magit-discard-files--discard (sections)
(when (magit-confirm-files 'discard (mapcar 'magit-section-value sections)
(format "Discard %s changes in" (magit-diff-type)))
(mapc 'magit-discard-apply sections)))
;;;; Reverse
(defun magit-reverse ()
"Reverse the change at point in the working tree."
(interactive)
(--when-let (magit-current-section)
(magit-maybe-backup)
(pcase (list (magit-diff-type) (magit-diff-scope))
(`(untracked ,_) (user-error "Cannot reverse untracked changes"))
(`(unstaged ,_) (user-error "Cannot reverse unstaged changes"))
(`(,_ list) (magit-reverse-files (magit-section-children it)))
(`(,_ files) (magit-reverse-files (magit-region-sections)))
(`(,_ file) (magit-reverse-files (list it)))
(_ (magit-reverse-apply it)))))
(defun magit-reverse-apply (section)
(let ((scope (magit-diff-scope section t)))
(when (or (eq scope 'file)
(magit-confirm 'reverse (format "Reverse %s" scope)))
(funcall (pcase scope
(`region 'magit-apply-region)
(`hunk 'magit-apply-hunk)
(`file 'magit-apply-diff))
section "--reverse"))))
(defun magit-reverse-files (sections)
(when (magit-confirm-files 'reverse (mapcar 'magit-section-value sections))
(mapc 'magit-reverse-apply sections)))
;;; magit-apply.el ends soon
(provide 'magit-apply)
;; Local Variables:
;; indent-tabs-mode: nil
;; End:
;;; magit-apply.el ends here