The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { |
| 2 | GitRunStackedActionInput, |
| 3 | GitStackedAction, |
| 4 | VcsStatusResult, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { type ChangeRequestTerminology, sourceControlClients } from "../sourceControlClients.ts"; |
| 7 | import { mFallback, msg } from "gt-i18n"; |
| 8 | import type { MFunctionType } from "gt-i18n/types"; |
| 9 | |
| 10 | export type GitActionIconName = "commit" | "push" | "pr"; |
| 11 | |
| 12 | export type GitDialogAction = "commit" | "push" | "create_pr"; |
| 13 | |
| 14 | export interface GitActionMenuItem { |
| 15 | id: "commit" | "push" | "pr"; |
| 16 | label: string; |
| 17 | disabled: boolean; |
| 18 | icon: GitActionIconName; |
| 19 | kind: "open_dialog" | "open_pr"; |
| 20 | dialogAction?: GitDialogAction; |
| 21 | } |
| 22 | |
| 23 | export interface GitQuickAction { |
| 24 | label: string; |
| 25 | disabled: boolean; |
| 26 | kind: "run_action" | "run_pull" | "open_pr" | "show_hint"; |
| 27 | action?: GitStackedAction; |
| 28 | hint?: string; |
| 29 | } |
| 30 | |
| 31 | export interface DefaultBranchActionDialogCopy { |
| 32 | title: string; |
| 33 | description: string; |
| 34 | continueLabel: string; |
| 35 | } |
| 36 | |
| 37 | export type DefaultBranchConfirmableAction = |
| 38 | | "push" |
| 39 | | "create_pr" |
| 40 | | "commit_push" |
| 41 | | "commit_push_pr"; |
| 42 | |
| 43 | export type GitActionRequestInput = Pick< |
| 44 | GitRunStackedActionInput, |
| 45 | "action" | "commitMessage" | "featureBranch" | "filePaths" |
| 46 | >; |
| 47 | |
| 48 | export function changeRequestTerminologyFor( |
| 49 | gitStatus: VcsStatusResult | null, |
| 50 | ): ChangeRequestTerminology { |
| 51 | return sourceControlClients.get(gitStatus?.sourceControlProvider?.kind).changeRequest; |
| 52 | } |
| 53 | |
| 54 | export function buildMenuItems( |
| 55 | gitStatus: VcsStatusResult | null, |
| 56 | isBusy: boolean, |
| 57 | hasOriginRemote = true, |
| 58 | resolve: MFunctionType = mFallback, |
| 59 | ): GitActionMenuItem[] { |
| 60 | if (!gitStatus) return []; |
| 61 | const terminology = changeRequestTerminologyFor(gitStatus); |
| 62 | |
| 63 | const hasBranch = gitStatus.refName !== null; |
| 64 | const hasChanges = gitStatus.hasWorkingTreeChanges; |
| 65 | const hasOpenPr = gitStatus.pr?.state === "open"; |
| 66 | const isBehind = gitStatus.behindCount > 0; |
| 67 | const canPushWithoutUpstream = hasOriginRemote && !gitStatus.hasUpstream; |
| 68 | const canCommit = !isBusy && hasChanges; |
| 69 | const canPush = |
| 70 | !isBusy && |
| 71 | hasBranch && |
| 72 | !hasChanges && |
| 73 | !isBehind && |
| 74 | gitStatus.aheadCount > 0 && |
| 75 | (gitStatus.hasUpstream || canPushWithoutUpstream); |
| 76 | const canCreatePr = |
| 77 | !isBusy && |
| 78 | hasBranch && |
| 79 | !hasOpenPr && |
| 80 | (gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) > 0 && |
| 81 | !isBehind && |
| 82 | (gitStatus.hasUpstream || canPushWithoutUpstream); |
| 83 | const canOpenPr = !isBusy && hasOpenPr; |
| 84 | |
| 85 | return [ |
| 86 | { |
| 87 | id: "commit", |
| 88 | label: resolve(msg("Commit", { $context: "verb, git action" })), |
| 89 | disabled: !canCommit, |
| 90 | icon: "commit", |
| 91 | kind: "open_dialog", |
| 92 | dialogAction: "commit", |
| 93 | }, |
| 94 | { |
| 95 | id: "push", |
| 96 | label: resolve(msg("Push", { $context: "verb, git action" })), |
| 97 | disabled: !canPush, |
| 98 | icon: "push", |
| 99 | kind: "open_dialog", |
| 100 | dialogAction: "push", |
| 101 | }, |
| 102 | hasOpenPr |
| 103 | ? { |
| 104 | id: "pr", |
| 105 | label: resolve(msg("View {shortLabel}", { shortLabel: terminology.shortLabel })), |
| 106 | disabled: !canOpenPr, |
| 107 | icon: "pr", |
| 108 | kind: "open_pr", |
| 109 | } |
| 110 | : { |
| 111 | id: "pr", |
| 112 | label: resolve(msg("Create {shortLabel}", { shortLabel: terminology.shortLabel })), |
| 113 | disabled: !canCreatePr, |
| 114 | icon: "pr", |
| 115 | kind: "open_dialog", |
| 116 | dialogAction: "create_pr", |
| 117 | }, |
| 118 | ]; |
| 119 | } |
| 120 | |
| 121 | export function resolveQuickAction( |
| 122 | gitStatus: VcsStatusResult | null, |
| 123 | isBusy: boolean, |
| 124 | isDefaultBranch = false, |
| 125 | hasOriginRemote = true, |
| 126 | resolve: MFunctionType = mFallback, |
| 127 | ): GitQuickAction { |
| 128 | if (isBusy) { |
| 129 | return { |
| 130 | label: resolve(msg("Commit", { $context: "verb, git action" })), |
| 131 | disabled: true, |
| 132 | kind: "show_hint", |
| 133 | hint: resolve(msg("Git action in progress.")), |
| 134 | }; |
| 135 | } |
| 136 | |
| 137 | if (!gitStatus) { |
| 138 | return { |
| 139 | label: resolve(msg("Commit", { $context: "verb, git action" })), |
| 140 | disabled: true, |
| 141 | kind: "show_hint", |
| 142 | hint: resolve(msg("Git status is unavailable.")), |
| 143 | }; |
| 144 | } |
| 145 | |
| 146 | const hasBranch = gitStatus.refName !== null; |
| 147 | const hasChanges = gitStatus.hasWorkingTreeChanges; |
| 148 | const hasOpenPr = gitStatus.pr?.state === "open"; |
| 149 | const isAhead = gitStatus.aheadCount > 0; |
| 150 | const hasDefaultBranchDelta = (gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) > 0; |
| 151 | const isBehind = gitStatus.behindCount > 0; |
| 152 | const isDiverged = isAhead && isBehind; |
| 153 | const terminology = changeRequestTerminologyFor(gitStatus); |
| 154 | |
| 155 | if (!hasBranch) { |
| 156 | return { |
| 157 | label: resolve(msg("Commit", { $context: "verb, git action" })), |
| 158 | disabled: true, |
| 159 | kind: "show_hint", |
| 160 | hint: resolve( |
| 161 | msg( |
| 162 | "{kind, select, pr {Create and checkout a branch before pushing or opening a PR.} mr {Create and checkout a branch before pushing or opening an MR.} generic {Create and checkout a branch before pushing or opening a change request.} other {Create and checkout a branch before pushing or opening {changeRequest}.}}", |
| 163 | { |
| 164 | kind: changeRequestKind(terminology), |
| 165 | changeRequest: terminology.shortLabelWithArticle, |
| 166 | }, |
| 167 | ), |
| 168 | ), |
| 169 | }; |
| 170 | } |
| 171 | |
| 172 | if (hasChanges) { |
| 173 | if (isBehind || (!gitStatus.hasUpstream && !hasOriginRemote)) { |
| 174 | return { |
| 175 | label: resolve(msg("Commit", { $context: "verb, git action" })), |
| 176 | disabled: false, |
| 177 | kind: "run_action", |
| 178 | action: "commit", |
| 179 | }; |
| 180 | } |
| 181 | if (hasOpenPr || isDefaultBranch) { |
| 182 | return { |
| 183 | label: resolve(msg("Commit & push")), |
| 184 | disabled: false, |
| 185 | kind: "run_action", |
| 186 | action: "commit_push", |
| 187 | }; |
| 188 | } |
| 189 | return { |
| 190 | label: resolve(msg("Commit, push & {shortLabel}", { shortLabel: terminology.shortLabel })), |
| 191 | disabled: false, |
| 192 | kind: "run_action", |
| 193 | action: "commit_push_pr", |
| 194 | }; |
| 195 | } |
| 196 | |
| 197 | if (!gitStatus.hasUpstream) { |
| 198 | if (!hasOriginRemote) { |
| 199 | if (hasOpenPr && !isAhead) { |
| 200 | return { |
| 201 | label: resolve(msg("View {shortLabel}", { shortLabel: terminology.shortLabel })), |
| 202 | disabled: false, |
| 203 | kind: "open_pr", |
| 204 | }; |
| 205 | } |
| 206 | return { |
| 207 | label: resolve(msg("Push", { $context: "verb, git action" })), |
| 208 | disabled: true, |
| 209 | kind: "show_hint", |
| 210 | hint: resolve( |
| 211 | msg( |
| 212 | '{kind, select, pr {Add an "origin" remote before pushing or creating a PR.} mr {Add an "origin" remote before pushing or creating an MR.} generic {Add an "origin" remote before pushing or creating a change request.} other {Add an "origin" remote before pushing or creating {changeRequest}.}}', |
| 213 | { |
| 214 | kind: changeRequestKind(terminology), |
| 215 | changeRequest: terminology.shortLabelWithArticle, |
| 216 | }, |
| 217 | ), |
| 218 | ), |
| 219 | }; |
| 220 | } |
| 221 | if (!isAhead) { |
| 222 | if (hasOpenPr) { |
| 223 | return { |
| 224 | label: resolve(msg("View {shortLabel}", { shortLabel: terminology.shortLabel })), |
| 225 | disabled: false, |
| 226 | kind: "open_pr", |
| 227 | }; |
| 228 | } |
| 229 | return { |
| 230 | label: resolve(msg("Push", { $context: "verb, git action" })), |
| 231 | disabled: true, |
| 232 | kind: "show_hint", |
| 233 | hint: resolve(msg("No local commits to push.")), |
| 234 | }; |
| 235 | } |
| 236 | if (hasOpenPr || isDefaultBranch) { |
| 237 | return { |
| 238 | label: resolve(msg("Push", { $context: "verb, git action" })), |
| 239 | disabled: false, |
| 240 | kind: "run_action", |
| 241 | action: isDefaultBranch ? "commit_push" : "push", |
| 242 | }; |
| 243 | } |
| 244 | return { |
| 245 | label: resolve(msg("Push & create {shortLabel}", { shortLabel: terminology.shortLabel })), |
| 246 | disabled: false, |
| 247 | kind: "run_action", |
| 248 | action: "create_pr", |
| 249 | }; |
| 250 | } |
| 251 | |
| 252 | if (isDiverged) { |
| 253 | return { |
| 254 | label: resolve(msg("Sync branch")), |
| 255 | disabled: true, |
| 256 | kind: "show_hint", |
| 257 | hint: resolve(msg("Branch has diverged from upstream. Rebase/merge first.")), |
| 258 | }; |
| 259 | } |
| 260 | |
| 261 | if (isBehind) { |
| 262 | return { |
| 263 | label: resolve(msg("Pull", { $context: "verb, git action" })), |
| 264 | disabled: false, |
| 265 | kind: "run_pull", |
| 266 | }; |
| 267 | } |
| 268 | |
| 269 | if (isAhead) { |
| 270 | if (hasOpenPr || isDefaultBranch) { |
| 271 | return { |
| 272 | label: resolve(msg("Push", { $context: "verb, git action" })), |
| 273 | disabled: false, |
| 274 | kind: "run_action", |
| 275 | action: isDefaultBranch ? "commit_push" : "push", |
| 276 | }; |
| 277 | } |
| 278 | return { |
| 279 | label: resolve(msg("Push & create {shortLabel}", { shortLabel: terminology.shortLabel })), |
| 280 | disabled: false, |
| 281 | kind: "run_action", |
| 282 | action: "create_pr", |
| 283 | }; |
| 284 | } |
| 285 | |
| 286 | if (hasOpenPr && gitStatus.hasUpstream) { |
| 287 | return { |
| 288 | label: resolve(msg("View {shortLabel}", { shortLabel: terminology.shortLabel })), |
| 289 | disabled: false, |
| 290 | kind: "open_pr", |
| 291 | }; |
| 292 | } |
| 293 | |
| 294 | if (hasDefaultBranchDelta && !isDefaultBranch) { |
| 295 | return { |
| 296 | label: resolve(msg("Create {shortLabel}", { shortLabel: terminology.shortLabel })), |
| 297 | disabled: false, |
| 298 | kind: "run_action", |
| 299 | action: "create_pr", |
| 300 | }; |
| 301 | } |
| 302 | |
| 303 | return { |
| 304 | label: resolve(msg("Commit", { $context: "verb, git action" })), |
| 305 | disabled: true, |
| 306 | kind: "show_hint", |
| 307 | hint: resolve(msg("Branch is up to date. No action needed.")), |
| 308 | }; |
| 309 | } |
| 310 | |
| 311 | export function getGitActionDisabledReason( |
| 312 | input: { |
| 313 | item: GitActionMenuItem; |
| 314 | gitStatus: VcsStatusResult | null; |
| 315 | isBusy: boolean; |
| 316 | hasOriginRemote: boolean; |
| 317 | }, |
| 318 | resolve: MFunctionType = mFallback, |
| 319 | ): string | null { |
| 320 | const { item, gitStatus, isBusy, hasOriginRemote } = input; |
| 321 | if (!item.disabled) return null; |
| 322 | if (isBusy) return resolve(msg("Git action in progress.")); |
| 323 | if (!gitStatus) return resolve(msg("Git status is unavailable.")); |
| 324 | |
| 325 | const hasBranch = gitStatus.refName !== null; |
| 326 | const hasChanges = gitStatus.hasWorkingTreeChanges; |
| 327 | const hasOpenPr = gitStatus.pr?.state === "open"; |
| 328 | const isAhead = gitStatus.aheadCount > 0; |
| 329 | const isBehind = gitStatus.behindCount > 0; |
| 330 | const terminology = changeRequestTerminologyFor(gitStatus); |
| 331 | |
| 332 | if (item.id === "commit") { |
| 333 | if (!hasChanges) { |
| 334 | return resolve(msg("Worktree is clean. Make changes before committing.")); |
| 335 | } |
| 336 | return resolve(msg("Commit is currently unavailable.")); |
| 337 | } |
| 338 | |
| 339 | if (item.id === "push") { |
| 340 | if (!hasBranch) { |
| 341 | return resolve(msg("Detached HEAD: checkout a branch before pushing.")); |
| 342 | } |
| 343 | if (hasChanges) { |
| 344 | return resolve(msg("Commit or stash local changes before pushing.")); |
| 345 | } |
| 346 | if (isBehind) { |
| 347 | return resolve(msg("Branch is behind upstream. Pull/rebase before pushing.")); |
| 348 | } |
| 349 | if (!gitStatus.hasUpstream && !hasOriginRemote) { |
| 350 | return resolve(msg('Add an "origin" remote before pushing.')); |
| 351 | } |
| 352 | if (!isAhead) { |
| 353 | return resolve(msg("No local commits to push.")); |
| 354 | } |
| 355 | return resolve(msg("Push is currently unavailable.")); |
| 356 | } |
| 357 | |
| 358 | if (hasOpenPr) { |
| 359 | return resolve( |
| 360 | msg("View {shortLabel} is currently unavailable.", { shortLabel: terminology.shortLabel }), |
| 361 | ); |
| 362 | } |
| 363 | if (!hasBranch) { |
| 364 | return resolve( |
| 365 | msg( |
| 366 | "{kind, select, pr {Detached HEAD: checkout a branch before creating a PR.} mr {Detached HEAD: checkout a branch before creating an MR.} generic {Detached HEAD: checkout a branch before creating a change request.} other {Detached HEAD: checkout a branch before creating {changeRequest}.}}", |
| 367 | { kind: changeRequestKind(terminology), changeRequest: terminology.shortLabelWithArticle }, |
| 368 | ), |
| 369 | ); |
| 370 | } |
| 371 | if (!gitStatus.hasUpstream && !hasOriginRemote) { |
| 372 | return resolve( |
| 373 | msg( |
| 374 | '{kind, select, pr {Add an "origin" remote before creating a PR.} mr {Add an "origin" remote before creating an MR.} generic {Add an "origin" remote before creating a change request.} other {Add an "origin" remote before creating {changeRequest}.}}', |
| 375 | { kind: changeRequestKind(terminology), changeRequest: terminology.shortLabelWithArticle }, |
| 376 | ), |
| 377 | ); |
| 378 | } |
| 379 | if ((gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) <= 0) { |
| 380 | return resolve( |
| 381 | msg( |
| 382 | "{kind, select, pr {No local commits to include in a PR.} mr {No local commits to include in an MR.} generic {No local commits to include in a change request.} other {No local commits to include in {changeRequest}.}}", |
| 383 | { kind: changeRequestKind(terminology), changeRequest: terminology.shortLabelWithArticle }, |
| 384 | ), |
| 385 | ); |
| 386 | } |
| 387 | if (isBehind) { |
| 388 | return resolve( |
| 389 | msg( |
| 390 | "{kind, select, pr {Branch is behind upstream. Pull/rebase before creating a PR.} mr {Branch is behind upstream. Pull/rebase before creating an MR.} generic {Branch is behind upstream. Pull/rebase before creating a change request.} other {Branch is behind upstream. Pull/rebase before creating {changeRequest}.}}", |
| 391 | { kind: changeRequestKind(terminology), changeRequest: terminology.shortLabelWithArticle }, |
| 392 | ), |
| 393 | ); |
| 394 | } |
| 395 | return resolve( |
| 396 | msg("Create {shortLabel} is currently unavailable.", { shortLabel: terminology.shortLabel }), |
| 397 | ); |
| 398 | } |
| 399 | |
| 400 | /** Picks the whole sentence for the provider's term; English keeps `shortLabelWithArticle`. */ |
| 401 | function changeRequestKind( |
| 402 | terminology: ChangeRequestTerminology, |
| 403 | ): "pr" | "mr" | "generic" | "other" { |
| 404 | switch (terminology.shortLabelWithArticle) { |
| 405 | case "a PR": |
| 406 | return "pr"; |
| 407 | case "an MR": |
| 408 | return "mr"; |
| 409 | case "a change request": |
| 410 | return "generic"; |
| 411 | default: |
| 412 | return "other"; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | export function requiresDefaultBranchConfirmation( |
| 417 | action: GitStackedAction, |
| 418 | isDefaultBranch: boolean, |
| 419 | ): boolean { |
| 420 | if (!isDefaultBranch) return false; |
| 421 | return ( |
| 422 | action === "push" || |
| 423 | action === "create_pr" || |
| 424 | action === "commit_push" || |
| 425 | action === "commit_push_pr" |
| 426 | ); |
| 427 | } |
| 428 | |
| 429 | export function resolveDefaultBranchActionDialogCopy( |
| 430 | input: { |
| 431 | action: DefaultBranchConfirmableAction; |
| 432 | branchName: string; |
| 433 | includesCommit: boolean; |
| 434 | terminology: ChangeRequestTerminology; |
| 435 | }, |
| 436 | resolve: MFunctionType = mFallback, |
| 437 | ): DefaultBranchActionDialogCopy { |
| 438 | const branchLabel = input.branchName; |
| 439 | const { shortLabel, shortLabelWithArticle } = input.terminology; |
| 440 | |
| 441 | if (input.action === "push" || input.action === "commit_push") { |
| 442 | if (input.includesCommit) { |
| 443 | return { |
| 444 | title: resolve(msg("Commit & push to default branch?")), |
| 445 | description: resolve( |
| 446 | msg( |
| 447 | 'This action will commit and push changes on "{branch}". You can continue on this branch or create a feature branch and run the same action there.', |
| 448 | { branch: branchLabel }, |
| 449 | ), |
| 450 | ), |
| 451 | continueLabel: resolve(msg("Commit & push to {branch}", { branch: branchLabel })), |
| 452 | }; |
| 453 | } |
| 454 | return { |
| 455 | title: resolve(msg("Push to default branch?")), |
| 456 | description: resolve( |
| 457 | msg( |
| 458 | 'This action will push local commits on "{branch}". You can continue on this branch or create a feature branch and run the same action there.', |
| 459 | { branch: branchLabel }, |
| 460 | ), |
| 461 | ), |
| 462 | continueLabel: resolve(msg("Push to {branch}", { branch: branchLabel })), |
| 463 | }; |
| 464 | } |
| 465 | |
| 466 | if (input.includesCommit) { |
| 467 | return { |
| 468 | title: resolve( |
| 469 | msg("Commit, push & create {shortLabel} from default branch?", { shortLabel }), |
| 470 | ), |
| 471 | description: resolve( |
| 472 | msg( |
| 473 | '{kind, select, pr {This action will commit, push, and create a PR on "{branch}". You can continue on this branch or create a feature branch and run the same action there.} mr {This action will commit, push, and create an MR on "{branch}". You can continue on this branch or create a feature branch and run the same action there.} generic {This action will commit, push, and create a change request on "{branch}". You can continue on this branch or create a feature branch and run the same action there.} other {This action will commit, push, and create {changeRequest} on "{branch}". You can continue on this branch or create a feature branch and run the same action there.}}', |
| 474 | { |
| 475 | kind: changeRequestKind(input.terminology), |
| 476 | changeRequest: shortLabelWithArticle, |
| 477 | branch: branchLabel, |
| 478 | }, |
| 479 | ), |
| 480 | ), |
| 481 | continueLabel: resolve(msg("Commit, push & create {shortLabel}", { shortLabel })), |
| 482 | }; |
| 483 | } |
| 484 | return { |
| 485 | title: resolve(msg("Push & create {shortLabel} from default branch?", { shortLabel })), |
| 486 | description: resolve( |
| 487 | msg( |
| 488 | '{kind, select, pr {This action will push local commits and create a PR on "{branch}". You can continue on this branch or create a feature branch and run the same action there.} mr {This action will push local commits and create an MR on "{branch}". You can continue on this branch or create a feature branch and run the same action there.} generic {This action will push local commits and create a change request on "{branch}". You can continue on this branch or create a feature branch and run the same action there.} other {This action will push local commits and create {changeRequest} on "{branch}". You can continue on this branch or create a feature branch and run the same action there.}}', |
| 489 | { |
| 490 | kind: changeRequestKind(input.terminology), |
| 491 | changeRequest: shortLabelWithArticle, |
| 492 | branch: branchLabel, |
| 493 | }, |
| 494 | ), |
| 495 | ), |
| 496 | continueLabel: resolve(msg("Push & create {shortLabel}", { shortLabel })), |
| 497 | }; |
| 498 | } |