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

@ -3,7 +3,7 @@ html {
transition: margin-left 0.2s ease;
-webkit-transition: margin-left 0.2s ease;
}
html.octotree {
html.octotree {
margin-left: 240px;
}
@ -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;
@ -126,17 +131,17 @@ html.octotree {
}
/* Token form */
.octotree_sidebar form div {
margin: 6px;
.octotree_sidebar form div {
margin: 6px;
}
.octotree_sidebar form input {
width: 237px;
.octotree_sidebar form input {
width: 237px;
}
.octotree_sidebar form .error {
color: #900;
.octotree_sidebar form .error {
color: #900;
}
.octotree_sidebar form button {
margin-right: 5px;
.octotree_sidebar form button {
margin-right: 5px;
}
/* Toggle button */
@ -178,4 +183,4 @@ html.octotree {
}
.octotree .octotree_toggle > span:after {
content: '\f0a4';
}
}

View File

@ -3,8 +3,8 @@
, TOKEN = 'octotree.github_access_token'
, SHOWN = 'octotree.shown'
, RESERVED_USER_NAMES = [
'settings', 'orgs', 'organizations',
'site', 'blog', 'about',
'settings', 'orgs', 'organizations',
'site', 'blog', 'about',
'styleguide', 'showcases', 'trending',
'stars', 'dashboard', 'notifications'
]
@ -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()
@ -95,7 +96,7 @@
// (username)/(reponame)[/(subpart)]
var match = location.pathname.match(/([^\/]+)\/([^\/]+)(?:\/([^\/]+))?/)
if (!match) return false
// Not a repository, skip
if (~RESERVED_USER_NAMES.indexOf(match[1])) return false
if (~RESERVED_REPO_NAMES.indexOf(match[2])) return false
@ -106,8 +107,8 @@
var branch = $('*[data-master-branch]').data('ref') ||
$('*[data-master-branch] > .js-select-button').text() ||
'master'
return {
username : match[1],
return {
username : match[1],
reponame : match[2],
branch : branch
}
@ -121,32 +122,44 @@
api.getTree(encodeURIComponent(repo.branch) + '?recursive=true', function(err, tree) {
if (err) return done(err)
tree.forEach(function(item) {
var path = item.path
, type = item.type
, index = path.lastIndexOf('/')
, name = path.substring(index + 1)
, folder = folders[path.substring(0, index)]
, url = '/' + repo.username + '/' + repo.reponame + '/' + type + '/' + repo.branch + '/' + path
fetchSubmoduleData(api, repo, tree, function(submods){
tree.forEach(function(item) {
var path = item.path
, type = item.type
, index = path.lastIndexOf('/')
, name = path.substring(index + 1)
, folder = folders[path.substring(0, index)]
, url = '/' + repo.username + '/' + repo.reponame + '/' + type + '/' + repo.branch + '/' + path
folder.push(item)
folder.push(item)
item.id = PREFIX + path
item.text = sanitize(name)
item.icon = type // use `type` as class name for tree node
if (type === 'tree') {
folders[item.path] = item.children = []
item.a_attr = { href: '#' }
}
else if (type === 'blob') {
item.a_attr = { href: url }
}
item.text = sanitize(name)
item.icon = type // use `type` as class name for tree node
if (type === 'tree') {
folders[item.path] = item.children = []
item.a_attr = { href: '#' }
}
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))
})
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)
@ -204,19 +256,23 @@
.on('click', function(e) {
var $target = $(e.target)
if ($target.is('a.jstree-anchor') && $target.children(':first').hasClass('blob')) {
$.pjax({
url : $target.attr('href'),
$.pjax({
url : $target.attr('href'),
timeout : 5000, //gives it more time, should really have a progress indicator...
container : $('#js-repo-pjax-container')
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">' +
repo.username + ' / ' + repo.reponame +
var headerText = '<div class="octotree_header_repo">' +
repo.username + ' / ' + repo.reponame +
'</div>' +
'<div class="octotree_header_branch">' +
repo.branch +
'<div class="octotree_header_branch">' +
repo.branch +
'</div>'
updateSidebar(headerText)
cb()
@ -247,7 +303,7 @@
if (shown) $html.removeClass(PREFIX)
else $html.addClass(PREFIX)
store.set(SHOWN, !shown)
}
}
function saveToken(event) {
event.preventDefault()
@ -278,4 +334,4 @@
return localStorage.setItem(key, JSON.stringify(val))
}
}
})()
})()