diff --git a/index.js b/index.js index fb1a0fd..37860ef 100644 --- a/index.js +++ b/index.js @@ -23,24 +23,30 @@ function ls (path, callback) { } else { folder = '' } - if (branch === 'master') { - branch = '' - prefix = 'trunk/' + folder - } else { - branch = branch + '/' - prefix = 'branches/' + branch + folder - } - ls.paths(GITHUB, slug, '', ls.errorCatch(callback, function (list) { - var rev - if (branch === '') { - rev = list[0].rev - } else { - rev = list[1].rev + var folderProcessor = ls.errorCatch(callback, function (list) { + callback(null, list.map(function (entry) { + return entry.path.substr(prefix.length) + })) + }) + ls.paths(GITHUB, slug, 'branches', ls.errorCatch(callback, function (list) { + for (var i = 0; i < list.length; i++) { + var item = list[i] + prefix = 'branches/' + branch + '/' + folder + if (item.path === prefix) { + /* + This branch is actually a branch! lets use it! + */ + return ls.paths(GITHUB, slug, item.rev + folder, folderProcessor) + } } - ls.paths(GITHUB, slug, rev + branch + folder, ls.errorCatch(callback, function (list) { - callback(null, list.map(function (entry) { - return entry.path.substr(prefix.length) - })) + /* + If the selected branch is not a dedicated branch it assumes that the branch is the trunk + and we need to fetch the version of the trunk to know which version of the tree we should + read + */ + ls.paths(GITHUB, slug, '', ls.errorCatch(callback, function (list) { + prefix = 'trunk/' + folder + ls.paths(GITHUB, slug, list[0].rev + folder, folderProcessor) })) })) }