Skip to content

Commit

Permalink
fix(gitUtils): support new repos / unborn branches
Browse files Browse the repository at this point in the history
This is not tested at the moment due to a mock-git limitation.
  • Loading branch information
motiz88 committed May 10, 2017
1 parent 9e06bbd commit 130b36b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`git-exec-and-restage no command, no args and no files staged 1`] = `[Er
exports[`git-exec-and-restage with command and arguments, no files 1`] = `
Array [
"git diff-index --cached --name-only --diff-filter=ACDMRTUXB HEAD",
"git diff-index --cached --name-only --diff-filter=ACDMRTUXB HEAD --",
"git diff --name-only --diff-filter=ACDMRTUXB",
"prettier --write",
]
Expand All @@ -20,7 +20,7 @@ Array [
exports[`git-exec-and-restage with command+args, files implicit 1`] = `
Array [
"git diff-index --cached --name-only --diff-filter=ACDMRTUXB HEAD",
"git diff-index --cached --name-only --diff-filter=ACDMRTUXB HEAD --",
"git diff --name-only --diff-filter=ACDMRTUXB fullystaged.js partiallystaged.js",
"prettier --write",
"git add fullystaged.js",
Expand Down Expand Up @@ -53,7 +53,7 @@ Array [
exports[`git-exec-and-restage with command, no args and no files 1`] = `
Array [
"git diff-index --cached --name-only --diff-filter=ACDMRTUXB HEAD",
"git diff-index --cached --name-only --diff-filter=ACDMRTUXB HEAD --",
"git diff --name-only --diff-filter=ACDMRTUXB",
"prettier",
]
Expand Down
22 changes: 20 additions & 2 deletions src/gitUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ function ensureRelativePath(s) {
return s;
}

async function getAllStaged() {
const GIT_EMPTY_HASH = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";

async function getAllStagedFromRevision(revision) {
const diffOut =
(await spawn(
"git",
Expand All @@ -17,7 +19,8 @@ async function getAllStaged() {
"--cached",
"--name-only",
"--diff-filter=ACDMRTUXB",
"HEAD"
revision,
"--"
],
{
encoding: "utf8"
Expand All @@ -26,6 +29,21 @@ async function getAllStaged() {
return diffOut.split("\n").filter(s => s !== "");
}

async function getAllStaged() {
try {
return await getAllStagedFromRevision("HEAD");
} catch (e) {
// istanbul ignore else: simple exception passthrough
if (
e.stderr &&
e.stderr.toString().indexOf("fatal: bad revision 'HEAD'") !== -1
) {
return await getAllStagedFromRevision(GIT_EMPTY_HASH);
}
throw e;
}
}

async function getFullyStaged(
/* istanbul ignore next: convenience default */
files = []
Expand Down

0 comments on commit 130b36b

Please sign in to comment.