forked from emacsorphanage/magit-p4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagit-p4.el
268 lines (227 loc) · 9.16 KB
/
magit-p4.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
;;; magit-p4.el --- git-p4 plug-in for Magit
;; Copyright (C) 2014 Damian T. Dobroczyński
;;
;; Author: Damian T. Dobroczy\\'nski <[email protected]>
;; Maintainer: Aleksey Fedotov <[email protected]>
;; Version: 1.1
;; Package-Requires: ((magit "2.1") (magit-popup "2.1") (p4 "12.0") (cl-lib "0.5"))
;; Keywords: vc tools
;; URL: https://github.com/qoocku/magit-p4
;; Package: magit-p4
;; Magit-p4 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-p4 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-p4. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This plug-in provides git-p4 functionality as a separate component
;; of Magit.
;;; Code:
(require 'magit)
(eval-when-compile
(require 'cl-lib)
(require 'find-lisp)
(require 'p4))
(declare-function find-lisp-find-files-internal 'find-lisp)
;;; Options
(defgroup magit-p4 nil
"Git-p4 support for Magit."
:group 'magit-extensions)
;;; Commands
;;;###autoload
(defun magit-p4-clone (depot-path &optional target-dir)
"Clone given DEPOT-PATH.
The first argument is P4 depot path to clone. The TARGET-DIR argument
is directory which will hold the Git repository."
(interactive
(append (list (p4-read-arg-string "Depot path: " "//" 'filespec))
(if (and (not (search "destination=" (magit-p4-clone-arguments)))
current-prefix-arg)
(read-directory-name "Target directory: ")
nil)))
(magit-run-git-async "p4" "clone" (cons depot-path (magit-p4-clone-arguments))))
;;;###autoload
(defun magit-p4-sync (&optional depot-path)
"Synchronize with default and/or given DEPOT-PATH.
The optional argument is P4 depot path which will be synchronized with.
If not present, git-p4 will try to synchronize with default depot path which
has been cloned to before."
(interactive
(when current-prefix-arg
(list (p4-read-arg-string "With (another) depot path: " "//" 'filespec))))
(magit-run-git-async "p4" "sync"
(cond (depot-path
(cons depot-path (magit-p4-sync-arguments)))
(t (magit-p4-sync-arguments)))))
;;;###autoload
(defun magit-p4-rebase ()
"Run git-p4 rebase."
(interactive)
(magit-run-git-async "p4" "rebase"))
;;;###autoload
(defun magit-p4-submit ()
"Run git-p4 submit."
(interactive)
(magit-p4-run-git-with-editor "p4" "submit" (magit-p4-submit-arguments)))
(defcustom magit-p4-process-yes-or-no-prompt-regexp "\\[\\(y\\)\\]es, \\[\\(n\\)\\]o"
"Regexp matching yes-or-no prompt for git-p4."
:group 'magit-p4
:type 'regexp)
(defun magit-p4-process-yes-or-no-prompt (process string)
(-when-let (beg (string-match magit-p4-process-yes-or-no-prompt-regexp string))
(let ((max-mini-window-height 30))
(process-send-string
process
(downcase
(concat
(match-string
(if (save-match-data
(magit-process-kill-on-abort process
(yes-or-no-p (substring string 0 beg)))) 1 2)
string)
"\n"))))))
(defcustom magit-p4-process-skip-or-quit-regexps '("\\[s\\]kip this commit but apply the rest, or \\[q\\]uit")
"List of regexp matching skip-or-quit prompts from git-p4."
:group 'magit-p4
:type '(repeat (regexp)))
(defun magit-p4-process-skip-or-quit (process string)
(--when-let (magit-process-match-prompt magit-p4-process-skip-or-quit-regexps string)
(process-send-string
process
(concat
(substring
(magit-process-kill-on-abort process
(magit-completing-read it '("skip" "quit") nil t))
0 1) "\n"))))
(defun magit-p4-process-filter (process string)
"Filter used by `magit-p4-run-git-with-editor'."
(with-current-buffer (process-buffer process)
(magit-p4-process-yes-or-no-prompt process string)
(magit-p4-process-skip-or-quit process string)))
;;;###autoload
(defun magit-p4-run-git-with-editor (&rest args)
"Similar to magit-run-git-with-editor, but also exports
P4EDITOR and uses custom process filter `magit-p4-process-filter'."
(let* ((process (with-editor "P4EDITOR"
(apply #'magit-run-git-with-editor args)))
(old-filter (process-filter process)))
(set-process-filter
process
`(lambda (process str)
(magit-p4-process-filter process str)
(funcall ,old-filter process str)))
process))
;; Menu
(easy-menu-define magit-p4-extension-menu
nil
"Git P4 extension menu"
'("Git P4"
:visible magit-p4-mode
["Clone" magit-p4-clone t]
["Sync" magit-p4-sync t]
["Rebase" magit-p4-rebase t]
["Submit" magit-p4-submit t]))
(easy-menu-add-item 'magit-mode-menu
'("Extensions")
magit-p4-extension-menu)
;;; Keymaps
;;;###autoload (autoload 'magit-p4-popup "magit-p4" nil t)
(magit-define-popup magit-p4-popup
"Show popup buffer featuring git p4 commands"
'magit-commands
:man-page "git-p4"
:actions '((?c "Clone" magit-p4-clone-popup)
(?s "Sync" magit-p4-sync-popup)
(?r "Rebase" magit-p4-rebase)
(?S "Submit" magit-p4-submit-popup)))
(defvar magit-p4-sync-clone-shared-options
'((?b "Branch" "--branch=")
(?c "Changes files" "--changesfile=" read-file-name)
(?m "Limit the number of imported changes" "--max-changes=")
(?s "Internal block size to use when iteratively calling p4 changes" "--changes-block-size=")
(?/ "Exclude depot path" "-/")))
(defvar magit-p4-sync-clone-shared-switches
'((?d "Detect branches" "--detect-branches")
(?l "Query p4 for labels" "--detect-labels")
(?b "Import labels" "--import-labels")
(?i "Import into refs/heads/ , not refs/remotes" "--import-local")
(?p "Keep entire BRANCH/DIR/SUBDIR prefix during import" "--keep-path")
(?s "Only sync files that are included in the p4 Client Spec" "--use-client-spec")))
(magit-define-popup magit-p4-sync-popup
"Pull changes from p4"
'magit-commands
:options magit-p4-sync-clone-shared-options
:switches magit-p4-sync-clone-shared-switches
:actions '((?s "Sync" magit-p4-sync)))
(magit-define-popup magit-p4-submit-popup
"Submit changes from git to p4"
:switches '((?M "Detect renames" "-M")
(?v "Be more verbose" "--verbose")
(?u "Preserve user" "--preserve-user")
(?l "Export labels" "--export-labels")
(?n "Dry run" "--dry-run")
(?p "Prepare P4 only" "--prepare-p4-only"))
:options '((?o "Origin" "--origin=" magit-read-branch-or-commit)
(?b "Sync with branch after submission" "--branch=" magit-read-branch)
(?N "Name of git branch to submit" " " magit-read-branch-or-commit)
(?c "Conflict resolution (ask|skip|quit)" "--conflict="
(lambda (prompt &optional default)
(magit-completing-read prompt '("ask" "skip" "quit") nil nil nil nil default))))
:actions '((?s "Submit all" magit-p4-submit)))
(magit-define-popup magit-p4-clone-popup
"Clone repository from p4"
:switches (append '((?b "Bare clone" "--bare"))
magit-p4-sync-clone-shared-switches)
:options (append '((?d "Destination directory" "--destination=" read-directory-name))
magit-p4-sync-clone-shared-options)
:actions '((?c "Clone" magit-p4-clone)))
(magit-define-popup-action 'magit-dispatch-popup ?4 "Git P4" 'magit-p4-popup ?!)
(defun magit-p4/insert-job (&optional job)
"Insert JOB reference in a buffer.
The insertion assumes that it should be 'Jobs:' entry in the buffer.
If not - it inserts such at the current point of the buffer. Then it asks (if
applied interactively) for a job id using `p4` completion function.
Finally it inserts the id under `Jobs:` entry."
(interactive
(list (p4-read-arg-string "Job: " "" 'job)))
(when job
(let* ((jobs-entry (save-excursion (re-search-backward "^Jobs:" nil t)))
(jobs-entry (if jobs-entry jobs-entry (re-search-forward "^Jobs:" nil t))))
(if (not jobs-entry)
;; it inserts "Jobs:" entry in the CURRENT point!
(insert "\nJobs:\n\t")
;; move to past the end of `Jobs:` entry
(progn
(goto-char jobs-entry)
(end-of-line)
(insert "\n\t")))
(insert job))))
(defvar magit-p4-mode-map
"Minor P4 mode key map.
So far used in submit log edit buufer."
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-x j") 'magit-p4/insert-job)
map))
;;; Mode
;;;###autoload
(define-minor-mode magit-p4-mode
"P4 support for Magit"
:lighter " P4"
:require 'magit-p4
:keymap 'magit-p4-mode-map
(or (derived-mode-p 'magit-mode)
(user-error "This mode only makes sense with magit"))
(when (called-interactively-p 'any)
(magit-refresh)))
(provide 'magit-p4)
;; Local Variables:
;; indent-tabs-mode: nil
;; End:
;;; magit-p4.el ends here