Closes #110881 - adds possibly rebased warning

This commit is contained in:
Eric Amodio 2021-01-12 12:15:15 -05:00
parent eba4da2727
commit 8832366467
3 changed files with 63 additions and 10 deletions

View file

@ -1742,6 +1742,11 @@
"description": "%config.ignoreLimitWarning%",
"default": false
},
"git.ignoreRebaseWarning": {
"type": "boolean",
"description": "%config.ignoreRebaseWarning%",
"default": false
},
"git.defaultCloneDirectory": {
"type": [
"string",

View file

@ -114,6 +114,7 @@
"config.ignoreMissingGitWarning": "Ignores the warning when Git is missing.",
"config.ignoreWindowsGit27Warning": "Ignores the warning when Git 2.25 - 2.26 is installed on Windows.",
"config.ignoreLimitWarning": "Ignores the warning when there are too many changes in a repository.",
"config.ignoreRebaseWarning": "Ignores the warning when it looks like the branch might have been rebased when pulling.",
"config.defaultCloneDirectory": "The default location to clone a git repository.",
"config.enableSmartCommit": "Commit all changes when there are no staged changes.",
"config.smartCommitChanges": "Control which changes are automatically staged by Smart Commit.",

View file

@ -1129,7 +1129,7 @@ export class Repository implements Disposable {
return this.run(Operation.HashObject, () => this.repository.hashObject(data));
}
async add(resources: Uri[], opts?: { update?: boolean }): Promise<void> {
async add(resources: Uri[], opts?: { update?: boolean; }): Promise<void> {
await this.run(Operation.Add, () => this.repository.add(resources.map(r => r.fsPath), opts));
}
@ -1260,11 +1260,11 @@ export class Repository implements Disposable {
await this.run(Operation.DeleteTag, () => this.repository.deleteTag(name));
}
async checkout(treeish: string, opts?: { detached?: boolean }): Promise<void> {
async checkout(treeish: string, opts?: { detached?: boolean; }): Promise<void> {
await this.run(Operation.Checkout, () => this.repository.checkout(treeish, [], opts));
}
async checkoutTracking(treeish: string, opts: { detached?: boolean } = {}): Promise<void> {
async checkoutTracking(treeish: string, opts: { detached?: boolean; } = {}): Promise<void> {
await this.run(Operation.CheckoutTracking, () => this.repository.checkout(treeish, [], { ...opts, track: true }));
}
@ -1297,7 +1297,7 @@ export class Repository implements Disposable {
}
@throttle
async fetchDefault(options: { silent?: boolean } = {}): Promise<void> {
async fetchDefault(options: { silent?: boolean; } = {}): Promise<void> {
await this._fetch({ silent: options.silent });
}
@ -1315,7 +1315,7 @@ export class Repository implements Disposable {
await this._fetch({ remote, ref, depth });
}
private async _fetch(options: { remote?: string, ref?: string, all?: boolean, prune?: boolean, depth?: number, silent?: boolean } = {}): Promise<void> {
private async _fetch(options: { remote?: string, ref?: string, all?: boolean, prune?: boolean, depth?: number, silent?: boolean; } = {}): Promise<void> {
if (!options.prune) {
const config = workspace.getConfiguration('git', Uri.file(this.root));
const prune = config.get<boolean>('pruneOnFetch');
@ -1363,7 +1363,9 @@ export class Repository implements Disposable {
await this.repository.fetch({ all: true });
}
await this.repository.pull(rebase, remote, branch, { unshallow, tags });
if (await this.checkIfMaybeRebased(branch)) {
await this.repository.pull(rebase, remote, branch, { unshallow, tags });
}
});
});
}
@ -1432,10 +1434,11 @@ export class Repository implements Disposable {
await this.repository.fetch({ all: true, cancellationToken });
}
await this.repository.pull(rebase, remoteName, pullBranch, { tags, cancellationToken });
if (await this.checkIfMaybeRebased(pullBranch)) {
await this.repository.pull(rebase, remoteName, pullBranch, { tags, cancellationToken });
}
};
if (supportCancellation) {
const opts: ProgressOptions = {
location: ProgressLocation.Notification,
@ -1463,6 +1466,50 @@ export class Repository implements Disposable {
});
}
private async checkIfMaybeRebased(branch?: string) {
const config = workspace.getConfiguration('git');
const shouldIgnore = config.get<boolean>('ignoreRebaseWarning') === true;
if (shouldIgnore) {
return true;
}
const maybeRebased = await this.run(Operation.Log, async () => {
const result = await this.repository.run(['log', '--oneline', '--cherry', `${branch ?? ''}...${branch ?? ''}@{upstream}`, '--']);
if (result.exitCode) {
return false;
}
return /^=/.test(result.stdout);
});
if (!maybeRebased) {
return true;
}
const always = { title: localize('always pull', "Always Pull") };
const pull = { title: localize('pull', "Pull") };
const cancel = { title: localize('dont pull', "Don't Pull") };
const result = await window.showWarningMessage(
branch
? localize('pull branch maybe rebased', "It looks like branch \'{0}\' might have been rebased. Are you sure you still want to pull?", branch)
: localize('pull maybe rebased', "It looks like the current branch might have been rebased. Are you sure you still want to pull?"),
always, pull, cancel
);
if (result === pull) {
return true;
}
if (result === always) {
await config.update('ignoreRebaseWarning', true, true);
return true;
}
return false;
}
async show(ref: string, filePath: string): Promise<string> {
return await this.run(Operation.Show, async () => {
const relativePath = path.relative(this.repository.root, filePath).replace(/\\/g, '/');
@ -1490,11 +1537,11 @@ export class Repository implements Disposable {
});
}
getObjectDetails(ref: string, filePath: string): Promise<{ mode: string, object: string, size: number }> {
getObjectDetails(ref: string, filePath: string): Promise<{ mode: string, object: string, size: number; }> {
return this.run(Operation.GetObjectDetails, () => this.repository.getObjectDetails(ref, filePath));
}
detectObjectType(object: string): Promise<{ mimetype: string, encoding?: string }> {
detectObjectType(object: string): Promise<{ mimetype: string, encoding?: string; }> {
return this.run(Operation.Show, () => this.repository.detectObjectType(object));
}