@matheusbsbs / matheus-bagliano / commits / 9de83169d3
Fork 0

git.el: More robust handling of subprocess errors when returning strings.

Make sure that functions that call a git process and return a string always return nil when the subprocess failed. Signed-off-by: Alexandre Julliard <julliard@winehq.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Alexandre Julliard committed Mar 19, 2006 at 10:05 UTC 9de83169d34825d617b7fe92b95c966d9910848d
1 file changed +13 -16
contrib/emacs/git.el
+13 -16
@@ -148,6 +148,12 @@ The default is to fall back to `add-log-mailing-address' and then `user-mail-add
148 (append (git-get-env-strings env) (list "git") args))
149 (apply #'call-process "git" nil buffer nil args)))
150
151 +(defun git-call-process-env-string (env &rest args)
152 + "Wrapper for call-process that sets environment strings, and returns the process output as a string."
153 + (with-temp-buffer
154 + (and (eq 0 (apply #' git-call-process-env t env args))
155 + (buffer-string))))
156 +
157 (defun git-run-process-region (buffer start end program args)
158 "Run a git process with a buffer region as input."
159 (let ((output-buffer (current-buffer))
@@ -189,8 +195,9 @@ The default is to fall back to `add-log-mailing-address' and then `user-mail-add
195
196 (defun git-get-string-sha1 (string)
197 "Read a SHA1 from the specified string."
192 - (let ((pos (string-match "[0-9a-f]\\{40\\}" string)))
193 - (and pos (substring string pos (match-end 0)))))
198 + (and string
199 + (string-match "[0-9a-f]\\{40\\}" string)
200 + (match-string 0 string)))
201
202 (defun git-get-committer-name ()
203 "Return the name to use as GIT_COMMITTER_NAME."
@@ -259,18 +266,12 @@ The default is to fall back to `add-log-mailing-address' and then `user-mail-add
266 (defun git-rev-parse (rev)
267 "Parse a revision name and return its SHA1."
268 (git-get-string-sha1
262 - (with-output-to-string
263 - (with-current-buffer standard-output
264 - (git-call-process-env t nil "rev-parse" rev)))))
269 + (git-call-process-env-string nil "rev-parse" rev)))
270
271 (defun git-symbolic-ref (ref)
272 "Wrapper for the git-symbolic-ref command."
268 - (car
269 - (split-string
270 - (with-output-to-string
271 - (with-current-buffer standard-output
272 - (git-call-process-env t nil "symbolic-ref" ref)))
273 - "\n")))
273 + (let ((str (git-call-process-env-string nil "symbolic-ref" ref)))
274 + (and str (car (split-string str "\n")))))
275
276 (defun git-update-ref (ref val &optional oldval)
277 "Update a reference by calling git-update-ref."
@@ -285,11 +286,7 @@ The default is to fall back to `add-log-mailing-address' and then `user-mail-add
286 (defun git-write-tree (&optional index-file)
287 "Call git-write-tree and return the resulting tree SHA1 as a string."
288 (git-get-string-sha1
288 - (with-output-to-string
289 - (with-current-buffer standard-output
290 - (git-call-process-env t
291 - (if index-file `(("GIT_INDEX_FILE" . ,index-file)) nil)
292 - "write-tree")))))
289 + (git-call-process-env-string (and index-file `(("GIT_INDEX_FILE" . ,index-file))) "write-tree")))
290
291 (defun git-commit-tree (buffer tree head)
292 "Call git-commit-tree with buffer as input and return the resulting commit SHA1."