Skip to content

Commit

Permalink
fix: change makeCommit to use simple-git
Browse files Browse the repository at this point in the history
Progress toward #86
  • Loading branch information
alangpierce committed Apr 17, 2017
1 parent 4f79905 commit a8e8716
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 34 deletions.
19 changes: 8 additions & 11 deletions src/convert.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { exec } from 'mz/child_process';
import { copy, move, readFile, unlink, writeFile } from 'fs-promise';
import path from 'path';
import git from 'simple-git/promise';
import zlib from 'zlib';

import makeCLIFn from './runner/makeCLIFn';
Expand Down Expand Up @@ -55,10 +56,9 @@ Re-run with the "check" command for more details.`);
let renameCommitMsg =
`decaffeinate: Rename ${shortDescription} from .coffee to .js`;
console.log(`Generating the first commit: "${renameCommitMsg}"...`);
await makeCommit(async function(index, resolvePath) {
await index.removeAll(baseFiles.map(p => resolvePath(`${p}.coffee`)), null, null);
await index.addAll(baseFiles.map(p => resolvePath(`${p}.js`)), 0, null, null);
}, renameCommitMsg, 'decaffeinate');
await git().rm(baseFiles.map(p => `${p}.coffee`));
await git().add(baseFiles.map(p => `${p}.js`));
await makeCommit(renameCommitMsg);

await runAsync(
'Moving files back...',
Expand All @@ -81,11 +81,9 @@ Re-run with the "check" command for more details.`);
let decaffeinateCommitMsg =
`decaffeinate: Convert ${shortDescription} to JS`;
console.log(`Generating the second commit: ${decaffeinateCommitMsg}...`);
await makeCommit(async function(index, resolvePath) {
await index.addAll(baseFiles.map(p => resolvePath(`${p}.js`)), 0, null, null);
}, decaffeinateCommitMsg, 'decaffeinate');

let jsFiles = baseFiles.map(f => `${f}.js`);
await git().add(jsFiles);
await makeCommit(decaffeinateCommitMsg);

if (config.jscodeshiftScripts) {
for (let scriptPath of config.jscodeshiftScripts) {
Expand Down Expand Up @@ -152,9 +150,8 @@ Re-run with the "check" command for more details.`);
let postProcessCommitMsg =
`decaffeinate: Run post-processing cleanups on ${shortDescription}`;
console.log(`Generating the third commit: ${postProcessCommitMsg}...`);
await makeCommit(async function(index, resolvePath) {
await index.addAll(thirdCommitModifiedFiles.map(resolvePath), 0, null, null);
}, postProcessCommitMsg, 'decaffeinate');
await git().add(thirdCommitModifiedFiles);
await makeCommit(postProcessCommitMsg);

console.log(`Successfully ran decaffeinate on ${pluralize(baseFiles.length, 'file')}.`);
console.log('You should now fix lint issues in any affected files.');
Expand Down
29 changes: 6 additions & 23 deletions src/util/makeCommit.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import Git from 'nodegit';
import path from 'path';
import git from 'simple-git/promise';

/**
* Use nodegit to create a git commit at HEAD.
* Make an autogenerated commit with the "decaffeinate" author.
*/
export default async function makeCommit(indexTransform, commitMessage, overrideAuthorName) {
let repo = await Git.Repository.openExt('.', 0, '');
let index = await repo.refreshIndex();
let resolvePath = (filePath) => path.relative(`${repo.path()}/..`, filePath);
await indexTransform(index, resolvePath);
await index.write();
let treeOid = await index.writeTree();
let head = await repo.getHeadCommit();

let signature = repo.defaultSignature();
let authorName = overrideAuthorName ? overrideAuthorName : signature.name();
let authorSignature = Git.Signature.create(
authorName, signature.email(), signature.when().time(), signature.when().offset());
await repo.createCommit(
'HEAD',
authorSignature,
signature,
commitMessage,
treeOid,
head ? [head] : null);
export default async function makeCommit(commitMessage) {
const userEmail = await git().raw(['config', 'user.email']);
const author = `decaffeinate <${userEmail}>`;
await git().commit(commitMessage, {'--author': author, '--no-verify': null});
}

0 comments on commit a8e8716

Please sign in to comment.