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 | GitRunStackedActionResult, |
| 3 | GitStackedAction, |
| 4 | PullRequestChangeBreakdown, |
| 5 | ReviewDiffPreviewSource, |
| 6 | VcsStatusResult, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { summarizeChangedFiles } from "@t3tools/shared/changedFileKind"; |
| 9 | import { isTemporaryWorktreeBranch } from "@t3tools/shared/git"; |
| 10 | import { msg } from "gt-react"; |
| 11 | import { m, type MessageResolver } from "../i18n/messages"; |
| 12 | import { changeRequestTermKind } from "./pullRequest/changeRequestTerm"; |
| 13 | import type { ChangeRequestTerminology } from "@t3tools/client-runtime/source-control-clients"; |
| 14 | import { |
| 15 | changeRequestTerminologyFor, |
| 16 | type DefaultBranchActionDialogCopy, |
| 17 | type DefaultBranchConfirmableAction, |
| 18 | type GitActionIconName, |
| 19 | type GitDialogAction, |
| 20 | vcsActionLabelText, |
| 21 | } from "@t3tools/client-runtime/state/vcs"; |
| 22 | |
| 23 | export { |
| 24 | requiresDefaultBranchConfirmation, |
| 25 | type DefaultBranchConfirmableAction, |
| 26 | type GitActionIconName, |
| 27 | } from "@t3tools/client-runtime/state/vcs"; |
| 28 | |
| 29 | export interface GitActionMenuItem { |
| 30 | id: "commit" | "push" | "pr"; |
| 31 | label: string; |
| 32 | disabled: boolean; |
| 33 | icon: GitActionIconName; |
| 34 | kind: "open_dialog"; |
| 35 | dialogAction?: GitDialogAction; |
| 36 | } |
| 37 | |
| 38 | export interface GitQuickAction { |
| 39 | label: string; |
| 40 | disabled: boolean; |
| 41 | kind: "run_action" | "run_pull" | "open_publish" | "show_hint"; |
| 42 | action?: GitStackedAction; |
| 43 | hint?: string; |
| 44 | } |
| 45 | |
| 46 | export interface GitActionProgressPresentation { |
| 47 | readonly status: string; |
| 48 | readonly output: string | null; |
| 49 | readonly startedAtMs: number | null; |
| 50 | } |
| 51 | |
| 52 | export interface GitActionResultToastTiming { |
| 53 | readonly timeout: 0; |
| 54 | readonly dismissAfterVisibleMs: number | null; |
| 55 | } |
| 56 | |
| 57 | export const GIT_ACTION_SUCCESS_VISIBLE_MS = 10_000; |
| 58 | |
| 59 | export function resolveGitActionResultToastTiming( |
| 60 | type: "error" | "success", |
| 61 | ): GitActionResultToastTiming { |
| 62 | return { |
| 63 | timeout: 0, |
| 64 | dismissAfterVisibleMs: type === "success" ? GIT_ACTION_SUCCESS_VISIBLE_MS : null, |
| 65 | }; |
| 66 | } |
| 67 | |
| 68 | export function resolveGitActionProgressPresentation( |
| 69 | input: { |
| 70 | readonly isRunning: boolean; |
| 71 | readonly operation: string | null; |
| 72 | readonly currentLabel: string | null; |
| 73 | readonly hookName?: string | null; |
| 74 | readonly lastOutputLine: string | null; |
| 75 | readonly phaseStartedAtMs: number | null; |
| 76 | readonly hookStartedAtMs: number | null; |
| 77 | }, |
| 78 | resolve: MessageResolver = m, |
| 79 | ): GitActionProgressPresentation | null { |
| 80 | if ( |
| 81 | !input.isRunning || |
| 82 | (input.operation !== "run_change_request" && input.operation !== "pull") |
| 83 | ) { |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | const currentLabel = input.currentLabel?.trim(); |
| 88 | const output = input.lastOutputLine?.trim(); |
| 89 | const isPull = input.operation === "pull"; |
| 90 | return { |
| 91 | status: |
| 92 | currentLabel && currentLabel !== "Running source control action" |
| 93 | ? (vcsActionLabelText({ currentLabel, hookName: input.hookName ?? null }, resolve) ?? |
| 94 | currentLabel) |
| 95 | : isPull |
| 96 | ? resolve(msg("Pulling latest changes...")) |
| 97 | : resolve(msg("Starting source control action...")), |
| 98 | output: !isPull && output ? output : null, |
| 99 | startedAtMs: isPull |
| 100 | ? input.phaseStartedAtMs |
| 101 | : (input.hookStartedAtMs ?? input.phaseStartedAtMs), |
| 102 | }; |
| 103 | } |
| 104 | |
| 105 | export function formatGitActionElapsed( |
| 106 | startedAtMs: number | null, |
| 107 | nowMs: number, |
| 108 | resolve: MessageResolver = m, |
| 109 | ): string | null { |
| 110 | if (startedAtMs === null) { |
| 111 | return null; |
| 112 | } |
| 113 | |
| 114 | const elapsedSeconds = Math.max(0, Math.floor((nowMs - startedAtMs) / 1_000)); |
| 115 | if (elapsedSeconds < 60) { |
| 116 | return resolve(msg("{seconds}s", { seconds: elapsedSeconds })); |
| 117 | } |
| 118 | |
| 119 | const minutes = Math.floor(elapsedSeconds / 60); |
| 120 | const seconds = elapsedSeconds % 60; |
| 121 | return resolve(msg("{minutes}m {seconds}s", { minutes, seconds })); |
| 122 | } |
| 123 | |
| 124 | export function buildMenuItems( |
| 125 | gitStatus: VcsStatusResult | null, |
| 126 | isBusy: boolean, |
| 127 | hasPrimaryRemote = true, |
| 128 | resolve: MessageResolver = m, |
| 129 | ): GitActionMenuItem[] { |
| 130 | if (!gitStatus) return []; |
| 131 | const terminology = changeRequestTerminologyFor(gitStatus); |
| 132 | |
| 133 | const hasBranch = gitStatus.refName !== null; |
| 134 | const hasChanges = gitStatus.hasWorkingTreeChanges; |
| 135 | const hasOpenPr = gitStatus.pr?.state === "open"; |
| 136 | const isBehind = gitStatus.behindCount > 0; |
| 137 | const hasDefaultBranchDelta = (gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) > 0; |
| 138 | const canPushWithoutUpstream = hasPrimaryRemote && !gitStatus.hasUpstream; |
| 139 | const canCommit = !isBusy && hasChanges; |
| 140 | const canPush = |
| 141 | !isBusy && |
| 142 | hasBranch && |
| 143 | !isBehind && |
| 144 | gitStatus.aheadCount > 0 && |
| 145 | (gitStatus.hasUpstream || canPushWithoutUpstream); |
| 146 | const canCreatePr = |
| 147 | !isBusy && |
| 148 | hasBranch && |
| 149 | !hasOpenPr && |
| 150 | hasDefaultBranchDelta && |
| 151 | !isBehind && |
| 152 | (gitStatus.hasUpstream || canPushWithoutUpstream); |
| 153 | |
| 154 | const commitItem: GitActionMenuItem = { |
| 155 | id: "commit", |
| 156 | label: resolve(msg("Commit", { $context: "verb, git action" })), |
| 157 | disabled: !canCommit, |
| 158 | icon: "commit", |
| 159 | kind: "open_dialog", |
| 160 | dialogAction: "commit", |
| 161 | }; |
| 162 | |
| 163 | if (!hasPrimaryRemote) { |
| 164 | return [commitItem]; |
| 165 | } |
| 166 | |
| 167 | const pushItem: GitActionMenuItem = { |
| 168 | id: "push", |
| 169 | label: resolve(msg("Push", { $context: "verb, git action" })), |
| 170 | disabled: !canPush, |
| 171 | icon: "push", |
| 172 | kind: "open_dialog", |
| 173 | dialogAction: "push", |
| 174 | }; |
| 175 | |
| 176 | if (hasOpenPr) { |
| 177 | return [commitItem, pushItem]; |
| 178 | } |
| 179 | |
| 180 | return [ |
| 181 | commitItem, |
| 182 | pushItem, |
| 183 | { |
| 184 | id: "pr", |
| 185 | label: resolve( |
| 186 | msg("Create {term, select, mr {MR} cr {change request} other {PR}}", { |
| 187 | term: changeRequestTermKind(terminology), |
| 188 | }), |
| 189 | ), |
| 190 | disabled: !canCreatePr, |
| 191 | icon: "pr", |
| 192 | kind: "open_dialog", |
| 193 | dialogAction: "create_pr", |
| 194 | }, |
| 195 | ]; |
| 196 | } |
| 197 | |
| 198 | export function getMenuActionDisabledReason( |
| 199 | { |
| 200 | item, |
| 201 | gitStatus, |
| 202 | isBusy, |
| 203 | hasPrimaryRemote, |
| 204 | }: { |
| 205 | item: GitActionMenuItem; |
| 206 | gitStatus: VcsStatusResult | null; |
| 207 | isBusy: boolean; |
| 208 | hasPrimaryRemote: boolean; |
| 209 | }, |
| 210 | resolve: MessageResolver = m, |
| 211 | ): string | null { |
| 212 | if (!item.disabled) return null; |
| 213 | if (isBusy) return resolve(msg("Git action in progress.")); |
| 214 | if (!gitStatus) return resolve(msg("Git status is unavailable.")); |
| 215 | |
| 216 | const hasBranch = gitStatus.refName !== null; |
| 217 | const hasChanges = gitStatus.hasWorkingTreeChanges; |
| 218 | const isAhead = gitStatus.aheadCount > 0; |
| 219 | const isBehind = gitStatus.behindCount > 0; |
| 220 | const terminology = changeRequestTerminologyFor(gitStatus); |
| 221 | |
| 222 | if (item.id === "commit") { |
| 223 | if (!hasChanges) { |
| 224 | return resolve(msg("Worktree is clean. Make changes before committing.")); |
| 225 | } |
| 226 | return resolve(msg("Commit is currently unavailable.")); |
| 227 | } |
| 228 | |
| 229 | if (item.id === "push") { |
| 230 | if (!hasBranch) { |
| 231 | return resolve(msg("Detached HEAD: check out a branch before pushing.")); |
| 232 | } |
| 233 | if (isBehind) { |
| 234 | return resolve(msg("Branch is behind upstream. Pull/rebase before pushing.")); |
| 235 | } |
| 236 | if (!gitStatus.hasUpstream && !hasPrimaryRemote) { |
| 237 | return resolve(msg('Add an "origin" remote before pushing.')); |
| 238 | } |
| 239 | if (!isAhead) { |
| 240 | return resolve(msg("No local commits to push.")); |
| 241 | } |
| 242 | return resolve(msg("Push is currently unavailable.")); |
| 243 | } |
| 244 | |
| 245 | if (!hasBranch) { |
| 246 | return resolve( |
| 247 | msg( |
| 248 | "Detached HEAD: check out a branch before creating a {term, select, mr {merge request} cr {change request} other {pull request}}.", |
| 249 | { term: changeRequestTermKind(terminology) }, |
| 250 | ), |
| 251 | ); |
| 252 | } |
| 253 | if (!gitStatus.hasUpstream && !hasPrimaryRemote) { |
| 254 | return resolve( |
| 255 | msg( |
| 256 | 'Add an "origin" remote before creating a {term, select, mr {merge request} cr {change request} other {pull request}}.', |
| 257 | { term: changeRequestTermKind(terminology) }, |
| 258 | ), |
| 259 | ); |
| 260 | } |
| 261 | if ((gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) <= 0) { |
| 262 | return resolve( |
| 263 | msg( |
| 264 | "No local commits to include in a {term, select, mr {merge request} cr {change request} other {pull request}}.", |
| 265 | { term: changeRequestTermKind(terminology) }, |
| 266 | ), |
| 267 | ); |
| 268 | } |
| 269 | if (isBehind) { |
| 270 | return resolve( |
| 271 | msg( |
| 272 | "Branch is behind upstream. Pull/rebase before creating a {term, select, mr {merge request} cr {change request} other {pull request}}.", |
| 273 | { term: changeRequestTermKind(terminology) }, |
| 274 | ), |
| 275 | ); |
| 276 | } |
| 277 | return resolve( |
| 278 | msg( |
| 279 | "Create {term, select, mr {merge request} cr {change request} other {pull request}} is currently unavailable.", |
| 280 | { term: changeRequestTermKind(terminology) }, |
| 281 | ), |
| 282 | ); |
| 283 | } |
| 284 | |
| 285 | export function hasUsableGitMenuContent(input: { |
| 286 | readonly menuItems: readonly GitActionMenuItem[]; |
| 287 | readonly canWriteSourceControl: boolean; |
| 288 | readonly canPublishRepository: boolean; |
| 289 | readonly hasNotice: boolean; |
| 290 | }): boolean { |
| 291 | return ( |
| 292 | input.canPublishRepository || |
| 293 | input.hasNotice || |
| 294 | (input.canWriteSourceControl && input.menuItems.some((item) => !item.disabled)) |
| 295 | ); |
| 296 | } |
| 297 | |
| 298 | export function resolveQuickAction( |
| 299 | gitStatus: VcsStatusResult | null, |
| 300 | isBusy: boolean, |
| 301 | isDefaultRef = false, |
| 302 | hasPrimaryRemote = true, |
| 303 | resolve: MessageResolver = m, |
| 304 | ): GitQuickAction { |
| 305 | if (isBusy) { |
| 306 | return { |
| 307 | label: resolve(msg("Commit", { $context: "verb, git action" })), |
| 308 | disabled: true, |
| 309 | kind: "show_hint", |
| 310 | hint: resolve(msg("Git action in progress.")), |
| 311 | }; |
| 312 | } |
| 313 | |
| 314 | if (!gitStatus) { |
| 315 | return { |
| 316 | label: resolve(msg("Commit", { $context: "verb, git action" })), |
| 317 | disabled: true, |
| 318 | kind: "show_hint", |
| 319 | hint: resolve(msg("Git status is unavailable.")), |
| 320 | }; |
| 321 | } |
| 322 | |
| 323 | const hasBranch = gitStatus.refName !== null; |
| 324 | const hasChanges = gitStatus.hasWorkingTreeChanges; |
| 325 | const hasOpenPr = gitStatus.pr?.state === "open"; |
| 326 | const isAhead = gitStatus.aheadCount > 0; |
| 327 | const hasDefaultBranchDelta = (gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) > 0; |
| 328 | const isBehind = gitStatus.behindCount > 0; |
| 329 | const isDiverged = isAhead && isBehind; |
| 330 | const terminology = changeRequestTerminologyFor(gitStatus); |
| 331 | |
| 332 | if (!hasBranch) { |
| 333 | return { |
| 334 | label: resolve(msg("Commit", { $context: "verb, git action" })), |
| 335 | disabled: true, |
| 336 | kind: "show_hint", |
| 337 | hint: resolve( |
| 338 | msg( |
| 339 | "Create and checkout a ref before pushing or opening a {term, select, mr {merge request} cr {change request} other {pull request}}.", |
| 340 | { term: changeRequestTermKind(terminology) }, |
| 341 | ), |
| 342 | ), |
| 343 | }; |
| 344 | } |
| 345 | |
| 346 | if (hasChanges) { |
| 347 | if (isBehind || (!gitStatus.hasUpstream && !hasPrimaryRemote)) { |
| 348 | return { |
| 349 | label: resolve(msg("Commit", { $context: "verb, git action" })), |
| 350 | disabled: false, |
| 351 | kind: "run_action", |
| 352 | action: "commit", |
| 353 | }; |
| 354 | } |
| 355 | if (hasOpenPr || isDefaultRef) { |
| 356 | return { |
| 357 | label: resolve(msg("Commit & push")), |
| 358 | disabled: false, |
| 359 | kind: "run_action", |
| 360 | action: "commit_push", |
| 361 | }; |
| 362 | } |
| 363 | return { |
| 364 | label: resolve( |
| 365 | msg("Commit, push & {term, select, mr {MR} cr {change request} other {PR}}", { |
| 366 | term: changeRequestTermKind(terminology), |
| 367 | }), |
| 368 | ), |
| 369 | disabled: false, |
| 370 | kind: "run_action", |
| 371 | action: "commit_push_pr", |
| 372 | }; |
| 373 | } |
| 374 | |
| 375 | if (!gitStatus.hasUpstream) { |
| 376 | if (!hasPrimaryRemote) { |
| 377 | return { |
| 378 | label: resolve(msg("Publish repository")), |
| 379 | disabled: false, |
| 380 | kind: "open_publish", |
| 381 | }; |
| 382 | } |
| 383 | if (!isAhead) { |
| 384 | if (hasOpenPr) { |
| 385 | return { |
| 386 | label: resolve(msg("Commit", { $context: "verb, git action" })), |
| 387 | disabled: true, |
| 388 | kind: "show_hint", |
| 389 | hint: resolve(msg("Branch is up to date. No action needed.")), |
| 390 | }; |
| 391 | } |
| 392 | return { |
| 393 | label: resolve(msg("Push", { $context: "verb, git action" })), |
| 394 | disabled: true, |
| 395 | kind: "show_hint", |
| 396 | hint: resolve(msg("No local commits to push.")), |
| 397 | }; |
| 398 | } |
| 399 | if (hasOpenPr || isDefaultRef) { |
| 400 | return { |
| 401 | label: resolve(msg("Push", { $context: "verb, git action" })), |
| 402 | disabled: false, |
| 403 | kind: "run_action", |
| 404 | action: isDefaultRef ? "commit_push" : "push", |
| 405 | }; |
| 406 | } |
| 407 | return { |
| 408 | label: resolve( |
| 409 | msg("Push & create {term, select, mr {MR} cr {change request} other {PR}}", { |
| 410 | term: changeRequestTermKind(terminology), |
| 411 | }), |
| 412 | ), |
| 413 | disabled: false, |
| 414 | kind: "run_action", |
| 415 | action: "create_pr", |
| 416 | }; |
| 417 | } |
| 418 | |
| 419 | if (isDiverged) { |
| 420 | return { |
| 421 | label: resolve(msg("Sync ref")), |
| 422 | disabled: true, |
| 423 | kind: "show_hint", |
| 424 | hint: resolve(msg("Branch has diverged from upstream. Rebase/merge first.")), |
| 425 | }; |
| 426 | } |
| 427 | |
| 428 | if (isBehind) { |
| 429 | return { |
| 430 | label: resolve(msg("Pull")), |
| 431 | disabled: false, |
| 432 | kind: "run_pull", |
| 433 | }; |
| 434 | } |
| 435 | |
| 436 | if (isAhead) { |
| 437 | if (hasOpenPr || isDefaultRef) { |
| 438 | return { |
| 439 | label: resolve(msg("Push", { $context: "verb, git action" })), |
| 440 | disabled: false, |
| 441 | kind: "run_action", |
| 442 | action: isDefaultRef ? "commit_push" : "push", |
| 443 | }; |
| 444 | } |
| 445 | return { |
| 446 | label: resolve( |
| 447 | msg("Push & create {term, select, mr {MR} cr {change request} other {PR}}", { |
| 448 | term: changeRequestTermKind(terminology), |
| 449 | }), |
| 450 | ), |
| 451 | disabled: false, |
| 452 | kind: "run_action", |
| 453 | action: "create_pr", |
| 454 | }; |
| 455 | } |
| 456 | |
| 457 | if (hasOpenPr && gitStatus.hasUpstream) { |
| 458 | return { |
| 459 | label: resolve(msg("Commit", { $context: "verb, git action" })), |
| 460 | disabled: true, |
| 461 | kind: "show_hint", |
| 462 | hint: resolve(msg("Branch is up to date. No action needed.")), |
| 463 | }; |
| 464 | } |
| 465 | |
| 466 | if (hasDefaultBranchDelta && !isDefaultRef) { |
| 467 | return { |
| 468 | label: resolve( |
| 469 | msg("Create {term, select, mr {MR} cr {change request} other {PR}}", { |
| 470 | term: changeRequestTermKind(terminology), |
| 471 | }), |
| 472 | ), |
| 473 | disabled: false, |
| 474 | kind: "run_action", |
| 475 | action: "create_pr", |
| 476 | }; |
| 477 | } |
| 478 | |
| 479 | return { |
| 480 | label: resolve(msg("Commit", { $context: "verb, git action" })), |
| 481 | disabled: true, |
| 482 | kind: "show_hint", |
| 483 | hint: resolve(msg("Branch is up to date. No action needed.")), |
| 484 | }; |
| 485 | } |
| 486 | |
| 487 | export function resolveDefaultBranchActionDialogCopy( |
| 488 | input: { |
| 489 | action: DefaultBranchConfirmableAction; |
| 490 | branchName: string; |
| 491 | includesCommit: boolean; |
| 492 | terminology: ChangeRequestTerminology; |
| 493 | }, |
| 494 | resolve: MessageResolver = m, |
| 495 | ): DefaultBranchActionDialogCopy { |
| 496 | const branch = input.branchName; |
| 497 | const term = changeRequestTermKind(input.terminology); |
| 498 | |
| 499 | if (input.action === "push" || input.action === "commit_push") { |
| 500 | if (input.includesCommit) { |
| 501 | return { |
| 502 | title: resolve(msg("Commit & push to default ref?")), |
| 503 | description: resolve( |
| 504 | msg( |
| 505 | 'This action will commit and push changes on "{branch}". You can continue on this ref or create a feature ref and run the same action there.', |
| 506 | { branch }, |
| 507 | ), |
| 508 | ), |
| 509 | continueLabel: resolve(msg("Commit & push to {branch}", { branch })), |
| 510 | }; |
| 511 | } |
| 512 | return { |
| 513 | title: resolve(msg("Push to default ref?")), |
| 514 | description: resolve( |
| 515 | msg( |
| 516 | 'This action will push local commits on "{branch}". You can continue on this ref or create a feature ref and run the same action there.', |
| 517 | { branch }, |
| 518 | ), |
| 519 | ), |
| 520 | continueLabel: resolve(msg("Push to {branch}", { branch })), |
| 521 | }; |
| 522 | } |
| 523 | |
| 524 | if (input.includesCommit) { |
| 525 | return { |
| 526 | title: resolve( |
| 527 | msg( |
| 528 | "Commit, push & create {term, select, mr {MR} cr {change request} other {PR}} from default ref?", |
| 529 | { term }, |
| 530 | ), |
| 531 | ), |
| 532 | description: resolve( |
| 533 | msg( |
| 534 | 'This action will commit, push, and create a {term, select, mr {merge request} cr {change request} other {pull request}} on "{branch}". You can continue on this ref or create a feature ref and run the same action there.', |
| 535 | { term, branch }, |
| 536 | ), |
| 537 | ), |
| 538 | continueLabel: resolve( |
| 539 | msg("Commit, push & create {term, select, mr {MR} cr {change request} other {PR}}", { |
| 540 | term, |
| 541 | }), |
| 542 | ), |
| 543 | }; |
| 544 | } |
| 545 | return { |
| 546 | title: resolve( |
| 547 | msg( |
| 548 | "Push & create {term, select, mr {MR} cr {change request} other {PR}} from default ref?", |
| 549 | { term }, |
| 550 | ), |
| 551 | ), |
| 552 | description: resolve( |
| 553 | msg( |
| 554 | 'This action will push local commits and create a {term, select, mr {merge request} cr {change request} other {pull request}} on "{branch}". You can continue on this ref or create a feature ref and run the same action there.', |
| 555 | { term, branch }, |
| 556 | ), |
| 557 | ), |
| 558 | continueLabel: resolve( |
| 559 | msg("Push & create {term, select, mr {MR} cr {change request} other {PR}}", { term }), |
| 560 | ), |
| 561 | }; |
| 562 | } |
| 563 | |
| 564 | /** The server names a feature branch from the commit it makes, so the action must commit changes. */ |
| 565 | export function resolveFeatureBranchUnavailableReason( |
| 566 | input: { |
| 567 | action: DefaultBranchConfirmableAction; |
| 568 | branchName: string; |
| 569 | hasWorkingTreeChanges: boolean; |
| 570 | }, |
| 571 | resolve: MessageResolver = m, |
| 572 | ): string | null { |
| 573 | if (!input.hasWorkingTreeChanges) { |
| 574 | return resolve( |
| 575 | msg( |
| 576 | "There are no changes to commit, so a feature branch can't be named. Create a branch yourself or continue on {branch}.", |
| 577 | { branch: input.branchName }, |
| 578 | ), |
| 579 | ); |
| 580 | } |
| 581 | if (input.action === "push" || input.action === "create_pr") { |
| 582 | return resolve( |
| 583 | msg( |
| 584 | "This action doesn't commit your changes, so a feature branch can't be named. Create a branch yourself or continue on {branch}.", |
| 585 | { branch: input.branchName }, |
| 586 | ), |
| 587 | ); |
| 588 | } |
| 589 | return null; |
| 590 | } |
| 591 | |
| 592 | export function resolveThreadBranchUpdate( |
| 593 | result: GitRunStackedActionResult, |
| 594 | ): { branch: string } | null { |
| 595 | if (result.branch.status !== "created" || !result.branch.name) { |
| 596 | return null; |
| 597 | } |
| 598 | |
| 599 | return { |
| 600 | branch: result.branch.name, |
| 601 | }; |
| 602 | } |
| 603 | |
| 604 | export function resolveThreadBranchMetadataPatch( |
| 605 | branch: string | null, |
| 606 | expectedBranch: string | null, |
| 607 | ): { |
| 608 | branch: string | null; |
| 609 | expectedBranch: string | null; |
| 610 | } { |
| 611 | return { branch, expectedBranch }; |
| 612 | } |
| 613 | |
| 614 | export function resolveLiveThreadBranchUpdate(input: { |
| 615 | threadBranch: string | null; |
| 616 | gitStatus: VcsStatusResult | null; |
| 617 | }): { branch: string | null } | null { |
| 618 | if (!input.gitStatus) { |
| 619 | return null; |
| 620 | } |
| 621 | |
| 622 | if (input.gitStatus.refName === null && input.threadBranch !== null) { |
| 623 | return null; |
| 624 | } |
| 625 | |
| 626 | if (input.threadBranch === input.gitStatus.refName) { |
| 627 | return null; |
| 628 | } |
| 629 | |
| 630 | if ( |
| 631 | input.threadBranch !== null && |
| 632 | input.gitStatus.refName !== null && |
| 633 | !isTemporaryWorktreeBranch(input.threadBranch) && |
| 634 | isTemporaryWorktreeBranch(input.gitStatus.refName) |
| 635 | ) { |
| 636 | return null; |
| 637 | } |
| 638 | |
| 639 | return { |
| 640 | branch: input.gitStatus.refName, |
| 641 | }; |
| 642 | } |
| 643 | |
| 644 | export type ChangesLineTotals = { |
| 645 | readonly insertions: number; |
| 646 | readonly deletions: number; |
| 647 | /** Ref the counts compare against; `null` means they are uncommitted changes against HEAD. */ |
| 648 | readonly baseRef: string | null; |
| 649 | /** `null` when the selected totals carry no per-file stats. */ |
| 650 | readonly breakdown: PullRequestChangeBreakdown; |
| 651 | }; |
| 652 | |
| 653 | // `branchChanges ?? workingTree` treats `{0,0}` as present, so a zero branch total hid a dirty |
| 654 | // working tree and the Changes row read +0 while Diff still showed the work. |
| 655 | // The Diff panel's branch comparison wins when it has file stats, so a remembered base or |
| 656 | // hidden whitespace reads the same in the row as in the panel. |
| 657 | export function resolveChangesTotals( |
| 658 | status: VcsStatusResult | null | undefined, |
| 659 | branchPreview?: ReviewDiffPreviewSource | null, |
| 660 | ): ChangesLineTotals | null { |
| 661 | if (branchPreview?.files) { |
| 662 | let insertions = 0; |
| 663 | let deletions = 0; |
| 664 | for (const file of branchPreview.files) { |
| 665 | insertions += file.additions; |
| 666 | deletions += file.deletions; |
| 667 | } |
| 668 | return { |
| 669 | insertions, |
| 670 | deletions, |
| 671 | baseRef: branchPreview.baseRef, |
| 672 | breakdown: summarizeChangedFiles(branchPreview.files), |
| 673 | }; |
| 674 | } |
| 675 | if (!status) return null; |
| 676 | const branch = status.branchChanges; |
| 677 | const tree = { ...status.workingTree, baseRef: null }; |
| 678 | const selected = |
| 679 | branch && (branch.insertions > 0 || branch.deletions > 0) |
| 680 | ? branch |
| 681 | : tree.insertions > 0 || tree.deletions > 0 |
| 682 | ? tree |
| 683 | : (branch ?? tree); |
| 684 | return { |
| 685 | insertions: selected.insertions, |
| 686 | deletions: selected.deletions, |
| 687 | baseRef: selected.baseRef, |
| 688 | breakdown: |
| 689 | selected === tree |
| 690 | ? summarizeChangedFiles( |
| 691 | tree.files.map((file) => ({ |
| 692 | path: file.path, |
| 693 | additions: file.insertions, |
| 694 | deletions: file.deletions, |
| 695 | })), |
| 696 | ) |
| 697 | : null, |
| 698 | }; |
| 699 | } |