From 1b85cf87f5875774e7fbc617f606923d8cbda6a4 Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Sat, 22 Dec 2018 13:33:33 +0100 Subject: [PATCH] Add gitHead in subdirectories too --- read-json.js | 12 +++++++++--- test/gitHead.js | 14 +++++++++----- test/non-json.js | 2 ++ test/readmes.js | 1 + 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/read-json.js b/read-json.js index 243af8e..976527d 100644 --- a/read-json.js +++ b/read-json.js @@ -329,12 +329,18 @@ function githead (file, data, cb) { var dir = path.dirname(file) var head = path.resolve(dir, '.git/HEAD') fs.readFile(head, 'utf8', function (er, head) { - if (er) return cb(null, data) - githead_(file, data, dir, head, cb) + if (er) { + var parent = path.dirname(dir) + if (parent === dir) { + return cb(null, data) + } + return githead(dir, data, cb) + } + githead_(data, dir, head, cb) }) } -function githead_ (file, data, dir, head, cb) { +function githead_ (data, dir, head, cb) { if (!head.match(/^ref: /)) { data.gitHead = head.trim() return cb(null, data) diff --git a/test/gitHead.js b/test/gitHead.js index 395101d..907de81 100644 --- a/test/gitHead.js +++ b/test/gitHead.js @@ -16,7 +16,7 @@ try { if (isGit) { tap.test('gitHead tests', function (t) { - t.plan(3) + t.plan(4) const repoProjectName = 'read-package-json' const repo = 'https://github.com/npm/' + repoProjectName + '.git' @@ -32,13 +32,13 @@ if (isGit) { }) }) - function testGitRepo (kind, extraRepoCommand, t) { + function testGitRepo (kind, file, extraRepoCommand, t) { var repoDirName = repoProjectName + '-' + kind var cmd = `cd ${__dirname} && git clone ${repo} ${repoDirName} && cd ${repoDirName}` if (extraRepoCommand) cmd += ` && ${extraRepoCommand}` childProcess.execSync(cmd) repoDirs.push(repoDirName) - var p = path.resolve(__dirname, repoDirName, 'package.json') + var p = path.resolve(__dirname, repoDirName, file) readJson(p, function (er, data) { if (er) throw er t.ok(data) @@ -48,11 +48,15 @@ if (isGit) { } t.test('basic case', function (tt) { - testGitRepo('basic', '', tt) + testGitRepo('basic', 'package.json', '', tt) + }) + + t.test('subdirectory', function (tt) { + testGitRepo('subdir', 'test/fixtures/bin.json', '', tt) }) t.test('git-pack-refs vs gitHead', function (tt) { - testGitRepo('git-pack-refs', 'git pack-refs --all', tt) + testGitRepo('git-pack-refs', 'package.json', 'git pack-refs --all', tt) }) t.tearDown(function () { diff --git a/test/non-json.js b/test/non-json.js index 8518632..9bb439c 100644 --- a/test/non-json.js +++ b/test/non-json.js @@ -40,6 +40,7 @@ var expect = { tap.test('from css', function (t) { var c = path.join(__dirname, 'fixtures', 'not-json.css') readJson(c, function (er, d) { + delete d.gitHead t.same(d, expect) t.end() }) @@ -47,6 +48,7 @@ tap.test('from css', function (t) { tap.test('from js', function (t) { readJson(__filename, function (er, d) { + delete d.gitHead t.same(d, expect) t.end() }) diff --git a/test/readmes.js b/test/readmes.js index bd89177..b148910 100644 --- a/test/readmes.js +++ b/test/readmes.js @@ -22,6 +22,7 @@ tap.test('readme test', function (t) { }) function test (t, data) { + delete data.gitHead t.deepEqual(data, expect) t.end() }