Skip to content

Commit

Permalink
Merge pull request #29 from ARMmbed/2166-hg-branches
Browse files Browse the repository at this point in the history
IOTIDE 2166: hg branches
  • Loading branch information
federicobozzini authored Nov 11, 2019
2 parents 48b03f3 + 4c86802 commit 67045e8
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 91 deletions.
4 changes: 2 additions & 2 deletions packages/hg/src/browser/diff/hg-diff-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ export class HgDiffWidget extends ScmNavigableListWidget<HgFileChangeNode> imple
if (scmRepository && scmRepository.provider.id === 'hg') {
const provider = scmRepository.provider as HgScmProvider;
const repository = { localUri: scmRepository.provider.rootUri };
const fileChanges: HgFileChange[] = await this.hg.status(repository, options);
const status = await this.hg.status(repository, options);
const fileChangeNodes: HgFileChangeNode[] = [];
for (const fileChange of fileChanges) {
for (const fileChange of status.changes) {
const fileChangeUri = new URI(fileChange.uri);
const [icon, label, description] = await Promise.all([
this.labelProvider.getIcon(fileChangeUri),
Expand Down
2 changes: 1 addition & 1 deletion packages/hg/src/browser/dirty-diff/dirty-diff-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class DirtyDiffManager {
protected async initialize(): Promise<void> {
this.editorManager.onCreated(async e => this.handleEditorCreated(e));
this.repositoryTracker.onHgEvent(throttle(async (event: HgStatusChangeEvent | undefined) =>
this.handleHgStatusUpdate(event && event.source, event ? event.status : []), 500));
this.handleHgStatusUpdate(event && event.source, event ? event.status.changes : []), 500));
const hgStatus = this.repositoryTracker.selectedRepositoryStatus;
const repository = this.repositoryTracker.selectedRepository;
if (hgStatus && repository) {
Expand Down
2 changes: 1 addition & 1 deletion packages/hg/src/browser/hg-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class HgDecorator implements TreeDecorator {

@postConstruct()
protected init(): void {
this.repositories.onHgEvent(event => this.fireDidChangeDecorations((tree: Tree) => this.collectDecorators(tree, event ? event.status : [])));
this.repositories.onHgEvent(event => this.fireDidChangeDecorations((tree: Tree) => this.collectDecorators(tree, event ? event.status.changes : [])));
this.preferences.onPreferenceChanged(event => this.handlePreferenceChange(event));
this.enabled = this.preferences['hg.decorations.enabled'];
this.showColors = this.preferences['hg.decorations.colors'];
Expand Down
26 changes: 9 additions & 17 deletions packages/hg/src/browser/hg-quick-open-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class HgQuickOpenService {
}

if (!useBookmarks) {
const currentBranch = await this.hg.branch(repository, { type: 'current' });
const currentBranch = await this.hg.currentBranch(repository);
if (!currentBranch) {
return;
}
Expand Down Expand Up @@ -202,8 +202,8 @@ export class HgQuickOpenService {
}

private async isClean(repository: Repository) {
const changes = await this.hg.status(repository);
return changes.length === 0;
const status = await this.hg.status(repository);
return status.changes.length === 0;
}

async getOtherHeads(repository: Repository, options?: { branch?: string }): Promise<CommitWithChanges[]> {
Expand All @@ -213,7 +213,7 @@ export class HgQuickOpenService {

private async doMerge(repository: Repository, otherRevision: string, otherBranchName?: string): Promise<string | undefined> {
const mergeResults = await this.hg.merge(repository, otherRevision);
const currentBranch = await this.hg.branch(repository, { type: 'current' });
const currentBranch = await this.getCurrentBranch(repository);

if (mergeResults.unresolvedCount > 0) {
const fileOrFiles = mergeResults.unresolvedCount === 1 ? 'file' : 'files';
Expand All @@ -235,12 +235,8 @@ export class HgQuickOpenService {
async checkout(): Promise<void> {
const repository = this.getRepository();
if (repository) {
const [branches, currentBranch] = await Promise.all([this.getBranches(), this.getCurrentBranch()]);
if (currentBranch) {
// We do not show the current branch.
const index = branches.findIndex(branch => branch && branch.name === currentBranch.name);
branches.splice(index, 1);
}
const branches = await this.getBranches();

const switchBranch = async (item: HgQuickOpenItem<Branch>) => {
try {
await this.hg.checkout(repository, { branch: item.ref.nameWithoutRemote });
Expand Down Expand Up @@ -272,7 +268,7 @@ export class HgQuickOpenService {
`Create a new local branch with name: ${lookFor}. ${suffix}`,
async () => {
try {
await hgQuickOpenService.hg.branch(repository, { toCreate: lookFor });
await hgQuickOpenService.hg.createBranch(repository, lookFor);
await hgQuickOpenService.hg.checkout(repository, { branch: lookFor });
} catch (error) {
hgQuickOpenService.hgErrorHandler.handleError(error);
Expand Down Expand Up @@ -386,11 +382,7 @@ export class HgQuickOpenService {
return [];
}
try {
const [local, remote] = await Promise.all([
this.hg.branch(repository, { type: 'local' }),
this.hg.branch(repository, { type: 'remote' })
]);
return [...local, ...remote];
return await this.hg.branches(repository);
} catch (error) {
this.hgErrorHandler.handleError(error);
return [];
Expand All @@ -402,7 +394,7 @@ export class HgQuickOpenService {
return undefined;
}
try {
return await this.hg.branch(repository, { type: 'current' });
return await this.hg.currentBranch(repository);
} catch (error) {
this.hgErrorHandler.handleError(error);
return undefined;
Expand Down
6 changes: 3 additions & 3 deletions packages/hg/src/browser/hg-repository-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ export class HgRepositoryTracker {
}, 50);

protected async setStatus(event: HgStatusChangeEvent | undefined, token: CancellationToken): Promise<void> {
const changes = event ? event.status : [];
const status = event && event.status;
const scmProvider = this.repositoryProvider.selectedScmProvider;
if (scmProvider) {
await scmProvider.setStatus(changes, token);
await scmProvider.setStatus(status, token);
}
if (token.isCancellationRequested) {
return;
}
this.workingDirectoryChanges = changes;
this.workingDirectoryChanges = status ? status.changes : [];
this.onHgEventEmitter.fire(event);
}

Expand Down
33 changes: 17 additions & 16 deletions packages/hg/src/browser/hg-scm-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,18 @@ export class HgScmProvider implements ScmProvider {
return this.state.mergeChanges;
}

getStatus(): WorkingDirectoryStatus {
return {
changes: this.state.changes,
exists: true
};
}
async setStatus(changes: HgFileChange[], token: CancellationToken): Promise<void> {
const state = HgScmProvider.initState();
for (const change of changes) {
if (change.status === HgFileStatus.Untracked) {
state.untrackedChanges.push(change);
} else {
state.changes.push(change);
getStatus(): WorkingDirectoryStatus | undefined {
return this.state.status;
}
async setStatus(status: WorkingDirectoryStatus | undefined, token: CancellationToken): Promise<void> {
const state = HgScmProvider.initState(status);
if (status) {
for (const change of status.changes) {
if (change.status === HgFileStatus.Untracked) {
state.untrackedChanges.push(change);
} else {
state.changes.push(change);
}
}
}

Expand Down Expand Up @@ -299,8 +298,8 @@ export class HgScmProvider implements ScmProvider {
}
async discard(uri: string): Promise<void> {
const { repository } = this;
const changes = this.getStatus().changes;
if (!changes.some(change => change.uri === uri)) {
const status = this.getStatus();
if (!(status && status.changes.some(change => change.uri === uri))) {
return;
}
// Allow deletion, only iff the same file is not managed by Hg.
Expand Down Expand Up @@ -393,13 +392,15 @@ export namespace HgScmProvider {
export const HG_COMMIT_DETAIL = 'hg-commit-detail-widget';

export interface State {
status?: WorkingDirectoryStatus
changes: HgFileChange[]
untrackedChanges: HgFileChange[]
mergeChanges: HgFileChange[],
groups: ScmResourceGroup[]
}
export function initState(): HgScmProvider.State {
export function initState(status?: WorkingDirectoryStatus): HgScmProvider.State {
return {
status,
changes: [],
untrackedChanges: [],
mergeChanges: [],
Expand Down
12 changes: 2 additions & 10 deletions packages/hg/src/browser/hg-sync-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ export class HgSyncService {
this.setSyncing(true);
try {
await this.hg.pull(repository);
let changes = await this.hg.status(repository);
let status: WorkingDirectoryStatus = {
changes,
exists: true
};
let status = await this.hg.status(repository);
this.setSyncing(false);

const method = await this.getSyncMethod(status);
Expand All @@ -87,11 +83,7 @@ export class HgSyncService {
update: method === 'pull-push',
rebase: method === 'rebase-push'
});
changes = await this.hg.status(repository);
status = {
changes,
exists: true
};
status = await this.hg.status(repository);
}
if (this.shouldPush(status)) {
await this.hg.push(repository, {
Expand Down
6 changes: 3 additions & 3 deletions packages/hg/src/common/hg-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { injectable, inject } from 'inversify';
import { JsonRpcServer, JsonRpcProxy } from '@theia/core';
import { Repository, HgFileChange } from './hg-model';
import { Repository, WorkingDirectoryStatus } from './hg-model';
import { Disposable, DisposableCollection, Emitter, Event } from '@theia/core/lib/common';

/**
Expand All @@ -32,12 +32,12 @@ export interface HgStatusChangeEvent {
/**
* The new working directory state.
*/
readonly status: HgFileChange[];
readonly status: WorkingDirectoryStatus;

/**
* The previous working directory state, if any.
*/
readonly oldStatus?: HgFileChange[];
readonly oldStatus?: WorkingDirectoryStatus;

}

Expand Down
30 changes: 16 additions & 14 deletions packages/hg/src/common/hg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
import { ChildProcess } from 'child_process';
import { Disposable } from '@theia/core';
import {
Repository, Branch, HgResult,
HgFileChange, CommitWithChanges, Remote, MergeResult
Repository,
Branch,
HgResult,
CommitWithChanges,
Remote,
MergeResult,
WorkingDirectoryStatus,
} from './hg-model';

/**
Expand Down Expand Up @@ -586,7 +591,7 @@ export interface Hg extends Disposable {
*
* @param repository the repository to get the changeset status from.
*/
status(repository: Repository, options?: Hg.Options.Status): Promise<HgFileChange[]>;
status(repository: Repository, options?: Hg.Options.Status): Promise<WorkingDirectoryStatus>;

/**
* Stages the given file or files in the working clone. The invocation will be rejected if
Expand All @@ -610,29 +615,26 @@ export interface Hg extends Disposable {
/**
* Returns with the currently active branch, or `undefined` if the current branch is in detached mode.
*
* @param the repository where the current branch has to be queried.
* @param options the type of the branch, which is always the `current`.
* @param repository the repository where the current branch has to be queried.
*/
branch(repository: Repository, options: { type: 'current' }): Promise<Branch | undefined>;
currentBranch(repository: Repository): Promise<Branch | undefined>;

/**
* Returns with an array of branches.
*
* @param the repository where the branches has to be queried.
* @param options the type of the branch, which is either the `local`, the `remote`, or `all` of them.
* @param repository the repository where the branches has to be queried.
*/
branch(repository: Repository, options: { type: 'local' | 'remote' | 'all' }): Promise<Branch[]>;
branches(repository: Repository): Promise<Branch[]>;

/**
* Creates, renames, and deletes a branch.
*
* @param the repository where the branch modification has to be performed.
* @param options further Hg command refinements for the branch modification.
* @param branchName The desired name of the new branch.
* @param branchStartPoint The new branch head will point to this commit. It may be given as a branch name, a commit-id, or a tag.
* If this parameter is omitted, the current `HEAD` will be used instead.
*/
branch(repository: Repository, options:
Hg.Options.BranchCommand.Create |
Hg.Options.BranchCommand.Rename |
Hg.Options.BranchCommand.Delete): Promise<void>;
createBranch(repository: Repository, branchName: string, branchStartPoint?: string): Promise<void>

/**
* Switches branches or restores working tree files.
Expand Down
Loading

0 comments on commit 67045e8

Please sign in to comment.