From 85cb4a69baa468133621c5bd7a76a02695ea8722 Mon Sep 17 00:00:00 2001 From: Nigel Westbury Date: Sun, 16 Jun 2019 16:01:39 +0100 Subject: [PATCH] Support amending of initial commit Signed-off-by: Nigel Westbury --- packages/git/src/browser/git-scm-provider.ts | 13 +++- .../scm/src/browser/scm-amend-component.tsx | 74 ++++++++++++------- packages/scm/src/browser/scm-provider.ts | 2 +- 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/packages/git/src/browser/git-scm-provider.ts b/packages/git/src/browser/git-scm-provider.ts index b4c94d94d2353..a8ac340e6aa1b 100644 --- a/packages/git/src/browser/git-scm-provider.ts +++ b/packages/git/src/browser/git-scm-provider.ts @@ -396,7 +396,7 @@ export class GitAmendSupport implements ScmAmendSupport { constructor(protected readonly repository: Repository, protected readonly git: Git) { } - public async getInitialAmendingCommits(amendingHeadCommitSha: string, latestCommitSha: string): Promise { + public async getInitialAmendingCommits(amendingHeadCommitSha: string, latestCommitSha: string | undefined): Promise { const commits = await this.git.log( this.repository, { @@ -414,7 +414,16 @@ export class GitAmendSupport implements ScmAmendSupport { } public async reset(commit: string): Promise { - await this.git.exec(this.repository, ['reset', commit, '--soft']); + if (commit === 'HEAD~' && await this.isHeadInitialCommit()) { + await this.git.exec(this.repository, ['update-ref', '-d', 'HEAD']); + } else { + await this.git.exec(this.repository, ['reset', commit, '--soft']); + } + } + + protected async isHeadInitialCommit(): Promise { + const result = await this.git.revParse(this.repository, { ref: 'HEAD~' }); + return !result; } public async getLastCommit(): Promise { diff --git a/packages/scm/src/browser/scm-amend-component.tsx b/packages/scm/src/browser/scm-amend-component.tsx index bb46af0cc4236..c7921075e4cc7 100644 --- a/packages/scm/src/browser/scm-amend-component.tsx +++ b/packages/scm/src/browser/scm-amend-component.tsx @@ -98,6 +98,8 @@ export class ScmAmendComponent extends React.Component { + const storageKey = this.getStorageKey(); + const nextCommit = await this.getLastCommit(); if (nextCommit && this.state.lastCommit && nextCommit.commit.id === this.state.lastCommit.commit.id) { // No change here @@ -119,32 +121,37 @@ export class ScmAmendComponent extends React.Component(storageKey, serializedState); + } + break; + case 'unamend': + amendingCommits.pop(); + if (amendingCommits.length === 0) { + this.props.storageService.setData(storageKey, undefined); + } else { + const serializedState = JSON.stringify({ + amendingHeadCommitSha: amendingCommits[0].commit.id, + latestCommitSha: nextCommit ? nextCommit.commit.id : undefined + }); + this.props.storageService.setData(storageKey, serializedState); + } + break; + } + if (this.state.lastCommit && nextCommit) { - const direction: 'up' | 'down' = this.transitionHint === 'amend' ? 'up' : 'down'; const transitionData = { direction, previousLastCommit: this.state.lastCommit }; - const amendingCommits = this.state.amendingCommits.concat([]); // copy the array - switch (this.transitionHint) { - case 'amend': - if (this.state.lastCommit) { - amendingCommits.push(this.state.lastCommit); - - const storageKey = this.getStorageKey(); - const serializedState = JSON.stringify({ - amendingHeadCommitSha: amendingCommits[0].commit.id, - latestCommitSha: nextCommit.commit.id - }); - this.props.storageService.setData(storageKey, serializedState); - } - break; - case 'unamend': - amendingCommits.pop(); - if (amendingCommits.length === 0) { - const storageKey = this.getStorageKey(); - this.props.storageService.setData(storageKey, undefined); - } - break; - } - this.setState({ lastCommit: nextCommit, amendingCommits, transition: { ...transitionData, state: 'start' } }); this.onNextFrame(() => { this.setState({ transition: { ...transitionData, state: 'transitioning' } }); @@ -157,7 +164,7 @@ export class ScmAmendComponent extends React.Component { const avatar = await this.props.avatarService.getAvatar(commit.authorEmail); @@ -199,6 +206,17 @@ export class ScmAmendComponent extends React.Component + getInitialAmendingCommits(amendingHeadCommitId: string, latestCommitId: string | undefined): Promise getMessage(commit: string): Promise; reset(commit: string): Promise; getLastCommit(): Promise;