From 6d2598ad889e8b3332b94c79e8c977c62d73ea4b Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Fri, 24 Aug 2018 09:49:48 +0200 Subject: [PATCH] git api: repository state --- extensions/git/src/api/api1.ts | 19 ++++++++++--- extensions/git/src/api/git.d.ts | 49 ++++++++++++++++++++++++++++++-- extensions/git/src/commands.ts | 3 +- extensions/git/src/git.ts | 36 ++++------------------- extensions/git/src/repository.ts | 3 +- extensions/git/src/statusbar.ts | 2 +- 6 files changed, 72 insertions(+), 40 deletions(-) diff --git a/extensions/git/src/api/api1.ts b/extensions/git/src/api/api1.ts index 0057e80fdeb..ca79572b6bc 100644 --- a/extensions/git/src/api/api1.ts +++ b/extensions/git/src/api/api1.ts @@ -7,7 +7,7 @@ import { Model } from '../model'; import { Repository as BaseRepository } from '../repository'; -import { InputBox, Git, API, Repository, Remote } from './git'; +import { InputBox, Git, API, Repository, Remote, RepositoryState, Branch, Ref, Submodule, Commit } from './git'; import { Event, SourceControlInputBox, Uri } from 'vscode'; import { mapEvent } from '../util'; @@ -17,13 +17,24 @@ class ApiInputBox implements InputBox { constructor(private _inputBox: SourceControlInputBox) { } } +export class ApiRepositoryState implements RepositoryState { + + get HEAD(): Branch | undefined { return this._repository.HEAD; } + get refs(): Ref[] { return [...this._repository.refs]; } + get remotes(): Remote[] { return [...this._repository.remotes]; } + get submodules(): Submodule[] { return [...this._repository.submodules]; } + get rebaseCommit(): Commit | undefined { return this._repository.rebaseCommit; } + + readonly onDidChange: Event = this._repository.onDidRunGitStatus; + + constructor(private _repository: BaseRepository) { } +} + export class ApiRepository implements Repository { readonly rootUri: Uri = Uri.file(this._repository.root); readonly inputBox: InputBox = new ApiInputBox(this._repository.inputBox); - get remotes(): Remote[] { return [...this._repository.remotes]; } - - readonly onDidRunGitStatus: Event = this._repository.onDidRunGitStatus; + readonly state: RepositoryState = new ApiRepositoryState(this._repository); constructor(private _repository: BaseRepository) { } diff --git a/extensions/git/src/api/git.d.ts b/extensions/git/src/api/git.d.ts index 7ba18e0d70a..caeaafa13a0 100644 --- a/extensions/git/src/api/git.d.ts +++ b/extensions/git/src/api/git.d.ts @@ -14,6 +14,42 @@ export interface InputBox { value: string; } +export const enum RefType { + Head, + RemoteHead, + Tag +} + +export interface Ref { + readonly type: RefType; + readonly name?: string; + readonly commit?: string; + readonly remote?: string; +} + +export interface UpstreamRef { + readonly remote: string; + readonly name: string; +} + +export interface Branch extends Ref { + readonly upstream?: UpstreamRef; + readonly ahead?: number; + readonly behind?: number; +} + +export interface Commit { + readonly hash: string; + readonly message: string; + readonly parents: string[]; +} + +export interface Submodule { + readonly name: string; + readonly path: string; + readonly url: string; +} + export interface Remote { readonly name: string; readonly fetchUrl?: string; @@ -21,12 +57,19 @@ export interface Remote { readonly isReadOnly: boolean; } +export interface RepositoryState { + readonly HEAD: Branch | undefined; + readonly refs: Ref[]; + readonly remotes: Remote[]; + readonly submodules: Submodule[]; + readonly rebaseCommit: Commit | undefined; + readonly onDidChange: Event; +} + export interface Repository { readonly rootUri: Uri; readonly inputBox: InputBox; - readonly remotes: Remote[]; - - readonly onDidRunGitStatus: Event; + readonly state: RepositoryState; status(): Promise; } diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 2e1c115c14f..25f3bc2979e 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -6,7 +6,7 @@ 'use strict'; import { Uri, commands, Disposable, window, workspace, QuickPickItem, OutputChannel, Range, WorkspaceEdit, Position, LineChange, SourceControlResourceState, TextDocumentShowOptions, ViewColumn, ProgressLocation, TextEditor, MessageOptions } from 'vscode'; -import { Ref, RefType, Git, GitErrorCodes, Branch } from './git'; +import { Git, GitErrorCodes } from './git'; import { Repository, Resource, Status, CommitOptions, ResourceGroupType } from './repository'; import { Model } from './model'; import { toGitUri, fromGitUri } from './uri'; @@ -17,6 +17,7 @@ import { lstat, Stats } from 'fs'; import * as os from 'os'; import TelemetryReporter from 'vscode-extension-telemetry'; import * as nls from 'vscode-nls'; +import { Ref, RefType, Branch } from './api/git'; const localize = nls.loadMessageBundle(); diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index 01e08bd3878..36e7b4f53ec 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -16,6 +16,7 @@ import * as filetype from 'file-type'; import { assign, groupBy, denodeify, IDisposable, toDisposable, dispose, mkdirp, readBytes, detectUnicodeEncoding, Encoding, onceEvent } from './util'; import { CancellationToken } from 'vscode'; import { detectEncoding } from './encoding'; +import { Ref, RefType, Branch, Remote } from './api/git'; const readfile = denodeify(fs.readFile); @@ -31,40 +32,15 @@ export interface IFileStatus { rename?: string; } -export interface Remote { - name: string; - fetchUrl?: string; - pushUrl?: string; - isReadOnly: boolean; -} - export interface Stash { index: number; description: string; } -export enum RefType { - Head, - RemoteHead, - Tag -} - -export interface Ref { - type: RefType; - name?: string; - commit?: string; - remote?: string; -} - -export interface UpstreamRef { - remote: string; - name: string; -} - -export interface Branch extends Ref { - upstream?: UpstreamRef; - ahead?: number; - behind?: number; +interface MutableRemote extends Remote { + fetchUrl?: string; + pushUrl?: string; + isReadOnly: boolean; } function parseVersion(raw: string): string { @@ -1320,7 +1296,7 @@ export class Repository { async getRemotes(): Promise { const result = await this.run(['remote', '--verbose']); const lines = result.stdout.trim().split('\n').filter(l => !!l); - const remotes: Remote[] = []; + const remotes: MutableRemote[] = []; for (const line of lines) { const parts = line.split(/\s/); diff --git a/extensions/git/src/repository.ts b/extensions/git/src/repository.ts index c2c9a2f344d..2135fa69091 100644 --- a/extensions/git/src/repository.ts +++ b/extensions/git/src/repository.ts @@ -6,7 +6,7 @@ 'use strict'; import { commands, Uri, Command, EventEmitter, Event, scm, SourceControl, SourceControlInputBox, SourceControlResourceGroup, SourceControlResourceState, SourceControlResourceDecorations, SourceControlInputBoxValidation, Disposable, ProgressLocation, window, workspace, WorkspaceEdit, ThemeColor, DecorationData, Memento, SourceControlInputBoxValidationType } from 'vscode'; -import { Repository as BaseRepository, Ref, Branch, Remote, Commit, GitErrorCodes, Stash, RefType, GitError, Submodule, DiffOptions } from './git'; +import { Repository as BaseRepository, Commit, GitErrorCodes, Stash, GitError, Submodule, DiffOptions } from './git'; import { anyEvent, filterEvent, eventToPromise, dispose, find, isDescendant, IDisposable, onceEvent, EmptyDisposable, debounceEvent } from './util'; import { memoize, throttle, debounce } from './decorators'; import { toGitUri } from './uri'; @@ -15,6 +15,7 @@ import * as path from 'path'; import * as nls from 'vscode-nls'; import * as fs from 'fs'; import { StatusBarCommands } from './statusbar'; +import { Branch, Ref, Remote, RefType } from './api/git'; const timeout = (millis: number) => new Promise(c => setTimeout(c, millis)); diff --git a/extensions/git/src/statusbar.ts b/extensions/git/src/statusbar.ts index 979c961f64d..8cabe51c016 100644 --- a/extensions/git/src/statusbar.ts +++ b/extensions/git/src/statusbar.ts @@ -6,10 +6,10 @@ 'use strict'; import { Disposable, Command, EventEmitter, Event } from 'vscode'; -import { Branch } from './git'; import { Repository, Operation } from './repository'; import { anyEvent, dispose } from './util'; import * as nls from 'vscode-nls'; +import { Branch } from './api/git'; const localize = nls.loadMessageBundle();