Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 70bfbb3

Browse filesBrowse files
committed
yeah, more changes to the impl and thus more doc changes
1 parent 9aa9609 commit 70bfbb3
Copy full SHA for 70bfbb3

File tree

Expand file treeCollapse file tree

2 files changed

+46
-49
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+46
-49
lines changed

‎githubconsole.html

Copy file name to clipboardExpand all lines: githubconsole.html
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,16 @@ <h2>Wiring up Josh.PathHandler</h2>
249249
name: 'localname',
250250
path: '/full/path/to/localname'
251251
}</code></pre>
252-
<p><code>PathHandler</code> tracks the current directory/file node in <code>PathHandler.current</code>. Access to the current node is useful in implementing relative path handling in getNode.</p>
252+
<p><a href="docs/githubconsole.html#getNode" target="docs"><code>getNode</code></a> is responsible for fetching the appropriate directory from the API either by relative or absolute path. A path is considered relative if it lacks a leading <code>/</code>. <code>PathHandler</code> tracks the current directory/file node in <code>PathHandler.current</code> which is used to convert a relative path to an <em>absolutish</em> path. Since we also support the standard file system <code>.</code> and <code>..</code> symbols and the github API does not, we then need to take the <em>absolutish</em> path and resolve these symbols before passing the resulting absolute path to <a href="docs/githubconsole.html#getDir" target="docs"><code>getDir(repo_full_name, branch, path, callback)</code></a>.</p>
253+
<p>It is the job of <a href="docs/githubconsole.html#getDir" target="docs"><code>getDir</code></a> to fetch a directory node via <code>GET /repos/:owner/:repo/contents/:path</code>. This API call returns either an array of file objects for a directory or a file object in case the path points directly at a file. We only care about directories for completion and <code>cd</code>, <code>ls</code>, <em>etc.</em> so we ignore file results and build a pathnode for the directory like this:</p>
254+
<pre><code>var node = {
255+
name: _.last(_.filter(path.split("/"), function(x) { return x; })) || "",
256+
path: path,
257+
children: data
258+
};</code></pre>
259+
<p>where <strong>name</strong> is set to the last segment in the path and <strong>children</strong> stores the actual API results (which are lazily converted to childNodes for completion by function <a href="docs/githubconsole.html#makeNodes" target="docs"><code>makeNodes(children)</code></a></p>
260+
261+
<h2>Putting it all together</h2>
253262
</section>
254263

255264
</div>

‎javascripts/githubconsole.js

Copy file name to clipboardExpand all lines: javascripts/githubconsole.js
+36-48Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,35 @@
281281
// a pathnode or null;
282282
_self.pathhandler.getNode = function(path, callback) {
283283
_console.log("looking for node at: " + path);
284+
285+
// If the given path is empty, just return the current pathnode.
284286
if(!path) {
285287
return callback(_self.pathhandler.current);
286288
}
289+
var parts = getPathParts(path);
290+
291+
// If the first part of path parts isn't empty, the path is a relative path, which can be turned into an
292+
// *absolutish* path by pre-pending the parts of the current pathnode.
293+
if(parts[0] !== '') {
294+
parts = getPathParts(_self.pathhandler.current.path).concat(parts);
295+
}
287296

288-
// `buildAbsolutePath` is called recursively to turn `path` into an absolute path that can be resolved against the
289-
// github API with `getDir`.
290-
buildAbsolutePath(path, _self.pathhandler.current, function(absPath) {
291-
_console.log("path to fetch: " + absPath);
292-
return getDir(_self.repo.full_name, _self.branch, absPath, callback);
297+
// At this point the path is *absolutish*, i.e. looks absolute, but all `.` and `..` mentions need to removed and
298+
// resolved before it is truly absolute.
299+
var resolved = [];
300+
_.each(parts, function(x) {
301+
if(x === '.') {
302+
return;
303+
}
304+
if(x === '..') {
305+
resolved.pop();
306+
} else {
307+
resolved.push(x);
308+
}
293309
});
310+
var absolute = resolved.join('/');
311+
_console.log("path to fetch: " + absolute);
312+
return getDir(_self.repo.full_name, _self.branch, absolute, callback);
294313
};
295314

296315
//<section id='getChildNodes'/>
@@ -348,7 +367,7 @@
348367
withCredentials: true
349368
}
350369
};
351-
$.ajax(request).done(function(response,status,xhr) {
370+
$.ajax(request).done(function(response, status, xhr) {
352371

353372
// Every response from the API includes rate limiting headers, as well as an indicator injected by the API proxy
354373
// whether the request was done with authentication. Both are used to display request rate information and a
@@ -526,58 +545,26 @@
526545
});
527546
}
528547

529-
//<section id='buildAbsolutePath'/>
548+
//<section id='getPathParts'/>
530549

531-
// buildAbsolutePath
532-
// -----------------
550+
// getPathParts
551+
// ------------
533552

534-
// This function resolves a path to an absolute path given a current node.
535-
function buildAbsolutePath(path, current, callback) {
536-
_console.log("resolving path: "+path);
553+
// This function splits a path on `/` and removes any empty trailing element.
554+
function getPathParts(path) {
537555
var parts = path.split("/");
538-
539-
// If the first part of the path is `..`, `current` is used to determine the parent path and construct an absolute
540-
// path from the combination of the parent path and the remainder of `path`. Since this compoint path may still
541-
// contain `.` or `..`, path operators that the github API does not understand, the resulting value is fed back
542-
// into `buildAbsolutePath`.
543-
if(parts[0] === '..' ) {
544-
var parentParts = _.filter(current.path.split("/"), function(x) {
545-
return x;
546-
});
547-
path = "/" + parentParts.slice(0, parentParts.length - 1).join('/') + "/" + parts.slice(1).join("/");
548-
return buildAbsolutePath(path, _self.root, callback);
549-
}
550-
551-
// If the first parht of the path is either a `.` or not empty (i.e. the path had started with a `/`, the path must
552-
// be relative and an absolute path can be constructed by combining the path and `current`. Once again, the value
553-
// isfed back into `buildAbsolutePath` for final resolution.
554-
if(parts[0] === '.' || parts[0] !== '') {
555-
path = current.path+"/"+path;
556-
return buildAbsolutePath(path, _self.root, callback);
556+
if(parts[parts.length - 1] === '') {
557+
return parts.slice(0, parts.length - 1);
557558
}
558-
559-
// At this point the path looks absolute, but all `.` and `..` mentions need to removed and resolved before a truly
560-
// absolute path can be returned.
561-
var resolved = [];
562-
_.each(parts, function(x) {
563-
if(x === '.') {
564-
return;
565-
}
566-
if(x === '..') {
567-
resolved.pop();
568-
} else {
569-
resolved.push(x);
570-
}
571-
});
572-
return callback(resolved.join('/'));
559+
return parts;
573560
}
574561

575562
//<section id='makeNodes'/>
576563

577564
// makeNodes
578565
// ---------
579566

580-
// This method builds child pathnodes from the directory information returned by getDir.
567+
// This function builds child pathnodes from the directory information returned by getDir.
581568
function makeNodes(children) {
582569
return _.map(children, function(node) {
583570
return {
@@ -651,4 +638,5 @@
651638
);
652639
});
653640
})(root, $, _);
654-
})(this, $, _);
641+
})
642+
(this, $, _);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.