Skip to content

Commit 28d2b5d

Browse files
committed
wip: git api
1 parent 811a6be commit 28d2b5d

6 files changed

Lines changed: 102 additions & 16 deletions

File tree

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,68 @@ export interface Remote {
5656
readonly isReadOnly: boolean;
5757
}
5858

59+
export interface Change {
60+
// TODO
61+
}
62+
5963
export interface RepositoryState {
6064
readonly HEAD: Branch | undefined;
6165
readonly refs: Ref[];
6266
readonly remotes: Remote[];
6367
readonly submodules: Submodule[];
6468
readonly rebaseCommit: Commit | undefined;
69+
70+
readonly mergeChanges: Change[];
71+
readonly indexChanges: Change[];
72+
readonly workingTreeChanges: Change[];
73+
6574
readonly onDidChange: Event<void>;
6675
}
6776

77+
export const enum ConfigScope {
78+
System,
79+
Global,
80+
Local
81+
}
82+
6883
export interface Repository {
84+
6985
readonly rootUri: Uri;
7086
readonly inputBox: InputBox;
7187
readonly state: RepositoryState;
7288

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>;
92+
93+
show(ref: string, path: string): Promise<string>;
94+
getCommit(ref: string): Promise<Commit>;
95+
getObjectDetails(treeish: string, path: string): Promise<{ mode: string, object: string, size: number }>;
96+
97+
diffWithHEAD(path: string): Promise<string>;
98+
diffWith(ref: string, path: string): Promise<string>;
99+
diffIndexWithHEAD(path: string): Promise<string>;
100+
diffIndexWith(ref: string, path: string): Promise<string>;
101+
diffBlobs(object1: string, object2: string): Promise<string>;
102+
diffBetween(ref1: string, ref2: string, path: string): Promise<string>;
103+
104+
hashObject(data: string): Promise<string>;
105+
106+
createBranch(name: string, checkout: boolean, ref?: string): Promise<void>;
107+
deleteBranch(name: string): Promise<void>;
108+
getBranch(name: string): Promise<Branch>;
109+
setBranchUpstream(name: string, upstream: string): Promise<void>;
110+
111+
getMergeBase(ref1: string, ref2: string): Promise<string>;
112+
73113
status(): Promise<void>;
114+
checkout(treeish: string): Promise<void>;
115+
116+
addRemote(name: string, url: string): Promise<void>;
117+
removeRemote(name: string): Promise<void>;
118+
119+
fetch(remote?: string, ref?: string): Promise<void>;
120+
pull(): Promise<void>;
74121
}
75122

76123
export interface API {

extensions/git/src/autofetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class AutoFetcher {
102102
}
103103

104104
try {
105-
await this.repository.fetch();
105+
await this.repository.fetchDefault();
106106
} catch (err) {
107107
if (err.gitErrorCode === GitErrorCodes.AuthenticationFailed) {
108108
this.disable();

extensions/git/src/commands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ export class CommandCenter {
12351235
}
12361236

12371237
const name = result.replace(/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$|\.$/g, '-');
1238-
await repository.branch(name);
1238+
await repository.branch(name, true);
12391239
}
12401240

12411241
@command('git.deleteBranch', { repository: true })
@@ -1355,7 +1355,7 @@ export class CommandCenter {
13551355
return;
13561356
}
13571357

1358-
await repository.fetch();
1358+
await repository.fetchDefault();
13591359
}
13601360

13611361
@command('git.pullFrom', { repository: true })

extensions/git/src/contentProvider.ts

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

95-
return await repository.diff(path, { cached: ref === 'index' });
95+
return await repository.diff(path, ref === 'index');
9696
}
9797

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

extensions/git/src/git.ts

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -654,10 +654,6 @@ export function parseLsFiles(raw: string): LsFilesElement[] {
654654
.map(([, mode, object, stage, file]) => ({ mode, object, stage, file }));
655655
}
656656

657-
export interface DiffOptions {
658-
cached?: boolean;
659-
}
660-
661657
export class Repository {
662658

663659
constructor(
@@ -828,10 +824,10 @@ export class Repository {
828824
}
829825
}
830826

831-
async diff(path: string, options: DiffOptions = {}): Promise<string> {
827+
async diff(path: string, cached = false): Promise<string> {
832828
const args = ['diff'];
833829

834-
if (options.cached) {
830+
if (cached) {
835831
args.push('--cached');
836832
}
837833

@@ -841,6 +837,20 @@ export class Repository {
841837
return result.stdout;
842838
}
843839

840+
async diffBetween(ref1: string, ref2: string, path: string): Promise<string> {
841+
const args = ['diff', `${ref1}...${ref2}`, '--', path];
842+
const result = await this.run(args);
843+
844+
return result.stdout.trim();
845+
}
846+
847+
async getMergeBase(ref1: string, ref2: string): Promise<string> {
848+
const args = ['merge-base', ref1, ref2];
849+
const result = await this.run(args);
850+
851+
return result.stdout.trim();
852+
}
853+
844854
async add(paths: string[]): Promise<void> {
845855
const args = ['add', '-A', '--'];
846856

@@ -961,8 +971,13 @@ export class Repository {
961971
throw commitErr;
962972
}
963973

964-
async branch(name: string, checkout: boolean): Promise<void> {
974+
async branch(name: string, checkout: boolean, ref?: string): Promise<void> {
965975
const args = checkout ? ['checkout', '-q', '-b', name] : ['branch', '-q', name];
976+
977+
if (ref) {
978+
args.push(ref);
979+
}
980+
966981
await this.run(args);
967982
}
968983

@@ -976,6 +991,11 @@ export class Repository {
976991
await this.run(args);
977992
}
978993

994+
async setBranchUpstream(name: string, upstream: string): Promise<void> {
995+
const args = ['branch', '--set-upstream-to', upstream, name];
996+
await this.run(args);
997+
}
998+
979999
async deleteRef(ref: string): Promise<void> {
9801000
const args = ['update-ref', '-d', ref];
9811001
await this.run(args);
@@ -1073,7 +1093,22 @@ export class Repository {
10731093
}
10741094
}
10751095

1076-
async fetch(): Promise<void> {
1096+
async addRemote(name: string, url: string): Promise<void> {
1097+
const args = ['remote', 'add', name, url];
1098+
await this.run(args);
1099+
}
1100+
1101+
async fetch(remote?: string, ref?: string): Promise<void> {
1102+
const args = ['fetch'];
1103+
1104+
if (remote) {
1105+
args.push(remote);
1106+
1107+
if (ref) {
1108+
args.push(ref);
1109+
}
1110+
}
1111+
10771112
try {
10781113
await this.run(['fetch']);
10791114
} catch (err) {

extensions/git/src/repository.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,8 @@ export class Repository implements Disposable {
655655
await this.run(Operation.Status);
656656
}
657657

658-
diff(path: string, options: DiffOptions = {}): Promise<string> {
659-
return this.run(Operation.Diff, () => this.repository.diff(path, options));
658+
diff(path: string, cached = false): Promise<string> {
659+
return this.run(Operation.Diff, () => this.repository.diff(path, cached));
660660
}
661661

662662
async add(resources: Uri[]): Promise<void> {
@@ -746,7 +746,7 @@ export class Repository implements Disposable {
746746
});
747747
}
748748

749-
async branch(name: string): Promise<void> {
749+
async branch(name: string, checkout: boolean, ref?: string): Promise<void> {
750750
await this.run(Operation.Branch, () => this.repository.branch(name, true));
751751
}
752752

@@ -783,7 +783,11 @@ export class Repository implements Disposable {
783783
}
784784

785785
@throttle
786-
async fetch(): Promise<void> {
786+
async fetchDefault(): Promise<void> {
787+
await this.run(Operation.Fetch, () => this.repository.fetch());
788+
}
789+
790+
async fetch(remote?: string, ref?: string): Promise<void> {
787791
await this.run(Operation.Fetch, () => this.repository.fetch());
788792
}
789793

0 commit comments

Comments
 (0)