Commit f7d39be8 authored by Ivanq's avatar Ivanq

Merge branch 'master' into pr

parents 54da3526 6cc22ee5
......@@ -41,6 +41,20 @@ class Git {
return char;
}).join("");
}
packSha(str) {
let items = str.split("").map(char => {
if(char >= "0" && char <= "9") {
return char.charCodeAt(0) - "0".charCodeAt(0);
} else if(char >= "a" && char <= "z") {
return char.charCodeAt(0) - "a".charCodeAt(0) + 10;
}
});
let result = [];
for(let i = 0; i < items.length; i += 2) {
result.push(items[i] * 16 + items[i + 1]);
}
return result;
}
subArray(array, begin, length) {
if(length === undefined) {
return array.slice(begin);
......@@ -50,10 +64,19 @@ class Git {
}
appendArray(source, destination) {
source.forEach(item => destination.push(item));
return destination;
}
concat(...arrs) {
let destination = [];
arrs.forEach(arr => this.appendArray(arr, destination));
return destination;
}
arrayToString(array) {
return Array.from(array).map(char => String.fromCharCode(char)).join("");
}
stringToArray(string) {
return string.split("").map(char => char.charCodeAt(0));
}
isSha(str) {
return (
str.length == 40 &&
......@@ -78,9 +101,25 @@ class Git {
readDirectory(path, recursive) {
return this.zeroFS.readDirectory(this.root + "/" + path, recursive);
}
writeFile(path, content) {
return this.zeroFS.writeFile(this.root + "/" + path, Array.from(content).map(char => String.fromCharCode(char)).join(""), true);
}
inflate(string) {
return pako.inflate(string);
}
deflate(string) {
return pako.deflate(string);
}
sha(string) {
if(string instanceof Array) {
string = new Uint8Array(string);
}
let sha = new jsSHA("SHA-1", "ARRAYBUFFER");
sha.update(string);
return sha.getHash("HEX");
}
// Object commands
readObject(id) {
......@@ -100,6 +139,13 @@ class Git {
};
});
}
writeObject(type, content) {
let data = this.concat(this.stringToArray(type + " " + content.length), [0], content);
let id = this.sha(data);
return this.writeFile("objects/" + id.substr(0, 2) + "/" + id.substr(2), this.deflate(data))
.then(() => id);
}
// Packed objects
findPackedObjects() {
......@@ -353,7 +399,7 @@ class Git {
currentPos += 20;
items.push({
mode: mode,
type: mode.length == 6 && mode.indexOf("10") == 0 ? "blob" : "tree",
name: name,
id: objectId
});
......@@ -468,6 +514,81 @@ class Git {
});
}
// Object saving
writeBlob(content) {
return this.writeObject("blob", content);
}
writeTree(items) {
let content = [];
items.forEach(item => {
this.appendArray(this.concat(this.stringToArray((item.type == "tree" ? "040000" : "100644") + " " + item.name), [0], this.packSha(item.id)), content);
});
return this.writeObject("tree", content);
}
writeTreeRecursive(items) {
let content = items.map(item => {
if(item.type == "tree") {
if(item.id) {
// Use existing tree
return Promise.resolve({
type: "tree",
name: item.name,
id: item.id
});
}
return this.writeTreeRecursive(item.content)
.then(id => {
return {
type: "tree",
name: item.name,
id: id
};
});
} else if(item.type == "blob") {
if(item.id) {
// Use existing blob
return Promise.resolve({
type: "blob",
name: item.name,
id: item.id
});
}
return this.writeBlob(item.content)
.then(id => {
return {
type: "blob",
name: item.name,
id: id
};
});
}
});
return Promise.all(content)
.then(content => {
return this.writeTree(content);
});
}
writePlainCommit(commit) {
let content = "";
content += "tree " + commit.tree + "\n";
content += commit.parents.map(parent => "parent " + parent + "\n").join("");
content += "author " + commit.author + "\n";
content += "committer " + commit.committer + "\n";
content += "\n";
content += commit.message;
return this.writeObject("commit", this.stringToArray(content));
}
writeCommit(commit) {
return this.writeTreeRecursive(commit.tree)
.then(treeId => {
commit.tree = treeId;
return this.writePlainCommit(commit);
});
}
// Refs commands
getRef(ref) {
return this.readFile(ref)
......@@ -565,4 +686,151 @@ class Git {
return Promise.reject("No HEAD ref");
});
}
// Pair-repo actions
importObject(other, id) {
return this.readObject(id)
.then(object => {
// Already exists
return false;
}, () => {
let object;
return other.readObject(id)
.then(o => {
object = o;
return this.writeObject(object.type, object.content);
})
.then(newId => {
if(newId != id) {
return Promise.reject("SHA1 mismatch during importing " + id + " (received " + newId + ") from " + other + " to " + this);
}
return true;
});
});
}
importObjectWithDependencies(other, id) {
return this.importObject(other, id)
.then(imported => {
if(!imported) {
// Already have object and dependencies locally
return;
}
return this.readUnknownObject(id)
.then(object => {
if(object.type == "blob") {
return this.importBlobDependencies(other, object);
} else if(object.type == "tree") {
return this.importTreeDependencies(other, object);
} else if(object.type == "commit") {
return this.importCommitDependencies(other, object);
}
});
});
}
importBlobDependencies(other, blob) {
// Blob has no dependencies
return Promise.resolve();
}
importTreeDependencies(other, tree) {
return Promise.all(
tree.content.map(item => {
return this.importObjectWithDependencies(other, item.id);
})
);
}
importCommitDependencies(other, commit) {
return Promise.all(
[
this.importObjectWithDependencies(other, commit.content.tree), // Import tree
].concat(
commit.content.parents.map(parent => {
return this.importObjectWithDependencies(other, parent) // Import parents
})
)
);
}
makeTreeDelta(base, changes) {
// changes:
// [
// {
// name: "dir",
// type: "tree",
// content: [
// {
// name: "subdir",
// type: "tree",
// content: [
// {
// name: "subfile",
// type: "blob",
// content: "neworchangedfile"
// }
// ]
// }
// ]
// },
// {
// name: "file",
// remove: true
// },
// {
// name: "olddir",
// type: "blob",
// content: "fds"
// }
// ]
//
// Should be read as:
// 1. Set <dir/subdir/subfile> blob to "neworchangedfile"
// 2. Remove <file>
// 3. Remove tree <olddir> and add blob <olddir>
let promise = Promise.all(
changes.map(change => {
let treeItemIndex = base.findIndex(item => item.name == change.name);
if(change.remove) {
// Remove
if(treeItemIndex > -1) {
base.splice(treeItemIndex, 1);
}
} else if(change.type == "blob") {
if(treeItemIndex == -1) {
// Add blob
base.push(change);
} else {
// Change type to blob or change blob
base[treeItemIndex] = change;
}
} else if(change.type == "tree") {
if(treeItemIndex == -1) {
// Add tree
base.push(change);
} else if(tree[treeItemIndex].type != "tree") {
// Change type to tree
base[treeItemIndex] = change;
} else {
// Change tree
let id = base[treeItemIndex].id;
delete base[treeItemIndex].id;
return this.readUnknownObject(id)
.then(subTree => {
return this.makeTreeDelta(subTree.content, change.content);
});
}
}
return Promise.resolve();
})
);
return promise.then(() => base);
}
toString() {
return "<Git " + this.root + ">";
}
};
\ No newline at end of file
......@@ -22,6 +22,64 @@ Use `getBranchCommit("master")` to get commit the branch references or use `read
Use `getHead()`.
### I can read objects, can I write them?
Yes. Currently ZeroGit only supports loose objects writing. Use `writeObject(type, plainContent)` which hashes object and returns its new SHA.
### What about auto formatting saved objects?
You can use:
1. `writeBlob(rawBlobContent)`
2. `writeTree(items)` where `items` have the same format as `readUnknownObject()` result for trees.
3. While `writeTree(items)` expects each subtree to be `{type: "tree", name: "mydir", id: "0123456789abcdefghij"}`, `writeTreeRecursive` also supports `{type: "tree", name: "mydir", content: [{type: "blob", name: "subfile", content: "myblob"}]}`. So you can build subtrees without need to call `writeTree` several times.
4. `writePlainCommit(commit)` expects `commit` to have the same format as the result of `readUnknownObject()` for commits, so it makes you set `tree` as SHA.
5. `writeCommit(commit)` works like `writePlainCommit(commit)` but also builds tree with `writeTreeRecursive(commit.tree)` and sets new SHA as commit tree.
### Can I simplify committing?
`makeTreeDelta` gives you power not to recreate all tree. It accepts `base` as result of `readUnknownObject().content` and `changes` as array of items where each item can be either `{name: "...", type: "tree", content: [...]}` or `{name: "...", remove: true}` or `{name: "...", type: "blob", content: "..."]`. The resulting value is new tree. Note that `base` argument is changed.
For example,
[
{
name: "dir",
type: "tree",
content: [
{
name: "subdir",
type: "tree",
content: [
{
name: "subfile",
type: "blob",
content: "neworchangedfile"
}
]
}
]
},
{
name: "file",
remove: true
},
{
name: "olddir",
type: "blob",
content: "fds"
}
]
...should be read as:
1. Set `dir/subdir/subfile` blob to `neworchangedfile`
2. Remove `file`
3. Replace tree `olddir` with blob `olddir`
4. Leave all not mentioned files (if there was `dir/somefile` or `readme.md` they would be added to resulting tree).
### And some actions between repositories?
Yes! Run `importObject(otherGit, sha)` to read `sha` from `otherGit` and save it to `this`. And `importObjectWithDependencies(otherGit, sha)` also imports commit parents and tree, tree subitems, etc.
## Getting loose objects
For getting loose object, ZeroGit splits SHA to 2-and-18 parts, reads file `objects/01/23456789abcdefghij` and deflates it. The result is:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment