summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Meyer <kyle@kyleam.com>2015-07-17 01:38:34 -0400
committerKyle Meyer <kyle@kyleam.com>2015-07-17 01:38:34 -0400
commita2363356619a810f55927ba158e81c76cea828c6 (patch)
tree55c9b207c060bc7e98175a74d65d604502dabbc7
parent12cee6e214e1cf71d6368dfc8ab5a79f50540f07 (diff)
downloademacs.d-a2363356619a810f55927ba158e81c76cea828c6.tar.gz
git-shorten-hash-at-point: Use magit-abbrev-length
-rw-r--r--lisp/init-git.el16
1 files changed, 13 insertions, 3 deletions
diff --git a/lisp/init-git.el b/lisp/init-git.el
index 0c84d19..5374167 100644
--- a/lisp/init-git.el
+++ b/lisp/init-git.el
@@ -287,6 +287,9 @@ command will still offer the staged files)."
(completing-read "Staged file: " files nil t)))))
(insert (if no-directory (file-name-nondirectory file) file)))))
+(defun km/magit-shorten-hash (hash)
+ (magit-rev-parse (format "--short=%s" (magit-abbrev-length)) hash))
+
(define-key ctl-x-4-map "g" 'magit-find-file-other-window)
(define-key km/file-map "g" 'magit-find-file)
@@ -392,11 +395,16 @@ command will still offer the staged files)."
(defun km/git-shorten-hash-at-point (&optional n)
"Shorten hash at point to N characters.
-N defaults to 10 and can be controlled by a numeric prefix
+
+N defaults to `magit-abbrev-length'. If the commit belongs to
+the current repo and the hash is ambiguous, the hash is extended
+as needed.
+
+To explicitly set the hash length, use a numeric prefix
argument."
(interactive (list (or (and current-prefix-arg
(prefix-numeric-value current-prefix-arg))
- 10)))
+ (magit-abbrev-length))))
(cond
((< n 4)
(user-error "Hash must be at least 4 characters"))
@@ -410,7 +418,9 @@ argument."
(hash (match-string 0)))
(when (< hash-len n)
(user-error "Desired hash length is greater than current"))
- (replace-match (substring hash 0 n) 'fixedcase)
+ (replace-match (or (km/magit-shorten-hash hash)
+ (substring hash 0 n))
+ 'fixedcase)
(when (< offset n)
(skip-chars-backward "A-z0-9")
(goto-char (+ (point) offset))))