Skip to content

Commit

Permalink
Changes unpublished commits icon to upload
Browse files Browse the repository at this point in the history
Adds undo commit action to first unpublished commit
  • Loading branch information
eamodio committed Oct 12, 2020
1 parent ea3d8bf commit aa51162
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 25 deletions.
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3570,6 +3570,12 @@
"title": "Revert Commit...",
"category": "GitLens"
},
{
"command": "gitlens.views.undoCommit",
"title": "Undo Commit",
"category": "GitLens",
"icon": "$(discard)"
},
{
"command": "gitlens.views.terminalRemoveRemote",
"title": "Remove Remote (via Terminal)",
Expand Down Expand Up @@ -4930,6 +4936,10 @@
"command": "gitlens.views.revert",
"when": "false"
},
{
"command": "gitlens.views.undoCommit",
"when": "false"
},
{
"command": "gitlens.views.terminalRemoveRemote",
"when": "false"
Expand Down Expand Up @@ -6418,6 +6428,11 @@
"when": "!gitlens:readonly && viewItem =~ /gitlens:branch\\b(?=.*?\\b\\+tracking\\b)/",
"group": "inline@9"
},
{
"command": "gitlens.views.undoCommit",
"when": "!gitlens:readonly && viewItem =~ /gitlens:commit\\b(?=.*?\\b\\+HEAD\\b)(?=.*?\\b\\+unpublished\\b)/",
"group": "inline@96"
},
{
"command": "gitlens.views.compareWithHead",
"when": "viewItem =~ /gitlens:(branch\\b(?!.*?\\b\\+current\\b)|commit\\b|stash\\b|tag\\b)/",
Expand Down Expand Up @@ -6608,6 +6623,11 @@
"when": "!gitlens:readonly && viewItem =~ /gitlens:commit\\b(?!.*?\\b\\+current\\b)/",
"group": "1_gitlens_actions@1"
},
{
"command": "gitlens.views.undoCommit",
"when": "!gitlens:readonly && viewItem =~ /gitlens:commit\\b(?=.*?\\b\\+HEAD\\b)(?=.*?\\b\\+unpublished\\b)/",
"group": "1_gitlens_actions@1"
},
{
"command": "gitlens.views.revert",
"when": "!gitlens:readonly && viewItem =~ /gitlens:commit\\b(?=.*?\\b\\+current\\b)/",
Expand Down
15 changes: 10 additions & 5 deletions src/commands/git/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface State {

export interface ResetGitCommandArgs {
readonly command: 'reset';
confirm?: boolean;
state?: Partial<State>;
}

Expand All @@ -53,13 +54,15 @@ export class ResetGitCommand extends QuickCommand<State> {

this.initialState = {
counter: counter,
confirm: true,
confirm: args?.confirm ?? true,
...args?.state,
};
this._canSkipConfirm = !this.initialState.confirm;
}

private _canSkipConfirm: boolean = false;
get canSkipConfirm(): boolean {
return false;
return this._canSkipConfirm;
}

execute(state: ResetStepState) {
Expand Down Expand Up @@ -138,10 +141,12 @@ export class ResetGitCommand extends QuickCommand<State> {
state.reference = result;
}

const result = yield* this.confirmStep(state as ResetStepState, context);
if (result === StepResult.Break) continue;
if (this.confirm(state.confirm)) {
const result = yield* this.confirmStep(state as ResetStepState, context);
if (result === StepResult.Break) continue;

state.flags = result;
state.flags = result;
}

QuickCommand.endSteps(state);
this.execute(state as ResetStepState);
Expand Down
10 changes: 8 additions & 2 deletions src/commands/gitCommands.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
Repository,
} from '../git/git';
import { GitUri } from '../git/gitUri';
import { ResetGitCommandArgs } from './git/reset';

export async function executeGitCommand(args: GitCommandsCommandArgs): Promise<void> {
void (await executeCommand<GitCommandsCommandArgs>(Commands.GitCommands, args));
Expand Down Expand Up @@ -77,10 +78,15 @@ export namespace GitActions {
});
}

export function reset(repo?: string | Repository, ref?: GitRevisionReference) {
export function reset(
repo?: string | Repository,
ref?: GitRevisionReference,
flags?: NonNullable<ResetGitCommandArgs['state']>['flags'],
) {
return executeGitCommand({
command: 'reset',
state: { repo: repo, reference: ref },
confirm: flags == null || flags.includes('--hard'),
state: { repo: repo, reference: ref, flags: flags },
});
}

Expand Down
21 changes: 18 additions & 3 deletions src/views/nodes/branchNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
GitBranchReference,
GitLog,
GitRemoteType,
GitRevision,
PullRequestState,
} from '../../git/git';
import { GitUri } from '../../git/gitUri';
Expand Down Expand Up @@ -160,8 +161,14 @@ export class BranchNode
new BranchTrackingStatusNode(this.view, this, this.branch, status, 'none', this.root),
);
}
} else if (pr != null) {
children.push(new PullRequestNode(this.view, this, pr, this.branch));
}

let unpublished: GitLog | undefined;
if (this.branch.tracking && this.branch.state.ahead) {
unpublished = await Container.git.getLog(this.uri.repoPath!, {
limit: 0,
ref: GitRevision.createRange(this.branch.tracking, 'HEAD'),
});
}

const getBranchAndTagTips = await Container.git.getBranchesAndTagsTipsFn(
Expand All @@ -172,7 +179,15 @@ export class BranchNode
...insertDateMarkers(
Iterables.map(
log.commits.values(),
c => new CommitNode(this.view, this, c, this.branch, getBranchAndTagTips),
c =>
new CommitNode(
this.view,
this,
c,
unpublished?.commits.has(c.ref),
this.branch,
getBranchAndTagTips,
),
),
this,
),
Expand Down
5 changes: 4 additions & 1 deletion src/views/nodes/branchTrackingStatusNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ export class BranchTrackingStatusNode extends ViewNode<ViewsWithFiles> implement

const children = [
...insertDateMarkers(
Iterables.map(commits, c => new CommitNode(this.view, this, c, this.branch)),
Iterables.map(
commits,
c => new CommitNode(this.view, this, c, this.upstreamType === 'ahead', this.branch),
),
this,
1,
),
Expand Down
4 changes: 2 additions & 2 deletions src/views/nodes/commitFileNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export class CommitFileNode extends ViewRefFileNode {
private get description() {
return this._options.displayAsCommit
? CommitFormatter.fromTemplate(this.getCommitDescriptionTemplate(), this.commit, {
messageTruncateAtNewLine: true,
dateFormat: Container.config.defaultDateFormat,
messageTruncateAtNewLine: true,
})
: StatusFileFormatter.fromTemplate(this.getCommitFileDescriptionTemplate(), this.file, {
relativePath: this.relativePath,
Expand All @@ -113,8 +113,8 @@ export class CommitFileNode extends ViewRefFileNode {
if (this._label === undefined) {
this._label = this._options.displayAsCommit
? CommitFormatter.fromTemplate(this.getCommitTemplate(), this.commit, {
messageTruncateAtNewLine: true,
dateFormat: Container.config.defaultDateFormat,
messageTruncateAtNewLine: true,
})
: StatusFileFormatter.fromTemplate(this.getCommitFileTemplate(), this.file, {
relativePath: this.relativePath,
Expand Down
22 changes: 13 additions & 9 deletions src/views/nodes/commitNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class CommitNode extends ViewRefNode<ViewsWithFiles, GitRevisionReference
view: ViewsWithFiles,
parent: ViewNode,
public readonly commit: GitLogCommit,
private readonly unpublished?: boolean,
public readonly branch?: GitBranch,
private readonly getBranchAndTagTips?: (sha: string) => string | undefined,
private readonly _options: { expand?: boolean } = {},
Expand All @@ -39,9 +40,9 @@ export class CommitNode extends ViewRefNode<ViewsWithFiles, GitRevisionReference
return CommitFormatter.fromTemplate(
this.commit.isUncommitted
? `\${author} ${GlyphChars.Dash} \${id}\n\${ago} (\${date})`
: `\${author}\${ (email)}\${" via "pullRequest} ${
GlyphChars.Dash
} \${id}\${ (tips)}\n\${ago} (\${date})\${\n\nmessage}${this.commit.getFormattedDiffStatus({
: `\${author}\${ (email)}\${" via "pullRequest} ${GlyphChars.Dash} \${id}${
this.unpublished ? ' (unpublished)' : ''
}\${ (tips)}\n\${ago} (\${date})\${\n\nmessage}${this.commit.getFormattedDiffStatus({
expand: true,
prefix: '\n\n',
separator: '\n',
Expand Down Expand Up @@ -103,16 +104,19 @@ export class CommitNode extends ViewRefNode<ViewsWithFiles, GitRevisionReference
this._options.expand ? TreeItemCollapsibleState.Expanded : TreeItemCollapsibleState.Collapsed,
);

item.contextValue = `${ContextValues.Commit}${this.branch?.current ? '+current' : ''}`;
item.contextValue = `${ContextValues.Commit}${this.branch?.current ? '+current' : ''}${
this.branch?.current && this.branch.sha === this.commit.ref ? '+HEAD' : ''
}${this.unpublished ? '+unpublished' : ''}`;

item.description = CommitFormatter.fromTemplate(this.view.config.commitDescriptionFormat, this.commit, {
messageTruncateAtNewLine: true,
dateFormat: Container.config.defaultDateFormat,
messageTruncateAtNewLine: true,
});
item.iconPath =
!(this.view instanceof StashesView) && this.view.config.avatars
? this.commit.getAvatarUri(Container.config.defaultGravatarsStyle)
: new ThemeIcon('git-commit');
item.iconPath = this.unpublished
? new ThemeIcon('cloud-upload')
: !(this.view instanceof StashesView) && this.view.config.avatars
? this.commit.getAvatarUri(Container.config.defaultGravatarsStyle)
: new ThemeIcon('git-commit');
item.tooltip = this.tooltip;

return item;
Expand Down
2 changes: 1 addition & 1 deletion src/views/nodes/contributorNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class ContributorNode extends ViewNode<ContributorsView | RepositoriesVie
...insertDateMarkers(
Iterables.map(
log.commits.values(),
c => new CommitNode(this.view, this, c, undefined, getBranchAndTagTips),
c => new CommitNode(this.view, this, c, undefined, undefined, getBranchAndTagTips),
),
this,
),
Expand Down
2 changes: 1 addition & 1 deletion src/views/nodes/resultsCommitsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class ResultsCommitsNode extends ViewNode<ViewsWithFiles> implements Page
...insertDateMarkers(
Iterables.map(
log.commits.values(),
c => new CommitNode(this.view, this, c, undefined, getBranchAndTagTips, options),
c => new CommitNode(this.view, this, c, undefined, undefined, getBranchAndTagTips, options),
),
this,
undefined,
Expand Down
2 changes: 1 addition & 1 deletion src/views/nodes/tagNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class TagNode extends ViewRefNode<TagsView | RepositoriesView, GitTagRefe
...insertDateMarkers(
Iterables.map(
log.commits.values(),
c => new CommitNode(this.view, this, c, undefined, getBranchAndTagTips),
c => new CommitNode(this.view, this, c, undefined, undefined, getBranchAndTagTips),
),
this,
),
Expand Down
16 changes: 16 additions & 0 deletions src/views/viewCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export class ViewCommands {
commands.registerCommand('gitlens.views.resetCommit', this.resetCommit, this);
commands.registerCommand('gitlens.views.resetToCommit', this.resetToCommit, this);
commands.registerCommand('gitlens.views.revert', this.revert, this);
commands.registerCommand('gitlens.views.undoCommit', this.undoCommit, this);

commands.registerCommand('gitlens.views.terminalRemoveRemote', this.terminalRemoveRemote, this);
}
Expand Down Expand Up @@ -525,6 +526,21 @@ export class ViewCommands {
);
}

@debug()
private undoCommit(node: CommitNode) {
if (!(node instanceof CommitNode)) return Promise.resolve();

return GitActions.reset(
node.repoPath,
GitReference.create(`${node.ref.ref}^`, node.ref.repoPath, {
refType: 'revision',
name: `${node.ref.name}^`,
message: node.ref.message,
}),
['--soft'],
);
}

@debug()
private unsetAsDefault(node: RemoteNode) {
if (!(node instanceof RemoteNode)) return Promise.resolve();
Expand Down

0 comments on commit aa51162

Please sign in to comment.