Commit 0047273f authored by Ivanq's avatar Ivanq

Merge branch 'submodules'

parents 9dd7e9c6 fe6cf0d8
<?xml version="1.0" ?><svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title/><g id="Git"><path d="M23.56,10.93,13.06.43a1.54,1.54,0,0,0-2.12,0l-2,2a.5.5,0,0,0,0,.71L10.74,5a.5.5,0,0,0,.54.11A2,2,0,0,1,14,7a2,2,0,0,1-.15.74.5.5,0,0,0,.11.54L15.72,10a.5.5,0,0,0,.54.11A2,2,0,1,1,15,12a2,2,0,0,1,.14-.71.5.5,0,0,0-.11-.53L13.35,9.07a.5.5,0,0,0-.85.35v5.28a.5.5,0,0,0,.3.46,2,2,0,1,1-1.6,0,.5.5,0,0,0,.3-.46V9.28a.5.5,0,0,0-.3-.46,2,2,0,0,1-1.06-2.56A.5.5,0,0,0,10,5.73L8.19,3.88a.5.5,0,0,0-.71,0l-7,7a1.5,1.5,0,0,0,0,2.12l10.5,10.51a1.5,1.5,0,0,0,2.12,0l10.5-10.51a1.5,1.5,0,0,0,0-2.12Z"/></g></svg>
\ No newline at end of file
......@@ -90,6 +90,32 @@ class Git {
})
);
}
parseConfig(config) {
let result = {
"": {}
};
let currentGroup = "";
config.split("\n").forEach(line => {
line = line.trim();
if(line[0] == ";") {
// Comment
return;
} else if(line[0] == "[" && line[line.length - 1] == "]") {
// Group
currentGroup = line.substr(1, line.length - 2);
result[currentGroup] = {};
} else {
let key = line.substr(0, line.indexOf("=")).trim();
let value = line.substr(line.indexOf("=") + 1).trim();
result[currentGroup][key] = value;
}
});
return result;
}
// FileSystem commands
readFile(path) {
......@@ -480,7 +506,7 @@ class Git {
currentPos += 20;
items.push({
type: mode.length == 6 && mode.indexOf("10") == 0 ? "blob" : "tree",
type: mode.length == 6 && mode.indexOf("10") == 0 ? "blob" : mode.indexOf("16") == 0 ? "submodule" : "tree",
name: name,
id: objectId
});
......@@ -967,6 +993,44 @@ class Git {
return this.makeTreeDelta(base, tree.content);
}
// Submodules
getSubmodules(tree) {
return this.readUnknownObject(tree)
.then(tree => {
let blob = tree.content.find(item => item.type == "blob" && item.name == ".gitmodules");
if(!blob) {
return Promise.reject("No .gitmodules");
}
return this.readUnknownObject(blob.id);
})
.then(gitmodules => {
return this.parseConfig(this.arrayToString(gitmodules.content));
})
.then(moduleList => {
let submodules = [];
Object.keys(moduleList).forEach(group => {
if(group.indexOf("submodule") == -1) {
return;
}
let name = group.match(/^submodule "(.*)"$/)[1];
let path = moduleList[group].path;
let url = moduleList[group].url;
submodules.push({
name: name,
path: path,
url: url
});
});
return submodules;
})
.catch(() => []);
}
toString() {
return "<Git " + this.root + ">";
}
......
......@@ -178,32 +178,37 @@ class Repository {
});
}
getTree(tree, dir) {
return this.git.readTreeItem(tree, dir)
let submodules;
return this.git.getSubmodules(tree)
.then(s => {
submodules = s;
return this.git.readTreeItem(tree, dir);
})
.then(tree => {
if(tree.type != "tree") {
return Promise.reject("Commit tree must be a tree");
}
return Promise.all(
tree.content.map(file => {
return this.git.readUnknownObject(file.id)
.then(object => {
if(object.type == "blob") {
file.type = "file";
} else if(object.type == "tree") {
file.type = "directory";
} else {
file.type = "unknown";
}
return file;
})
.catch(object => {
file.type = "error";
return file;
});
})
);
tree.content.forEach(file => {
file.type = {
blob: "file",
tree: "directory",
submodule: "submodule"
}[file.type] || "unknown";
if(file.type == "submodule") {
let submodule = submodules.find(submodule => submodule.path == (dir ? dir + "/" + file.name : file.name));
if(submodule) {
file.submodule = submodule;
} else {
file.type = "error";
}
}
});
return tree.content;
});
}
getFile(branch, path) {
......
......@@ -59,12 +59,22 @@ repo.addMerger()
return;
}
location.href = (
(file.type == "file" ? "file/" : "") +
"?" + address +
"/" + ((path ? path + "/" : "") + file.name).replace(/@/g, "@@") +
"@" + branch.replace(/@/g, "@@")
);
if(file.type == "submodule") {
let url = file.submodule.url.match(/(1[A-Za-z0-9]{25,34})/);
if(url) {
// Likely Git Center URL
location.href = "?" + url[1];
} else {
parent.location.href = file.submodule.url.replace(/\.git$/, "");
}
} else {
location.href = (
(file.type == "file" ? "file/" : "") +
"?" + address +
"/" + ((path ? path + "/" : "") + file.name).replace(/@/g, "@@") +
"@" + branch.replace(/@/g, "@@")
);
}
};
let name = document.createElement("td");
......
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