Skip to content

Commit 5e18314

Browse files
fix(forgejo): fast-forward-only repositories can merge pull requests (pingdotgg#32738)
1 parent 15cf8b1 commit 5e18314

11 files changed

Lines changed: 147 additions & 21 deletions

File tree

‎apps/mobile/src/features/pull-requests/pullRequestActions.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export const MERGE_METHODS = {
3333
description: "Keep the commits and add a merge commit.",
3434
},
3535
rebase: { title: "Rebase and merge", description: "Add the commits without a merge commit." },
36+
"fast-forward": {
37+
title: "Fast-forward merge",
38+
description: "Add the commits as they are. Needs the branch to contain the base.",
39+
},
3640
} as const satisfies Record<PullRequestMergeMethod, { title: string; description: string }>;
3741

3842
export function pullRequestActionLabel(

‎apps/mobile/src/persistence/mobile-preferences.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
PullRequestListFilters,
1212
PullRequestListState,
1313
type ProviderInstanceId,
14-
type PullRequestMergeMethod,
14+
PullRequestMergeMethod,
1515
type SidebarProjectGroupingMode,
1616
} from "@t3tools/contracts";
1717
import { ToolCallVisibility } from "@t3tools/contracts/settings";
@@ -174,6 +174,7 @@ export class MobilePreferencesStore extends Context.Service<
174174
>()("@t3tools/mobile/persistence/mobile-preferences/MobilePreferencesStore") {}
175175

176176
const isToolCallVisibility = Schema.is(ToolCallVisibility);
177+
const isPullRequestMergeMethod = Schema.is(PullRequestMergeMethod);
177178

178179
function sanitizePreferences(parsed: Preferences): Preferences {
179180
const preferences: {
@@ -232,11 +233,7 @@ function sanitizePreferences(parsed: Preferences): Preferences {
232233
preferences.pullRequestHideTestFiles = parsed.pullRequestHideTestFiles;
233234
}
234235

235-
if (
236-
parsed.pullRequestMergeMethod === "squash" ||
237-
parsed.pullRequestMergeMethod === "merge" ||
238-
parsed.pullRequestMergeMethod === "rebase"
239-
) {
236+
if (isPullRequestMergeMethod(parsed.pullRequestMergeMethod)) {
240237
preferences.pullRequestMergeMethod = parsed.pullRequestMergeMethod;
241238
}
242239

‎apps/web/src/components/pullRequest/pullRequestDetail.logic.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const PULL_REQUEST_MERGE_METHOD_LABELS: Record<PullRequestMergeMethod, st
5454
merge: "Merge",
5555
squash: "Squash and merge",
5656
rebase: "Rebase and merge",
57+
"fast-forward": "Fast-forward merge",
5758
};
5859

5960
export function allowsSinglePullRequestMerge(input: {

‎apps/web/src/uiStateStore.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ describe("parsePersistedState", () => {
136136
});
137137
});
138138

139+
it("keeps a persisted fast-forward merge method", () => {
140+
expect(
141+
parsePersistedState({ pullRequestMergeMethod: "fast-forward" }).pullRequestMergeMethod,
142+
).toBe("fast-forward");
143+
});
144+
139145
it.each([undefined, 1])("ignores changed-file expansion version %s", (version) => {
140146
const parsed = parsePersistedState({
141147
...(version === undefined ? {} : { threadChangedFilesExpansionVersion: version }),

‎apps/web/src/uiStateStore.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Debouncer } from "@tanstack/react-pacer";
2-
import type { PullRequestMergeMethod } from "@t3tools/contracts";
2+
import { PullRequestMergeMethod } from "@t3tools/contracts";
3+
import * as Schema from "effect/Schema";
34
import { create } from "zustand";
45
import { normalizeProjectPathForComparison } from "@t3tools/client-runtime/state/projects";
56

@@ -99,6 +100,8 @@ function sanitizeOptionalKey(value: unknown): string | null {
99100
return typeof value === "string" && value.length > 0 ? value : null;
100101
}
101102

103+
const isPullRequestMergeMethod = Schema.is(PullRequestMergeMethod);
104+
102105
function sanitizeTimestampRecord(value: unknown): Record<string, string> {
103106
if (!value || typeof value !== "object") {
104107
return {};
@@ -114,10 +117,6 @@ function sanitizeTimestampRecord(value: unknown): Record<string, string> {
114117
);
115118
}
116119

117-
function isPullRequestMergeMethod(value: unknown): value is PullRequestMergeMethod {
118-
return value === "merge" || value === "squash" || value === "rebase";
119-
}
120-
121120
export function parsePersistedState(parsed: PersistedUiState): UiState {
122121
const projectExpandedById =
123122
parsed.projectExpandedById === undefined

‎packages/contracts/src/pullRequest.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe("PullRequestCapabilities", () => {
104104
capabilities: {
105105
...base,
106106
actions: options.actions,
107-
mergeMethods: ["squash", "fast-forward", "merge"],
107+
mergeMethods: ["squash", "semi-linear", "merge"],
108108
updateMethods: options.updateMethods,
109109
review: { ...base.review, verdicts: options.verdicts },
110110
},

‎packages/contracts/src/pullRequest.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ export type PullRequestChecksState = typeof PullRequestChecksState.Type;
5454
export const PullRequestMergeability = Schema.Literals(["mergeable", "conflicting", "unknown"]);
5555
export type PullRequestMergeability = typeof PullRequestMergeability.Type;
5656

57-
export const PullRequestMergeMethod = Schema.Literals(["merge", "squash", "rebase"]);
57+
/** `fast-forward` refuses unless the head contains the base, where `rebase` rewrites the branch onto it. */
58+
export const PullRequestMergeMethod = Schema.Literals([
59+
"merge",
60+
"squash",
61+
"rebase",
62+
"fast-forward",
63+
]);
5864
export type PullRequestMergeMethod = typeof PullRequestMergeMethod.Type;
5965

6066
export const PullRequestAction = Schema.Literals([
@@ -335,6 +341,8 @@ export const PullRequestMergeCapabilities = Schema.Struct({
335341
merge: Schema.Boolean,
336342
squash: Schema.Boolean,
337343
rebase: Schema.Boolean,
344+
/** Optional so servers that predate it still decode; absent means not allowed. */
345+
"fast-forward": Schema.optional(Schema.Boolean),
338346
});
339347
export type PullRequestMergeCapabilities = typeof PullRequestMergeCapabilities.Type;
340348

‎packages/source-control-forgejo/src/server/ForgejoPullRequestProvider.test.ts‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,106 @@ it.effect("filters listed pull requests by the requested state", () =>
121121
assert.deepStrictEqual(yield* numbers("open"), []);
122122
}),
123123
);
124+
125+
const fastForwardOnly = {
126+
cwd: "/forgejo",
127+
repository: "maria/project",
128+
host: "forge.example:3000",
129+
number: 3,
130+
};
131+
const fastForwardOnlySettings = {
132+
allow_merge_commits: false,
133+
allow_squash_merge: false,
134+
allow_rebase: false,
135+
allow_rebase_explicit: false,
136+
allow_fast_forward_only_merge: true,
137+
default_merge_style: "fast-forward-only",
138+
};
139+
const fastForwardOnlyProvider = (
140+
writes: Array<{ path: string; body: unknown }>,
141+
mergeSettings: object = fastForwardOnlySettings,
142+
) =>
143+
ForgejoPullRequestProvider.make.pipe(
144+
Effect.provide(
145+
Layer.mock(ForgejoCli.ForgejoCli)({
146+
api: (input) => {
147+
const [path] = input.path.split("?");
148+
const reply = (value: unknown) =>
149+
Effect.succeed(TestSourceControlHost.processOutput(JSON.stringify(value)));
150+
if (input.method === "POST") {
151+
writes.push({ path: input.path, body: input.body });
152+
return reply("");
153+
}
154+
switch (path) {
155+
case "user":
156+
return reply({ login: "maria" });
157+
case "repos/maria/project":
158+
return reply({
159+
full_name: "maria/project",
160+
permissions: { push: true, admin: true },
161+
...mergeSettings,
162+
});
163+
case "repos/maria/project/pulls/3":
164+
return reply({
165+
number: 3,
166+
title: "Fast-forward me",
167+
body: "",
168+
html_url: "https://forge.example:3000/maria/project/pulls/3",
169+
user: { login: "maria" },
170+
state: "open",
171+
merged: false,
172+
mergeable: true,
173+
head: { ref: "feature", sha: "head-sha", repo: null },
174+
base: { ref: "main", sha: "base-sha", repo: null },
175+
merge_base: "base-sha",
176+
created_at: "2026-10-12T00:00:00Z",
177+
updated_at: "2026-10-12T00:00:00Z",
178+
closed_at: null,
179+
merged_at: null,
180+
labels: [],
181+
});
182+
case "repos/maria/project/branches/main":
183+
return reply({});
184+
default:
185+
return reply([]);
186+
}
187+
},
188+
}),
189+
),
190+
);
191+
192+
it.effect("offers a fast-forward merge on a repository that only allows fast-forward", () =>
193+
Effect.gen(function* () {
194+
const provider = yield* fastForwardOnlyProvider([]);
195+
const detail = yield* provider.getChangeRequest(fastForwardOnly);
196+
const allowed = provider.capabilities.mergeMethods.filter(
197+
(method) => detail.mergeCapabilities[method],
198+
);
199+
assert.deepStrictEqual(allowed, ["fast-forward"]);
200+
}),
201+
);
202+
203+
it.effect("does not offer fast-forward when Forgejo omits the setting", () =>
204+
Effect.gen(function* () {
205+
const provider = yield* fastForwardOnlyProvider([], {});
206+
const detail = yield* provider.getChangeRequest(fastForwardOnly);
207+
const allowed = provider.capabilities.mergeMethods.filter(
208+
(method) => detail.mergeCapabilities[method],
209+
);
210+
assert.deepStrictEqual(allowed, ["merge", "squash", "rebase"]);
211+
}),
212+
);
213+
214+
it.effect("merges with Forgejo's fast-forward-only style", () =>
215+
Effect.gen(function* () {
216+
const writes: Array<{ path: string; body: unknown }> = [];
217+
const provider = yield* fastForwardOnlyProvider(writes);
218+
yield* provider.runAction({ ...fastForwardOnly, action: "merge", mergeMethod: "fast-forward" });
219+
assert.deepStrictEqual(writes, [
220+
{
221+
path: "repos/maria/project/pulls/3/merge",
222+
body: { Do: "fast-forward-only", delete_branch_after_merge: false },
223+
},
224+
]);
225+
}),
226+
);

‎packages/source-control-forgejo/src/server/ForgejoPullRequestProvider.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const CAPABILITIES: PullRequestCapabilities = {
4444
viewedFiles: "environment",
4545
comment: true,
4646
actions: [...definition.changeRequestActions],
47-
mergeMethods: ["merge", "squash", "rebase"],
47+
mergeMethods: ["merge", "squash", "rebase", "fast-forward"],
4848
updateMethods: ["merge", "rebase"],
4949
search: false,
5050
reactions: true,
@@ -385,6 +385,8 @@ export const make = Effect.gen(function* () {
385385
merge: repo.allow_merge_commits ?? true,
386386
squash: repo.allow_squash_merge ?? true,
387387
rebase: repo.allow_rebase ?? true,
388+
// Added in Forgejo 7; older servers cannot fast-forward-only merge.
389+
"fast-forward": repo.allow_fast_forward_only_merge ?? false,
388390
},
389391
} satisfies ProviderChangeRequestDetail;
390392
}),
@@ -542,7 +544,13 @@ export const make = Effect.gen(function* () {
542544
...input,
543545
path: `${pullPath(input)}/merge`,
544546
method: "POST",
545-
body: { Do: input.mergeMethod ?? "merge", delete_branch_after_merge: deleteBranch },
547+
body: {
548+
Do:
549+
input.mergeMethod === "fast-forward"
550+
? "fast-forward-only"
551+
: (input.mergeMethod ?? "merge"),
552+
delete_branch_after_merge: deleteBranch,
553+
},
546554
}).pipe(
547555
Effect.catchTags({
548556
// Forgejo merges before deleting the branch, so a refused deletion still merged.

‎packages/source-control-forgejo/src/server/forgejoPullRequestJson.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const ForgejoRepository = Schema.Struct({
3232
allow_merge_commits: Schema.optional(Schema.Boolean),
3333
allow_squash_merge: Schema.optional(Schema.Boolean),
3434
allow_rebase: Schema.optional(Schema.Boolean),
35+
allow_fast_forward_only_merge: Schema.optional(Schema.Boolean),
3536
allow_rebase_update: Schema.optional(Schema.Boolean),
3637
default_delete_branch_after_merge: Schema.optional(Schema.Boolean),
3738
});

0 commit comments

Comments
 (0)