forked from heapwolf/jsgit-stream
/
repo.js
167 lines (146 loc) · 4.96 KB
/
repo.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// This class encapsulates an on-disk Repo
//
module.exports = function Repo(gitDir) {
var that = {}
that.dir = path.join(gitDir, "..")
that.gitDir = gitDir
if (!path.existsSync(that.dir)) {
throw "Error: " + that.dir + " does not exist"
}
if (!path.existsSync(that.gitDir)) {
throw "Error: " + that.gitDir + " does not exist"
}
var firstIndexOfByte = function(bytes, b) {
var i = 0
while (bytes.getByteAt(i) != b) i += 1
return i
}
// If an object exists loose, returns it. Otherwise, returns null
that.getLooseObject = function(sha, callback) {
if (sha == "" || !sha) { return callback(err, null) }
var objectPath = path.join(that.gitDir, "/objects/" + sha.slice(0, 2) + "/" + sha.slice(2))
fs.readFile(objectPath, function(err, data) {
if (err) return callback(null, null)
var compressedData = data.toString('binary')
var deflated = Git.stripZlibHeader(Git.stringToBytes(compressedData))
var uncompressedData = new Buffer(RawDeflate.inflate(Git.bytesToString(deflated)).toString())
var result = Git.bytesToString(uncompressedData.slice(0))
var objectType = result.slice(0, 4)
var objectData = result.slice(firstIndexOfByte(uncompressedData, 0) + 1)
var obj = Git.objects.make(sha, objectType, objectData)
callback(null, obj)
})
}
// Calls callback with a list of PackFiles
that.getPackFiles = function(callback) {
fs.readdir(path.join(that.gitDir, "/objects/pack"), function(err, files) {
var packFiles = []
files.forEach(function(file) {
var match = /pack-(.*)\.idx/.exec(file)
if (match) {
packFiles.push(new Git.PackFile(that, match[1]))
}
})
callback(packFiles)
})
}
// Get the object with the specified sha, or null if it doesn't exist in
// this repo.
// callback takes (err, object)
that.getObject = function(sha, callback) {
if (sha == "" || !sha) return callback(null)
if (sha.slice(-1) == "\n") sha = sha.slice(0, sha.length - 1)
that.getLooseObject(sha, function(err, object) {
if (err) return callback(err)
if (object) {
return callback(null, object)
} else {
that.getPackFiles(function(packFiles) {
for(var i = 0; i < packFiles.length; i++) {
;(function(ix) {
packFiles[ix].getObject(sha, function(object) {
if (object) {
return callback(null, object)
} else {
if (ix == packFiles.length - 1 ) {
return callback(null, null)
}
}
})
})(i)
}
})
}
})
return null
}
// Get an array of heads [[name, sha], ...].
// Callback receives(err, data)
that.getBranches = function(cb) {
var headsDir = path.join(that.gitDir, "/refs/heads")
var result = {branchNames: []}
return fs.readdir(headsDir, parseLooseHeadFiles)
function parseLooseHeadFiles(err, files) {
if (err) return cb(err)
if (files.length == 0) return parsePackedRefFile()
for(var i = 0; i < files.length; i++) {
(function(ix) {
var headPath = path.join(headsDir, files[ix])
return fs.readFile(headPath, function(err, contents) {
if (!err) {
result.branchNames.push(files[ix])
result[files[ix]] = contents.toString("ascii")
}
if (ix == files.length - 1) {
return parsePackedRefFile()
}
})
})(i)
}
}
function parsePackedRefFile() {
var packedRefPath = path.join(that.gitDir, "/packed-refs")
fs.readFile(packedRefPath, function(err, contents) {
if (err) {
if (err.code == "ENOENT") {
return cb(null, result)
} else {
return cb(err)
}
}
var lines = contents.toString("ascii").split("\n")
for(var i = 0; i < lines.length; i++) {
var line = lines[i]
if (line.match(/^\s*#/) || line.match(/^\s*$/)) {
} else {
var bits = lines[i].split(" ")
var sha = bits[0]
var refPath = bits[1]
var nameMatch = refPath.match(/^refs\/heads\/(.*)$/)
if (nameMatch) {
result.branchNames.push(nameMatch[1])
result[nameMatch[1]] = sha
}
}
}
return cb(null, result)
})
}
}
// Get the current head. Callback receives (err, data)
that.getHead = function(cb) {
var headFile = path.join(that.gitDir, "/HEAD")
fs.readFile(headFile, function(err, contents) {
if (err) return cb(err)
var head = contents.toString("ascii")
var match = /ref: refs\/heads\/(.*)\n$/.exec(head)
if (match) {
cb(null, match[1])
} else {
cb(null, null)
}
})
}
return that
};