Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 70b0925

Browse files
authored
Merge pull request #2426 from atom/create-repository-for-single-file
Create repository for single file
2 parents 600e9ba + 3a5b5ad commit 70b0925

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

lib/github-package.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,29 @@ export default class GithubPackage {
549549
* projects and pane items.
550550
*/
551551
async getNextContext(usePath = null) {
552+
// Internal utility function to normalize paths not contained within a git
553+
// working tree.
554+
const workdirForNonGitPath = async sourcePath => {
555+
const containingRoot = this.project.getDirectories().find(root => root.contains(sourcePath));
556+
if (containingRoot) {
557+
return containingRoot.getPath();
558+
/* istanbul ignore else */
559+
} else if (!(await fs.stat(sourcePath)).isDirectory()) {
560+
return path.dirname(sourcePath);
561+
} else {
562+
return sourcePath;
563+
}
564+
};
565+
566+
// Internal utility function to identify the working directory to use for
567+
// an arbitrary (file or directory) path.
568+
const workdirForPath = async sourcePath => {
569+
return (await Promise.all([
570+
this.workdirCache.find(sourcePath),
571+
workdirForNonGitPath(sourcePath),
572+
])).find(Boolean);
573+
};
574+
552575
// Identify paths that *could* contribute a git working directory to the pool. This is drawn from
553576
// the roots of open projects, the currently locked context if one is present, and the path of the
554577
// open workspace item.
@@ -574,14 +597,16 @@ export default class GithubPackage {
574597
const workdirs = new Set(
575598
await Promise.all(
576599
Array.from(candidatePaths, async candidatePath => {
577-
const workdir = (await this.workdirCache.find(candidatePath)) || candidatePath;
600+
const workdir = await workdirForPath(candidatePath);
601+
578602
// Note the workdirs associated with the active pane item and the first open project so we can
579603
// prefer them later.
580604
if (candidatePath === activeItemPath) {
581605
activeItemWorkdir = workdir;
582606
} else if (candidatePath === this.project.getPaths()[0]) {
583607
firstProjectWorkdir = workdir;
584608
}
609+
585610
return workdir;
586611
}),
587612
),
@@ -593,7 +618,17 @@ export default class GithubPackage {
593618
// 1 - Explicitly requested workdir. This is either selected by the user from a context tile or
594619
// deserialized from package state. Choose this context only if it still exists in the pool.
595620
if (usePath) {
596-
const stateContext = this.contextPool.getContext(usePath);
621+
// Normalize usePath in a similar fashion to the way we do activeItemPath.
622+
let useWorkdir = usePath;
623+
if (usePath === activeItemPath) {
624+
useWorkdir = activeItemWorkdir;
625+
} else if (usePath === this.project.getPaths()[0]) {
626+
useWorkdir = firstProjectWorkdir;
627+
} else {
628+
useWorkdir = await workdirForPath(usePath);
629+
}
630+
631+
const stateContext = this.contextPool.getContext(useWorkdir);
597632
if (stateContext.isPresent()) {
598633
return stateContext;
599634
}

test/github-package.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,29 @@ describe('GithubPackage', function() {
455455
assert.strictEqual(githubPackage.getActiveWorkdir(), workdirPath2);
456456
});
457457

458+
it('uses a context associated with a project root for paths within that root', async function() {
459+
const workdirPath4 = await getTempDir();
460+
await fs.mkdir(path.join(workdirPath4, 'subdir'));
461+
const itemPath4 = path.join(workdirPath4, 'subdir/file.txt');
462+
await fs.writeFile(itemPath4, 'content\n');
463+
464+
await contextUpdateAfter(githubPackage, () => project.setPaths([workdirPath1, workdirPath4]));
465+
466+
assert.strictEqual(githubPackage.getActiveWorkdir(), workdirPath1);
467+
await contextUpdateAfter(githubPackage, () => atomEnv.workspace.open(itemPath4));
468+
assert.strictEqual(githubPackage.getActiveWorkdir(), workdirPath4);
469+
});
470+
471+
it('uses a context of the containing directory for file item paths not in any root', async function() {
472+
const workdirPath4 = await getTempDir();
473+
const itemPath4 = path.join(workdirPath4, 'file.txt');
474+
await fs.writeFile(itemPath4, 'content\n');
475+
476+
assert.strictEqual(githubPackage.getActiveWorkdir(), workdirPath1);
477+
await contextUpdateAfter(githubPackage, () => atomEnv.workspace.open(itemPath4));
478+
assert.strictEqual(githubPackage.getActiveWorkdir(), workdirPath4);
479+
});
480+
458481
it('does nothing if the context is locked', async function() {
459482
const itemPath2 = path.join(workdirPath2, 'c.txt');
460483

0 commit comments

Comments
 (0)