From df0a30d574c07e7ca664dd8daf13f44f961415ef Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Tue, 8 Jan 2019 02:02:44 -0500 Subject: [PATCH] Adds recognition for Skybbles // L5474's contribution Tweaks shorten logic to only allow >=5 chars & always append any suffix --- CHANGELOG.md | 1 + README.md | 5 +++-- package.json | 2 +- src/git/git.ts | 20 ++++++++++++++------ 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7be30a..7238b20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p - Fixes [#606](https://github.com/eamodio/vscode-gitlens/issues/606) - ID for xxx is already registered?! - Fixes [#607](https://github.com/eamodio/vscode-gitlens/issues/607) - Open file in Remote Doesn't URL encode +- Fixes [#608](https://github.com/eamodio/vscode-gitlens/issues/608) - Add an option to change the abbreviated commit SHA length — thanks to [PR #611](https://github.com/eamodio/vscode-gitlens/pull/611) by Skybbles // L5474 ([@Luxray5474](https://github.com/Luxray5474)) - Fixes [#613](https://github.com/eamodio/vscode-gitlens/issues/613) - Change Copy Remote URL to Clipboard to always copy a permalink (e.g. revision link) ## [9.3.0] - 2019-01-02 diff --git a/README.md b/README.md index d103234..0574d7e 100644 --- a/README.md +++ b/README.md @@ -841,8 +841,8 @@ See also [View Settings](#view-settings- 'Jump to the View settings') ### Advanced Settings [#](#advanced-settings- 'Advanced Settings') | Name | Description | -| ----------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------| -| `gitlens.advanced.abbreviatedShaLength` | Specifies the length of the abbreviated commit SHA (a.k.a. commit ID). Default is 7. +| ----------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | +| `gitlens.advanced.abbreviatedShaLength` | Specifies the length of abbreviated commit ids (shas) | | `gitlens.advanced.blame.customArguments` | Specifies additional arguments to pass to the `git blame` command | | `gitlens.advanced.blame.delayAfterEdit` | Specifies the time (in milliseconds) to wait before re-blaming an unsaved document after an edit. Use 0 to specify an infinite wait | | `gitlens.advanced.blame.sizeThresholdAfterEdit` | Specifies the maximum document size (in lines) allowed to be re-blamed after an edit while still unsaved. Use 0 to specify no maximum | @@ -919,6 +919,7 @@ A big thanks to the people that have contributed to this project: - Zack Schuster ([@zackschuster](https://github.com/zackschuster)) — [contributions](https://github.com/eamodio/vscode-gitlens/commits?author=zackschuster) - sgtwilko ([@sgtwilko](https://github.com/sgtwilko)) — [contributions](https://github.com/eamodio/vscode-gitlens/commits?author=sgtwilko) - SpaceEEC ([@SpaceEEC](https://github.com/SpaceEEC)) — [contributions](https://github.com/eamodio/vscode-gitlens/commits?author=SpaceEEC) +- Skybbles // L5474 ([@Luxray5474](https://github.com/Luxray5474)) — [contributions](https://github.com/eamodio/vscode-gitlens/commits?author=Luxray5474) - Alexey Vasyukov ([@notmedia](https://github.com/notmedia)) — [contributions](https://github.com/eamodio/vscode-gitlens/commits?author=notmedia) - Zyck ([@qzyse2017](https://github.com/qzyse2017)) — [contributions](https://github.com/eamodio/vscode-gitlens/commits?author=qzyse2017) diff --git a/package.json b/package.json index ce20e88..7a2bd14 100644 --- a/package.json +++ b/package.json @@ -1586,7 +1586,7 @@ "gitlens.advanced.abbreviatedShaLength": { "type": "number", "default": "7", - "markdownDescription": "Specifies the length of the abbreviated commit SHA (a.k.a. commit ID). Default is 7.", + "markdownDescription": "Specifies the length of abbreviated commit ids (shas)", "scope": "window" }, "gitlens.advanced.blame.customArguments": { diff --git a/src/git/git.ts b/src/git/git.ts index 8de4a15..6144f17 100644 --- a/src/git/git.ts +++ b/src/git/git.ts @@ -223,6 +223,7 @@ export class Git { static deletedOrMissingSha = '0000000000000000000000000000000000000000-'; static shaLikeRegex = /(^[0-9a-f]{40}([\^@~:]\S*)?$)|(^[0]{40}(:|-)$)/; static shaRegex = /(^[0-9a-f]{40}$)|(^[0]{40}(:|-)$)/; + static shaShortenRegex = /^(.*?)([\^@~:].*)?$/; static stagedUncommittedRegex = /^[0]{40}([\^@~]\S*)?:$/; static stagedUncommittedSha = '0000000000000000000000000000000000000000:'; static uncommittedRegex = /^[0]{40}(?:[\^@~:]\S*)?:?$/; @@ -281,13 +282,20 @@ export class Git { return strings.uncommitted; } - const index = ref.indexOf('^'); - if (index > 5) { - // Only grab a max of 5 chars for the suffix - const suffix = ref.substring(index).substring(0, 5); - return `${ref.substring(0, Container.config.advanced.abbreviatedShaLength - suffix.length)}${suffix}`; + // Don't allow shas to be shortened to less than 5 characters + const len = Math.max(5, Container.config.advanced.abbreviatedShaLength); + + // If we have a suffix, append it + const match = Git.shaShortenRegex.exec(ref); + if (match != null) { + const [, rev, suffix] = match; + + if (suffix != null) { + return `${rev.substr(0, len)}${suffix}`; + } } - return ref.substring(0, Container.config.advanced.abbreviatedShaLength); + + return ref.substr(0, len); } static splitPath(fileName: string, repoPath: string | undefined, extract: boolean = true): [string, string] {