Skip to content

Commit 8b9fd6e

Browse files
Refactor to use CommonJS idioms.
- Holy god, I'm sorry for this patchbomb. - This also includes some other fixes. Sorry. * the author parser in lib/git/objects.js only matched one side of UTC (+0000 vs -0000). * the repo branch collector failed on branch names that contain a slash (e.g, "feature/refactor", etc). - All tests pass. - All client commands seem to still work.
1 parent 3927842 commit 8b9fd6e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1546
-1462
lines changed

bin/git.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env node
22

3-
require('../lib/git-server')
3+
var Cli = require('../lib/git/cli')
44

55
// Get rid of process runner ('node' in most cases)
66
var arg, args = [], argv = process.argv.slice(2);
77

8-
var cli = new Git.Cli(argv)
8+
var cli = new Cli(argv)
99
cli.run()
1010

lib/binary_file.js

+108-90
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,117 @@
1+
var btoa = this.btoa || require('./btoa');
12

2-
BinaryFile = function(strData, iDataOffset, iDataLength) {
3-
var data = strData;
4-
var dataOffset = iDataOffset || 0;
5-
var dataLength = 0;
3+
// these are created (dynamically!) via
4+
// a <script type="text/vbscript"> insertion.
5+
var IEBinary_getLength = this.IEBinary_getLength
6+
, IEBinary_getByteAt = this.IEBinary_getByteAt;
67

7-
this.getRawData = function() {
8-
return data;
9-
}
10-
11-
this.slice = function(begin, optionalEnd) {
12-
var arr = [];
13-
var end = optionalEnd || this.getLength();
14-
var i;
15-
for (i = begin; i < end; i++) {
16-
arr.push(this.getByteAt(i));
17-
}
18-
return arr;
19-
}
8+
BinaryFile = function(strData, iDataOffset, iDataLength) {
9+
this.data = strData;
10+
this.dataOffset = iDataOffset || 0;
11+
this.dataLength = 0;
2012

2113
if (typeof strData == "string") {
22-
dataLength = iDataLength || data.length;
23-
24-
this.getByteAt = function(iOffset) {
25-
return data.charCodeAt(iOffset + dataOffset) & 0xFF;
26-
}
14+
this.dataLength = iDataLength || this.data.length;
15+
this.getByteAt = this.getByteAt_CharCode;
2716
} else if (typeof strData == "unknown") {
28-
dataLength = iDataLength || IEBinary_getLength(data);
29-
30-
this.getByteAt = function(iOffset) {
31-
return IEBinary_getByteAt(data, iOffset + dataOffset);
32-
}
17+
this.dataLength = iDataLength || IEBinary_getLength(this.data);
18+
this.getByteAt = this.getByteAt_IEBinary;
3319
} else {
34-
throw(Error("BinaryFile was given a " + (typeof strData) + " and doesn't know what to do with it"))
20+
throw new Error(
21+
"BinaryFile was given a " + (typeof strData) + " and doesn't know what to do with it"
22+
);
3523
}
24+
};
3625

37-
this.getLength = function() {
38-
return dataLength;
39-
}
40-
41-
this.getSByteAt = function(iOffset) {
42-
var iByte = this.getByteAt(iOffset);
43-
if (iByte > 127)
44-
return iByte - 256;
45-
else
46-
return iByte;
47-
}
48-
49-
this.getShortAt = function(iOffset, bBigEndian) {
50-
var iShort = bBigEndian ?
51-
(this.getByteAt(iOffset) << 8) + this.getByteAt(iOffset + 1)
52-
: (this.getByteAt(iOffset + 1) << 8) + this.getByteAt(iOffset)
53-
if (iShort < 0) iShort += 65536;
54-
return iShort;
55-
}
56-
this.getSShortAt = function(iOffset, bBigEndian) {
57-
var iUShort = this.getShortAt(iOffset, bBigEndian);
58-
if (iUShort > 32767)
59-
return iUShort - 65536;
60-
else
61-
return iUShort;
62-
}
63-
this.getLongAt = function(iOffset, bBigEndian) {
64-
var iByte1 = this.getByteAt(iOffset),
65-
iByte2 = this.getByteAt(iOffset + 1),
66-
iByte3 = this.getByteAt(iOffset + 2),
67-
iByte4 = this.getByteAt(iOffset + 3);
68-
69-
var iLong = bBigEndian ?
70-
(((((iByte1 << 8) + iByte2) << 8) + iByte3) << 8) + iByte4
71-
: (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1;
72-
if (iLong < 0) iLong += 4294967296;
73-
return iLong;
74-
}
75-
this.getSLongAt = function(iOffset, bBigEndian) {
76-
var iULong = this.getLongAt(iOffset, bBigEndian);
77-
if (iULong > 2147483647)
78-
return iULong - 4294967296;
79-
else
80-
return iULong;
81-
}
82-
this.getStringAt = function(iOffset, iLength) {
83-
var aStr = [];
84-
for (var i=iOffset,j=0;i<iOffset+iLength;i++,j++) {
85-
aStr[j] = String.fromCharCode(this.getByteAt(i));
86-
}
87-
return aStr.join("");
88-
}
89-
90-
this.getCharAt = function(iOffset) {
91-
return String.fromCharCode(this.getByteAt(iOffset));
92-
}
93-
this.toBase64 = function() {
94-
return window.btoa(data);
95-
}
96-
this.fromBase64 = function(strBase64) {
97-
data = window.atob(strBase64);
98-
}
26+
BinaryFile.prototype.getByteAt_CharCode = function(iOffset) {
27+
return this.data.charCodeAt(iOffset + this.dataOffset) & 0xFF;
9928
};
29+
30+
BinaryFile.prototype.getByteAt_IEBinary = function(iOffset) {
31+
return IEBinary_getByteAt(this.data, iOffset + this.dataOffset);
32+
};
33+
34+
BinaryFile.prototype.getRawData = function() {
35+
return this.data;
36+
}
37+
38+
BinaryFile.prototype.slice = function(begin, optionalEnd) {
39+
var arr = [];
40+
var end = optionalEnd || this.getLength();
41+
var i;
42+
for (i = begin; i < end; i++) {
43+
arr.push(this.getByteAt(i));
44+
}
45+
return arr;
46+
}
47+
48+
BinaryFile.prototype.getLength = function() {
49+
return this.dataLength;
50+
}
51+
52+
BinaryFile.prototype.getSByteAt = function(iOffset) {
53+
var iByte = this.getByteAt(iOffset);
54+
if (iByte > 127)
55+
return iByte - 256;
56+
else
57+
return iByte;
58+
}
59+
60+
BinaryFile.prototype.getShortAt = function(iOffset, bBigEndian) {
61+
var iShort = bBigEndian ?
62+
(this.getByteAt(iOffset) << 8) + this.getByteAt(iOffset + 1)
63+
: (this.getByteAt(iOffset + 1) << 8) + this.getByteAt(iOffset)
64+
if (iShort < 0) iShort += 65536;
65+
return iShort;
66+
}
67+
68+
BinaryFile.prototype.getSShortAt = function(iOffset, bBigEndian) {
69+
var iUShort = this.getShortAt(iOffset, bBigEndian);
70+
if (iUShort > 32767)
71+
return iUShort - 65536;
72+
else
73+
return iUShort;
74+
}
75+
76+
BinaryFile.prototype.getLongAt = function(iOffset, bBigEndian) {
77+
var iByte1 = this.getByteAt(iOffset),
78+
iByte2 = this.getByteAt(iOffset + 1),
79+
iByte3 = this.getByteAt(iOffset + 2),
80+
iByte4 = this.getByteAt(iOffset + 3);
81+
82+
var iLong = bBigEndian ?
83+
(((((iByte1 << 8) + iByte2) << 8) + iByte3) << 8) + iByte4
84+
: (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1;
85+
if (iLong < 0) iLong += 4294967296;
86+
return iLong;
87+
}
88+
89+
BinaryFile.prototype.getSLongAt = function(iOffset, bBigEndian) {
90+
var iULong = this.getLongAt(iOffset, bBigEndian);
91+
if (iULong > 2147483647)
92+
return iULong - 4294967296;
93+
else
94+
return iULong;
95+
}
96+
97+
BinaryFile.prototype.getStringAt = function(iOffset, iLength) {
98+
var aStr = [];
99+
for (var i=iOffset,j=0;i<iOffset+iLength;i++,j++) {
100+
aStr[j] = String.fromCharCode(this.getByteAt(i));
101+
}
102+
return aStr.join("");
103+
}
104+
105+
BinaryFile.prototype.getCharAt = function(iOffset) {
106+
return String.fromCharCode(this.getByteAt(iOffset));
107+
}
108+
109+
BinaryFile.prototype.toBase64 = function() {
110+
return btoa(this.data);
111+
}
112+
113+
BinaryFile.prototype.fromBase64 = function(strBase64) {
114+
data = window.atob(strBase64);
115+
}
116+
117+
module.exports = exports = BinaryFile;

lib/git-server.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ _ = require('../vendor/underscore-min')
77
require('../vendor/js-deflate/rawdeflate')
88
require('../vendor/js-deflate/rawinflate')
99
require('../vendor/sha1')
10-
require('../vendor/sha2')
1110
require('../vendor/md5')
1211
require('../vendor/diff')
1312
require('./string_helpers')
1413
require('./binary_file')
15-
require('./git')
1614
require('./git/upload_pack_parser')
1715
require('./git/pack')
1816
require('./git/remote')
@@ -35,3 +33,5 @@ require('./git/commands/log-command')
3533
require('./git/commands/diff-command')
3634
require('./git/pack-file')
3735

36+
var Git = require('./git')
37+
module.exports = exports = new Git

lib/git.js

+46-51
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,59 @@
11

22
/* Main object */
3-
Git = {
4-
OBJECT_TYPES: ["tag", "commit", "tree", "blob"],
5-
REMOTE_TYPE: "HttpRemote",
6-
7-
// Print an error either to the console if in node, or to div#jsgit-errors
8-
// if in the client.
9-
handleError: function(message) {
10-
if (jsGitInNode) {
11-
console.log(message)
12-
}
13-
else {
14-
$('#jsgit-errors').append(message)
15-
}
16-
},
17-
18-
// Turn an array of bytes into a String
19-
bytesToString: function(bytes) {
20-
var result = "";
3+
4+
var Git = function() {
5+
6+
};
7+
8+
// constants
9+
10+
Git.OBJECT_TYPES = ["tag", "commit", "tree", "blob"]
11+
Git.REMOTE_TYPE = 'HttpRemote'
12+
13+
Git.prototype.handleError = typeof console !== 'undefined' ?
14+
function() { console.log.apply(console, arguments) } :
15+
function() { (this._logs || []).push([].slice.call(arguments)) }
16+
17+
// Turn an array of bytes into a String
18+
Git.prototype.bytesToString = function(bytes) {
19+
var result = [];
2120
var i;
2221
for (i = 0; i < bytes.length; i++) {
23-
result = result.concat(String.fromCharCode(bytes[i]));
22+
result.push(String.fromCharCode(bytes[i]));
2423
}
25-
return result;
26-
},
27-
28-
stringToBytes: function(string) {
24+
return result.join('');
25+
};
26+
27+
Git.prototype.stringToBytes = function(string) {
2928
var bytes = [];
3029
var i;
3130
for(i = 0; i < string.length; i++) {
3231
bytes.push(string.charCodeAt(i) & 0xff);
3332
}
3433
return bytes;
35-
},
36-
37-
toBinaryString: function(binary) {
38-
if (Array.isArray(binary)) {
39-
return Git.bytesToString(binary)
40-
}
41-
else {
42-
return binary
43-
}
44-
},
45-
46-
// returns the next pkt-line
47-
nextPktLine: function(data) {
48-
var length = parseInt(data.substring(0, 4), 16);
49-
return data.substring(4, length);
50-
},
51-
52-
// zlib files contain a two byte header. (RFC 1950)
53-
stripZlibHeader: function(zlib) {
54-
return zlib.slice(2)
55-
},
56-
57-
escapeHTML: function(s) {
58-
return s
59-
.replace(/&/g, '&amp;')
60-
.replace(/</g, '&lt;')
61-
.replace(/>/g, '&gt;');
62-
}
34+
};
35+
36+
Git.prototype.toBinaryString = function(binary) {
37+
return (binary instanceof Array) ?
38+
this.bytesToString(binary) :
39+
binary
40+
};
41+
42+
Git.prototype.nextPktLine = function(data) {
43+
var len = parseInt(data.slice(0, 4), 16);
44+
return data.slice(4, len);
45+
};
46+
47+
Git.prototype.stripZlibHeader = function(zlib) {
48+
return zlib.slice(2);
6349
}
50+
51+
Git.prototype.escapeHTML = function(str) {
52+
return str.replace(/\&/g, '&amp;')
53+
.replace(/</g, '&lt;')
54+
.replace(/>/g, '&gt;')
55+
.replace(/"/g, '&quot;')
56+
replace(/'/g, '&#39;');
57+
};
6458

59+
module.exports = exports = Git

0 commit comments

Comments
 (0)