-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitActions.ts
More file actions
174 lines (164 loc) · 6.62 KB
/
gitActions.ts
File metadata and controls
174 lines (164 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { buildWorktreeLocation } from "@/shared/worktree";
import { readBridge } from "@/renderer/bridge";
import { captureRendererException } from "@/renderer/diagnostics/sentry";
import { useAppStore } from "@/renderer/state/appStore";
import { usePanelStore } from "@/renderer/state/panelStore";
import { usePullFromSourceDialogStore } from "@/renderer/state/pullFromSourceDialogStore";
import { useSharedSettings } from "@/renderer/state/sharedSettingsStore";
import { resolveWorktreeBranch } from "@/renderer/utils/gitHelpers";
import { closeThreads } from "@/renderer/utils/shellUtils";
import { performWorktreeRemoval } from "./worktreeActions";
function captureGitActionError(error: unknown): void {
captureRendererException(error, { featureArea: "git" });
}
export function openGitReviewForWorktree(projectId: string, worktreePath: string): void {
const mode = useSharedSettings.getState().gitReviewMode;
const panelStore = usePanelStore.getState();
panelStore.setGitReviewContext({ projectId, worktreePath });
if (mode === "panel") {
panelStore.setGitReviewAsPanel(true);
} else {
panelStore.setGitOverlayOpen(true);
}
}
export function gitSync(projectId: string, worktreePath?: string): void {
const project = useAppStore.getState().projects.find((p) => p.id === projectId);
if (!project) return;
const location = worktreePath
? buildWorktreeLocation(project.location, worktreePath)
: project.location;
void readBridge().gitSync({ projectLocation: location }).catch(captureGitActionError);
}
export function gitSyncRebase(projectId: string, worktreePath?: string): void {
const project = useAppStore.getState().projects.find((p) => p.id === projectId);
if (!project) return;
const location = worktreePath
? buildWorktreeLocation(project.location, worktreePath)
: project.location;
void readBridge().gitSyncRebase({ projectLocation: location }).catch(captureGitActionError);
}
export function gitPush(projectId: string, worktreePath: string): void {
const project = useAppStore.getState().projects.find((p) => p.id === projectId);
if (!project) return;
const worktreeBranch = resolveWorktreeBranch(projectId, worktreePath);
if (!worktreeBranch) return;
const worktreeLocation = buildWorktreeLocation(project.location, worktreePath);
void readBridge()
.gitPush({
projectLocation: worktreeLocation,
remote: "origin",
branch: worktreeBranch,
setUpstream: true,
})
.catch(captureGitActionError);
}
export function gitPull(projectId: string, worktreePath: string): void {
const project = useAppStore.getState().projects.find((p) => p.id === projectId);
if (!project) return;
const worktreeLocation = buildWorktreeLocation(project.location, worktreePath);
void readBridge()
.gitPull({ projectLocation: worktreeLocation, remote: "origin" })
.catch(captureGitActionError);
}
export function gitPullRebase(projectId: string, worktreePath: string): void {
const project = useAppStore.getState().projects.find((p) => p.id === projectId);
if (!project) return;
const worktreeLocation = buildWorktreeLocation(project.location, worktreePath);
void readBridge()
.gitPullRebase({ projectLocation: worktreeLocation, remote: "origin" })
.catch(captureGitActionError);
}
export function gitMergeToSource(projectId: string, worktreePath: string): void {
const project = useAppStore.getState().projects.find((p) => p.id === projectId);
if (!project) return;
const worktreeBranch = resolveWorktreeBranch(projectId, worktreePath);
if (!worktreeBranch) return;
void (async () => {
try {
const { sourceBranch } = await readBridge().gitGetWorktreeSourceBranch({
projectLocation: project.location,
branch: worktreeBranch,
});
if (!sourceBranch) return;
const worktreeLocation = buildWorktreeLocation(project.location, worktreePath);
await readBridge().gitMergeToSource({
projectLocation: project.location,
worktreeLocation,
worktreeBranch,
sourceBranch,
});
} catch (error) {
captureGitActionError(error);
// ignored — user can open git review for details
}
})();
}
export function gitMergeAndRemove(projectId: string, worktreePath: string): void {
const project = useAppStore.getState().projects.find((p) => p.id === projectId);
if (!project) return;
const worktreeBranch = resolveWorktreeBranch(projectId, worktreePath);
if (!worktreeBranch) return;
void (async () => {
try {
const { sourceBranch } = await readBridge().gitGetWorktreeSourceBranch({
projectLocation: project.location,
branch: worktreeBranch,
});
if (!sourceBranch) return;
const worktreeLocation = buildWorktreeLocation(project.location, worktreePath);
const result = await readBridge().gitMergeToSource({
projectLocation: project.location,
worktreeLocation,
worktreeBranch,
sourceBranch,
});
if (!result.merged) return;
const allThreads = useAppStore.getState().threads;
const siblings = allThreads.filter((t) => t.worktreePath === worktreePath);
const deleteThread = useAppStore.getState().deleteThread;
for (const sib of siblings) {
deleteThread(sib.id);
}
await closeThreads(siblings.map((sib) => sib.id));
await performWorktreeRemoval(project, worktreePath, worktreeBranch);
} catch (error) {
captureGitActionError(error);
// ignored — user can open git review for details
}
})();
}
export function gitPullFromSource(projectId: string, worktreePath: string): void {
const project = useAppStore.getState().projects.find((p) => p.id === projectId);
if (!project) return;
const worktreeBranch = resolveWorktreeBranch(projectId, worktreePath);
if (!worktreeBranch) return;
void (async () => {
try {
const { sourceBranch } = await readBridge().gitGetWorktreeSourceBranch({
projectLocation: project.location,
branch: worktreeBranch,
});
if (!sourceBranch) return;
const worktreeLocation = buildWorktreeLocation(project.location, worktreePath);
const result = await readBridge().gitPullFromSource({
worktreeLocation,
sourceBranch,
preserveLocalChanges: false,
});
if (result.needsStash) {
usePullFromSourceDialogStore.getState().setDialog({
projectId,
worktreePath,
sourceBranch,
});
return;
}
if (result.conflicting) {
openGitReviewForWorktree(projectId, worktreePath);
}
} catch (error) {
captureGitActionError(error);
// ignored — user can open git review for details
}
})();
}