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

Commit

Permalink
Merge pull request #1635 from atom/aw/undo-first-commit
Browse files Browse the repository at this point in the history
Undo an initial commit
  • Loading branch information
smashwilson committed Aug 9, 2018
2 parents edcabfb + abd40e1 commit ac67069
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/git-shell-out-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,10 @@ export default class GitShellOutStrategy {
return this.exec(['reset', `--${type}`, revision]);
}

deleteRef(ref) {
return this.exec(['update-ref', '-d', ref]);
}

/**
* Branches
*/
Expand Down
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
25 changes: 25 additions & 0 deletions test/git-strategies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,31 @@ import * as reporterProxy from '../lib/reporter-proxy';
});
});

describe('deleteRef()', function() {
it('soft-resets an initial commit', async function() {
const workingDirPath = await cloneRepository('three-files');
const git = createTestStrategy(workingDirPath);

// Ensure that three-files still has only a single commit
assert.lengthOf(await git.getCommits({max: 10}), 1);

// Put something into the index to ensure it doesn't get lost
fs.appendFileSync(path.join(workingDirPath, 'a.txt'), 'zzz\n', 'utf8');
await git.exec(['add', '.']);

await git.deleteRef('HEAD');

const after = await git.getCommit('HEAD');
assert.isTrue(after.unbornRef);

const stagedChanges = await git.getDiffsForFilePath('a.txt', {staged: true});
assert.lengthOf(stagedChanges, 1);
const stagedChange = stagedChanges[0];
assert.strictEqual(stagedChange.newPath, 'a.txt');
assert.deepEqual(stagedChange.hunks[0].lines, ['+foo', '+zzz']);
});
});

describe('getBranches()', function() {
const sha = '66d11860af6d28eb38349ef83de475597cb0e8b4';

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 ac67069

Please sign in to comment.