git.el: Improve error handling for commits.
Display all errors happening in the various subcommands of the commit sequence, and abort on any error. Signed-off-by: Alexandre Julliard <julliard@winehq.org>
Alexandre Julliard committed
Nov 1, 2008 at 20:34 UTC
36d2078ff19c849aef94f045bf2b4fb73a5098e8
1 file changed
+29
-17
contrib/emacs/git.el
+29
-17
@@ -208,6 +208,15 @@ and returns the process output as a string, or nil if the git failed."
208
(and (eq 0 (apply #' git-call-process-env t env args))
209
(buffer-string))))
210
211
+(defun git-call-process-string-display-error (&rest args)
212
+ "Wrapper for call-process that displays error message and returns
213
+the process output as a string, or nil if the git command failed."
214
+ (with-temp-buffer
215
+ (if (eq 0 (apply #'git-call-process-env (list t t) nil args))
216
+ (buffer-string)
217
+ (display-message-or-buffer (current-buffer))
218
+ nil)))
219
+
220
(defun git-run-process-region (buffer start end program args)
221
"Run a git process with a buffer region as input."
222
(let ((output-buffer (current-buffer))
@@ -391,14 +400,16 @@ and returns the process output as a string, or nil if the git failed."
400
401
(defun git-read-tree (tree &optional index-file)
402
"Read a tree into the index file."
394
- (apply #'git-call-process-env nil
395
- (if index-file `(("GIT_INDEX_FILE" . ,index-file)) nil)
396
- "read-tree" (if tree (list tree))))
403
+ (let ((process-environment
404
+ (append (and index-file (list (concat "GIT_INDEX_FILE=" index-file))) process-environment)))
405
+ (apply 'git-call-process-display-error "read-tree" (if tree (list tree)))))
406
407
(defun git-write-tree (&optional index-file)
408
"Call git-write-tree and return the resulting tree SHA1 as a string."
400
- (git-get-string-sha1
401
- (git-call-process-env-string (and index-file `(("GIT_INDEX_FILE" . ,index-file))) "write-tree")))
409
+ (let ((process-environment
410
+ (append (and index-file (list (concat "GIT_INDEX_FILE=" index-file))) process-environment)))
411
+ (git-get-string-sha1
412
+ (git-call-process-string-display-error "write-tree"))))
413
414
(defun git-commit-tree (buffer tree head)
415
"Call git-commit-tree with buffer as input and return the resulting commit SHA1."
@@ -824,19 +835,18 @@ Return the list of files that haven't been handled."
835
836
(defun git-update-index (index-file files)
837
"Run git-update-index on a list of files."
827
- (let ((env (and index-file `(("GIT_INDEX_FILE" . ,index-file))))
838
+ (let ((process-environment (append (and index-file (list (concat "GIT_INDEX_FILE=" index-file)))
839
+ process-environment))
840
added deleted modified)
841
(dolist (info files)
842
(case (git-fileinfo->state info)
843
('added (push info added))
844
('deleted (push info deleted))
845
('modified (push info modified))))
834
- (when added
835
- (apply #'git-call-process-env nil env "update-index" "--add" "--" (git-get-filenames added)))
836
- (when deleted
837
- (apply #'git-call-process-env nil env "update-index" "--remove" "--" (git-get-filenames deleted)))
838
- (when modified
839
- (apply #'git-call-process-env nil env "update-index" "--" (git-get-filenames modified)))))
846
+ (and
847
+ (or (not added) (apply #'git-call-process-display-error "update-index" "--add" "--" (git-get-filenames added)))
848
+ (or (not deleted) (apply #'git-call-process-display-error "update-index" "--remove" "--" (git-get-filenames deleted)))
849
+ (or (not modified) (apply #'git-call-process-display-error "update-index" "--" (git-get-filenames modified))))))
850
851
(defun git-run-pre-commit-hook ()
852
"Run the pre-commit hook if any."
@@ -862,17 +872,19 @@ Return the list of files that haven't been handled."
872
(message "You cannot commit unmerged files, resolve them first.")
873
(unwind-protect
874
(let ((files (git-marked-files-state 'added 'deleted 'modified))
865
- head head-tree)
875
+ head tree head-tree)
876
(unless (git-empty-db-p)
877
(setq head (git-rev-parse "HEAD")
878
head-tree (git-rev-parse "HEAD^{tree}")))
879
(if files
880
(progn
881
(message "Running git commit...")
872
- (git-read-tree head-tree index-file)
873
- (git-update-index nil files) ;update both the default index
874
- (git-update-index index-file files) ;and the temporary one
875
- (let ((tree (git-write-tree index-file)))
882
+ (when
883
+ (and
884
+ (git-read-tree head-tree index-file)
885
+ (git-update-index nil files) ;update both the default index
886
+ (git-update-index index-file files) ;and the temporary one
887
+ (setq tree (git-write-tree index-file)))
888
(if (or (not (string-equal tree head-tree))
889
(yes-or-no-p "The tree was not modified, do you really want to perform an empty commit? "))
890
(let ((commit (git-commit-tree buffer tree head)))