Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Fall back to update-ref -d on the initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
smashwilson committed Aug 9, 2018
1 parent 6ed02ab commit abd40e1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/models/repository-states/present.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,18 @@ export default class Present extends State {
...Keys.filePatch.eachWithOpts({staged: true}),
Keys.headDescription,
],
() => this.git().reset('soft', 'HEAD~'),
async () => {
try {
await this.git().reset('soft', 'HEAD~');
} catch (e) {
if (/unknown revision/.test(e.stdErr)) {
// Initial commit
await this.git().deleteRef('HEAD');
} else {
throw e;
}
}
},
);
}

Expand Down
57 changes: 57 additions & 0 deletions test/models/repository.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,63 @@ describe('Repository', function() {
});
});

describe('undoLastCommit()', function() {
it('performs a soft reset', async function() {
const workingDirPath = await cloneRepository('multiple-commits');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();

fs.appendFileSync(path.join(workingDirPath, 'file.txt'), 'qqq\n', 'utf8');
await repo.git.exec(['add', '.']);
await repo.git.commit('add stuff');

const parentCommit = await repo.git.getCommit('HEAD~');

await repo.undoLastCommit();

const commitAfterReset = await repo.git.getCommit('HEAD');
assert.strictEqual(commitAfterReset.sha, parentCommit.sha);

const fp = await repo.getFilePatchForPath('file.txt', {staged: true});
assert.strictEqual(
fp.toString(),
dedent`
diff --git a/file.txt b/file.txt
--- a/file.txt
+++ b/file.txt
@@ -1,1 +1,2 @@
three
+qqq\n
`,
);
});

it('deletes the HEAD ref when only a single commit is present', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();

fs.appendFileSync(path.join(workingDirPath, 'b.txt'), 'qqq\n', 'utf8');
await repo.git.exec(['add', '.']);

await repo.undoLastCommit();

const fp = await repo.getFilePatchForPath('b.txt', {staged: true});
assert.strictEqual(
fp.toString(),
dedent`
diff --git a/b.txt b/b.txt
new file mode 100644
--- /dev/null
+++ b/b.txt
@@ -0,0 +1,2 @@
+bar
+qqq\n
`,
);
});
});

describe('fetch(branchName, {remoteName})', function() {
it('brings commits from the remote and updates remote branch, and does not update branch', async function() {
const {localRepoPath} = await setUpLocalAndRemoteRepositories({remoteAhead: true});
Expand Down

0 comments on commit abd40e1

Please sign in to comment.