@@ -30,34 +30,48 @@ function findRepo(startingPath) {
30
30
return null ;
31
31
}
32
32
33
- function findPackedTag ( gitPath , sha ) {
34
- var packedRefsFilePath = path . join ( gitPath , 'packed-refs' ) ;
35
- 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 ] ;
40
-
41
- if ( tagLine ) {
42
- return tagLine . split ( 'tags/' ) [ 1 ] ;
43
- }
44
- }
33
+ function findPackedTag ( gitPath , refPath ) {
34
+ return getPackedRefsForType ( gitPath , refPath , 'tag' ) ;
45
35
}
46
36
47
37
function findPackedCommit ( gitPath , refPath ) {
38
+ return getPackedRefsForType ( gitPath , refPath , 'commit' ) ;
39
+ }
40
+
41
+ function getPackedRefsForType ( gitPath , refPath , type ) {
48
42
var packedRefsFilePath = path . join ( gitPath , 'packed-refs' ) ;
49
43
if ( fs . existsSync ( packedRefsFilePath ) ) {
50
44
var packedRefsFile = fs . readFileSync ( packedRefsFilePath , { encoding : 'utf8' } ) ;
51
- var shaLine = packedRefsFile . split ( '\n' ) . filter ( function ( line ) {
52
- return line . indexOf ( 'refs/heads' ) > - 1 && line . indexOf ( refPath ) > - 1 ;
53
- } ) [ 0 ] ;
45
+ var shaLine = getLineForRefPath ( packedRefsFile , type , refPath ) ;
54
46
55
47
if ( shaLine ) {
56
- return shaLine . split ( ' ' ) [ 0 ] ;
48
+ return getShaBasedOnType ( type , shaLine ) ;
57
49
}
58
50
}
59
51
}
60
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
+
61
75
function commitForTag ( gitPath , tag ) {
62
76
var tagPath = path . join ( gitPath , 'refs' , 'tags' , tag ) ;
63
77
var taggedObject = fs . readFileSync ( tagPath , { encoding : 'utf8' } ) . trim ( ) ;
@@ -71,7 +85,8 @@ function commitForTag(gitPath, tag) {
71
85
72
86
var objectContents = zlib . inflateSync ( fs . readFileSync ( objectPath ) ) . toString ( ) ;
73
87
74
- // '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'
75
90
if ( objectContents . slice ( 0 , 3 ) === 'tag' ) {
76
91
var sections = objectContents . split ( / \0 | \n / ) ;
77
92
var sha = sections [ 1 ] . slice ( 7 ) ;
@@ -130,13 +145,10 @@ module.exports = function(gitPath) {
130
145
var branchPath = path . join ( gitPath , refPath ) ;
131
146
132
147
result . branch = branchName ;
133
- try {
148
+ if ( fs . existsSync ( branchPath ) ) {
134
149
result . sha = fs . readFileSync ( branchPath , { encoding : 'utf8' } ) . trim ( ) ;
135
- } catch ( err ) {
136
- if ( err . code === 'ENOENT' ) {
137
- console . log ( 'Could not find ' + branchPath + ' trying to find ref in packed-refs instead.' ) ;
138
- result . sha = findPackedCommit ( gitPath , refPath ) ;
139
- }
150
+ } else {
151
+ result . sha = findPackedCommit ( gitPath , refPath ) ;
140
152
}
141
153
} else {
142
154
result . sha = branchName ;
0 commit comments