From a2363356619a810f55927ba158e81c76cea828c6 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Fri, 17 Jul 2015 01:38:34 -0400 Subject: git-shorten-hash-at-point: Use magit-abbrev-length --- lisp/init-git.el | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'lisp') 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)))) -- cgit v1.2.3