git: implement API

This commit is contained in:
Joao Moreno
2018-08-24 12:04:29 +02:00
parent 28d2b5d2d6
commit 103715c3cc
5 changed files with 238 additions and 17 deletions

View File

@@ -682,7 +682,7 @@ export class Repository {
return this.git.spawn(args, options);
}
async config(scope: string, key: string, value: any, options: SpawnOptions): Promise<string> {
async config(scope: string, key: string, value: any = null, options: SpawnOptions = {}): Promise<string> {
const args = ['config'];
if (scope) {
@@ -699,6 +699,24 @@ export class Repository {
return result.stdout;
}
async getConfigs(scope: string): Promise<{ key: string; value: string; }[]> {
const args = ['config'];
if (scope) {
args.push('--' + scope);
}
args.push('-l');
const result = await this.run(args);
const lines = result.stdout.trim().split(/\r|\r\n|\n/);
return lines.map(entry => {
const equalsIndex = entry.indexOf('=');
return { key: entry.substr(0, equalsIndex), value: entry.substr(equalsIndex + 1) };
});
}
async bufferString(object: string, encoding: string = 'utf8', autoGuessEncoding = false): Promise<string> {
const stdout = await this.buffer(object);
@@ -837,6 +855,36 @@ export class Repository {
return result.stdout;
}
async diffWithHEAD(path: string): Promise<string> {
const args = ['diff', '--', path];
const result = await this.run(args);
return result.stdout;
}
async diffWith(ref: string, path: string): Promise<string> {
const args = ['diff', ref, '--', path];
const result = await this.run(args);
return result.stdout;
}
async diffIndexWithHEAD(path: string): Promise<string> {
const args = ['diff', '--cached', '--', path];
const result = await this.run(args);
return result.stdout;
}
async diffIndexWith(ref: string, path: string): Promise<string> {
const args = ['diff', '--cached', ref, '--', path];
const result = await this.run(args);
return result.stdout;
}
async diffBlobs(object1: string, object2: string): Promise<string> {
const args = ['diff', object1, object2];
const result = await this.run(args);
return result.stdout;
}
async diffBetween(ref1: string, ref2: string, path: string): Promise<string> {
const args = ['diff', `${ref1}...${ref2}`, '--', path];
const result = await this.run(args);
@@ -851,6 +899,13 @@ export class Repository {
return result.stdout.trim();
}
async hashObject(data: string): Promise<string> {
const args = ['hash-object', '-w', '--stdin'];
const result = await this.run(args, { input: data });
return result.stdout.trim();
}
async add(paths: string[]): Promise<void> {
const args = ['add', '-A', '--'];
@@ -1098,6 +1153,11 @@ export class Repository {
await this.run(args);
}
async removeRemote(name: string): Promise<void> {
const args = ['remote', 'rm', name];
await this.run(args);
}
async fetch(remote?: string, ref?: string): Promise<void> {
const args = ['fetch'];