Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Add gitHead in subdirectories too #80

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions read-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be githead_?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so - githead() reads the .git/HEAD file (if found) and passes the content to githead_(). So to traverse up the tree and find the .git/HEAD file, we need to call githead() recursively.
githead_() is what then handles the contents of the .git/HEAD file by reading the ref it points to etc.

It's easy to get confused though with the naming of the functions and no comments...

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)
Expand Down
14 changes: 9 additions & 5 deletions test/gitHead.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand All @@ -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 () {
Expand Down
2 changes: 2 additions & 0 deletions test/non-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ 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()
})
})

tap.test('from js', function (t) {
readJson(__filename, function (er, d) {
delete d.gitHead
t.same(d, expect)
t.end()
})
Expand Down
1 change: 1 addition & 0 deletions test/readmes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tap.test('readme test', function (t) {
})

function test (t, data) {
delete data.gitHead
t.deepEqual(data, expect)
t.end()
}