From 83e0421a9688428aef14b32a5f2f2b573c7d3005 Mon Sep 17 00:00:00 2001 From: David Caldwell Date: Sun, 21 Mar 2010 04:48:07 -0700 Subject: [PATCH] [commit-patch-buffer.el] Add function that autodetects the appropriate directory to commit from. This takes commit-patch down to zero arguments! --- commit-patch-buffer.el | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/commit-patch-buffer.el b/commit-patch-buffer.el index 81ca68a..cb651fc 100644 --- a/commit-patch-buffer.el +++ b/commit-patch-buffer.el @@ -60,6 +60,34 @@ (interactive "DDirectory: ") (commit-patch-buffer (buffer-name) directory)) +(defun commit-patch-current-buffer-autodetect-directory () + "Commit the patch in the current buffer applying it to the appropriate directory." + (interactive) + (commit-patch-current-buffer (autodetect-patch-directory-root))) + +(defun autodetect-patch-directory-root () + "Tries to autodect where a patch should be committed from using the +following algorithm: + + 1. Grab the path mentioned in the first diff hunk of the + current buffer and its buffer's full path. + + 2. Strip common files/directories from end of paths. + + 3. Return whatever is left over of buffer's path." + (save-excursion + (beginning-of-buffer) + (diff-hunk-next) ;; Have to be in a hunk or diff-hunk-file-names won't work. + (let ((diff-path (reverse (split-string (car (diff-hunk-file-names)) "/"))) + (file-path (reverse (split-string (buffer-file-name (car (diff-find-source-location))) "/")))) + (while (string-equal (car file-path) (car diff-path)) + (setq file-path (cdr file-path)) + (setq diff-path (cdr diff-path))) + ;; The extra "" here adds a / onto the end of our directory. Otherwise call-process (via process-file) + ;; calls unhandled-file-name-directory which strips the last part of the path off if it doesn't end with + ;; a /. Yes, this took multiple hours to figure out. + (combine-and-quote-strings (reverse (cons "" file-path)) "/")))) + (eval-after-load 'diff-mode '(progn (setq diff-default-read-only nil) (define-key diff-mode-map "\C-c\C-c" 'commit-patch-buffer))) -- 2.51.2