git lib: getCommit

This commit is contained in:
Joao Moreno 2016-08-12 12:43:03 +02:00
parent 7ddbab5494
commit 1d8ceaa19d
7 changed files with 36 additions and 42 deletions

View file

@ -1125,7 +1125,9 @@ export class UndoLastCommitAction extends GitAction {
}
public run():Promise {
return this.gitService.getLog({ prevCount: 1, format: '%B' }).then(prevCommitMsg => this.storageService.store('prevCommitMsg', prevCommitMsg)).then(_ => this.gitService.reset('HEAD~'));
return this.gitService.getCommit('HEAD')
.then(commit => this.storageService.store('prevCommitMsg', commit.message))
.then(_ => this.gitService.reset('HEAD~'));
}
}

View file

@ -38,7 +38,6 @@ import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage'
import Event from 'vs/base/common/event';
import { domEvent } from 'vs/base/browser/event';
import {IEditorGroupService} from 'vs/workbench/services/group/common/groupService';
import { ILogOptions } from 'vs/workbench/parts/git/node/git.lib';
function toReadablePath(path: string): string {
if (!platform.isWindows) {
@ -696,8 +695,8 @@ export class GitService extends ee.EventEmitter
return this.raw.getCommitTemplate();
}
public getLog(options?: ILogOptions): winjs.Promise {
return this.raw.getLog(options);
public getCommit(ref: string): winjs.TPromise<git.ICommit> {
return this.raw.getCommit(ref);
}
public detectMimetypes(path: string, treeish: string = '~'): winjs.Promise {

View file

@ -10,7 +10,6 @@ import { IEventEmitter } from 'vs/base/common/eventEmitter';
import { IDisposable } from 'vs/base/common/lifecycle';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import Event from 'vs/base/common/event';
import { ILogOptions } from 'vs/workbench/parts/git/node/git.lib';
// Model raw interfaces
@ -55,6 +54,11 @@ export interface IRawStatus {
remotes: IRemote[];
}
export interface ICommit {
hash: string;
message: string;
}
// Model enums
export enum StatusType {
@ -288,7 +292,7 @@ export interface IRawGitService {
detectMimetypes(path: string, treeish?: string): TPromise<string[]>;
show(path: string, treeish?: string): TPromise<string>;
getCommitTemplate(): TPromise<string>;
getLog(options?: ILogOptions): TPromise<string>;
getCommit(ref: string): TPromise<ICommit>;
}
export const GIT_SERVICE_ID = 'gitService';
@ -326,7 +330,7 @@ export interface IGitService extends IEventEmitter {
getRunningOperations(): IGitOperation[];
getAutoFetcher(): IAutoFetcher;
getCommitTemplate(): TPromise<string>;
getLog(options?: ILogOptions): TPromise<string>;
getCommit(ref: string): TPromise<ICommit>;
}
export interface IAskpassService {

View file

@ -9,8 +9,7 @@ import { TPromise } from 'vs/base/common/winjs.base';
import { IChannel, eventToCall, eventFromCall } from 'vs/base/parts/ipc/common/ipc';
import Event from 'vs/base/common/event';
import { IRawGitService, RawServiceState, IRawStatus, IPushOptions, IAskpassService, ICredentials,
ServiceState, IRawFileStatus, IBranch, RefType, IRef, IRemote } from './git';
import { ILogOptions } from 'vs/workbench/parts/git/node/git.lib';
ServiceState, IRawFileStatus, IBranch, RefType, IRef, IRemote, ICommit } from './git';
type ISerializer<A,B> = { to(a: A): B; from(b: B): A; };
@ -89,7 +88,7 @@ export interface IGitChannel extends IChannel {
call(command: 'show', args: [string, string]): TPromise<string>;
call(command: 'onOutput'): TPromise<void>;
call(command: 'getCommitTemplate'): TPromise<string>;
call(command: 'getLog', options?: ILogOptions): TPromise<string>;
call(command: 'getCommit', ref: string): TPromise<ICommit>;
call(command: string, args: any): TPromise<any>;
}
@ -121,7 +120,7 @@ export class GitChannel implements IGitChannel {
case 'show': return this.service.then(s => s.show(args[0], args[1]));
case 'onOutput': return this.service.then(s => eventToCall(s.onOutput));
case 'getCommitTemplate': return this.service.then(s => s.getCommitTemplate());
case 'getLog': return this.service.then(s => s.getLog(args));
case 'getCommit': return this.service.then(s => s.getCommit(args));
}
}
}
@ -227,8 +226,8 @@ export class GitChannelClient implements IRawGitService {
return this.channel.call('getCommitTemplate');
}
getLog(options?: ILogOptions): TPromise<string> {
return this.channel.call('getLog', options);
getCommit(ref: string): TPromise<ICommit> {
return this.channel.call('getCommit', ref);
}
}

View file

@ -4,10 +4,9 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import { IRawGitService, IRawStatus, ServiceState, RawServiceState } from 'vs/workbench/parts/git/common/git';
import { IRawGitService, IRawStatus, ServiceState, RawServiceState, ICommit } from 'vs/workbench/parts/git/common/git';
import { TPromise } from 'vs/base/common/winjs.base';
import Event, { Emitter } from 'vs/base/common/event';
import { ILogOptions } from 'vs/workbench/parts/git/node/git.lib';
export class NoOpGitService implements IRawGitService {
@ -107,7 +106,7 @@ export class NoOpGitService implements IRawGitService {
return TPromise.as(null);
}
getLog(options?: ILogOptions): TPromise<string> {
getCommit(ref: string): TPromise<ICommit> {
return TPromise.as(null);
}
}

View file

@ -119,18 +119,6 @@ export class GitError {
}
}
export interface ILogOptions {
/**
* @example `git log -1 --format=%B` to get the last commit log, message only
*/
prevCount?: number;
/**
* @example format: "%B" translates to `git log --format=%B` to extract only the message
*/
format?: string;
}
export interface IGitOptions {
gitPath:string;
version: string;
@ -273,6 +261,11 @@ export class Git {
}
}
export interface ICommit {
hash: string;
message: string;
}
export class Repository {
private git: Git;
@ -735,17 +728,15 @@ export class Repository {
}, () => '');
}
/** Implemented for use case `git log` and `git log -N`. */
getLog(options?: ILogOptions): TPromise<string> {
const args = ['log'];
getCommit(ref: string): TPromise<ICommit> {
return this.run(['show', '-s', '--format=%H\n%B', ref]).then(result => {
const match = /^([0-9a-f]{40})\n([^]*)$/m.exec(result.stdout.trim());
if (options) {
if (options.prevCount) { args.push(`-${options.prevCount}`); }
if (options.format) { args.push(`--format=${options.format}`); }
}
if (!match) {
return TPromise.wrapError('bad commit format');
}
return this.run(args, { log: false }).then(result => {
return result.stdout.trim();
return { hash: match[1], message: match[2] };
});
}

View file

@ -8,8 +8,8 @@ import { join } from 'path';
import { TPromise, Promise } from 'vs/base/common/winjs.base';
import { detectMimesFromFile, detectMimesFromStream } from 'vs/base/node/mime';
import { realpath, exists} from 'vs/base/node/pfs';
import { Repository, GitError, ILogOptions } from 'vs/workbench/parts/git/node/git.lib';
import { IRawGitService, RawServiceState, IRawStatus, IRef, GitErrorCodes, IPushOptions } from 'vs/workbench/parts/git/common/git';
import { Repository, GitError } from 'vs/workbench/parts/git/node/git.lib';
import { IRawGitService, RawServiceState, IRawStatus, IRef, GitErrorCodes, IPushOptions, ICommit } from 'vs/workbench/parts/git/common/git';
import Event, { Emitter, fromPromise } from 'vs/base/common/event';
export class RawGitService implements IRawGitService {
@ -200,8 +200,8 @@ export class RawGitService implements IRawGitService {
return this.repo.getCommitTemplate();
}
getLog(options?: ILogOptions): TPromise<string> {
return this.repo.getLog(options);
getCommit(ref: string): TPromise<ICommit> {
return this.repo.getCommit(ref);
}
}
@ -229,5 +229,5 @@ export class DelayedRawGitService implements IRawGitService {
detectMimetypes(path: string, treeish?: string): TPromise<string[]> { return this.raw.then(r => r.detectMimetypes(path, treeish)); }
show(path: string, treeish?: string): TPromise<string> { return this.raw.then(r => r.show(path, treeish)); }
getCommitTemplate(): TPromise<string> { return this.raw.then(r => r.getCommitTemplate()); }
getLog(options?: ILogOptions): TPromise<string> { return this.raw.then(r => r.getLog(options)); }
getCommit(ref: string): TPromise<ICommit> { return this.raw.then(r => r.getCommit(ref)); }
}