Added submodule support

This commit is contained in:
Jerry Wang 2014-05-17 17:03:23 -04:00
parent 8ca7de1623
commit d3f59a8811
2 changed files with 107 additions and 46 deletions

View File

@ -75,7 +75,8 @@ html.octotree {
/* Source tree */
.jstree-icon.tree,
.jstree-icon.blob {
.jstree-icon.blob,
.jstree-icon.commit {
font: normal normal 16px octicons;
display: inline-block;
margin-right: 5px;
@ -90,6 +91,10 @@ html.octotree {
content: '\f011';
color: #777;
}
.jstree-icon.commit:before {
content: '\f017';
color: #777;
}
.jstree-anchor {
color: #4183c4 !important;

View File

@ -32,6 +32,7 @@
, store = new Storage()
, domInitialized = false
, currentRepo = false
, baseUrl = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port + '/'
$(document).ready(function() {
loadRepo()
@ -121,6 +122,7 @@
api.getTree(encodeURIComponent(repo.branch) + '?recursive=true', function(err, tree) {
if (err) return done(err)
fetchSubmoduleData(api, repo, tree, function(submods){
tree.forEach(function(item) {
var path = item.path
, type = item.type
@ -140,13 +142,24 @@
else if (type === 'blob') {
item.a_attr = { href: url }
}
else if (type === 'commit'){
if(submods){
submod = submods[item.path]
item.a_attr = { href: baseUrl + submod.owner + '/' + submod.repo }
// TODO: link to commit sha also
item.a2_attr = { href: item.a_attr.href + '/tree/' + item.sha }
}
}
})
done(null, sort(root))
})
function sort(folder) {
folder.sort(function(a, b) {
if (a.type === b.type) return a.text.localeCompare(b.text)
//github treats submodules like folders
var compare = ((a.type === 'tree' || a.type === 'commit') &&
(b.type === 'tree' || b.type === 'commit'))
if (a.type === b.type || compare) return a.text.localeCompare(b.text)
return a.type === 'tree' ? -1 : 1
})
folder.forEach(function(item) {
@ -157,6 +170,45 @@
})
}
function fetchSubmoduleData(api, repo, tree, cb){
var submodules = []
, item = null
//fetch submodule from .gitmodules file
//use tree to find sha
item = _.find(tree, function(file) { return /\.gitmodules/i.test(file.path) })
if(item){
api.getBlob(item.sha, function (err, content, sha){
if(err) cb(null)
if(content){
lines = content.match(/[^\r\n]+/g)
// each submodule is defined on 3 lines, group them for easier iterating
grouped = groupBy(lines, 3)
_.each(grouped, function(submodule) {
path = submodule[1].match(/=\s([\w\d\/]+)/i)[1] // = (path)
repoParts = submodule[2].match(/([\w]+)\/([\w]+).git$/i) // (owner)/(repo).git$
owner = repoParts[1]
repo = repoParts[2]
submodules[path]= {
owner: owner,
repo: repo
}
})
cb(submodules)
}
})
}
else
{
cb(null)
}
}
function groupBy(data, n){
return _.groupBy(data, function(element, index){
return Math.floor(index/n);
});
}
function onFetchError(err) {
var header = 'Error: ' + err.error
, hasToken = !!store.get(TOKEN)
@ -210,6 +262,10 @@
container : $('#js-repo-pjax-container')
})
}
else if ($target.is('a.jstree-anchor') && $target.children(':first').hasClass('commit')) {
//link to submodule new page
window.location.href = $target.attr('href');
}
})
.on('ready.jstree', function() {
var headerText = '<div class="octotree_header_repo">' +