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

Commit 1869940

Browse files
artemvzkat
authored andcommitted
fix(git): support git packed refs --all mode (#77)
* test: git-pack-refs vs gitHead also, let's have gitHead tests for both refs-packed and non-packed modes re #76 (git packed refs --all mode not supported) * Fall back to .git/packed-refs if .git/<ref> does not exist * refactor: minor
1 parent b5a9f47 commit 1869940

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

read-json.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,26 @@ function githead_ (file, data, dir, head, cb) {
339339
data.gitHead = head.trim()
340340
return cb(null, data)
341341
}
342-
var headFile = head.replace(/^ref: /, '').trim()
343-
headFile = path.resolve(dir, '.git', headFile)
342+
var headRef = head.replace(/^ref: /, '').trim()
343+
var headFile = path.resolve(dir, '.git', headRef)
344344
fs.readFile(headFile, 'utf8', function (er, head) {
345-
if (er || !head) return cb(null, data)
345+
if (er || !head) {
346+
var packFile = path.resolve(dir, '.git/packed-refs')
347+
return fs.readFile(packFile, 'utf8', function (er, refs) {
348+
if (er || !refs) {
349+
return cb(null, data)
350+
}
351+
refs = refs.split('\n')
352+
for (var i = 0; i < refs.length; i++) {
353+
var match = refs[i].match(/^([0-9a-f]{40}) (.+)$/)
354+
if (match && match[2].trim() === headRef) {
355+
data.gitHead = match[1]
356+
break
357+
}
358+
}
359+
return cb(null, data)
360+
})
361+
}
346362
head = head.replace(/^ref: /, '').trim()
347363
data.gitHead = head
348364
return cb(null, data)

test/gitHead.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
var childProcess = require('child_process')
2+
var fs = require('fs')
3+
var path = require('path')
4+
5+
var tap = require('tap')
6+
7+
var readJson = require('../')
8+
9+
var isGit
10+
try {
11+
fs.readFileSync(path.resolve(__dirname, '../.git/HEAD'))
12+
isGit = true
13+
} catch (e) {
14+
isGit = false
15+
}
16+
17+
if (isGit) {
18+
tap.test('gitHead tests', function (t) {
19+
t.plan(3)
20+
21+
const repoProjectName = 'read-package-json'
22+
const repo = 'https://github.com/npm/' + repoProjectName + '.git'
23+
var repoDirs = []
24+
25+
t.test('detached case', function (tt) {
26+
var p = path.resolve(__dirname, '..', 'package.json')
27+
readJson(p, function (er, data) {
28+
if (er) throw er
29+
tt.ok(data)
30+
tt.similar(data.gitHead, /^[a-f0-9]{40}$/)
31+
tt.end()
32+
})
33+
})
34+
35+
function testGitRepo (kind, extraRepoCommand, t) {
36+
var repoDirName = repoProjectName + '-' + kind
37+
var cmd = `cd ${__dirname} && git clone ${repo} ${repoDirName} && cd ${repoDirName}`
38+
if (extraRepoCommand) cmd += ` && ${extraRepoCommand}`
39+
childProcess.execSync(cmd)
40+
repoDirs.push(repoDirName)
41+
var p = path.resolve(__dirname, repoDirName, 'package.json')
42+
readJson(p, function (er, data) {
43+
if (er) throw er
44+
t.ok(data)
45+
t.similar(data.gitHead, /^[a-f0-9]{40}$/)
46+
t.end()
47+
})
48+
}
49+
50+
t.test('basic case', function (tt) {
51+
testGitRepo('basic', '', tt)
52+
})
53+
54+
t.test('git-pack-refs vs gitHead', function (tt) {
55+
testGitRepo('git-pack-refs', 'git pack-refs --all', tt)
56+
})
57+
58+
t.tearDown(function () {
59+
repoDirs.forEach(function (d) {
60+
childProcess.execSync(`rm -rf ${path.resolve(__dirname, d)}`)
61+
})
62+
})
63+
})
64+
}

0 commit comments

Comments
 (0)