Skip to content

Commit 77108be

Browse files
authored
Merge pull request #586 from dolthub/taylor/conflicts-resolve
graphql,web: Simple resolve conflicts from the web
2 parents 7863117 + 28929ea commit 77108be

File tree

20 files changed

+412
-29
lines changed

20 files changed

+412
-29
lines changed

graphql-server/schema.gql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ type Mutation {
416416
doltClone(databaseName: String!, ownerName: String!, remoteDbName: String!): Boolean!
417417
loadDataFile(schemaName: String, tableName: String!, refName: String!, databaseName: String!, importOp: ImportOperation!, fileType: FileType!, file: Upload!, modifier: LoadDataModifier): Boolean!
418418
mergePull(fromBranchName: String!, toBranchName: String!, databaseName: String!, author: AuthorInfo): Boolean!
419+
mergeAndResolveConflicts(author: AuthorInfo, fromBranchName: String!, toBranchName: String!, databaseName: String!, conflictResolveType: ConflictResolveType!): Boolean!
419420
addRemote(remoteName: String!, databaseName: String!, remoteUrl: String!): String!
420421
deleteRemote(databaseName: String!, remoteName: String!): Boolean!
421422
pullFromRemote(remoteName: String!, databaseName: String!, refName: String!, branchName: String!): PullRes!
@@ -448,4 +449,9 @@ enum LoadDataModifier {
448449
input AuthorInfo {
449450
name: String!
450451
email: String!
452+
}
453+
454+
enum ConflictResolveType {
455+
Ours
456+
Theirs
451457
}

graphql-server/src/pulls/pull.enums.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ export enum PullState {
77
}
88

99
registerEnumType(PullState, { name: "PullState" });
10+
11+
export enum ConflictResolveType {
12+
Ours = "ours",
13+
Theirs = "theirs",
14+
}
15+
16+
registerEnumType(ConflictResolveType, { name: "ConflictResolveType" });

graphql-server/src/pulls/pull.resolver.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { CommitResolver } from "../commits/commit.resolver";
1010
import { ConnectionProvider } from "../connections/connection.provider";
1111
import { AuthorInfo, PullArgs } from "../utils/commonTypes";
12+
import { ConflictResolveType } from "./pull.enums";
1213
import { PullWithDetails, fromAPIModelPullWithDetails } from "./pull.model";
1314

1415
@ArgsType()
@@ -17,6 +18,12 @@ class MergePullArgs extends PullArgs {
1718
author?: AuthorInfo;
1819
}
1920

21+
@ArgsType()
22+
class MergeAndResolveArgs extends MergePullArgs {
23+
@Field(_type => ConflictResolveType)
24+
conflictResolveType: ConflictResolveType;
25+
}
26+
2027
@Resolver(_of => PullWithDetails)
2128
export class PullResolver {
2229
constructor(
@@ -48,4 +55,19 @@ export class PullResolver {
4855
});
4956
return true;
5057
}
58+
59+
@Mutation(_returns => Boolean)
60+
async mergeAndResolveConflicts(
61+
@Args() args: MergeAndResolveArgs,
62+
): Promise<boolean> {
63+
const conn = this.conn.connection();
64+
await conn.callMergeWithResolveConflicts({
65+
databaseName: args.databaseName,
66+
fromBranchName: args.fromBranchName,
67+
toBranchName: args.toBranchName,
68+
author: args.author,
69+
conflictResolveType: args.conflictResolveType,
70+
});
71+
return true;
72+
}
5173
}

graphql-server/src/queryFactory/dolt/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,43 @@ export class DoltQueryFactory
326326
);
327327
}
328328

329+
async callMergeWithResolveConflicts(
330+
args: t.BranchesArgs & {
331+
author?: t.CommitAuthor;
332+
conflictResolveType: string;
333+
},
334+
): Promise<boolean> {
335+
return this.queryMultiple(
336+
async query => {
337+
await query("BEGIN");
338+
await query("SET @autocommit = 0");
339+
340+
const msg = `Merge branch ${args.fromBranchName}`;
341+
const params = [msg];
342+
if (args.author) {
343+
params.push(getAuthorString(args.author));
344+
}
345+
const res = await query(qh.getCallMerge(!!args.author), [
346+
args.fromBranchName,
347+
...params,
348+
]);
349+
350+
if (res.length && res[0].conflicts !== "0") {
351+
await query(qh.resolveConflicts, [`--${args.conflictResolveType}`]);
352+
await query(qh.getCommitMerge(!!args.author), params);
353+
} else {
354+
await query("ROLLBACK");
355+
throw new Error("expected conflicts but none found");
356+
}
357+
358+
await query("COMMIT");
359+
return true;
360+
},
361+
args.databaseName,
362+
args.toBranchName,
363+
);
364+
}
365+
329366
async resolveRefs(
330367
args: t.RefsArgs & { type?: CommitDiffType },
331368
): t.CommitsRes {

graphql-server/src/queryFactory/dolt/queries.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ export const mergeConflictsSummaryQuery = `SELECT * FROM DOLT_PREVIEW_MERGE_CONF
8787

8888
export const mergeConflictsQuery = `SELECT * FROM DOLT_PREVIEW_MERGE_CONFLICTS(?, ?, ?) LIMIT ? OFFSET ?`;
8989

90+
export const resolveConflicts = `CALL DOLT_CONFLICTS_RESOLVE(?, '.')`;
91+
92+
export const getCommitMerge = (hasAuthor = false) =>
93+
`CALL DOLT_COMMIT("-Am", ?${getAuthorNameString(hasAuthor)})`;
94+
9095
// REMOTES
9196

9297
export const callAddRemote = `CALL DOLT_REMOTE("add", ?, ?)`;

graphql-server/src/queryFactory/doltgres/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,43 @@ export class DoltgresQueryFactory
336336
);
337337
}
338338

339+
async callMergeWithResolveConflicts(
340+
args: t.BranchesArgs & {
341+
author?: t.CommitAuthor;
342+
conflictResolveType: string;
343+
},
344+
): Promise<boolean> {
345+
return this.queryMultiple(
346+
async query => {
347+
await query("BEGIN");
348+
// await query("SET autocommit off");
349+
350+
const msg = `Merge branch ${args.fromBranchName}`;
351+
const params = [msg];
352+
if (args.author) {
353+
params.push(getAuthorString(args.author));
354+
}
355+
const res = await query(qh.getCallMerge(!!args.author), [
356+
args.fromBranchName,
357+
...params,
358+
]);
359+
360+
if (res.length && res[0].conflicts !== "0") {
361+
await query(qh.resolveConflicts, [`--${args.conflictResolveType}`]);
362+
await query(qh.getCommitMerge(!!args.author), params);
363+
} else {
364+
await query("ROLLBACK");
365+
throw new Error("expected conflicts but none found");
366+
}
367+
368+
await query("COMMIT");
369+
return true;
370+
},
371+
args.databaseName,
372+
args.toBranchName,
373+
);
374+
}
375+
339376
async resolveRefs(
340377
args: t.RefsArgs & { type?: CommitDiffType },
341378
): t.CommitsRes {

graphql-server/src/queryFactory/doltgres/queries.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ export const mergeConflictsSummaryQuery = `SELECT * FROM DOLT_PREVIEW_MERGE_CONF
9595
export const getMergeConflictsQuery = (offset: number) =>
9696
`SELECT * FROM DOLT_PREVIEW_MERGE_CONFLICTS($1::text, $2::text, $3::text) LIMIT ${ROW_LIMIT + 1} OFFSET ${offset}`;
9797

98+
export const resolveConflicts = `SELECT DOLT_CONFLICTS_RESOLVE($1::text, '.')`;
99+
100+
export const getCommitMerge = (hasAuthor = false) =>
101+
`CALL DOLT_COMMIT("-Am", $1::text${getAuthorNameString(hasAuthor, "$2::text")})`;
102+
98103
// TAGS
99104

100105
export const callDeleteTag = `SELECT DOLT_TAG('-d', $1::text)`;

graphql-server/src/queryFactory/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ export declare class QueryFactory {
139139
args: t.BranchesArgs & { author?: t.CommitAuthor },
140140
): Promise<boolean>;
141141

142+
callMergeWithResolveConflicts(
143+
args: t.BranchesArgs & {
144+
author?: t.CommitAuthor;
145+
conflictResolveType: string;
146+
},
147+
): Promise<boolean>;
148+
142149
resolveRefs(
143150
args: t.RefsArgs & { type?: CommitDiffType },
144151
): Promise<{ fromCommitId: string; toCommitId: string }>;

graphql-server/src/queryFactory/mysql/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ export class MySQLQueryFactory
298298
throw notDoltError("merge branches");
299299
}
300300

301+
async callMergeWithResolveConflicts(_: t.BranchesArgs): Promise<boolean> {
302+
throw notDoltError("merge branches with conflicts");
303+
}
304+
301305
async resolveRefs(
302306
args: t.RefsArgs,
303307
): Promise<{ fromCommitId: string; toCommitId: string }> {

web/renderer/components/pageComponents/ConnectionsPage/ExistingConnections/Item.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import { DatabaseTypeLabel } from "@components/ConnectionsAndDatabases/DatabaseTypeLabel";
12
import { getDatabaseType } from "@components/DatabaseTypeLabel";
23
import { Button, ErrorMsg, Loader } from "@dolthub/react-components";
34
import { DatabaseConnectionFragment } from "@gen/graphql-types";
45
import { IoMdClose } from "@react-icons/all-files/io/IoMdClose";
6+
import cx from "classnames";
57
import Image from "next/legacy/image";
68
import { SyntheticEvent, useState } from "react";
7-
import cx from "classnames";
8-
import { DatabaseTypeLabel } from "@components/ConnectionsAndDatabases/DatabaseTypeLabel";
9-
import useAddConnection from "./useAddConnection";
109
import css from "./index.module.css";
10+
import useAddConnection from "./useAddConnection";
1111

1212
const forElectron = process.env.NEXT_PUBLIC_FOR_ELECTRON === "true";
1313

@@ -84,7 +84,7 @@ export default function Item({
8484
{conn.name}
8585
</Button.Link>
8686
</div>
87-
<ErrorMsg err={err || startDoltServerError} className={css.err} />
87+
<ErrorMsg err={err ?? startDoltServerError} className={css.err} />
8888
</div>
8989
</div>
9090
</li>

0 commit comments

Comments
 (0)