summaryrefslogtreecommitdiff
path: root/.config
diff options
context:
space:
mode:
authorThanos Apollo <[email protected]>2024-11-06 06:11:11 +0200
committerThanos Apollo <[email protected]>2024-11-06 06:11:11 +0200
commitb2405105d4c9f7416870634521aa556a3f1ed252 (patch)
tree9ccf2fb183ffd4e1ee20b8b21dd889b51f576150 /.config
parent2d9f9af4df6b5793ac32755973dbfd1b908acd37 (diff)
emacs: Add vc configuration.
Diffstat (limited to '.config')
-rw-r--r--.config/emacs/init.el95
1 files changed, 95 insertions, 0 deletions
diff --git a/.config/emacs/init.el b/.config/emacs/init.el
index 8a017bd..35331ae 100644
--- a/.config/emacs/init.el
+++ b/.config/emacs/init.el
@@ -657,6 +657,101 @@ rss.xml" anna piracy)
'(("TODO" . "#cc9393")
("FIXME" . "#FF0000"))))
+(use-package vc
+ :ensure t
+ :config
+ (defun vc-git--commit-hash-at-point ()
+ "Return commit hash at point."
+ (let ((hash (thing-at-point 'word t)))
+ (and (stringp hash)
+ (string-match-p "^[0-9a-f]\\{7,40\\}$" hash)
+ hash)))
+
+ (defun vc-git-reset-to-commit-at-point (&optional hash)
+ "Reset the current branch to the commit at point in the vc log buffer."
+ (let ((commit-hash (or (thing-at-point 'word t) hash)))
+ (vc-git--reset commit-hash)))
+
+ (defun vc-git--reset (commit-hash)
+ "Reset to commit HASH."
+ (if (and commit-hash (string-match-p "^[0-9a-f]\\{7,40\\}$" commit-hash))
+ (if (yes-or-no-p (format "Reset current branch to commit %s?" commit-hash))
+ (progn
+ (let ((default-directory (vc-root-dir)))
+ (vc-git-command nil 0 nil "reset" "--hard" commit-hash)
+ (message "Reset to commit %s completed." commit-hash)))
+ (message "Reset cancelled."))
+ "Invalid commit hash"))
+
+ (defun vc-git--select-commit ()
+ (let* ((history-add-new-input nil)
+ (commit-strings (split-string (shell-command-to-string "git log --oneline") "\n" t))
+ (selected (completing-read "Select commit: " commit-strings nil t))
+ (selected-hash (car (split-string selected))))
+ selected-hash))
+
+ (defun vc-git-reset (&optional hash)
+ (interactive)
+ (vc-git--reset (or hash
+ (vc-git--commit-hash-at-point)
+ (vc-git--select-commit))))
+
+ (defun vc-git-get-commit-hash-at-point ()
+ "Get the commit hash at point in the vc log buffer."
+ (let ((commit-hash (thing-at-point 'word t)))
+ (if (and commit-hash (string-match-p "^[0-9a-f]\\{7,40\\}$" commit-hash))
+ commit-hash
+ (error "No valid commit hash found at point"))))
+
+ (defun vc-git-get-commit-message (commit-hash)
+ "Retrieve the commit message for COMMIT-HASH."
+ (with-temp-buffer
+ (if (zerop (vc-git-command t 0 nil "log" "--format=%B" "-n" "1" commit-hash))
+ (buffer-string)
+ (error "Failed to retrieve commit message for %s" commit-hash))))
+
+ (defun vc-git-setup-log-edit-buffer (commit-message)
+ "Set up a log edit buffer with COMMIT-MESSAGE."
+ (let ((log-edit-buffer (get-buffer-create "*VC Reword Commit*")))
+ (with-current-buffer log-edit-buffer
+ (erase-buffer)
+ (insert commit-message)
+ (vc-git-log-edit-mode))
+ log-edit-buffer))
+
+ (defun vc-git-save-and-amend-commit (commit-hash)
+ "Save the commit message from the current buffer and amend COMMIT-HASH."
+ (let ((commit-msg-file (make-temp-file "git-commit-msg")))
+ (write-region (point-min) (point-max) commit-msg-file)
+ (vc-git-command nil 0 nil "commit" "--amend" "--file" commit-msg-file)
+ (delete-file commit-msg-file)
+ (message "Commit message reworded.")))
+ ;; TODO: Fix this!
+ (defun vc-git-reword-commit-at-point ()
+ "Reword the commit message of the commit at point in the vc log buffer."
+ (interactive)
+ (let* ((commit-hash (vc-git-get-commit-hash-at-point))
+ (commit-message (vc-git-get-commit-message commit-hash))
+ (log-edit-buffer (vc-git-setup-log-edit-buffer commit-message)))
+ ;; Bind C-c C-c to save changes and amend the commit
+ (with-current-buffer log-edit-buffer
+ (setq-local vc-log-operation 'commit)
+ (setq-local vc-log-after-commit-function
+ `(lambda (&rest _args)
+ (vc-git-save-and-amend-commit ,commit-hash)
+ (kill-buffer ,log-edit-buffer)))
+ (local-set-key (kbd "C-c C-c")
+ (lambda ()
+ (interactive)
+ (run-hooks 'vc-log-after-commit-function)))
+ (pop-to-buffer log-edit-buffer))))
+
+ (defun vc-git-format-patches (num)
+ "Format patches for NUM of last commits"
+ (interactive (list (read-number "Number of commits: ")))
+ (vc-git-command nil 0 nil "format-patch" (format "-%d" num))
+ (message "Done.")))
+
(use-package corfu
:ensure t
:config