Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/mrcrowl/vscode-hg
Browse files Browse the repository at this point in the history
  • Loading branch information
epben committed May 30, 2020
2 parents ed1be3d + d058cbe commit 46363c4
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 15 deletions.
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,31 @@
"dark": "resources/icons/dark/check.svg"
}
},
{
"command": "hg.commitAmend",
"title": "%command.commitAmend%",
"category": "Hg"
},
{
"command": "hg.commitAll",
"title": "%command.commitAll%",
"category": "Hg"
},
{
"command": "hg.commitAllAmend",
"title": "%command.commitAllAmend%",
"category": "Hg"
},
{
"command": "hg.commitStaged",
"title": "%command.commitStaged%",
"category": "Hg"
},
{
"command": "hg.commitStagedAmend",
"title": "%command.commitStagedAmend%",
"category": "Hg"
},
{
"command": "hg.clone",
"title": "%command.clone%",
Expand Down Expand Up @@ -380,14 +395,26 @@
"command": "hg.commit",
"when": "config.hg.enabled && hgOpenRepositoryCount != 0"
},
{
"command": "hg.commitAmend",
"when": "config.hg.enabled && hgOpenRepositoryCount != 0"
},
{
"command": "hg.commitStaged",
"when": "config.hg.enabled && hgOpenRepositoryCount != 0"
},
{
"command": "hg.commitStagedAmend",
"when": "config.hg.enabled && hgOpenRepositoryCount != 0"
},
{
"command": "hg.commitAll",
"when": "config.hg.enabled && hgOpenRepositoryCount != 0"
},
{
"command": "hg.commitAllAmend",
"when": "config.hg.enabled && hgOpenRepositoryCount != 0"
},
{
"command": "hg.undoRollback",
"when": "config.hg.enabled && hgOpenRepositoryCount != 0"
Expand Down Expand Up @@ -484,16 +511,31 @@
"group": "1_sync",
"when": "config.hg.enabled && scmProvider == hg"
},
{
"command": "hg.commitAmend",
"group": "3_commit",
"when": "config.hg.enabled && scmProvider == hg"
},
{
"command": "hg.commitStaged",
"group": "3_commit",
"when": "config.hg.enabled && scmProvider == hg"
},
{
"command": "hg.commitStagedAmend",
"group": "3_commit",
"when": "config.hg.enabled && scmProvider == hg"
},
{
"command": "hg.commitAll",
"group": "3_commit",
"when": "config.hg.enabled && scmProvider == hg"
},
{
"command": "hg.commitAllAmend",
"group": "3_commit",
"when": "config.hg.enabled && scmProvider == hg"
},
{
"command": "hg.undoRollback",
"group": "3_commit",
Expand Down
3 changes: 3 additions & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
"command.clean": "Discard Changes",
"command.cleanAll": "Discard All Changes",
"command.commit": "Commit",
"command.commitAmend": "Commit (Amend)",
"command.commitStaged": "Commit Staged",
"command.commitStagedAmend": "Commit Staged (Amend)",
"command.commitAll": "Commit All",
"command.commitAllAmend": "Commit All (Amend)",
"command.undoRollback": "Undo / Rollback",
"command.update": "Update to...",
"command.branch": "Create Branch...",
Expand Down
64 changes: 52 additions & 12 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ export class CommandCenter {
}
}

private async smartCommit(repository: Repository, getCommitMessage: () => Promise<string | undefined>, opts?: CommitOptions): Promise<boolean> {
private async smartCommit(repository: Repository, getCommitMessage: () => Promise<string | undefined>, opts: CommitOptions = {}): Promise<boolean> {
// validate no conflicts
const numConflictResources = repository.conflictGroup.resources.length;
if (numConflictResources > 0) {
Expand All @@ -570,22 +570,18 @@ export class CommandCenter {
const isMergeCommit = repository.repoStatus && repository.repoStatus.isMerge;
if (isMergeCommit) {
// merge-commit
opts = { scope: CommitScope.ALL };
opts.scope = CommitScope.ALL;
}
else {
// validate non-merge commit
const numWorkingResources = repository.workingDirectoryGroup.resources.length;
const numStagingResources = repository.stagingGroup.resources.length;
if (!opts || opts.scope === undefined) {
if (opts.scope === undefined) {
if (numStagingResources > 0) {
opts = {
scope: CommitScope.STAGED_CHANGES
};
opts.scope = CommitScope.STAGED_CHANGES;
}
else {
opts = {
scope: CommitScope.CHANGES
};
opts.scope = CommitScope.CHANGES;
}
}

Expand All @@ -602,8 +598,8 @@ export class CommandCenter {
}

if ((numWorkingResources === 0 && numStagingResources === 0) // no changes
|| (opts && opts.scope === CommitScope.STAGED_CHANGES && numStagingResources === 0) // no staged changes
|| (opts && opts.scope === CommitScope.CHANGES && numWorkingResources === 0) // no working directory changes
|| (opts.scope === CommitScope.STAGED_CHANGES && numStagingResources === 0) // no staged changes
|| (opts.scope === CommitScope.CHANGES && numWorkingResources === 0) // no working directory changes
) {
interaction.informNoChangesToCommit();
return false;
Expand All @@ -625,7 +621,36 @@ export class CommandCenter {

private async commitWithAnyInput(repository: Repository, opts?: CommitOptions): Promise<void> {
const message = scm.inputBox.value;
const didCommit = await this.smartCommit(repository, () => interaction.inputCommitMessage(message), opts);
const getCommitMessage = async () => {
let _message: string | undefined = message;

if (!_message) {
let value: string | undefined = undefined;

if (opts && opts.amend) {
value = await repository.getLastCommitMessage()
}

const branchName = repository.headShortName;
let placeHolder: string;

if (branchName) {
placeHolder = localize('commitMessageWithHeadLabel2', "Message (commit on '{0}')", branchName);
} else {
placeHolder = localize('commit message', "Commit message");
}

_message = await window.showInputBox({
value: value,
placeHolder: placeHolder,
prompt: localize('provide commit message', "Please provide a commit message"),
ignoreFocusOut: true
});
}

return _message;
}
const didCommit = await this.smartCommit(repository, getCommitMessage, opts);

if (message && didCommit) {
scm.inputBox.value = "";
Expand All @@ -637,6 +662,11 @@ export class CommandCenter {
await this.commitWithAnyInput(repository);
}

@command('hg.commitAmend', { repository: true })
async commitAmend(repository: Repository): Promise<void> {
await this.commitWithAnyInput(repository, { amend: true });
}

@command('hg.commitWithInput', { repository: true })
async commitWithInput(repository: Repository): Promise<void> {
const didCommit = await this.smartCommit(repository, async () => repository.sourceControl.inputBox.value);
Expand All @@ -651,11 +681,21 @@ export class CommandCenter {
await this.commitWithAnyInput(repository, { scope: CommitScope.STAGED_CHANGES });
}

@command('hg.commitStagedAmend', { repository: true })
async commitStagedAmend(repository: Repository): Promise<void> {
await this.commitWithAnyInput(repository, { scope: CommitScope.STAGED_CHANGES, amend: true });
}

@command('hg.commitAll', { repository: true })
async commitAll(repository: Repository): Promise<void> {
await this.commitWithAnyInput(repository, { scope: CommitScope.ALL_WITH_ADD_REMOVE });
}

@command('hg.commitAllAmend', { repository: true })
async commitAllAmend(repository: Repository): Promise<void> {
await this.commitWithAnyInput(repository, { scope: CommitScope.ALL_WITH_ADD_REMOVE, amend: true });
}

private focusScm() {
commands.executeCommand("workbench.view.scm");
}
Expand Down
6 changes: 5 additions & 1 deletion src/hg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,14 +698,18 @@ export class Repository {
}
}

async commit(message: string, opts: { addRemove?: boolean, fileList: string[] } = Object.create(null)): Promise<void> {
async commit(message: string, opts: { addRemove?: boolean, amend?: boolean, fileList: string[] } = Object.create(null)): Promise<void> {
const disposables: IDisposable[] = [];
const args = ['commit'];

if (opts.addRemove) {
args.push('--addremove');
}

if (opts.amend) {
args.push('--amend');
}

if (opts.fileList && opts.fileList.length) {
args.push(...opts.fileList);
}
Expand Down
34 changes: 32 additions & 2 deletions src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ export const enum CommitScope {
}

export interface CommitOptions {
scope: CommitScope;
scope?: CommitScope;
amend?: boolean;
}

export class Repository implements IDisposable {
Expand Down Expand Up @@ -305,6 +306,31 @@ export class Repository implements IDisposable {
get workingDirectoryGroup(): WorkingDirectoryGroup { return this._groups.working; }
get untrackedGroup(): UntrackedGroup { return this._groups.untracked; }

get head(): Ref | undefined {
const useBookmarks = typedConfig.useBookmarks
return useBookmarks ? this.activeBookmark : this.currentBranch;
}

get headShortName(): string | undefined {
const head = this.head;

if (!head) {
return;
}

if (head.name) {
return head.name;
}
const tag = this.refs.filter(iref => iref.type === RefType.Tag && iref.commit === head.commit)[0];
const tagName = tag && tag.name;

if (tagName) {
return tagName;
}

return (head.commit || '').substr(0, 8);
}

private _currentBranch: Ref | undefined;
get currentBranch(): Ref | undefined { return this._currentBranch; }

Expand Down Expand Up @@ -605,7 +631,7 @@ export class Repository implements IDisposable {
fileList = selectedResources.map(r => this.mapResourceToRepoRelativePath(r));
}

await this.repository.commit(message, { addRemove: opts.scope === CommitScope.ALL_WITH_ADD_REMOVE, fileList });
await this.repository.commit(message, { amend: opts.amend, addRemove: opts.scope === CommitScope.ALL_WITH_ADD_REMOVE, fileList });
});
}

Expand Down Expand Up @@ -1111,6 +1137,10 @@ export class Repository implements IDisposable {
}
}

public async getLastCommitMessage(): Promise<string> {
return this.repository.getLastCommitMessage();
}

@throttle
public getLogEntries(options: LogEntriesOptions = {}): Promise<Commit[]> {
let filePaths: string[] | undefined = undefined;
Expand Down

0 comments on commit 46363c4

Please sign in to comment.