Skip to content

Commit

Permalink
feat: Added Merge command (#1540)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrLalelu committed Nov 8, 2021
1 parent 00f273f commit 87060b3
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 1 deletion.
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,11 @@
"title": "Switch Branch",
"category": "SVN"
},
{
"command": "svn.merge",
"title": "Merge",
"category": "SVN"
},
{
"command": "svn.treeview.pullIncomingChange",
"title": "Update selected",
Expand Down Expand Up @@ -686,6 +691,10 @@
"command": "svn.switchBranch",
"when": "config.svn.enabled && svnOpenRepositoryCount != 0"
},
{
"command": "svn.merge",
"when": "config.svn.enabled && svnOpenRepositoryCount != 0"
},
{
"command": "svn.treeview.pullIncomingChange",
"when": "false"
Expand Down Expand Up @@ -814,6 +823,10 @@
"command": "svn.switchBranch",
"when": "config.svn.enabled && scmProvider == svn"
},
{
"command": "svn.merge",
"when": "config.svn.enabled && scmProvider == svn"
},
{
"command": "svn.update",
"when": "config.svn.enabled && scmProvider == svn"
Expand Down
2 changes: 2 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { Upgrade } from "./commands/upgrade";
import { SourceControlManager } from "./source_control_manager";
import { SearchLogByRevision } from "./commands/search_log_by_revision";
import { SearchLogByText } from "./commands/search_log_by_text";
import { Merge } from "./commands/merge";

export function registerCommands(
sourceControlManager: SourceControlManager,
Expand All @@ -63,6 +64,7 @@ export function registerCommands(
disposables.push(new OpenResourceHead());
disposables.push(new OpenChangeBase());
disposables.push(new SwitchBranch());
disposables.push(new Merge());
disposables.push(new Revert());
disposables.push(new Update());
disposables.push(new PullIncommingChange());
Expand Down
54 changes: 54 additions & 0 deletions src/commands/merge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { commands, window } from "vscode";
import { IBranchItem } from "../common/types";
import { isTrunk, selectBranch } from "../helpers/branch";
import { Repository } from "../repository";
import { Command } from "./command";

export class Merge extends Command {
constructor() {
super("svn.merge", { repository: true });
}

public async execute(repository: Repository) {
const branch = await selectBranch(repository);

if (!branch) {
return;
}

await this.merge(repository, branch);
}

async merge(repository: Repository, branch: IBranchItem) {
let reintegrate = false;
if (isTrunk(repository.currentBranch)) {
reintegrate = true;
}

try {
await repository.merge(branch.path, reintegrate);
} catch (error) {
if (typeof error === "object" && error.hasOwnProperty("stderrFormated")) {
if (error.stderrFormated.includes("try updating first")) {
const answer = await window.showErrorMessage(
"Seems like you need to update first prior to merging. " +
"Would you like to update now and try merging again?",
"Yes",
"No"
);
if (answer === "Yes") {
await commands.executeCommand("svn.update");
await this.merge(repository, branch);
}
} else {
window.showErrorMessage(
"Unable to merge branch: " + error.stderrFormated
);
}
} else {
console.log(error);
window.showErrorMessage("Unable to merge branch");
}
}
}
}
1 change: 1 addition & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export enum Operation {
Info = "Info",
Ignore = "Ignore",
Log = "Log",
Merge = "Merge",
NewBranch = "NewBranch",
Patch = "Patch",
Remove = "Remove",
Expand Down
13 changes: 13 additions & 0 deletions src/helpers/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,16 @@ export async function selectBranch(

return;
}

export function isTrunk(folder: string): boolean {
const conf = "layout.trunkRegex";
const layout = configuration.get<string>(conf);
const regex = new RegExp(`(^|/)(${layout})$`);
const matches = folder.match(regex);
const group = configuration.get<number>(`${conf}Name`, 1) + 2;

if (matches && matches[2] && matches[group]) {
return true;
}
return false;
}
11 changes: 11 additions & 0 deletions src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,17 @@ export class Repository implements IRemoteRepository {
});
}

public async merge(
name: string,
reintegrate: boolean = false,
accept_action: string = "postpone"
) {
await this.run(Operation.Merge, async () => {
await this.repository.merge(name, reintegrate, accept_action);
this.updateRemoteChangedFiles();
});
}

public async updateRevision(
ignoreExternals: boolean = false
): Promise<string> {
Expand Down
3 changes: 2 additions & 1 deletion src/statusbar/syncStatusBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export class SyncStatusBar {
const isSyncRunning =
this.repository.operations.isRunning(Operation.SwitchBranch) ||
this.repository.operations.isRunning(Operation.NewBranch) ||
this.repository.operations.isRunning(Operation.Update);
this.repository.operations.isRunning(Operation.Update) ||
this.repository.operations.isRunning(Operation.Merge);

const isStatusRemoteRunning = this.repository.operations.isRunning(
Operation.StatusRemote
Expand Down
18 changes: 18 additions & 0 deletions src/svnRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,24 @@ export class Repository {
return true;
}

public async merge(
ref: string,
reintegrate: boolean = false,
accept_action: string = "postpone"
) {
const repoUrl = await this.getRepoUrl();
const branchUrl = repoUrl + "/" + ref;

let args = ["merge", "--accept", accept_action];
args = args.concat(reintegrate ? ["--reintegrate"] : []);
args = args.concat([branchUrl]);

await this.exec(args);

this.resetInfoCache();
return true;
}

public async revert(files: string[], depth: keyof typeof SvnDepth) {
files = files.map(file => this.removeAbsolutePath(file));
const result = await this.exec(["revert", "--depth", depth, ...files]);
Expand Down

0 comments on commit 87060b3

Please sign in to comment.