提交 75d4fcbe 编写于 作者: J Joao Moreno

Merge branch '23797-git-manage-ignore-files' of...

Merge branch '23797-git-manage-ignore-files' of https://github.com/BugraC/vscode into BugraC-23797-git-manage-ignore-files
......@@ -211,6 +211,11 @@
"command": "git.showOutput",
"title": "%command.showOutput%",
"category": "Git"
},
{
"command": "git.ignore",
"title": "%command.ignore%",
"category": "Git"
}
],
"menus": {
......@@ -525,6 +530,11 @@
"command": "git.stage",
"when": "config.git.enabled && scmProvider == git && gitState == idle && scmResourceGroup == workingTree",
"group": "inline"
},
{
"command": "git.ignore",
"when": "config.git.enabled && scmProvider == git && gitState == idle && scmResourceGroup == workingTree",
"group": "1_modification@3"
}
],
"editor/title": [
......
......@@ -29,6 +29,7 @@
"command.sync": "Sync",
"command.publish": "Publish Branch",
"command.showOutput": "Show Git Output",
"command.ignore": "Ignore",
"config.enabled": "Whether git is enabled",
"config.path": "Path to the git executable",
"config.autorefresh": "Whether auto refreshing is enabled",
......
......@@ -879,6 +879,18 @@ export class CommandCenter {
this.outputChannel.show();
}
@command('git.ignore')
async ignore(...resourceStates: SourceControlResourceState[]): Promise<void> {
const resources = resourceStates
.filter(s => s instanceof Resource) as Resource[];
if (!resources.length) {
return;
}
await this.model.ignore(resources);
}
private createCommand(id: string, key: string, method: Function, skipModelCheck: boolean): (...args: any[]) => any {
const result = (...args) => {
if (!skipModelCheck && !this.model) {
......
......@@ -5,18 +5,21 @@
'use strict';
import { Uri, Command, EventEmitter, Event, SourceControlResourceState, SourceControlResourceDecorations, Disposable, ProgressLocation, window, workspace } from 'vscode';
import { Uri, Command, EventEmitter, Event, SourceControlResourceState, SourceControlResourceDecorations, Disposable, ProgressLocation, window, workspace, WorkspaceEdit, Position } from 'vscode';
import { Git, Repository, Ref, Branch, Remote, Commit, GitErrorCodes } from './git';
import { anyEvent, eventToPromise, filterEvent, EmptyDisposable, combinedDisposable, dispose } from './util';
import { memoize, throttle, debounce } from './decorators';
import * as path from 'path';
import * as nls from 'vscode-nls';
import * as fs from 'fs';
const timeout = (millis: number) => new Promise(c => setTimeout(c, millis));
const localize = nls.loadMessageBundle();
const iconsRootPath = path.join(path.dirname(__dirname), 'resources', 'icons');
const ignoreFileName = '.gitignore';
function getIconUri(iconName: string, theme: string): Uri {
return Uri.file(path.join(iconsRootPath, theme, `${iconName}.svg`));
}
......@@ -211,7 +214,8 @@ export enum Operation {
Show = 1 << 13,
Stage = 1 << 14,
GetCommitTemplate = 1 << 15,
DeleteBranch = 1 << 16
DeleteBranch = 1 << 16,
Ignore = 1 << 17
}
// function getOperationName(operation: Operation): string {
......@@ -525,6 +529,42 @@ export class Model implements Disposable {
return await this.run(Operation.GetCommitTemplate, async () => this.repository.getCommitTemplate());
}
async ignore(files: Resource[]): Promise<void> {
const newLineChar = '\n';
return await this.run(Operation.Ignore, async () => {
const ignoreFile = `${this.repository.root}${path.sep}${ignoreFileName}`;
let textToAppend = files
.map(file => path.relative(this.repository.root, file.resourceUri.fsPath).replace(/\\/g, '/'))
.join(newLineChar);
if (fs.existsSync(ignoreFile)) {
const edit = new WorkspaceEdit();
const ignoreFileToWrite = await workspace.openTextDocument(ignoreFile);
//Let's check if that entry exists:
const gitIgnoreEntries = ignoreFileToWrite.getText();
if (gitIgnoreEntries.indexOf(textToAppend) === -1) { //Entry doesn't exist let's insert this entry:
// Append in new line if there is entries in the file:
textToAppend = (gitIgnoreEntries.length > 0 ? newLineChar : '') + textToAppend;
edit.insert(ignoreFileToWrite.uri, ignoreFileToWrite.lineAt(ignoreFileToWrite.lineCount - 1).range.end, textToAppend);
workspace.applyEdit(edit);
}
}
else {
await workspace.openTextDocument(Uri.file(ignoreFile).with({ scheme: 'untitled' })).then((doc) => {
return window.showTextDocument(doc);
}).then((editor) => {
const edit = new WorkspaceEdit();
edit.insert(editor.document.uri, new Position(0, 0), textToAppend);
workspace.applyEdit(edit);
});
}
});
}
private async run<T>(operation: Operation, runOperation: () => Promise<T> = () => Promise.resolve<any>(null)): Promise<T> {
const run = async () => {
this._operations = this._operations.start(operation);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册