rawGitService.ts 7.6 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import path = require('path');
J
Joao Moreno 已提交
8
import { TPromise, Promise } from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
9 10 11
import mime = require('vs/base/node/mime');
import pfs = require('vs/base/node/pfs');
import { Repository, GitError } from 'vs/workbench/parts/git/node/git.lib';
J
Joao Moreno 已提交
12
import { IRawGitService, RawServiceState, IRawStatus, IHead, GitErrorCodes, IPushOptions } from 'vs/workbench/parts/git/common/git';
E
Erich Gamma 已提交
13 14 15 16

export class RawGitService implements IRawGitService {

	private repo: Repository;
J
Joao Moreno 已提交
17
	private _repositoryRoot: TPromise<string>;
E
Erich Gamma 已提交
18 19 20

	constructor(repo: Repository) {
		this.repo = repo;
J
Joao Moreno 已提交
21 22 23 24
	}

	private getRepositoryRoot(): TPromise<string> {
		return this._repositoryRoot || (this._repositoryRoot = pfs.realpath(this.repo.path));
E
Erich Gamma 已提交
25 26
	}

J
Joao Moreno 已提交
27 28
	public serviceState(): TPromise<RawServiceState> {
		return TPromise.as<RawServiceState>(this.repo
E
Erich Gamma 已提交
29 30 31 32 33
			? RawServiceState.OK
			: RawServiceState.GitNotFound
		);
	}

J
Joao Moreno 已提交
34
	public status(): TPromise<IRawStatus> {
J
Joao Moreno 已提交
35
		return this.repo.getStatus()
E
Erich Gamma 已提交
36 37 38 39 40 41 42 43
			.then(status => this.repo.getHEAD()
				.then(HEAD => {
					if (HEAD.name) {
						return this.repo.getBranch(HEAD.name).then(null, () => HEAD);
					} else {
						return HEAD;
					}
				}, (): IHead => null)
J
Joao Moreno 已提交
44
				.then(HEAD => Promise.join([this.getRepositoryRoot(), this.repo.getHeads(), this.repo.getTags(), this.repo.getRemotes()]).then(r => {
E
Erich Gamma 已提交
45
					return {
J
Joao Moreno 已提交
46
						repositoryRoot: r[0],
E
Erich Gamma 已提交
47 48
						status: status,
						HEAD: HEAD,
J
Joao Moreno 已提交
49
						heads: r[1],
J
Joao Moreno 已提交
50 51
						tags: r[2],
						remotes: r[3]
E
Erich Gamma 已提交
52 53 54 55
					};
				})))
			.then(null, (err) => {
				if (err.gitErrorCode === GitErrorCodes.BadConfigFile) {
J
Joao Moreno 已提交
56
					return Promise.wrapError(err);
E
Erich Gamma 已提交
57
				} else if (err.gitErrorCode === GitErrorCodes.NotAtRepositoryRoot) {
J
Joao Moreno 已提交
58
					return Promise.wrapError(err);
E
Erich Gamma 已提交
59 60 61 62 63 64
				}

				return null;
			});
	}

J
Joao Moreno 已提交
65
	public init(): TPromise<IRawStatus> {
E
Erich Gamma 已提交
66 67 68
		return this.repo.init().then(() => this.status());
	}

J
Joao Moreno 已提交
69
	public add(filePaths?: string[]): TPromise<IRawStatus> {
E
Erich Gamma 已提交
70 71 72
		return this.repo.add(filePaths).then(() => this.status());
	}

J
Joao Moreno 已提交
73
	public stage(filePath: string, content: string): TPromise<IRawStatus> {
E
Erich Gamma 已提交
74 75 76
		return this.repo.stage(filePath, content).then(() => this.status());
	}

J
Joao Moreno 已提交
77
	public branch(name: string, checkout?: boolean): TPromise<IRawStatus> {
E
Erich Gamma 已提交
78 79 80
		return this.repo.branch(name, checkout).then(() => this.status());
	}

J
Joao Moreno 已提交
81
	public checkout(treeish?: string, filePaths?: string[]): TPromise<IRawStatus> {
E
Erich Gamma 已提交
82 83 84
		return this.repo.checkout(treeish, filePaths).then(() => this.status());
	}

J
Joao Moreno 已提交
85
	public clean(filePaths: string[]): TPromise<IRawStatus> {
E
Erich Gamma 已提交
86 87 88
		return this.repo.clean(filePaths).then(() => this.status());
	}

J
Joao Moreno 已提交
89
	public undo(): TPromise<IRawStatus> {
E
Erich Gamma 已提交
90 91 92
		return this.repo.undo().then(() => this.status());
	}

J
Joao Moreno 已提交
93
	public reset(treeish: string, hard?: boolean): TPromise<IRawStatus> {
E
Erich Gamma 已提交
94 95 96
		return this.repo.reset(treeish, hard).then(() => this.status());
	}

J
Joao Moreno 已提交
97
	public revertFiles(treeish: string, filePaths?: string[]): TPromise<IRawStatus> {
E
Erich Gamma 已提交
98 99 100
		return this.repo.revertFiles(treeish, filePaths).then(() => this.status());
	}

J
Joao Moreno 已提交
101
	public fetch(): TPromise<IRawStatus> {
E
Erich Gamma 已提交
102 103
		return this.repo.fetch().then(null, (err) => {
			if (err.gitErrorCode === GitErrorCodes.NoRemoteRepositorySpecified) {
A
Alex Dima 已提交
104
				return TPromise.as(null);
E
Erich Gamma 已提交
105 106
			}

J
Joao Moreno 已提交
107
			return Promise.wrapError(err);
E
Erich Gamma 已提交
108 109 110
		}).then(() => this.status());
	}

J
Joao Moreno 已提交
111 112
	public pull(rebase?: boolean): TPromise<IRawStatus> {
		return this.repo.pull(rebase).then(() => this.status());
E
Erich Gamma 已提交
113 114
	}

J
Joao Moreno 已提交
115 116
	public push(remote?: string, name?: string, options?:IPushOptions): TPromise<IRawStatus> {
		return this.repo.push(remote, name, options).then(() => this.status());
E
Erich Gamma 已提交
117 118
	}

J
Joao Moreno 已提交
119
	public sync(): TPromise<IRawStatus> {
E
Erich Gamma 已提交
120 121 122
		return this.repo.sync().then(() => this.status());
	}

J
Joao Moreno 已提交
123
	public commit(message:string, amend?: boolean, stage?: boolean): TPromise<IRawStatus> {
A
Alex Dima 已提交
124
		var promise: Promise = TPromise.as(null);
E
Erich Gamma 已提交
125 126 127 128 129 130 131 132 133 134

		if (stage) {
			promise = this.repo.add(null);
		}

		return promise
			.then(() => this.repo.commit(message, stage, amend))
			.then(() => this.status());
	}

J
Joao Moreno 已提交
135
	public detectMimetypes(filePath: string, treeish?: string): TPromise<string[]> {
E
Erich Gamma 已提交
136 137
		return pfs.exists(path.join(this.repo.path, filePath)).then((exists) => {
			if (exists) {
J
Joao Moreno 已提交
138
				return new TPromise<string[]>((c, e) => {
E
Erich Gamma 已提交
139 140 141 142 143 144 145 146 147
					mime.detectMimesFromFile(path.join(this.repo.path, filePath), (err, result) => {
						if (err) { e(err); }
						else { c(result.mimes); }
					});
				});
			}

			var child = this.repo.show(treeish + ':' + filePath);

J
Joao Moreno 已提交
148
			return new TPromise<string[]>((c, e) => {
E
Erich Gamma 已提交
149 150 151 152 153 154 155 156 157
				mime.detectMimesFromStream(child.stdout, filePath, (err, result) => {
					if (err) { e(err); }
					else { c(result.mimes); }
				});
			});
		});
	}

	// careful, this buffers the whole object into memory
J
Joao Moreno 已提交
158
	public show(filePath: string, treeish?: string): TPromise<string> {
E
Erich Gamma 已提交
159 160 161 162 163 164
		treeish = treeish === '~' ? '' : treeish;
		return this.repo.buffer(treeish + ':' + filePath).then(null, e => {
			if (e instanceof GitError) {
				return ''; // mostly untracked files end up in a git error
			}

J
Joao Moreno 已提交
165
			return TPromise.wrapError<string>(e);
E
Erich Gamma 已提交
166 167 168
		});
	}

J
Joao Moreno 已提交
169
	public onOutput(): Promise {
E
Erich Gamma 已提交
170 171
		var cancel: () => void;

J
Joao Moreno 已提交
172
		return new Promise((c, e, p) => {
E
Erich Gamma 已提交
173 174 175 176
			cancel = this.repo.onOutput(p);
		}, () => cancel());
	}
}
J
Joao Moreno 已提交
177

J
Joao Moreno 已提交
178
export class DelayedRawGitService implements IRawGitService {
J
Joao Moreno 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

	constructor(private raw: TPromise<IRawGitService>) { }

	public serviceState(): TPromise<RawServiceState> {
		return this.raw.then(raw => raw.serviceState());
	}

	public status(): TPromise<IRawStatus> {
		return this.raw.then(raw => raw.status());
	}

	public init(): TPromise<IRawStatus> {
		return this.raw.then(raw => raw.init());
	}

	public add(filesPaths?: string[]): TPromise<IRawStatus> {
		return this.raw.then(raw => raw.add(filesPaths));
	}

	public stage(filePath: string, content: string): TPromise<IRawStatus> {
		return this.raw.then(raw => raw.stage(filePath, content));
	}

	public branch(name: string, checkout?: boolean): TPromise<IRawStatus> {
		return this.raw.then(raw => raw.branch(name, checkout));
	}

	public checkout(treeish?: string, filePaths?: string[]): TPromise<IRawStatus> {
		return this.raw.then(raw => raw.checkout(treeish, filePaths));
	}

	public clean(filePaths: string[]): TPromise<IRawStatus> {
		return this.raw.then(raw => raw.clean(filePaths));
	}

	public undo(): TPromise<IRawStatus> {
		return this.raw.then(raw => raw.undo());
	}

	public reset(treeish: string, hard?: boolean): TPromise<IRawStatus> {
		return this.raw.then(raw => raw.reset(treeish, hard));
	}

	public revertFiles(treeish: string, filePaths?: string[]): TPromise<IRawStatus> {
		return this.raw.then(raw => raw.revertFiles(treeish, filePaths));
	}

	public fetch(): TPromise<IRawStatus> {
		return this.raw.then(raw => raw.fetch());
	}

J
Joao Moreno 已提交
230 231
	public pull(rebase?: boolean): TPromise<IRawStatus> {
		return this.raw.then(raw => raw.pull(rebase));
J
Joao Moreno 已提交
232 233
	}

J
Joao Moreno 已提交
234 235
	public push(origin?: string, name?: string, options?:IPushOptions): TPromise<IRawStatus> {
		return this.raw.then(raw => raw.push(origin, name, options));
J
Joao Moreno 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
	}

	public sync(): TPromise<IRawStatus> {
		return this.raw.then(raw => raw.sync());
	}

	public commit(message: string, amend?: boolean, stage?: boolean): TPromise<IRawStatus> {
		return this.raw.then(raw => raw.commit(message, amend, stage));
	}

	public detectMimetypes(path: string, treeish?: string): TPromise<string[]> {
		return this.raw.then(raw => raw.detectMimetypes(path, treeish));
	}

	public show(path: string, treeish?: string): TPromise<string> {
		return this.raw.then(raw => raw.show(path, treeish));
	}

	public onOutput(): Promise {
		return this.raw.then(raw => raw.onOutput());
	}
}