Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion lib/controllers/root-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import GitTimingsView from '../views/git-timings-view';
import Conflict from '../models/conflicts/conflict';
import {getEndpoint} from '../models/endpoint';
import Switchboard from '../switchboard';
import {WorkdirContextPoolPropType} from '../prop-types';
import {WorkdirContextPoolPropType, WorkdirCachePropType} from '../prop-types';
import {destroyFilePatchPaneItems, destroyEmptyFilePatchPaneItems, autobind} from '../helpers';
import {GitError} from '../git-shell-out-strategy';
import {incrementCounter, addEvent} from '../reporter-proxy';
Expand All @@ -54,6 +54,7 @@ export default class RootController extends React.Component {
// Models
loginModel: PropTypes.object.isRequired,
workdirContextPool: WorkdirContextPoolPropType.isRequired,
workdirCache: WorkdirCachePropType.isRequired,
repository: PropTypes.object.isRequired,
resolutionProgress: PropTypes.object.isRequired,
statusBar: PropTypes.object,
Expand Down Expand Up @@ -164,6 +165,10 @@ export default class RootController extends React.Component {
command="github:view-staged-changes-for-current-file"
callback={this.viewStagedChangesForCurrentFile}
/>
<Command
command="github:select-active-pane-directory"
callback={this.selectActivePaneWorkDir}
/>
<Command
command="github:close-all-diff-views"
callback={this.destroyFilePatchPaneItems}
Expand Down Expand Up @@ -662,6 +667,52 @@ export default class RootController extends React.Component {
return this.props.workspace.toggle(CommitPreviewItem.buildURI(workdir));
}

selectActivePaneWorkDir = async () => {
const paneItem = this.props.workspace.getCenter().getActivePaneItem();

if (!paneItem) {
return;
}

let itemPath = null;

// Likely GitHub package provided pane item
if (typeof paneItem.getWorkingDirectory === 'function') {
itemPath = path.normalize(paneItem.getWorkingDirectory());
}

// TextEditor-like
if (typeof paneItem.getPath === 'function') {
itemPath = path.normalize(paneItem.getPath());
}

// Oh well
if (!itemPath) {
return;
}

// Find valid git working directory
const workdir = await this.props.workdirCache.find(itemPath);

if (workdir) {
this.props.changeWorkingDirectory(workdir);
return;
}

// At this point, there is no parent git working directory
// But, we can still assign the current context to a valid context
// if the file is a child of a current working directory
const workdirs = this.props.workdirContextPool.getCurrentWorkDirs();
const itemTokens = itemPath.split(path.sep);
for (const dir of workdirs) {
const tokens = path.normalize(dir).split(path.sep).filter(t => t.length);
if (tokens.every((t, i) => itemTokens[i] === t)) {
// is a child of
this.props.changeWorkingDirectory(dir);
}
}
}

showWaterfallDiagnostics() {
this.props.workspace.open(GitTimingsView.buildURI());
}
Expand Down
1 change: 1 addition & 0 deletions lib/github-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ export default class GithubPackage {
confirm={this.confirm}
currentWindow={this.currentWindow}
workdirContextPool={this.contextPool}
workdirCache={this.workdirCache}
loginModel={this.loginModel}
repository={this.getActiveRepository()}
resolutionProgress={this.getActiveResolutionProgress()}
Expand Down
4 changes: 4 additions & 0 deletions lib/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export const WorkdirContextPoolPropType = PropTypes.shape({
getContext: PropTypes.func.isRequired,
});

export const WorkdirCachePropType = PropTypes.shape({
find: PropTypes.func.isRequired,
});

export const GithubLoginModelPropType = PropTypes.shape({
getToken: PropTypes.func.isRequired,
setToken: PropTypes.func.isRequired,
Expand Down
18 changes: 17 additions & 1 deletion test/controllers/root-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {cloneRepository, buildRepository} from '../helpers';
import {multiFilePatchBuilder} from '../builder/patch';
import Repository from '../../lib/models/repository';
import WorkdirContextPool from '../../lib/models/workdir-context-pool';
import WorkdirCache from '../../lib/models/workdir-cache';
import ResolutionProgress from '../../lib/models/conflicts/resolution-progress';
import RemoteSet from '../../lib/models/remote-set';
import Remote from '../../lib/models/remote';
Expand All @@ -33,7 +34,8 @@ import RootController from '../../lib/controllers/root-controller';
describe('RootController', function() {
let atomEnv, app;
let workspace, commands, notificationManager, tooltips, config, confirm, deserializers, grammars, project;
let workdirContextPool;
let workdirContextPool, workdirCache;
let changeWorkingDirectory;

beforeEach(function() {
atomEnv = global.buildAtomEnvironment();
Expand All @@ -46,13 +48,15 @@ describe('RootController', function() {
config = atomEnv.config;
project = atomEnv.project;

workdirCache = new WorkdirCache();
workdirContextPool = new WorkdirContextPool();

const loginModel = new GithubLoginModel(InMemoryStrategy);
const absentRepository = Repository.absent();
const emptyResolutionProgress = new ResolutionProgress();

confirm = sinon.stub(atomEnv, 'confirm');
changeWorkingDirectory = sinon.spy();
app = (
<RootController
workspace={workspace}
Expand All @@ -69,6 +73,7 @@ describe('RootController', function() {

loginModel={loginModel}
workdirContextPool={workdirContextPool}
workdirCache={workdirCache}
repository={absentRepository}
resolutionProgress={emptyResolutionProgress}

Expand All @@ -77,6 +82,8 @@ describe('RootController', function() {

startOpen={false}
startRevealed={false}

changeWorkingDirectory={changeWorkingDirectory}
/>
);
});
Expand Down Expand Up @@ -355,6 +362,15 @@ describe('RootController', function() {
});
});

describe('selectActivePaneWorkDir()', function() {
it('does not update if there is no active pane', function() {
const wrapper = shallow(app);
wrapper.find('Command[command="github:select-active-pane-directory"]').prop('callback')();

assert.isTrue(changeWorkingDirectory.notCalled);
});
});

describe('openCloneDialog()', function() {
let clone;

Expand Down