@@ -30,20 +30,48 @@ function findRepo(startingPath) {
30
30
return null ;
31
31
}
32
32
33
- function findPackedTag ( gitPath , sha ) {
33
+ function findPackedTag ( gitPath , refPath ) {
34
+ return getPackedRefsForType ( gitPath , refPath , 'tag' ) ;
35
+ }
36
+
37
+ function findPackedCommit ( gitPath , refPath ) {
38
+ return getPackedRefsForType ( gitPath , refPath , 'commit' ) ;
39
+ }
40
+
41
+ function getPackedRefsForType ( gitPath , refPath , type ) {
34
42
var packedRefsFilePath = path . join ( gitPath , 'packed-refs' ) ;
35
43
if ( fs . existsSync ( packedRefsFilePath ) ) {
36
- var packedRefsFile = fs . readFileSync ( packedRefsFilePath , { encoding : 'utf8' } ) ;
37
- var tagLine = packedRefsFile . split ( '\n' ) . filter ( function ( line ) {
38
- return line . indexOf ( 'refs/tags' ) > - 1 && line . indexOf ( sha ) > - 1 ;
39
- } ) [ 0 ] ;
44
+ var packedRefsFile = fs . readFileSync ( packedRefsFilePath , { encoding : 'utf8' } ) ;
45
+ var shaLine = getLineForRefPath ( packedRefsFile , type , refPath ) ;
40
46
41
- if ( tagLine ) {
42
- return tagLine . split ( 'tags/' ) [ 1 ] ;
47
+ if ( shaLine ) {
48
+ return getShaBasedOnType ( type , shaLine ) ;
43
49
}
44
50
}
45
51
}
46
52
53
+ function getLineForRefPath ( packedRefsFile , type , refPath ) {
54
+ return packedRefsFile . split ( '\n' ) . filter ( function ( line ) {
55
+ return doesLineMatchRefPath ( type , line , refPath ) ;
56
+ } ) [ 0 ] ;
57
+ }
58
+
59
+ function doesLineMatchRefPath ( type , line , refPath ) {
60
+ var refPrefix = type === 'tag' ? 'refs/tags' : 'refs/heads' ;
61
+ return line . indexOf ( refPrefix ) > - 1 && line . indexOf ( refPath ) > - 1 ;
62
+ }
63
+
64
+ function getShaBasedOnType ( type , shaLine ) {
65
+ var shaResult = '' ;
66
+ if ( type === 'tag' ) {
67
+ shaResult = shaLine . split ( 'tags/' ) [ 1 ] ;
68
+ } else if ( type === 'commit' ) {
69
+ shaResult = shaLine . split ( ' ' ) [ 0 ] ;
70
+ }
71
+
72
+ return shaResult ;
73
+ }
74
+
47
75
function commitForTag ( gitPath , tag ) {
48
76
var tagPath = path . join ( gitPath , 'refs' , 'tags' , tag ) ;
49
77
var taggedObject = fs . readFileSync ( tagPath , { encoding : 'utf8' } ) . trim ( ) ;
@@ -57,7 +85,8 @@ function commitForTag(gitPath, tag) {
57
85
58
86
var objectContents = zlib . inflateSync ( fs . readFileSync ( objectPath ) ) . toString ( ) ;
59
87
60
- // 'tag 172\u0000object c1ee41c325d54f410b133e0018c7a6b1316f6cda\ntype commit\ntag awesome-tag\ntagger Robert Jackson <[email protected] > 1429100021 -0400\n\nI am making an annotated tag.\n'
88
+ // 'tag 172\u0000object c1ee41c325d54f410b133e0018c7a6b1316f6cda\ntype commit\ntag awesome-tag\ntagger Robert Jackson
89
+ // <[email protected] > 1429100021 -0400\n\nI am making an annotated tag.\n'
61
90
if ( objectContents . slice ( 0 , 3 ) === 'tag' ) {
62
91
var sections = objectContents . split ( / \0 | \n / ) ;
63
92
var sha = sections [ 1 ] . slice ( 7 ) ;
@@ -112,10 +141,15 @@ module.exports = function(gitPath) {
112
141
113
142
// Find branch and SHA
114
143
if ( refPath ) {
115
- var branchPath = path . join ( gitPath , refPath . trim ( ) ) ;
144
+ refPath = refPath . trim ( ) ;
145
+ var branchPath = path . join ( gitPath , refPath ) ;
116
146
117
147
result . branch = branchName ;
118
- result . sha = fs . readFileSync ( branchPath , { encoding : 'utf8' } ) . trim ( ) ;
148
+ if ( fs . existsSync ( branchPath ) ) {
149
+ result . sha = fs . readFileSync ( branchPath , { encoding : 'utf8' } ) . trim ( ) ;
150
+ } else {
151
+ result . sha = findPackedCommit ( gitPath , refPath ) ;
152
+ }
119
153
} else {
120
154
result . sha = branchName ;
121
155
}
0 commit comments