Skip to content

Commit f2417cd

Browse files
only21milcodexmaria-rcks
authored
fix(mobile): await branch refs before choosing an automatic feature branch (pingdotgg#22098)
Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 33c6401 commit f2417cd

3 files changed

Lines changed: 91 additions & 4 deletions

File tree

‎apps/mobile/src/features/threads/git/GitConfirmSheet.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export function GitConfirmSheet(props: GitConfirmSheetProps) {
9494
gitState.selectedThreadBranches.length > 0
9595
? gitState.selectedThreadBranches
9696
: await gitActions.refreshSelectedThreadBranches();
97+
if (branches === null) return;
9798
const newBranchName = resolveAutoFeatureBranchName(
9899
Arr.filterMap(branches, (branch) =>
99100
branch.isRemote ? Result.failVoid : Result.succeed(branch.name),

‎apps/mobile/src/state/use-selected-thread-git-actions.test.ts‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { AuthOrchestrationOperateScope, AuthSourceControlWriteScope } from "@t3tools/contracts";
2+
import type { VcsListRefsResult } from "@t3tools/contracts";
3+
import { resolveAutoFeatureBranchName } from "@t3tools/shared/git";
24
import * as Cause from "effect/Cause";
35
import { AsyncResult } from "effect/reactivity";
46
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
@@ -20,6 +22,8 @@ const state = vi.hoisted(() => ({
2022
results: [] as { type: string; description?: string }[],
2123
afterGitAction: undefined as (() => void) | undefined,
2224
statusRefreshes: 0,
25+
readRefs: vi.fn(),
26+
cachedRefs: [] as VcsListRefsResult["refs"],
2327
}));
2428

2529
vi.mock("react", () => ({
@@ -46,7 +50,7 @@ vi.mock("./use-selected-thread-worktree", () => ({
4650
}),
4751
}));
4852
vi.mock("./queries", () => ({
49-
useBranches: () => ({ data: { refs: [] }, refresh: () => {} }),
53+
useBranches: () => ({ data: { refs: state.cachedRefs }, refresh: () => {} }),
5054
}));
5155
vi.mock("./use-atom-command", () => ({ useAtomCommand: (command: unknown) => command }));
5256
vi.mock("./atom-registry", () => ({ appAtomRegistry: {} }));
@@ -79,6 +83,7 @@ vi.mock("./threads", () => ({
7983
}));
8084
vi.mock("./vcs", () => ({
8185
vcsEnvironment: {
86+
readRefs: state.readRefs,
8287
refreshStatus: async () => {
8388
state.statusRefreshes += 1;
8489
return AsyncResult.success({ refName: state.branch });
@@ -231,3 +236,71 @@ describe("thread Git mutation permissions", () => {
231236
},
232237
);
233238
});
239+
240+
describe("automatic feature branch refs", () => {
241+
const empty: VcsListRefsResult = {
242+
refs: [],
243+
isRepo: true,
244+
hasPrimaryRemote: true,
245+
nextCursor: null,
246+
totalCount: 0,
247+
};
248+
249+
it.each([false, true])("awaits fresh refs before naming, cached: %s", async (cached) => {
250+
const request = Promise.withResolvers<VcsListRefsResult>();
251+
state.cachedRefs = cached
252+
? [
253+
{
254+
name: "old-branch",
255+
isRemote: false,
256+
current: false,
257+
isDefault: false,
258+
worktreePath: null,
259+
},
260+
]
261+
: [];
262+
state.readRefs.mockReturnValue(request.promise.then(AsyncResult.success));
263+
let settled = false;
264+
const refresh = useSelectedThreadGitActions()
265+
.refreshSelectedThreadBranches()
266+
.then((refs) => {
267+
settled = true;
268+
return refs;
269+
});
270+
await Promise.resolve();
271+
expect(settled).toBe(false);
272+
const local = {
273+
name: "feature/update",
274+
isRemote: false,
275+
current: false,
276+
isDefault: false,
277+
worktreePath: null,
278+
};
279+
request.resolve({
280+
...empty,
281+
refs: [
282+
local,
283+
{ ...local, name: "origin/feature/update", isRemote: true, remoteName: "origin" },
284+
],
285+
totalCount: 2,
286+
});
287+
const refs = await refresh;
288+
expect(refs).toEqual([local]);
289+
expect(resolveAutoFeatureBranchName(refs!.map((ref) => ref.name))).toBe("feature/update-2");
290+
expect(state.readRefs).toHaveBeenCalledWith({
291+
environmentId: "environment",
292+
input: { cwd: "/repo", limit: 100, refresh: true },
293+
});
294+
});
295+
296+
it("reports lookup failure instead of returning empty refs", async () => {
297+
state.results = [];
298+
state.readRefs.mockResolvedValue(
299+
AsyncResult.failure(Cause.fail(new Error("Cannot list refs"))),
300+
);
301+
expect(await useSelectedThreadGitActions().refreshSelectedThreadBranches()).toBeNull();
302+
expect(state.results).toEqual([
303+
{ type: "error", title: "Git action failed", description: "Cannot list refs" },
304+
]);
305+
});
306+
});

‎apps/mobile/src/state/use-selected-thread-git-actions.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export function useSelectedThreadGitActions() {
5656
const updateThreadMetadata = useAtomCommand(threadEnvironment.updateMetadata, {
5757
reportFailure: false,
5858
});
59+
const readRefs = useAtomCommand(vcsEnvironment.readRefs, { reportFailure: false });
5960
const refreshStatus = useAtomCommand(vcsEnvironment.refreshStatus, { reportFailure: false });
6061
const switchRef = useAtomCommand(vcsEnvironment.switchRef, { reportFailure: false });
6162
const createRef = useAtomCommand(vcsEnvironment.createRef, { reportFailure: false });
@@ -206,9 +207,21 @@ export function useSelectedThreadGitActions() {
206207
return result.value;
207208
};
208209

209-
const refreshSelectedThreadBranches = async (): Promise<ReadonlyArray<VcsRef>> => {
210-
branchState.refresh();
211-
return dedupeRemoteBranchesWithLocalMatches(branchState.data?.refs ?? []).filter(
210+
const refreshSelectedThreadBranches = async (): Promise<ReadonlyArray<VcsRef> | null> => {
211+
if (branchTarget.environmentId === null || branchTarget.cwd === null) return null;
212+
const result = await readRefs({
213+
environmentId: branchTarget.environmentId,
214+
input: { cwd: branchTarget.cwd, limit: 100, refresh: true },
215+
});
216+
if (AsyncResult.isFailure(result)) {
217+
const error = Cause.squash(result.cause);
218+
const message = error instanceof Error ? error.message : "Failed to refresh branches.";
219+
setPendingConnectionError(message);
220+
showGitActionResult({ type: "error", title: "Git action failed", description: message });
221+
return null;
222+
}
223+
setPendingConnectionError(null);
224+
return dedupeRemoteBranchesWithLocalMatches(result.value.refs).filter(
212225
(branch) => !branch.isRemote,
213226
);
214227
};

0 commit comments

Comments
 (0)