git.el: Update a file status in the git buffer upon save.
Signed-off-by: Alexandre Julliard <julliard@winehq.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Alexandre Julliard committed
Sep 29, 2007 at 11:59 UTC
0365d885ad17031de27440fec3553675d02aa4c3
1 file changed
+16
-2
contrib/emacs/git.el
+16
-2
@@ -36,7 +36,6 @@
36
;; TODO
37
;; - portability to XEmacs
38
;; - better handling of subprocess errors
39
-;; - hook into file save (after-save-hook)
39
;; - diff against other branch
40
;; - renaming files from the status buffer
41
;; - creating tags
@@ -1352,9 +1351,24 @@ Commands:
1351
(cd dir)
1352
(git-status-mode)
1353
(git-refresh-status)
1355
- (goto-char (point-min)))
1354
+ (goto-char (point-min))
1355
+ (add-hook 'after-save-hook 'git-update-saved-file))
1356
(message "%s is not a git working tree." dir)))
1357
1358
+(defun git-update-saved-file ()
1359
+ "Update the corresponding git-status buffer when a file is saved.
1360
+Meant to be used in `after-save-hook'."
1361
+ (let* ((file (expand-file-name buffer-file-name))
1362
+ (dir (condition-case nil (git-get-top-dir (file-name-directory file))))
1363
+ (buffer (and dir (git-find-status-buffer dir))))
1364
+ (when buffer
1365
+ (with-current-buffer buffer
1366
+ (let ((filename (file-relative-name file dir)))
1367
+ ; skip files located inside the .git directory
1368
+ (unless (string-match "^\\.git/" filename)
1369
+ (git-call-process-env nil nil "add" "--refresh" "--" filename)
1370
+ (git-update-status-files (list filename) 'uptodate)))))))
1371
+
1372
(defun git-help ()
1373
"Display help for Git mode."
1374
(interactive)