aboutsummaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
authorJonas Bernoulli <jonas@bernoul.li>2026-01-21 09:17:54 +0100
committerJonas Bernoulli <jonas@bernoul.li>2026-01-21 09:17:54 +0100
commit405042afeb18f07c130c8a4676f3a8152325f80e (patch)
tree9d6a9e6e49c7dae6c930ed477ccf2646e1b5c451 /lisp
parentac898412280198cc1d7a975ffb5b6c13e8fb2d5e (diff)
magit-commit-oid: New function
Replace `magit-rev-hash' and some uses of `magit-commit-p', preparing for `magit-commit-p' to return t instead of the oid.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/magit-diff.el8
-rw-r--r--lisp/magit-ediff.el6
-rw-r--r--lisp/magit-git.el38
-rw-r--r--lisp/magit-sequence.el5
-rw-r--r--lisp/magit-stash.el2
5 files changed, 37 insertions, 22 deletions
diff --git a/lisp/magit-diff.el b/lisp/magit-diff.el
index 6155c79..e6e8859 100644
--- a/lisp/magit-diff.el
+++ b/lisp/magit-diff.el
@@ -1437,8 +1437,8 @@ for a revision."
(when module
(setq default-directory
(expand-file-name (file-name-as-directory module))))
- (unless (magit-commit-p rev)
- (user-error "%s is not a commit" rev))
+ (unless (magit-commit-oid rev t)
+ (user-error "%s cannot be dereferenced as a commit" rev))
(when file
(save-buffer))
(let ((buf (magit-revision-setup-buffer rev args files)))
@@ -1931,7 +1931,7 @@ commit or stash at point, then prompt for a commit."
(setq cmd #'magit-show-commit)
(setq buf (magit-get-mode-buffer 'magit-revision-mode)))
(tag
- (setq rev (magit-rev-hash (oref it value)))
+ (setq rev (magit-commit-oid (oref it value)))
(setq cmd #'magit-show-commit)
(setq buf (magit-get-mode-buffer 'magit-revision-mode)))
(stash
@@ -2760,7 +2760,7 @@ Staging and applying changes is documented in info node
(magit-buffer-diff-files-suspended nil)))
(defun magit-revision-refresh-buffer ()
- (setq magit-buffer-revision-oid (magit-rev-hash magit-buffer-revision))
+ (setq magit-buffer-revision-oid (magit-commit-oid magit-buffer-revision))
(magit-set-header-line-format
(concat (magit-object-type magit-buffer-revision)
" " magit-buffer-revision
diff --git a/lisp/magit-ediff.el b/lisp/magit-ediff.el
index f59520a..c5c4b58 100644
--- a/lisp/magit-ediff.el
+++ b/lisp/magit-ediff.el
@@ -223,12 +223,12 @@ and alternative commands."
(magit-with-toplevel
(let* ((dir (magit-gitdir))
(revA (or (magit-name-branch "HEAD")
- (magit-commit-p "HEAD")))
+ (magit-commit-oid "HEAD")))
(revB (cl-find-if (##file-exists-p (expand-file-name % dir))
'("MERGE_HEAD" "CHERRY_PICK_HEAD" "REVERT_HEAD")))
(revB (or (magit-name-branch revB)
- (magit-commit-p revB)))
- (revC (magit-commit-p (magit-git-string "merge-base" revA revB)))
+ (magit-commit-oid revB)))
+ (revC (magit-commit-oid (magit-git-string "merge-base" revA revB)))
(fileA (magit--rev-file-name file revA revB))
(fileB (magit--rev-file-name file revB revA))
(fileC (or (magit--rev-file-name file revC revA)
diff --git a/lisp/magit-git.el b/lisp/magit-git.el
index 4c85532..eb78174 100644
--- a/lisp/magit-git.el
+++ b/lisp/magit-git.el
@@ -1443,14 +1443,23 @@ string \"true\", otherwise return nil."
(magit-git-string-p "rev-parse" "--verify" rev))
(defun magit-commit-p (rev)
- "Return full hash for REV if it names an existing commit."
+ "Return commit oid for REV if it can be dereferences as a commit.
+Otherwise return nil. Use `magit-commit-oid' if you actually need
+the oid; eventually this function will return t instead of the oid."
+ ;; TODO Return t instead of the oid.
(magit-rev-verify (magit--rev-dereference rev)))
-(defalias 'magit-rev-hash #'magit-commit-p)
+(defun magit-commit-oid (rev &optional noerror)
+ "Return commit oid for REV if it can be dereferences as a commit.
+Otherwise signal an error, or return nil, if optional NOERROR is non-nil."
+ (cond ((magit-rev-verify (magit--rev-dereference rev)))
+ (noerror nil)
+ ((error "%s cannot be dereferenced as a commit" rev))))
(defun magit--rev-dereference (rev)
"Return a rev that forces Git to interpret REV as a commit.
-If REV is nil or has the form \":/TEXT\", return REV itself."
+Do so by appending \"^{commit}\"; see \"--verify\" in git-rev-parse(1).
+However, if REV is nil or has the form \":/TEXT\", return REV itself."
(cond ((not rev) nil)
((string-prefix-p ":/" rev) rev)
((concat rev "^{commit}"))))
@@ -1461,8 +1470,8 @@ If REV is nil or has the form \":/TEXT\", return REV itself."
(defun magit-rev-eq (a b)
"Return t if A and B refer to the same commit."
- (let ((a (magit-commit-p a))
- (b (magit-commit-p b)))
+ (let ((a (magit-commit-oid a t))
+ (b (magit-commit-oid b t)))
(and a b (equal a b))))
(defun magit-rev-ancestor-p (a b)
@@ -2582,12 +2591,12 @@ and this option only controls what face is used.")
(if (string-match magit-range-re range)
(magit-bind-match-strings (beg sep end) range
(and (or beg end)
- (let ((beg-hash (and beg (magit-rev-hash beg)))
- (end-hash (and end (magit-rev-hash end))))
- (and (or (not beg) beg-hash)
- (or (not end) end-hash)
- (concat beg-hash sep end-hash)))))
- (magit-rev-hash range)))
+ (let ((beg-oid (and beg (magit-commit-oid beg)))
+ (end-oid (and end (magit-commit-oid end))))
+ (and (or (not beg) beg-oid)
+ (or (not end) end-oid)
+ (concat beg-oid sep end-oid)))))
+ (magit-commit-oid range)))
(defvar magit-revision-faces
'(magit-hash
@@ -2641,7 +2650,7 @@ and this option only controls what face is used.")
(and (not (equal string "@"))
(or (and (>= (length string) 7)
(string-match-p "[a-z]" string)
- (magit-commit-p string))
+ (magit-commit-oid string t))
(and (magit-ref-p string)
(member (get-text-property (point) 'face)
magit-revision-faces)))
@@ -2993,6 +3002,11 @@ out. Only existing branches can be selected."
(define-obsolete-function-alias 'magit-rev-verify-commit
#'magit-commit-p "Magit 4.6.0")
+(define-obsolete-function-alias 'magit-rev-hash
+ #'magit-commit-p "Magit 4.6.0"
+ "Return oid for REV if it names an existing commit, nil otherwise.
+Instead use `magit-commit-p' or `magit-commit-oid'.")
+
(provide 'magit-git)
;; Local Variables:
;; read-symbol-shorthands: (
diff --git a/lisp/magit-sequence.el b/lisp/magit-sequence.el
index 2a655dd..5d2c437 100644
--- a/lisp/magit-sequence.el
+++ b/lisp/magit-sequence.el
@@ -932,8 +932,9 @@ If no such sequence is in progress, do nothing."
patch commit)
(while (and patches (>= i cur))
(setq patch (pop patches))
- (setq commit (magit-commit-p
- (cadr (split-string (magit-file-line patch)))))
+ (setq commit
+ (magit-commit-oid (cadr (split-string (magit-file-line patch)))
+ t))
(cond ((and commit (= i cur))
(magit-sequence-insert-commit
"stop" commit 'magit-sequence-stop))
diff --git a/lisp/magit-stash.el b/lisp/magit-stash.el
index d328df0..0327bd0 100644
--- a/lisp/magit-stash.el
+++ b/lisp/magit-stash.el
@@ -631,7 +631,7 @@ See also info node `(magit)Section Movement'."
'font-lock-face
(list :weight 'normal :foreground
(face-attribute 'default :foreground)))))
- (setq magit-buffer-revision-oid (magit-rev-hash magit-buffer-revision))
+ (setq magit-buffer-revision-oid (magit-commit-oid magit-buffer-revision))
(magit-insert-section (stash)
(magit-run-section-hook 'magit-stash-sections-hook)))