Skip to content

Commit 103715c

Browse files
committed
git: implement API
1 parent 28d2b5d commit 103715c

5 files changed

Lines changed: 238 additions & 17 deletions

File tree

extensions/git/src/api/api1.ts

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
'use strict';
77

88
import { Model } from '../model';
9-
import { Repository as BaseRepository } from '../repository';
10-
import { InputBox, Git, API, Repository, Remote, RepositoryState, Branch, Ref, Submodule, Commit } from './git';
9+
import { Repository as BaseRepository, Resource } from '../repository';
10+
import { InputBox, Git, API, Repository, Remote, RepositoryState, Branch, Ref, Submodule, Commit, Change } from './git';
1111
import { Event, SourceControlInputBox, Uri } from 'vscode';
1212
import { mapEvent } from '../util';
1313

@@ -17,6 +17,11 @@ class ApiInputBox implements InputBox {
1717
constructor(private _inputBox: SourceControlInputBox) { }
1818
}
1919

20+
export class ApiChange implements Change {
21+
22+
constructor(_resource: Resource) { }
23+
}
24+
2025
export class ApiRepositoryState implements RepositoryState {
2126

2227
get HEAD(): Branch | undefined { return this._repository.HEAD; }
@@ -25,6 +30,10 @@ export class ApiRepositoryState implements RepositoryState {
2530
get submodules(): Submodule[] { return [...this._repository.submodules]; }
2631
get rebaseCommit(): Commit | undefined { return this._repository.rebaseCommit; }
2732

33+
get mergeChanges(): Change[] { return this._repository.mergeGroup.resourceStates.map(r => new ApiChange(r)); }
34+
get indexChanges(): Change[] { return this._repository.indexGroup.resourceStates.map(r => new ApiChange(r)); }
35+
get workingTreeChanges(): Change[] { return this._repository.workingTreeGroup.resourceStates.map(r => new ApiChange(r)); }
36+
2837
readonly onDidChange: Event<void> = this._repository.onDidRunGitStatus;
2938

3039
constructor(private _repository: BaseRepository) { }
@@ -38,9 +47,101 @@ export class ApiRepository implements Repository {
3847

3948
constructor(private _repository: BaseRepository) { }
4049

50+
getConfigs(): Promise<{ key: string; value: string; }[]> {
51+
return this._repository.getConfigs();
52+
}
53+
54+
getConfig(key: string): Promise<string> {
55+
return this._repository.getConfig(key);
56+
}
57+
58+
setConfig(key: string, value: string): Promise<string> {
59+
return this._repository.setConfig(key, value);
60+
}
61+
62+
show(ref: string, path: string): Promise<string> {
63+
return this._repository.show(ref, path);
64+
}
65+
66+
getCommit(ref: string): Promise<Commit> {
67+
return this._repository.getCommit(ref);
68+
}
69+
70+
getObjectDetails(treeish: string, path: string): Promise<{ mode: string; object: string; size: number; }> {
71+
return this._repository.getObjectDetails(treeish, path);
72+
}
73+
74+
diffWithHEAD(path: string): Promise<string> {
75+
return this._repository.diffWithHEAD(path);
76+
}
77+
78+
diffWith(ref: string, path: string): Promise<string> {
79+
return this._repository.diffWith(ref, path);
80+
}
81+
82+
diffIndexWithHEAD(path: string): Promise<string> {
83+
return this._repository.diffIndexWithHEAD(path);
84+
}
85+
86+
diffIndexWith(ref: string, path: string): Promise<string> {
87+
return this._repository.diffIndexWith(ref, path);
88+
}
89+
90+
diffBlobs(object1: string, object2: string): Promise<string> {
91+
return this._repository.diffBlobs(object1, object2);
92+
}
93+
94+
diffBetween(ref1: string, ref2: string, path: string): Promise<string> {
95+
return this._repository.diffBetween(ref1, ref2, path);
96+
}
97+
98+
hashObject(data: string): Promise<string> {
99+
return this._repository.hashObject(data);
100+
}
101+
102+
createBranch(name: string, checkout: boolean, ref?: string | undefined): Promise<void> {
103+
return this._repository.branch(name, checkout, ref);
104+
}
105+
106+
deleteBranch(name: string): Promise<void> {
107+
return this._repository.deleteBranch(name);
108+
}
109+
110+
getBranch(name: string): Promise<Branch> {
111+
return this._repository.getBranch(name);
112+
}
113+
114+
setBranchUpstream(name: string, upstream: string): Promise<void> {
115+
return this._repository.setBranchUpstream(name, upstream);
116+
}
117+
118+
getMergeBase(ref1: string, ref2: string): Promise<string> {
119+
throw new Error('Method not implemented.');
120+
}
121+
41122
status(): Promise<void> {
42123
return this._repository.status();
43124
}
125+
126+
checkout(treeish: string): Promise<void> {
127+
return this._repository.checkout(treeish);
128+
}
129+
130+
addRemote(name: string, url: string): Promise<void> {
131+
return this._repository.addRemote(name, url);
132+
}
133+
134+
removeRemote(name: string): Promise<void> {
135+
return this._repository.removeRemote(name);
136+
}
137+
138+
fetch(remote?: string | undefined, ref?: string | undefined): Promise<void> {
139+
return this._repository.fetch(remote, ref);
140+
}
141+
142+
pull(): Promise<void> {
143+
return this._repository.pull();
144+
}
44145
}
45146

46147
export class ApiGit implements Git {

extensions/git/src/api/git.d.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,15 @@ export interface RepositoryState {
7474
readonly onDidChange: Event<void>;
7575
}
7676

77-
export const enum ConfigScope {
78-
System,
79-
Global,
80-
Local
81-
}
82-
8377
export interface Repository {
8478

8579
readonly rootUri: Uri;
8680
readonly inputBox: InputBox;
8781
readonly state: RepositoryState;
8882

89-
getConfigs(scope: ConfigScope): Promise<{ key: string; value: string; }[]>;
90-
getConfig(scope: ConfigScope, key: string): Promise<string>;
91-
setConfig(scope: ConfigScope, key: string, value: string): Promise<string>;
83+
getConfigs(): Promise<{ key: string; value: string; }[]>;
84+
getConfig(key: string): Promise<string>;
85+
setConfig(key: string, value: string): Promise<string>;
9286

9387
show(ref: string, path: string): Promise<string>;
9488
getCommit(ref: string): Promise<Commit>;

extensions/git/src/contentProvider.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ export class GitContentProvider {
9292
return '';
9393
}
9494

95-
return await repository.diff(path, ref === 'index');
95+
if (ref === 'index') {
96+
return await repository.diffIndexWithHEAD(path);
97+
} else {
98+
return await repository.diffWithHEAD(path);
99+
}
96100
}
97101

98102
const repository = this.model.getRepository(uri);

extensions/git/src/git.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ export class Repository {
682682
return this.git.spawn(args, options);
683683
}
684684

685-
async config(scope: string, key: string, value: any, options: SpawnOptions): Promise<string> {
685+
async config(scope: string, key: string, value: any = null, options: SpawnOptions = {}): Promise<string> {
686686
const args = ['config'];
687687

688688
if (scope) {
@@ -699,6 +699,24 @@ export class Repository {
699699
return result.stdout;
700700
}
701701

702+
async getConfigs(scope: string): Promise<{ key: string; value: string; }[]> {
703+
const args = ['config'];
704+
705+
if (scope) {
706+
args.push('--' + scope);
707+
}
708+
709+
args.push('-l');
710+
711+
const result = await this.run(args);
712+
const lines = result.stdout.trim().split(/\r|\r\n|\n/);
713+
714+
return lines.map(entry => {
715+
const equalsIndex = entry.indexOf('=');
716+
return { key: entry.substr(0, equalsIndex), value: entry.substr(equalsIndex + 1) };
717+
});
718+
}
719+
702720
async bufferString(object: string, encoding: string = 'utf8', autoGuessEncoding = false): Promise<string> {
703721
const stdout = await this.buffer(object);
704722

@@ -837,6 +855,36 @@ export class Repository {
837855
return result.stdout;
838856
}
839857

858+
async diffWithHEAD(path: string): Promise<string> {
859+
const args = ['diff', '--', path];
860+
const result = await this.run(args);
861+
return result.stdout;
862+
}
863+
864+
async diffWith(ref: string, path: string): Promise<string> {
865+
const args = ['diff', ref, '--', path];
866+
const result = await this.run(args);
867+
return result.stdout;
868+
}
869+
870+
async diffIndexWithHEAD(path: string): Promise<string> {
871+
const args = ['diff', '--cached', '--', path];
872+
const result = await this.run(args);
873+
return result.stdout;
874+
}
875+
876+
async diffIndexWith(ref: string, path: string): Promise<string> {
877+
const args = ['diff', '--cached', ref, '--', path];
878+
const result = await this.run(args);
879+
return result.stdout;
880+
}
881+
882+
async diffBlobs(object1: string, object2: string): Promise<string> {
883+
const args = ['diff', object1, object2];
884+
const result = await this.run(args);
885+
return result.stdout;
886+
}
887+
840888
async diffBetween(ref1: string, ref2: string, path: string): Promise<string> {
841889
const args = ['diff', `${ref1}...${ref2}`, '--', path];
842890
const result = await this.run(args);
@@ -851,6 +899,13 @@ export class Repository {
851899
return result.stdout.trim();
852900
}
853901

902+
async hashObject(data: string): Promise<string> {
903+
const args = ['hash-object', '-w', '--stdin'];
904+
const result = await this.run(args, { input: data });
905+
906+
return result.stdout.trim();
907+
}
908+
854909
async add(paths: string[]): Promise<void> {
855910
const args = ['add', '-A', '--'];
856911

@@ -1098,6 +1153,11 @@ export class Repository {
10981153
await this.run(args);
10991154
}
11001155

1156+
async removeRemote(name: string): Promise<void> {
1157+
const args = ['remote', 'rm', name];
1158+
await this.run(args);
1159+
}
1160+
11011161
async fetch(remote?: string, ref?: string): Promise<void> {
11021162
const args = ['fetch'];
11031163

extensions/git/src/repository.ts

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'use strict';
77

88
import { commands, Uri, Command, EventEmitter, Event, scm, SourceControl, SourceControlInputBox, SourceControlResourceGroup, SourceControlResourceState, SourceControlResourceDecorations, SourceControlInputBoxValidation, Disposable, ProgressLocation, window, workspace, WorkspaceEdit, ThemeColor, DecorationData, Memento, SourceControlInputBoxValidationType } from 'vscode';
9-
import { Repository as BaseRepository, Commit, GitErrorCodes, Stash, GitError, Submodule, DiffOptions } from './git';
9+
import { Repository as BaseRepository, Commit, GitErrorCodes, Stash, GitError, Submodule } from './git';
1010
import { anyEvent, filterEvent, eventToPromise, dispose, find, isDescendant, IDisposable, onceEvent, EmptyDisposable, debounceEvent } from './util';
1111
import { memoize, throttle, debounce } from './decorators';
1212
import { toGitUri } from './uri';
@@ -277,14 +277,20 @@ export class Resource implements SourceControlResourceState {
277277

278278
export enum Operation {
279279
Status = 'Status',
280+
Config = 'Config',
280281
Diff = 'Diff',
282+
MergeBase = 'MergeBase',
281283
Add = 'Add',
282284
RevertFiles = 'RevertFiles',
283285
Commit = 'Commit',
284286
Clean = 'Clean',
285287
Branch = 'Branch',
288+
GetBranch = 'GetBranch',
289+
SetBranchUpstream = 'SetBranchUpstream',
290+
HashObject = 'HashObject',
286291
Checkout = 'Checkout',
287292
Reset = 'Reset',
293+
Remote = 'Remote',
288294
Fetch = 'Fetch',
289295
Pull = 'Pull',
290296
Push = 'Push',
@@ -650,13 +656,53 @@ export class Repository implements Disposable {
650656
}
651657
}
652658

659+
getConfigs(): Promise<{ key: string; value: string; }[]> {
660+
return this.run(Operation.Config, () => this.repository.getConfigs('local'));
661+
}
662+
663+
getConfig(key: string): Promise<string> {
664+
return this.run(Operation.Config, () => this.repository.config('local', key));
665+
}
666+
667+
setConfig(key: string, value: string): Promise<string> {
668+
return this.run(Operation.Config, () => this.repository.config('local', key, value));
669+
}
670+
653671
@throttle
654672
async status(): Promise<void> {
655673
await this.run(Operation.Status);
656674
}
657675

658-
diff(path: string, cached = false): Promise<string> {
659-
return this.run(Operation.Diff, () => this.repository.diff(path, cached));
676+
diffWithHEAD(path: string): Promise<string> {
677+
return this.run(Operation.Diff, () => this.repository.diffWithHEAD(path));
678+
}
679+
680+
diffWith(ref: string, path: string): Promise<string> {
681+
return this.run(Operation.Diff, () => this.repository.diffWith(ref, path));
682+
}
683+
684+
diffIndexWithHEAD(path: string): Promise<string> {
685+
return this.run(Operation.Diff, () => this.repository.diffIndexWithHEAD(path));
686+
}
687+
688+
diffIndexWith(ref: string, path: string): Promise<string> {
689+
return this.run(Operation.Diff, () => this.repository.diffIndexWith(ref, path));
690+
}
691+
692+
diffBlobs(object1: string, object2: string): Promise<string> {
693+
return this.run(Operation.Diff, () => this.repository.diffBlobs(object1, object2));
694+
}
695+
696+
diffBetween(ref1: string, ref2: string, path: string): Promise<string> {
697+
return this.run(Operation.Diff, () => this.repository.diffBetween(ref1, ref2, path));
698+
}
699+
700+
getMergeBase(ref1: string, ref2: string): Promise<string> {
701+
return this.run(Operation.MergeBase, () => this.repository.getMergeBase(ref1, ref2));
702+
}
703+
704+
async hashObject(data: string): Promise<string> {
705+
return this.run(Operation.HashObject, () => this.repository.hashObject(data));
660706
}
661707

662708
async add(resources: Uri[]): Promise<void> {
@@ -758,6 +804,14 @@ export class Repository implements Disposable {
758804
await this.run(Operation.RenameBranch, () => this.repository.renameBranch(name));
759805
}
760806

807+
async getBranch(name: string): Promise<Branch> {
808+
return await this.run(Operation.GetBranch, () => this.repository.getBranch(name));
809+
}
810+
811+
async setBranchUpstream(name: string, upstream: string): Promise<void> {
812+
await this.run(Operation.SetBranchUpstream, () => this.repository.setBranchUpstream(name, upstream));
813+
}
814+
761815
async merge(ref: string): Promise<void> {
762816
await this.run(Operation.Merge, () => this.repository.merge(ref));
763817
}
@@ -782,6 +836,14 @@ export class Repository implements Disposable {
782836
await this.run(Operation.DeleteRef, () => this.repository.deleteRef(ref));
783837
}
784838

839+
async addRemote(name: string, url: string): Promise<void> {
840+
await this.run(Operation.Remote, () => this.repository.addRemote(name, url));
841+
}
842+
843+
async removeRemote(name: string): Promise<void> {
844+
await this.run(Operation.Remote, () => this.repository.removeRemote(name));
845+
}
846+
785847
@throttle
786848
async fetchDefault(): Promise<void> {
787849
await this.run(Operation.Fetch, () => this.repository.fetch());
@@ -805,7 +867,7 @@ export class Repository implements Disposable {
805867
}
806868

807869
@throttle
808-
async pull(head: Branch | undefined): Promise<void> {
870+
async pull(head?: Branch): Promise<void> {
809871
let remote: string | undefined;
810872
let branch: string | undefined;
811873

0 commit comments

Comments
 (0)