-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
95 lines (73 loc) · 2.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'use strict';
const fs = require( 'fs' );
const glob = require( 'glob' );
const ObjManage = require( 'object-manage' );
const path = require( 'path' );
module.exports = function ( gitDirectory, callback ) {
let defaults = {
'heads' : {},
'remotes' : {}
};
let refs = new ObjManage( defaults );
// Use the current .git folder if not passed in
if ( typeof gitDirectory === 'function' ) {
callback = gitDirectory;
gitDirectory = '.git';
}
let options = {
'glob' : { 'nodir' : true },
'read' : { 'encoding' : 'utf8' }
};
function getRefsFiles ( directory ) {
// Find all the files in the refs folder
let files = glob.sync( path.join( directory, 'refs', '**', '*' ), options.glob );
files.forEach( function ( filePath ) {
let currentRef = filePath.replace( path.join( directory, 'refs' ) + path.sep, '' ).replace( /[\/]/g, '.' );
let fileContents = fs.readFileSync( filePath, options.read ).replace( /\n/, '' );
refs.$set( currentRef, fileContents );
} );
}
function getRefsPacked ( directory ) {
try {
let packedFile = path.join( directory, 'packed-refs' );
// See if the file exists before trying to read it
fs.accessSync( packedFile, fs.F_OK );
let file = fs.readFileSync( packedFile, options.read );
let fileLines = file.split( '\n' );
fileLines.forEach( function ( line ) {
line = line.split( ' ' );
// Only parse commit lines
// TODO: update to handle tags properly. It shows the tag commit hash instead of what it's point at.
if ( line[ 0 ].length === 40 ) {
line[ 1 ] = line[ 1 ].replace( 'refs' + path.sep, '' ).replace( /[\/]/g, '.' );
refs.$set( line[ 1 ], line[ 0 ] );
}
} );
} catch ( error ) {
return;
}
}
function getRefs ( directory ) {
getRefsPacked( directory );
getRefsFiles( directory );
// Set the current head
let current = fs.readFileSync( path.join( directory, 'HEAD' ), options.read ).replace( /\n/, '' );
refs.$set( 'current.head', current );
refs.$set( 'current.ref', null );
// Find actual reference if it points to another location
if ( current.match( 'ref' ) ) {
let referencePath = current.split( /\s+/ )[ 1 ];
refs.$set( 'current.ref', referencePath );
// Get the key to find the reference
let key = referencePath.replace( /refs[\/]/, '' ).split( /[\/]/ );
refs.$set( 'current.head', refs.$get( key ) );
}
return refs;
}
try {
fs.accessSync( gitDirectory, fs.F_OK );
callback( null, getRefs( gitDirectory ) );
} catch ( error ) {
callback( error );
}
};