patcher / lisp / patcher-instance.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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
;;; patcher-instance.el --- Process instantiation

;; Copyright (C) 2008, 2009, 2010, 2011 Didier Verna
;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007 Didier Verna

;; Author:        Didier Verna <didier@xemacs.org>
;; Maintainer:    Didier Verna <didier@xemacs.org>
;; Created:       Sat Feb 13 22:43:33 2010
;; Last Revision: Fri Dec  9 13:22:42 2011
;; Keywords:      maint


;; This file is part of Patcher.

;; Patcher is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License version 3,
;; as published by the Free Software Foundation.

;; Patcher 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 this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


;;; Commentary:

;; Contents management by FCM version 0.1.


;;; Code:

(require 'cl)

(eval-when-compile (require 'patcher-cutil))
(require 'patcher-util)
(require 'patcher-project)



;; ==========================================================================
;; Utilities
;; ==========================================================================

(patcher-define-error 'status
  "Patcher status error.")




;; ==========================================================================
;; Project instantiation
;; ==========================================================================

(defstruct (patcher-project (:constructor patcher-make-project))
  name
  subject
  committed-p
  window-configuration
  mail-buffer
  process-buffer
  cmtcmd-buffer
  logmsg-buffer
  ;; The name of the temporary file in which to store the log message. The
  ;; reason why the logmsg-buffer does not visit this file (hence the need for
  ;; this variable) is that we want the logmsg buffer's name to be readable,
  ;; and the user doesn't care about the name of the temporary file.
  logmsg-file-name
  ;; The diff command to use. This string is not supposed to include the files
  ;; to which the command applies. Only the command itself. This variable is
  ;; needed because the user has the ability to override the project's diff
  ;; command by giving a prefix to `patcher-generate-diff'.
  diff-command
  ;; The commit command to use. This variable is needed because the user has
  ;; the ability to override the project's commit command by giving a prefix
  ;; to `patcher-logmsg-commit'.
  commit-command
  ;; The project's directory. This variable is needed because a temporary
  ;; subproject may modify the original project's value for it.
  directory
  ;; The project's command directory. This variable is needed for supporting
  ;; relocatable projects.
  command-directory
  ;; List of files/directories command-line specification for the diff
  ;; command. This variable is needed because a temporary subproject may
  ;; modify the original project's value for it.
  specification
  ;; List of ChangeLog absolute file names.
  change-logs
  ;; List of source absolute file names.
  sources)


;; Accessors ================================================================

(put 'patcher-project-option 'lisp-indent-function 1)
(defun* patcher-project-option
    (project option &optional non-nil
		    &aux (name (patcher-project-name project))
			 (opt (patcher-descriptor-option
			       (patcher-project-descriptor name) option 0)))
  ;; Return the value of OPTION in PROJECT, or fall back to the value of
  ;; `patcher-default-OPTION'. If NON-NIL, check that the retreived value is
  ;; not null.
  (if opt
      (cadr opt)
    (or (symbol-value (intern-soft (concat "patcher-default-"
					   (substring
					    (symbol-name option) 1))))
	(when non-nil
	  (patcher-error "Project %s: option %s is null" name option)))))

(defun patcher-project-command-name (project)
  ;; Return the value of the :name option in PROJECT, or PROJECT's name.
  (or (patcher-project-option project :name)
      (patcher-project-name project)))

(defun patcher-project-files (project)
  ;; Return the sorted list of PROJECT's files.
  ;; The files are returned relative to PROJECT's command directory.
  ;; Files include both sources and ChangeLogs, unless ChangeLogs are
  ;; ephemeral in PROJECT.
  (let ((dir (patcher-project-command-directory project)))
    (patcher-sort-files
     (mapcar (lambda (file)
	       (patcher-file-relative-name file dir))
	     (if (eq (patcher-project-option project :change-logs-status)
		     'ephemeral)
		 (patcher-project-sources project)
		 (append (patcher-project-change-logs project)
			 (patcher-project-sources project)))))))

(defun patcher-project-sources-string (project)
  ;; Return a string of all PROJECT's source files.
  (patcher-files-string (patcher-project-sources project)))

(defun patcher-project-change-logs-string (project)
  ;; Return a string of all PROJECT's ChangeLog files.
  (patcher-files-string (patcher-project-change-logs project)))


;; Instantiation ============================================================

;; #### NOTE: this variable is set in every auxiliary buffer (see below).
;; Technically, it is not required in process buffers; only in buffers where
;; interactive functions might be called without a project as argument. This
;; variable is not set in reference buffers (see also below) because they
;; might be shared by different instances of patcher. In ChangeLog files for
;; instance, we have extents providing the necessary information about every
;; entry instead.
(make-variable-buffer-local
 (defvar patcher-project nil
   ;; Instance of the PATCHER-PROJECT structure describing the current
   ;; project.
   ))

(defvar patcher-subject-history nil)

(defun* patcher-prompt-subject
    (project
     &aux (subject (read-string "Subject: "
		     (let ((s (patcher-project-option project :subject)))
		       (unless (zerop (length s))
			 (patcher-substitute-name project s)))
		     'patcher-subject-history)))
  ;; Prompt for, and return a subject for PROJECT.
  ;; Also create the 'patcher-subject extent in the string.
  (set-extent-properties (make-extent 0 (length subject) subject)
			 '(duplicable t
			   start-open t
			   patcher-subject t))
  subject)

(defvar patcher-directory-history nil)


;; #### NOTE: supporting subprojects is in fact rather complicated because of
;; the various ways the supported RCSes behave. The situation is as follows:

;; 1/ At first glance, it may seem simpler to always execute commands from the
;; (super)project's base directory, turning a :subdirectory into a
;; specification. However, this would make things difficult for the user of a
;; Mercurial repository with plenty of submodules (e.g. the XEmacs packages).
;; Indeed, it would then be necessary to define one distinct project for every
;; submodule because the commands need to be executed locally in every
;; submodule. Patcher could perhaps support this by parsing .hgsub and
;; dynamically create all subprojects as regular projects but we're not quite
;; there yet (this brings the more general question of submodules that exist
;; in various forms in different RCSes). So at least for the time being, we
;; choose to work directly in the specified subdirectory, if any. This,
;; however, causes additional problems.

;; 2/ PRCS can't work in subdirectories. Only from the toplevel. Hence the
;; :command-directory option which cancels the above policy.

;; 3/ Some RCSes like CVS output diff paths relative to the current directory
;; by default. Some others like Git don't (Git needs --relative, so we need
;; to be careful in themes). In Mercurial, there doesn't seem to be a way to
;; get relative paths and that is currently a problem.

;; 4/ Then, there is the commit command problem. While Git diff --relative
;; restricts to the current directory, there's no corresponding behavior for
;; the commit command. For instance, if you git commit -a from a subdirctory,
;; even the changes outside the current directory will be committed. Because
;; of that, we can't work with empty specifications in subdirectories (we need
;; at least to use ".").

;; All in all, this is why the function below is such a mess.

(defun* patcher-prompt-project
    (relocate override
     &aux (project (patcher-make-project :name (patcher-prompt-name)))
	  (base-directory
	   (patcher-descriptor-directory
	    (patcher-project-descriptor (patcher-project-name project))))
	  (subdirectory (patcher-project-option project :subdirectory))
	  (command-directory
	   (patcher-project-option project :command-directory))
	  (files (patcher-project-option project :files))
	  default-working-directory working-directory)
  ;; Create and return a new project by prompting for it.
  ;; Always prompt for the project's name and subject.
  ;; When RELOCATE, offer to use an alternate directory.
  ;; When OVERRIDE, offer to work on a temporary subproject.
  ;; Note that it is possible to both relocate and  override.

  ;; Initialize non problematic slots.
  (setf (patcher-project-subject project) (patcher-prompt-subject project))
  (setf (patcher-project-diff-command project)
	(patcher-project-option project :diff-command t))
  (setf (patcher-project-commit-command project)
	(patcher-project-option project :commit-command t))

  ;; Handle relocation. SUBDIRECTORY, COMMAND-DIRECTORY and FILES are all
  ;; supposed to be relative paths so only BASE-DIRECTORY needs to be modified
  ;; at that point.
  (when (or relocate (null base-directory))
    (setq base-directory
	  (read-directory-name
	   (if subdirectory
	       (format (if base-directory
			   "Relocate %s under: "
			 "%s location: ")
		   (file-name-as-directory subdirectory))
	     (if base-directory "Relocate to: " "Project location: "))
	   ;; #### NOTE: I think it's nice to start from ../ because many
	   ;; people keep clones of the same project under one directory. It
	   ;; might however not be so intuitive that if you don't type
	   ;; anything, you will get the original directory, not the one
	   ;; that's printed in the minibuffer.
	   (when base-directory
	     (file-name-as-directory (expand-file-name ".." base-directory)))
	   base-directory t nil patcher-directory-history))
    ;; Check that the relocation is valid: we need both a valid subdirectory
    ;; and command (super) directory.
    (when subdirectory
      (unless (file-exists-p (expand-file-name subdirectory base-directory))
	(patcher-error "Unable to relocate subdirectory %s under %s"
		       subdirectory base-directory)))
    (when command-directory
      (unless (file-exists-p (expand-file-name command-directory
					       base-directory))
	(patcher-error "Unable to relocate command directory %s under %s"
		       command-directory base-directory))))

  ;; Compute the default working directory (that is, before a potential
  ;; override) and the actual one which may be overridden.
  (setq default-working-directory
	(directory-file-name
	 (if subdirectory
	     (expand-file-name subdirectory base-directory)
	   base-directory)))
  (setq working-directory
	(if override
	    (directory-file-name
	     (read-directory-name "Subdirectory: "
				  (file-name-as-directory
				   default-working-directory)
				  default-working-directory
				  t))
	  default-working-directory))
  (setf (patcher-project-directory project) working-directory)

  ;; #### WARNING: this filtering is very complicated to do because of the
  ;; possibility to use wildcards. The code below doesn't really work so for
  ;; the time being, I'm backing this out and just cancelling the :files
  ;; preselection.

;;;  ;; If we have overridden the working directory, we need to filter out the
;;;  ;; :files that do not belong to that new subdirectory.
;;;  (unless (string= default-working-directory working-directory)
;;;    ;; #### FIXME: this will break if someone dares to define a subproject
;;;    ;; with files starting with ../ and other clever tricks. Let's just hope
;;;    ;; nobody does that.
;;;    (setq files (loop with subdir = (directory-file-name
;;;				     (patcher-file-relative-name
;;;					     working-directory
;;;					     default-working-directory))
;;;		      with length = (length subdir)
;;;		      for file in files
;;;		      if (string= subdir (directory-file-name file))
;;;			collect "*" and collect ".*"
;;;		      else
;;;			when (and (> (length file) (1+ length))
;;;				  (string= subdir
;;;					   (substring file 0 length))
;;;				  (char= ?/ (aref file length)))
;;;			  collect (substring file (1+ length)))))

  (unless (string= default-working-directory working-directory)
    (setq files nil))

  ;; Handle a request for overriding the :files.
  (when override
    (setq files
	  ;; #### FIXME: this will break when a filename contains spaces!
	  (split-string
	   (let ((default-directory
		  (file-name-as-directory working-directory)))
	     (or (when files
		   (let ((files-string (mapconcat #'identity files " ")))
		     (read-shell-command "Files: "
					 (concat files-string " "))))
		 (let* ((default-file (when (buffer-file-name)
					(patcher-file-relative-name
					 (buffer-file-name)
					 working-directory)))
			(default-file
			 ;; If the file is not actually underneath the
			 ;; project, then don't suggest it as a possibility.
			 (when default-file
			   (if (string-match "^\\.\\.$\\|^\\.\\.[/\\]"
					     default-file)
			       nil
			     default-file))))
		   (read-shell-command "Files: " default-file)))))))

  ;; If we are working in a subdirectory with no specification (no :files
  ;; option provided), then we need to make an explicit specification of ".".
  ;; See point #4 in the comment above this function.
  (when (and (or (not (string= default-working-directory working-directory))
		 subdirectory)
	     (not files))
    (setq files (list ".")))

  ;; Initialize the project's command directory and files specification.
  ;; When a command directory is given, we further need to relativize all the
  ;; :files to this directory.
  (cond (command-directory
	 (setq command-directory
	       (directory-file-name
		(expand-file-name command-directory base-directory)))
	 (setf (patcher-project-command-directory project) command-directory)
	 (setf (patcher-project-specification project)
	       (if files
		   (mapcar
		    (lambda (file)
		      (patcher-file-relative-name
		       (expand-file-name file working-directory)
		       command-directory))
		    files)
		 (unless (string= command-directory working-directory)
		   (list (patcher-file-relative-name
			  working-directory command-directory))))))
	(t
	 (setf (patcher-project-command-directory project) working-directory)
	 (setf (patcher-project-specification project) files)))

  ;; Finally, initialize the process buffer. This needs to wait until the
  ;; command directory is properly initialized.
  ;; unclean forward reference
  (patcher-setup-auxiliary-buffer project
    (setf (patcher-project-process-buffer project)
	  (generate-new-buffer " *Patcher Process*")))

  project)

(defun patcher-project-interactive (prefix)
  ;; Create and return a new project based on PREFIX requests in a list.
  ;; See `patcher-mail' for the semantics of PREFIX.
  (list (patcher-prompt-project (member prefix '(1 -1))
				(and prefix (not (eq prefix 1))))))





;; ==========================================================================
;; Project-related utilities
;; ==========================================================================

(put 'patcher-substitute-name 'lisp-indent-function 1)
(defun* patcher-substitute-name
    (project str &aux (command-name (patcher-project-command-name project))
		      (name (patcher-project-name project)))
  ;; Replace a %n in STR with PROJECT's command name.
  ;; Replace a %N in STR with PROJECT's name.
  (let (case-fold-search)
    (replace-in-string (replace-in-string str "%N" name) "%n" command-name)))

(defun* patcher-prefixed-subject
    (project &optional old-subject
	     &aux (subject-prefix (patcher-project-option project
				    :subject-prefix))
		  (subject (patcher-project-subject project)))
  ;; Return PROJECT's prefixed subject, possibly adapting OLD-SUBJECT.
  (unless (zerop (length subject-prefix))
    (setq subject-prefix (patcher-substitute-name project subject-prefix))
    (set-extent-properties (make-extent 0 (length subject-prefix)
					subject-prefix)
			   '(duplicable t
			     start-open t
			     patcher-subject-prefix t)))
  (unless (zerop (length old-subject))
    (setq subject
	  (if (zerop (length subject))
	      old-subject
	    (let (case-fold-search)
	      (replace-in-string
	       (replace-in-string
		(patcher-project-option project :subject-rewrite-format)
		"%S" old-subject t)
	       "%s" subject t)) )))
  (concat subject-prefix
	  (unless (or (zerop (length subject-prefix))
		      (zerop (length subject)))
	    " ")
	  subject))

(defun patcher-change-subject (project)
  ;; Read a new subject for PROJECT and propagate it to relevant buffers.
  (setf (patcher-project-subject project)
	(read-string "New subject: " (patcher-project-subject project)))
  (dolist (buffer (list (patcher-project-mail-buffer   project)
			(patcher-project-logmsg-buffer project)))
    (when buffer
      (with-current-buffer buffer
	(patcher-within-extent (extent 'subject)
	  (insert (patcher-project-subject project))
	  (delete-region (point) (extent-end-position extent)))))))

(put 'patcher-command 'lisp-indent-function 1)
(defun patcher-command (project command &optional files)
  ;; Build a Patcher command from COMMAND that applies to FILES.
  ;; This involves %n, %N, %f, %!f and %?f substitutions,
  ;; as well as :pre-command handling.
  ;; Special value t for FILES means use all ChangeLog (unless ephemeral) and
  ;; source files explicitely.
  (when command
    (when (eq files t)
      (setq files (if (patcher-project-specification project)
		      (patcher-project-files project)
		    nil)))
    (setq command (patcher-substitute-name project command))
    (setq command (replace-in-string command "%!f{\\(.*?\\)}"
				     (if files "" "\\1")))
    (setq command (replace-in-string command "%\\?f{\\(.*?\\)}"
				     (if files "\\1" "")))
    (setq command (replace-in-string command "%f"
				     (if files
					 (mapconcat #'identity files " ")
				       "")
				     t))
    (let ((precmd (patcher-project-option project :pre-command)))
      (unless (zerop (length precmd))
	(setq command (concat precmd " " command))))
    (setq command (replace-in-string command "[ \t]+" " " t))
    command))


;; Auxiliary buffers ========================================================

;; Auxiliary buffers are used by Patcher to perform specific tasks. They are
;; not shared across Patcher instances, may be lazily generated and behave
;; like singletons. Patcher process, log message and commit command buffers
;; are auxiliary buffers.

(put 'patcher-setup-auxiliary-buffer 'lisp-indent-function 1)
(defun patcher-setup-auxiliary-buffer (project buffer)
  ;; Setup BUFFER as auxiliary for PROJECT.
  (with-current-buffer buffer
    (setq patcher-project project)
    (cd (patcher-project-command-directory project))))


;; Reference buffers ========================================================

;; Reference buffers are used by Patcher to get project-related information.
;; They may be shared across patcher instances and may exist independently
;; from patcher. Source and ChangeLog buffers are reference buffers.

(make-variable-buffer-local
 (defvar patcher-references nil
   ;; List of Patcher instances referencing this buffer.
   ;; Automatically becomes buffer-local when set.
   ;; - If this list contains the special value t, it means that the buffer
   ;; existed before any instance of Patcher required it, so it will never be
   ;; killed (by Patcher).
   ;; - Otherwise, Patcher may kill this buffer when there are no more
   ;; references to it.
   ))

(put 'patcher-reference-buffer 'lisp-indent-function 1)
(defun patcher-reference-buffer (project buffer)
  ;; Add a reference to PROJECT in BUFFER.
  ;; Return non-nil if the reference didn't exist before.
  (with-current-buffer buffer
    (unless (member* project patcher-references :test #'eq)
      (push project patcher-references))))

(put 'patcher-unreference-buffer 'lisp-indent-function 2)
(defun patcher-unreference-buffer (project buffer &optional kill force-save)
  ;; Remove the reference to PROJECT from BUFFER.
  ;; If KILL and PROJECT was the last reference in BUFFER, authorize Patcher
  ;; to kill BUFFER. If FORCE-SAVE, force saving before killing.
  ;; Return two values: whether PROJECT was the last Patcher reference in
  ;; BUFFER, and whether BUFFER was killed.
  (with-current-buffer buffer
    (assert (member* project patcher-references :test #'eq))
    (setq patcher-references (delete* project patcher-references :test #'eq))
    (let ((lastp (or (null patcher-references)
		     (equal patcher-references '(t))))
	  killp)
      (when (and (null patcher-references) kill)
	(patcher-save-buffer buffer force-save)
	(kill-buffer buffer)
	(setq killp t))
      (values lastp killp))))


;; Project cleanup ==========================================================

(defun patcher-delete-project (project &optional kill-mail-buffer)
  ;; Delete PROJECT and remove all dependencies.
  (declare-fboundp (patcher-unlink-sources     project))
  (declare-fboundp (patcher-unlink-change-logs project))
  (kill-buffer (patcher-project-process-buffer project))
  (when (patcher-project-logmsg-buffer project)
    (kill-buffer (patcher-project-logmsg-buffer project)))
  (when (patcher-project-cmtcmd-buffer project)
    (kill-buffer (patcher-project-cmtcmd-buffer project)))
  (when (patcher-project-mail-buffer project)
    (if kill-mail-buffer
	(kill-buffer (patcher-project-mail-buffer project))
      (with-current-buffer (patcher-project-mail-buffer project)
	(setq patcher-project nil)))))

(defun patcher-kill-project (project)
  ;; Abort PROJECT.
  (when (yes-or-no-p "Really abort the project? ")
    (unless (or (eq (patcher-project-option project :change-logs-status)
		    'ephemeral)
		(patcher-project-committed-p project))
      (with-fboundp '(patcher-generated-change-logs
		      patcher-ungenerate-change-logs)
	(let ((change-log-buffers
	       (patcher-generated-change-logs patcher-project)))
	  (when change-log-buffers
	    (case (patcher-with-message
		      (format "\
Some ChangeLog skeletons for this project have been generated.
The relevant ChangeLog files are the following: %s.

Before killing the project, please answer the question below to
remove all skeletons (y), keep them all (n) or choose interactively (i)."
			  (patcher-buffers-string change-log-buffers))
		    (patcher-read-char "Remove ChangeLog skeletons? " "yni"))
	      (?y (patcher-ungenerate-change-logs patcher-project
		    change-log-buffers))
	      (?i (patcher-ungenerate-change-logs patcher-project
		    change-log-buffers
		    :interactive t
		    :prompt "Remove this skeleton? ")))))))
    (patcher-delete-project project 'kill-mail-buffer)))


;; Project commands ==========================================================

(put 'patcher-call-command 'common-lisp-indent-function 2)
(defun* patcher-call-command
    (project command &key (erase t) progression ignore-exit-status)
  ;; Call COMMAND for PROJECT.
  ;; Output goes to process buffer.
  ;; If ERASE (the default), erase BUFFER first.
  ;; Return two values delimiting the process output's region.
  ;; Throw a PATCHER-PROCESS error for non-zero exit status or a
  ;; failed-command-regexp option match in the process output.
  (with-current-buffer (patcher-project-process-buffer project)
    (when erase
      (erase-buffer))
    (let ((beg (point))
	  (end (patcher-call-process command progression ignore-exit-status))
	  (failed-command-regexp (patcher-project-option project
				   :failed-command-regexp)))
      (when failed-command-regexp
	(patcher-with-progression "Checking command output"
	  (goto-char beg)
	  (when (re-search-forward failed-command-regexp end t)
	    (patcher-error 'process command))))
      (values beg end))))


;; Project commit ===========================================================

(patcher-define-error 'committed
  "Patcher committed error."
  'status)

(defun patcher-detect-committed-project (project)
  ;; Detect an already committed PROJECT.
  ;; Throw a committed error when detected.
  (when (patcher-project-committed-p project)
    (patcher-error 'committed)))


(patcher-define-error 'undiffable
  "Patcher undiffable error."
  'status)

(defun patcher-detect-undiffable-project (project)
  ;; Detect an undiffable PROJECT.
  ;; Throw an undiffable error when detected.
  (when (patcher-project-committed-p project)
    (patcher-error 'undiffable)))


(patcher-define-error 'commit
  "Patcher commit error."
  'process)

;; #### NOTE: this function assumes that PROJECT is committable because it is
;; too low-level to perform the check. Other, upper-level functions will
;; perform the check.
(defun patcher-commit-project (project command)
  ;; Commit PROJECT with commit COMMAND.
  (patcher-condition-case nil
      (patcher-call-command project command
			    :progression "Committing changes")
    (process
     (patcher-error 'commit command)))
  (setf (patcher-project-committed-p project) t)
  ;; Record the successful commit in the mail message.
  (set-window-configuration (patcher-project-window-configuration project))
  (with-current-buffer (patcher-project-mail-buffer project)
    (save-excursion
      ;; Possibly change the subject:
      (let ((subject-committed-prefix
	     (patcher-project-option project :subject-committed-prefix)))
	(when subject-committed-prefix
	  (setq subject-committed-prefix
		(patcher-substitute-name project subject-committed-prefix))
	  (patcher-within-extent (extent 'subject-prefix)
	    (insert subject-committed-prefix)
	    (delete-region (point) (extent-end-position extent)))
	  (goto-char (extent-end-position (patcher-extent 'subject-prefix)))
	  (when (looking-at "\\S-")
	    (insert " "))))
      ;; Insert the `committed' notice:
      (goto-char (point-min))
      (when (re-search-forward
	     (concat "^" (regexp-quote mail-header-separator))
	     nil t)
	(forward-line 1)
	(let ((notice (patcher-project-option patcher-project
			:committed-notice)))
	  (unless (zerop (length notice))
	    (insert notice "\n")))))))


(provide 'patcher-instance)

;;; patcher-instance.el ends here
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.