1
1
const fs = require ( 'fs' ) ;
2
2
const path = require ( 'path' ) ;
3
3
const gitUrlParse = require ( 'git-url-parse' ) ;
4
- const getRemoteOrigin = require ( 'remote-origin-url ' ) ;
4
+ const ini = require ( 'ini ' ) ;
5
5
6
6
/**
7
7
* Sometimes git will [pack refs](https://git-scm.com/docs/git-pack-refs)
@@ -32,15 +32,15 @@ function parsePackedRefs(packedRefs, branchName) {
32
32
* @returns {string } base HTTPS url of the GitHub repository
33
33
* @throws {Error } if the root is not a git repo
34
34
*/
35
- function getGithubURLPrefix ( root ) {
35
+ function getGithubURLPrefix ( { git , root } ) {
36
36
let sha ;
37
37
try {
38
- const head = fs . readFileSync ( path . join ( root , '. git' , 'HEAD' ) , 'utf8' ) ;
38
+ const head = fs . readFileSync ( path . join ( git , 'HEAD' ) , 'utf8' ) ;
39
39
const branch = head . match ( / r e f : ( .* ) / ) ;
40
40
if ( branch ) {
41
41
const branchName = branch [ 1 ] ;
42
- const branchFileName = path . join ( root , '. git' , branchName ) ;
43
- const packedRefsName = path . join ( root , '. git' , 'packed-refs' ) ;
42
+ const branchFileName = path . join ( git , branchName ) ;
43
+ const packedRefsName = path . join ( git , 'packed-refs' ) ;
44
44
if ( fs . existsSync ( branchFileName ) ) {
45
45
sha = fs . readFileSync ( branchFileName , 'utf8' ) ;
46
46
} else if ( fs . existsSync ( packedRefsName ) ) {
@@ -57,7 +57,19 @@ function getGithubURLPrefix(root) {
57
57
sha = head ;
58
58
}
59
59
if ( sha ) {
60
- const parsed = gitUrlParse ( getRemoteOrigin . sync ( root ) ) ;
60
+ let origin ;
61
+ if ( git . indexOf ( root ) === 0 ) {
62
+ const config = ini . parse (
63
+ fs . readFileSync ( path . join ( git , 'config' ) , 'utf8' )
64
+ ) ;
65
+ origin = config [ 'remote "origin"' ] . url ;
66
+ } else {
67
+ const config = ini . parse (
68
+ fs . readFileSync ( path . join ( git , '..' , '..' , 'config' ) , 'utf8' )
69
+ ) ;
70
+ origin = config [ `submodule "${ path . basename ( git ) } "` ] . url ;
71
+ }
72
+ const parsed = gitUrlParse ( origin ) ;
61
73
parsed . git_suffix = false ; // eslint-disable-line
62
74
return parsed . toString ( 'https' ) + '/blob/' + sha . trim ( ) + '/' ;
63
75
}
0 commit comments