emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: On the adoption of transient.el


From: Alfred M. Szmidt
Subject: Re: On the adoption of transient.el
Date: Sat, 07 Aug 2021 21:42:33 -0400

   Currently I'm finishing implementation of a new vc command that
   will allow committing from a diff buffer.  

This sounds similar to my diff-commit-hunk.el hack; but it is entierly
VC agonstic.

   So for example, after
   displaying all changes with e.g. 'C-x v D' you can remove
   some unneeded hunks (with e.g. 'M-k' 'diff-hunk-kill'),
   then on the remaining hunks type 'C-c C-c' in the diff buffer.
   It will create a usual *vc-log* buffer where typing 'C-c C-c'
   will commit only changes from the diff buffer.  Internally
   it works by running three git commands:

   `git stash push -m stash_name -- list_of_file_names_from_diff`
   `cat diff_buffer.patch | git am` # should also handle return code
   `git stash pop -q`

===File ~/diff-commit-hunk.el===============================
;;;; diff-commit-hunk.el --- commit parts of a hunk in `diff-mode'

(require 'diff-mode)

(defun current-buffer-file-name ()
  (buffer-file-name (current-buffer)))

(defun restore-source-file ()
  (with-current-buffer (current-buffer)
    (erase-buffer)
    (insert-buffer "*diff-commit-hunk*")
    (write-file (current-buffer-file-name)))
  (remove-hook 'vc-checkin-hook 'restore-source-file))

(defmacro with-original-file (&rest body)
  "Reverts associated source file temporarily in a `diff-mode'
buffer to the latest found in VCS, executes BODY and commits the
changes back VCS."
  `(progn
     (save-excursion
       (diff-goto-source)
       (let ((buf (current-buffer)))
         (with-current-buffer (get-buffer-create "*diff-commit-hunk*")
           (erase-buffer)
           (insert-buffer buf)))
       (vc-revert-file (current-buffer-file-name)))
     ,@body
     (save-excursion
       (diff-goto-source)
       (write-file (current-buffer-file-name))
       (add-hook 'vc-checkin-hook 'restore-source-file)
       (vc-checkin (list (current-buffer-file-name))
                   (vc-backend (current-buffer-file-name))))))

;;;###autoload
(defun diff-commit-hunk ()
  "Revert associated source file to the latest version from VCS,
and apply (and commit) current hunk."
  (interactive)
  (with-original-file
   (let ((diff-advance-after-apply-hunk nil))
     (diff-apply-hunk))))

;;;###autoload
(defun diff-commit-all ()
  "Like `diff-commit-hunk', but applies all hunks in the current
diff buffer."
  (interactive)
  (with-original-file
   (goto-char (point-min))
   (diff-hunk-next)                     ;Skip header.
   (while (not (eobp))
     (diff-apply-hunk))))

(define-key diff-mode-map (kbd "s-a") #'diff-commit-hunk)
(define-key diff-mode-map (kbd "H-a") #'diff-commit-all)

;;;; diff-commit-hunk.el ends here.
============================================================



reply via email to

[Prev in Thread] Current Thread [Next in Thread]