@@ -3,6 +3,17 @@ var path = require('path');
33var gitUrlParse = require ( 'git-url-parse' ) ;
44var getRemoteOrigin = require ( 'remote-origin-url' ) ;
55
6+ function parsePackedRefs ( packedRefs , branchName ) {
7+ return packedRefs . split ( / \n / )
8+ . filter ( function ( line ) {
9+ return line [ 0 ] !== '#' && line [ 0 ] !== '^' ;
10+ } )
11+ . reduce ( function ( memo , line ) {
12+ memo [ line . split ( ' ' ) [ 1 ] ] = line . split ( ' ' ) [ 0 ] ;
13+ return memo ;
14+ } , { } ) [ branchName ] ;
15+ }
16+
617/**
718 * Given a a root directory, find its git configuration and figure out
819 * the HTTPS URL at the base of that GitHub repository.
@@ -16,21 +27,18 @@ function getGithubURLPrefix(root) {
1627 var head = fs . readFileSync ( path . join ( root , '.git' , 'HEAD' ) , 'utf8' ) ;
1728 var branch = head . match ( / r e f \: ( .* ) / ) ;
1829 if ( branch ) {
19- var branchFileName = path . join ( root , '.git' , branch [ 1 ] ) ;
30+ var branchName = branch [ 1 ] ;
31+ var branchFileName = path . join ( root , '.git' , branchName ) ;
2032 var packedRefsName = path . join ( root , '.git' , 'packed-refs' ) ;
2133 var sha ;
2234 if ( fs . existsSync ( branchFileName ) ) {
2335 sha = fs . readFileSync ( branchFileName , 'utf8' ) ;
2436 } else if ( fs . existsSync ( packedRefsName ) ) {
25- sha = fs . readFileSync ( packedRefsName , 'utf8' )
26- . split ( / \n / )
27- . filter ( function ( line ) {
28- return line [ 0 ] !== '#' && line [ 0 ] !== '^' ;
29- } )
30- . reduce ( function ( memo , line ) {
31- memo [ line . split ( ' ' ) [ 1 ] ] = line . split ( ' ' ) [ 0 ] ;
32- return memo ;
33- } , { } ) [ branch [ 1 ] ] ;
37+ // packed refs are a compacted version of the refs folder. usually
38+ // you have a folder filled with files that just contain sha
39+ // hashes. since this folder can be really big, packed refs
40+ // stores all the refs in one file instead.
41+ sha = parsePackedRefs ( fs . readFileSync ( packedRefsName , 'utf8' ) , branchName ) ;
3442 }
3543 } else {
3644 sha = head ;
@@ -42,3 +50,4 @@ function getGithubURLPrefix(root) {
4250}
4351
4452module . exports = getGithubURLPrefix ;
53+ module . exports . parsePackedRefs = parsePackedRefs ;
0 commit comments