Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[lib] started adding repo, remotes

  • Loading branch information...
commit 024e0ac1a2010b499874554a3dc9525444c93c20 1 parent c192cce
@hij1nx authored
View
41 README.md
@@ -4,23 +4,42 @@
```js
var gitstream = require('git-stream')
-var r = gitstream.Repo()
-
//
-// get a repo from disk
+// An in-memory representation of the repository
+// this does not include all of the files, only
+// the git components such as trees and blobs.
+//
+// A path to where this information is kept can
+// be specified. `createRepo()` returns an instace
+// of the Repo constructor which is a duplex stream.
//
-// r.read('./repoA').pipe(r.push('origin', 'master'))
+var r = gitstream.createRepo()
//
-// add a remote
+// add a remote (an in memory operation) currently
+// only supports the tcp transport protocol.
//
-
r.remote.add({
name: 'origin',
branch: 'master',
- port: 8000,
- address: '127.0.0.1',
- id: 'username/origin'
+ host: '127.0.0.1',
+ port: '8000',
+ path: '/foo/bar'
+})
+
+//
+// or read the config from the repo and get a
+// remote from there.
+//
+r.readConfig({ path: './git' }, function(config) {
+
+ var origin = config.remote.origin;
+
+ r.pull('origin')
+ .pipe(r.add(file))
+ .pipe(r.commit({ m: 'first commit!' }))
+ .pipe(r.push('origin'))
+
})
var file = fs.createReadStream('./README.md')
@@ -28,10 +47,6 @@ var file = fs.createReadStream('./README.md')
//
// add a file, commit it and push it to the remote
//
-r.pull('origin')
- .pipe(r.add(file))
- .pipe(r.commit({ m: 'first commit!' }))
- .pipe(r.push('origin'))
```
View
13 git.js
@@ -13,10 +13,6 @@ var Git = module.exports = function Git(opts) {
var buf = [];
var destroyed = false;
- if (!(this instanceof Git)) {
- return new Git(opts);
- }
-
Stream.call(this);
that.writable = true;
@@ -26,6 +22,12 @@ var Git = module.exports = function Git(opts) {
util.inherits(Git, Stream);
+Git.prototype.createRepo = function(opts) {
+
+ var repo = new this.Repo(opts);
+ return repo;
+};
+
Git.prototype.write = function(data) {
};
@@ -36,9 +38,10 @@ Git.prototype.end = function(data) {
};
Git.prototype.remote = require('./lib/remote');
+Git.prototype.diff = require('./lib/diff');
/*
-Git.prototype.Diff = require('./lib/diff');
+
Git.prototype.push = require('./lib/push');
Git.prototype.pull = require('./lib/pull');
View
159 lib/filediff.js
@@ -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
34 lib/repo.js
@@ -2,15 +2,11 @@
var util = require('util');
var Stream = require("stream").Stream;
var remote = require('./remote');
+var ini = require('ini');
var stream = new Stream();
-//
-// TODO:
-// need to parse the `./git/config` file and add it as an instance member
-//
-
-var Repo = module.exports = function Repo(dir) {
+var Repo = module.exports = function Repo(opts) {
if (!(this instanceof Repo)) {
return new Repo(opts);
@@ -20,13 +16,12 @@ var Repo = module.exports = function Repo(dir) {
var that = this;
+ that.path = opts.path || process.cwd() + './git';
that.remote = Remote(that);
that.writable = true;
that.readable = true;
- iniparser.parseString(this.repo.config);
-
};
Repo.prototype.write = function() {
@@ -37,8 +32,29 @@ Repo.prototype.read = function() {
};
-Git.prototype.end = function(data) {
+Repo.prototype.end = function(data) {
+
+};
+
+Repo.prototype.readConfig = function(opts, callback) {
+
+ if (opts.path) {
+
+ fs.stat(opts.path, function(err) {
+ if (err) {
+
+ callback(err);
+ }
+ else {
+
+ fs.readFile(opts.path, function(data) {
+
+ callback(null, ini.parse(data));
+ });
+ }
+ });
+ }
};
util.inherits(Repo, Stream);
1  test/fixtures/tmp/repo-pack
@@ -0,0 +1 @@
+Subproject commit a6671b63665652df3ac70f9b049bcbe24bc28a91
Please sign in to comment.
Something went wrong with that request. Please try again.