Permalink
Browse files

[merge] undelete data-utils.js

  • Loading branch information...
1 parent b378cba commit 0e494b192e40f6d6ad54e885a996dac8bfe0495b @tmpvar tmpvar committed Nov 7, 2012
Showing with 102 additions and 272 deletions.
  1. +88 −3 README.md
  2. +6 −66 git.js
  3. +1 −1 lib/cache.js
  4. +0 −159 lib/filediff.js
  5. +2 −39 lib/remote.js
  6. +3 −2 test/tests/applydelta.js
  7. +2 −2 test/tests/unpack.js
View
@@ -1,4 +1,89 @@
-node-git
-========
-a javascript based subset of git
+# As a client
+
+```js
+var gitstream = require('git-stream')
+
+var repo = gitstream.Repo()
+
+//
+// get a repo from disk
+//
+repo
+ .read('./repoA')
+ .on('end', function() {
+
+ })
+
+//
+// add an origin
+//
+
+repo.remote.add({
+ name: 'origin',
+ port: 8000,
+ address: '127.0.0.1',
+ id: 'username/origin'
+});
+
+repo.remote.add({
+ name: 'fork',
+ port: 8000,
+ address: '127.0.0.1',
+ id: 'username/fork'
+});
+
+repo.pull('origin')
+
+repo
+ .pull('fork')
+ .on('conflict', function(conflicts) {
+ console.dir(conflicts);
+ process.exit(0);
+ })
+
+//
+// add some stuff to it
+//
+repo.add(fs.createReadStream('./README.md'))
+
+//
+// commit some stuff and push
+//
+repo.commit({ m: 'first commit!' }).push('origin')
+```
+
+# As a server
+
+```js
+var fs = require('fs')
+var net = require('net')
+var gitstream = require('git-stream')
+
+//
+// create a TCP server
+//
+net.createServer(function(socket) {
+
+ //
+ // when the socket connects and gets data
+ //
+ socket.on('data', function(data) {
+
+ data
+ .pipe(gitstream.Repo())
+ .on('end', function(repo) {
+ repo.checkout('master', function() {
+ repo.on('file', onfile)
+ })
+ })
+ })
+
+}).listen(8000)
+
+var onfile = function(filename, file) {
+ if (!file.isDirectory) {
+ fs.createWriteStream(filename).pipe(file);
+ }
+}
+```
View
72 git.js
@@ -35,71 +35,11 @@ Git.prototype.read = function(data) {
Git.prototype.end = function(data) {
};
-Git.prototype.objects = require('./lib/objects');
+Git.prototype.remote = require('./lib/remote');
-Git.prototype.Remote = require('./lib/remote');
-Git.prototype.HttpRemote = require('./lib/httpremote');
+Git.prototype.Diff = require('./lib/diff');
+Git.prototype.push = require('./lib/push');
+Git.prototype.pull = require('./lib/pull');
-Git.prototype.Pack = require('./lib/pack');
-Git.prototype.FileDiff = require('./lib/filediff');
-
-Git.prototype.OBJECT_TYPES = ["tag", "commit", "tree", "blob"];
-Git.prototype.REMOTE_TYPE = "HttpRemote";
-
-var errors = [];
-
-//
-// Turn an array of bytes into a String
-//
-Git.prototype.bytesToString = function(bytes) {
-
- var result = '';
-
- for (var i = 0; i < bytes.length; i++) {
- result += String.fromCharCode(bytes[i]);
- }
- return result;
-};
-
-//
-//
-//
-Git.prototype.stringToBytes = function(string) {
-
- var bytes = [];
- var i;
- for(i = 0; i < string.length; i++) {
- bytes.push(string.charCodeAt(i) & 0xff);
- }
- return bytes;
-};
-
-//
-//
-//
-Git.prototype.toBinaryString = function(binary) {
-
- if (Array.isArray(binary)) {
- return this.bytesToString(binary)
- }
- else {
- return binary
- }
-};
-
-//
-// returns the next pkt-line
-//
-Git.prototype.nextPktLine = function(data) {
-
- var length = parseInt(data.substring(0, 4), 16);
- return data.substring(4, length);
-};
-
-//
-// zlib files contain a two byte header. (RFC 1950)
-//
-Git.prototype.stripZlibHeader = function(zlib) {
-
- return zlib.slice(2);
-};
+Git.prototype.add = require('./lib/add');
+Git.prototype.commit = require('./lib/commit');
View
@@ -49,7 +49,7 @@ var Cache = module.exports = function Cache() {
}
that.addRemote = function(name, url) {
- remotes[name] = new Git[Git.REMOTE_TYPE](this, name, url)
+ remotes[name] = new Git.HttpRemote(this, name, url)
}
that.addRemoteObject = function(name, remote) {
View
@@ -1,159 +0,0 @@
-
-var helpers = require('../common/helpers');
-var Diff = require('../common/diff');
-
-module.exports = function FileDiff(file1, file2, options) {
-
- if (!(this instanceof FileDiff)) {
- return new FileDiff(file1, file2, options);
- }
-
- this.lines1 = file1.split("\n");
- this.lines2 = file2.split("\n");
- this.diffChunks = Diff.createPatch(this.lines1, this.lines2);
-
- var MAX_LINE_CHARS = 140;
- var options = options || {};
- this.contextLength = options.context || 3;
-
- this.toInfo = function() {
-
- if (file1 === "") {
-
- var infoChunk = { offset: 1, lines:[] };
-
- helpers.each(this.lines2, function(line, i) {
- infoChunk.lines.push({
- oldIndex: null,
- newIndex: (i + 1),
- line: line,
- type:"added"
- });
- });
-
- return [infoChunk];
- }
- if (file2 === "") {
-
- var infoChunk = { offset: 1, lines:[] };
-
- helpers.each(this.lines1, function(line, i) {
- infoChunk.lines.push({
- oldIndex: (i + 1),
- newIndex: null,
- line: line,
- type:"removed"
- });
- });
-
- return [infoChunk];
- }
-
- var infos = [];
- var diff = this;
- var totalAdded = 0;
- var totalRemoved = 0;
- var lastInfoChunk = null;
- var infoChunk = null;
-
- helpers.each(this.diffChunks, function(chunk) {
-
- var removed = chunk.file1;
- var added = chunk.file2;
-
- infoChunk = null;
-
- var removeContext = null;
- var overlapLength = null;
- var lastLineNewIndex = null;
-
- if (lastInfoChunk) {
-
- var lastLine = lastInfoChunk.lines[lastInfoChunk.lines.length - 1];
-
- if (lastLine.oldIndex >= Math.max(removed.offset - diff.contextLength, 0)) {
-
- infoChunk = lastInfoChunk;
- overlapLength = lastLine.oldIndex - Math.max(removed.offset - diff.contextLength, 0);
- removeContext = Math.min(overlapLength, diff.contextLength);
- lastInfoChunk.lines = lastInfoChunk.lines.slice(0, lastInfoChunk.lines.length - removeContext);
- lastLineNewIndex = lastInfoChunk.lines[lastInfoChunk.lines.length - 1].newIndex;
- }
- }
-
- infoChunk = infoChunk || { offset: added.offset + 1, lines:[] };
-
- var preContextRange = [Math.max(removed.offset - diff.contextLength, 0), Math.max(removed.offset, 0)]
- var preContext = diff.lines1.slice(preContextRange[0], preContextRange[1])
-
- helpers.each(preContext, function(line, i) {
- var oldIx = preContextRange[0] + i + 1
- var newIx = oldIx + totalAdded - totalRemoved
- if (newIx > lastLineNewIndex) {
- infoChunk.lines.push({oldIndex: oldIx, newIndex:newIx, line: line, type:"context"})
- }
- })
-
- if (removed.length > 0) {
- helpers.each(removed.chunk, function(line, i) {
- var oldIx = removed.offset + i + 1
- infoChunk.lines.push({oldIndex: oldIx, newIndex:null, line: line, type:"removed"})
- })
- }
-
- if (added.length > 0) {
- helpers.each(added.chunk, function(line, i) {
- var newIx = added.offset + i + 1
- infoChunk.lines.push({oldIndex: null, newIndex:newIx, line: line, type:"added"})
- })
- }
-
- var postContextRange = [(added.offset - totalAdded + totalRemoved) + removed.length, (added.offset - totalAdded + totalRemoved) + diff.contextLength + removed.length]
- var postContext = diff.lines1.slice(postContextRange[0], postContextRange[1])
-
- helpers.each(postContext, function(line, i) {
-
- var oldIx = postContextRange[0] + i + 1
- var newIx = oldIx + added.length - removed.length + totalAdded - totalRemoved
- infoChunk.lines.push({oldIndex: oldIx, newIndex:newIx, line: line, type:"context"})
- });
-
- totalAdded += added.length;
- totalRemoved += removed.length;
-
- if (infoChunk !== lastInfoChunk) {
-
- infos.push(infoChunk);
- }
-
- lastInfoChunk = infoChunk;
- })
- return infos;
- };
-
- this.info = this.toInfo();
-
- this.toString = function() {
- return JSON.stringify(this.info);
- };
-
- this.stat = function() {
-
- var result = {insertions: 0, deletions: 0}
-
- helpers.each(this.info, function(chunk) {
- helpers.each(chunk.lines, function(line) {
- if (line.type == "context") {
- // do nothing.
- }
- else if (line.type == "added") {
- result.insertions += 1;
- }
- else if (line.type == "removed") {
- result.deletions += 1;
- }
- });
- });
- return result;
- };
-};
View
@@ -1,32 +1,11 @@
var helpers = require('../common/helpers');
-var Remote = module.exports = function(repo, name, repoUrl) {
+var Remote = module.exports = function(repo, name) {
this.repo = repo;
this.name = name;
this.refs = {};
- this.url = repoUrl.replace(/\?.*/, "").replace(/\/$/, "");
- this.urlOptions = Git.Remote.queryParams(repoUrl);
-
- this.makeUri = function(path, extraOptions) {
-
- var uri = this.url + path;
- var options = helpers.extend(this.urlOptions, extraOptions || {});
-
- if (options && Object.keys(options).length > 0) {
-
- var optionKeys = Object.keys(options);
- var optionPairs = optionKeys.map(function(optionName) {
- return optionName + "=" + encodeURI(options[optionName]);
- });
-
- return uri + "?" + optionPairs.join("&");
- }
- else {
- return uri;
- }
- };
// Add a ref to this remote. fullName is of the form:
// refs/heads/master or refs/tags/123
@@ -61,22 +40,6 @@ var Remote = module.exports = function(repo, name, repoUrl) {
};
};
-Remote.prototype.queryParams = function(uri) {
-
- var paramString = uri.split("?")[1];
-
- if (!paramString) {
- return {};
- }
-
- var paramStrings = paramString.split("&");
- var params = {};
-
- helpers.each(paramStrings, function(paramString) {
-
- var pair = paramString.split("=");
- params[pair[0]] = decodeURI(pair[1]);
- });
+Remote.add = function(name, url) {
- return params;
};
Oops, something went wrong.

0 comments on commit 0e494b1

Please sign in to comment.