Skip to content

Commit

Permalink
Fixes #792 - Adds tips token to commit formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Jul 18, 2019
1 parent 3784836 commit 9b9d10f
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 521 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
### Fixed

- Fixes [#791](https://github.com/eamodio/vscode-gitlens/issues/791) - Notification of unstashed changes in working directory on failed checkout
- Fixes [#792](https://github.com/eamodio/vscode-gitlens/issues/792) - Show last commit message on repositories view instead of Git reference

## [9.8.5] - 2019-07-10

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@
},
"gitlens.views.commitFormat": {
"type": "string",
"default": "${message}",
"default": "${❰ tips ❱➤ }${message}",
"markdownDescription": "Specifies the format of committed changes in the views. See [_Commit Tokens_](https://github.com/eamodio/vscode-gitlens/wiki/Custom-Formatting#commit-tokens) in the GitLens docs",
"scope": "window"
},
Expand Down
4 changes: 3 additions & 1 deletion src/annotations/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ export class Annotations {
// editorLine: number,
format: string,
dateFormat: string | null,
scrollable: boolean = true
scrollable: boolean = true,
getBranchAndTagTips?: (sha: string) => string | undefined
): Partial<DecorationOptions> {
// TODO: Enable this once there is better caching
// let diffUris;
Expand All @@ -336,6 +337,7 @@ export class Annotations {

const message = CommitFormatter.fromTemplate(format, commit, {
dateFormat: dateFormat,
getBranchAndTagTips: getBranchAndTagTips,
// previousLineDiffUris: diffUris,
truncateMessageAtNewLine: true
});
Expand Down
8 changes: 7 additions & 1 deletion src/annotations/gutterBlameAnnotationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DecorationOptions, Range, TextEditorDecorationType, window } from 'vsco
import { FileAnnotationType, GravatarDefaultStyle } from '../configuration';
import { GlyphChars } from '../constants';
import { Container } from '../container';
import { CommitFormatOptions, GitBlameCommit } from '../git/gitService';
import { CommitFormatOptions, CommitFormatter, GitBlameCommit } from '../git/gitService';
import { Logger } from '../logger';
import { log, Objects, Strings } from '../system';
import { Annotations } from './annotations';
Expand Down Expand Up @@ -31,8 +31,14 @@ export class GutterBlameAnnotationProvider extends BlameAnnotationProviderBase {
return map;
}, Object.create(null));

let getBranchAndTagTips;
if (CommitFormatter.has(cfg.format, 'tips')) {
getBranchAndTagTips = await Container.git.getBranchesAndTagsTipsFn(blame.repoPath);
}

const options: CommitFormatOptions = {
dateFormat: cfg.dateFormat === null ? Container.config.defaultDateFormat : cfg.dateFormat,
getBranchAndTagTips: getBranchAndTagTips,
tokenOptions: tokenOptions
};

Expand Down
9 changes: 8 additions & 1 deletion src/annotations/lineAnnotationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { LinesChangeEvent } from '../trackers/gitLineTracker';
import { Annotations } from './annotations';
import { debug, log } from '../system';
import { Logger } from '../logger';
import { CommitFormatter } from '../git/gitService';

const annotationDecoration: TextEditorDecorationType = window.createTextEditorDecorationType({
after: {
Expand Down Expand Up @@ -178,6 +179,11 @@ export class LineAnnotationController implements Disposable {
cc.exitDetails = ` ${GlyphChars.Dot} line(s)=${lines.join()}`;
}

let getBranchAndTagTips;
if (CommitFormatter.has(cfg.format, 'tips')) {
getBranchAndTagTips = await Container.git.getBranchesAndTagsTipsFn(trackedDocument.uri.repoPath);
}

const decorations = [];
for (const l of lines) {
const state = Container.lineTracker.getState(l);
Expand All @@ -189,7 +195,8 @@ export class LineAnnotationController implements Disposable {
// l,
cfg.format,
cfg.dateFormat === null ? Container.config.defaultDateFormat : cfg.dateFormat,
cfg.scrollable
cfg.scrollable,
getBranchAndTagTips
) as DecorationOptions;
decoration.range = editor.document.validateRange(
new Range(l, Number.MAX_SAFE_INTEGER, l, Number.MAX_SAFE_INTEGER)
Expand Down
2 changes: 1 addition & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class Configuration {
affectsConfiguration: (section: string, resource?: Uri) => true
};

get<T>(section?: string, resource?: Uri | null, defaultValue?: T) {
get<T>(section?: string, resource?: Uri | null, defaultValue?: T): T {
return defaultValue === undefined
? workspace
.getConfiguration(section === undefined ? undefined : extensionId, resource!)
Expand Down
168 changes: 9 additions & 159 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
import { commands, ExtensionContext, extensions, window, workspace } from 'vscode';
import { Commands, registerCommands } from './commands';
import { ModeConfig, ViewShowBranchComparison } from './config';
import { ViewShowBranchComparison } from './config';
import { Config, configuration, Configuration } from './configuration';
import { CommandContext, extensionQualifiedId, GlobalState, GlyphChars, setCommandContext } from './constants';
import { Container } from './container';
Expand Down Expand Up @@ -105,7 +105,14 @@ async function migrateSettings(context: ExtensionContext, previousVersion: strin
const previous = Versions.fromString(previousVersion);

try {
if (Versions.compare(previous, Versions.from(9, 8, 2)) !== 1) {
if (Versions.compare(previous, Versions.from(9, 8, 5)) !== 1) {
const name = configuration.name('views')('commitFormat').value;
const value = configuration.get<string>(name);
if (!/\btips\b/.test(value)) {
await configuration.updateEffective(name, `\${❰ tips ❱➤ }${value}`);
}
}
else if (Versions.compare(previous, Versions.from(9, 8, 2)) !== 1) {
const name = configuration.name('views')('repositories')('showBranchComparison').value;
await configuration.migrate(name, name, {
migrationFn: (v: boolean) => (v === false ? false : ViewShowBranchComparison.Working)
Expand Down Expand Up @@ -141,163 +148,6 @@ async function migrateSettings(context: ExtensionContext, previousVersion: strin
)
);
}
else if (Versions.compare(previous, Versions.from(9, 2, 2)) !== 1) {
await configuration.migrate('views.avatars', configuration.name('views')('compare')('avatars').value);
await configuration.migrate('views.avatars', configuration.name('views')('repositories')('avatars').value);
await configuration.migrate('views.avatars', configuration.name('views')('search')('avatars').value);
}
else if (Versions.compare(previous, Versions.from(9, 0, 0)) !== 1) {
await configuration.migrate(
'gitExplorer.autoRefresh',
configuration.name('views')('repositories')('autoRefresh').value
);
await configuration.migrate(
'gitExplorer.branches.layout',
configuration.name('views')('repositories')('branches')('layout').value
);
await configuration.migrate(
'gitExplorer.enabled',
configuration.name('views')('repositories')('enabled').value
);
await configuration.migrate(
'gitExplorer.files.compact',
configuration.name('views')('repositories')('files')('compact').value
);
await configuration.migrate(
'gitExplorer.files.layout',
configuration.name('views')('repositories')('files')('layout').value
);
await configuration.migrate(
'gitExplorer.files.threshold',
configuration.name('views')('repositories')('files')('threshold').value
);
await configuration.migrate(
'gitExplorer.includeWorkingTree',
configuration.name('views')('repositories')('includeWorkingTree').value
);
await configuration.migrate(
'gitExplorer.location',
configuration.name('views')('repositories')('location').value
);
await configuration.migrate(
'gitExplorer.showTrackingBranch',
configuration.name('views')('repositories')('showTrackingBranch').value
);

await configuration.migrate(
'historyExplorer.avatars',
configuration.name('views')('fileHistory')('avatars').value
);
await configuration.migrate(
'historyExplorer.enabled',
configuration.name('views')('fileHistory')('enabled').value
);
await configuration.migrate(
'historyExplorer.location',
configuration.name('views')('fileHistory')('location').value
);

await configuration.migrate(
'historyExplorer.avatars',
configuration.name('views')('lineHistory')('avatars').value
);
await configuration.migrate(
'historyExplorer.enabled',
configuration.name('views')('lineHistory')('enabled').value
);
await configuration.migrate(
'historyExplorer.location',
configuration.name('views')('lineHistory')('location').value
);

await configuration.migrate(
'resultsExplorer.files.compact',
configuration.name('views')('compare')('files')('compact').value
);
await configuration.migrate(
'resultsExplorer.files.layout',
configuration.name('views')('compare')('files')('layout').value
);
await configuration.migrate(
'resultsExplorer.files.threshold',
configuration.name('views')('compare')('files')('threshold').value
);
await configuration.migrate(
'resultsExplorer.location',
configuration.name('views')('compare')('location').value
);

await configuration.migrate(
'resultsExplorer.files.compact',
configuration.name('views')('search')('files')('compact').value
);
await configuration.migrate(
'resultsExplorer.files.layout',
configuration.name('views')('search')('files')('layout').value
);
await configuration.migrate(
'resultsExplorer.files.threshold',
configuration.name('views')('search')('files')('threshold').value
);
await configuration.migrate(
'resultsExplorer.location',
configuration.name('views')('search')('location').value
);

await configuration.migrate('explorers.avatars', configuration.name('views')('compare')('avatars').value);
await configuration.migrate(
'explorers.avatars',
configuration.name('views')('repositories')('avatars').value
);
await configuration.migrate('explorers.avatars', configuration.name('views')('search')('avatars').value);
await configuration.migrate(
'explorers.commitFileFormat',
configuration.name('views')('commitFileFormat').value
);
await configuration.migrate('explorers.commitFormat', configuration.name('views')('commitFormat').value);
await configuration.migrate(
'explorers.defaultItemLimit',
configuration.name('views')('defaultItemLimit').value
);
await configuration.migrate(
'explorers.stashFileFormat',
configuration.name('views')('stashFileFormat').value
);
await configuration.migrate('explorers.stashFormat', configuration.name('views')('stashFormat').value);
await configuration.migrate(
'explorers.statusFileFormat',
configuration.name('views')('statusFileFormat').value
);

await configuration.migrate<
{
[key: string]: {
name: string;
statusBarItemName?: string;
description?: string;
codeLens?: boolean;
currentLine?: boolean;
explorers?: boolean;
hovers?: boolean;
statusBar?: boolean;
};
},
{
[key: string]: ModeConfig;
}
>('modes', configuration.name('modes').value, {
migrationFn: v => {
const modes = Object.create(null);

for (const k in v) {
const { explorers, ...mode } = v[k];
modes[k] = { ...mode, views: explorers };
}

return modes;
}
});
}
}
catch (ex) {
Logger.error(ex, 'migrateSettings');
Expand Down
32 changes: 28 additions & 4 deletions src/git/formatters/commitFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ const emptyStr = '';
const escapeMarkdownRegex = /[`>#*_\-+.]/g;
// const sampleMarkdown = '## message `not code` *not important* _no underline_ \n> don\'t quote me \n- don\'t list me \n+ don\'t list me \n1. don\'t list me \nnot h1 \n=== \nnot h2 \n---\n***\n---\n___';
const markdownHeaderReplacement = `${GlyphChars.ZeroWidthSpace}===`;
const hasTokenRegexMap = new Map<string, RegExp>();

export interface CommitFormatOptions extends FormatOptions {
annotationType?: FileAnnotationType;
dateStyle?: DateStyle;
getBranchAndTagTips?: (sha: string) => string | undefined;
line?: number;
markdown?: boolean;
presence?: ContactPresence;
Expand All @@ -39,12 +41,17 @@ export interface CommitFormatOptions extends FormatOptions {
author?: Strings.TokenOptions;
authorAgo?: Strings.TokenOptions;
authorAgoOrDate?: Strings.TokenOptions;
authorDate?: Strings.TokenOptions;
changes?: Strings.TokenOptions;
changesShort?: Strings.TokenOptions;
committerAgo?: Strings.TokenOptions;
committerAgoOrDate?: Strings.TokenOptions;
committerDate?: Strings.TokenOptions;
date?: Strings.TokenOptions;
email?: Strings.TokenOptions;
id?: Strings.TokenOptions;
message?: Strings.TokenOptions;
tips?: Strings.TokenOptions;
};
}

Expand Down Expand Up @@ -117,7 +124,7 @@ export class CommitFormatter extends Formatter<GitCommit, CommitFormatOptions> {
}

get authorDate() {
return this._padOrTruncate(this._authorDate, this._options.tokenOptions.date);
return this._padOrTruncate(this._authorDate, this._options.tokenOptions.authorDate);
}

get avatar() {
Expand Down Expand Up @@ -247,15 +254,15 @@ export class CommitFormatter extends Formatter<GitCommit, CommitFormatOptions> {
}

get committerAgo() {
return this._padOrTruncate(this._committerDateAgo, this._options.tokenOptions.ago);
return this._padOrTruncate(this._committerDateAgo, this._options.tokenOptions.committerAgo);
}

get committerAgoOrDate() {
return this._padOrTruncate(this._committerDateOrAgo, this._options.tokenOptions.agoOrDate);
return this._padOrTruncate(this._committerDateOrAgo, this._options.tokenOptions.committerAgoOrDate);
}

get committerDate() {
return this._padOrTruncate(this._committerDate, this._options.tokenOptions.date);
return this._padOrTruncate(this._committerDate, this._options.tokenOptions.committerDate);
}

get date() {
Expand Down Expand Up @@ -327,6 +334,13 @@ export class CommitFormatter extends Formatter<GitCommit, CommitFormatOptions> {
return this.id;
}

get tips() {
const branchAndTagTips = this._options.getBranchAndTagTips && this._options.getBranchAndTagTips(this._item.sha);
if (branchAndTagTips === undefined) return emptyStr;

return this._padOrTruncate(branchAndTagTips, this._options.tokenOptions.tips);
}

static fromTemplate(template: string, commit: GitCommit, dateFormat: string | null): string;
static fromTemplate(template: string, commit: GitCommit, options?: CommitFormatOptions): string;
static fromTemplate(
Expand All @@ -341,4 +355,14 @@ export class CommitFormatter extends Formatter<GitCommit, CommitFormatOptions> {
): string {
return super.fromTemplateCore(this, template, commit, dateFormatOrOptions);
}

static has(format: string, token: string) {
let regex = hasTokenRegexMap.get(token);
if (regex === undefined) {
regex = new RegExp(`\\b${token}\\b`);
hasTokenRegexMap.set(token, regex);
}

return regex.test(format);
}
}

0 comments on commit 9b9d10f

Please sign in to comment.