【架构优化】合并 gitwork/workbranch.go 中 push/pull 重复的非工作分支严格校验为 verifyOffWorkBranch

Aperta
#451 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@fx-kurumi ci sta già lavorando.

Dal 13/11/2026.

  • #452 di @fx-kurumi — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
15/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
git, go
Ambito
tooling

Direzione di ricerca

The duplicated block sits at the top of pushWorkBranch and pullWorkBranch in internal/gitwork/workbranch.go. Read how onWorkBranch and fetchExistingWorkBranch are used there, then check whether PR #452 already covers this refactor before starting. Done means a package-private verifyOffWorkBranch with unchanged error text and order, plus unit tests in internal/gitwork/ passing.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

现状问题

internal/gitwork/workbranch.go 中 pushWorkBranch 与 pullWorkBranch 开头有一段逐字相同的分支:

if !onWorkBranch(ctx, workspace, branch) {
    if requireExisting {
        if _, err := fetchExistingWorkBranch(ctx, workspace, branch); err != nil {
            return "", err
        }
        if current, _ := C(ctx, workspace, "branch", "--show-current"); strings.TrimSpace(current) != branch {
            return "", fmt.Errorf("工作区未检出已有工作分支 %s", branch)
        }
    }
    return "", nil
}

即“不在工作分支上(或有 merge/rebase 进行中)时跳过,但严格模式仍要校验远端分支存在且已检出”。两份副本必须同步维护,任何一边改动报错文字或校验顺序都会造成 push/pull 行为分叉。

重构方案

抽出包内私有函数:

// verifyOffWorkBranch 处理工作区不在工作分支上的情况:
// 非严格模式直接放行;严格模式仍校验远端分支存在且当前已检出它。
func verifyOffWorkBranch(ctx context.Context, workspace, branch string, requireExisting bool) error

pushWorkBranch / pullWorkBranch 改为:

if !onWorkBranch(ctx, workspace, branch) {
    // (push 侧保留原有注释)
    return "", verifyOffWorkBranch(ctx, workspace, branch, requireExisting)
}

报错文字、校验顺序、返回值完全不变。

影响范围

  • 仅 internal/gitwork/workbranch.go 内部两个私有函数;
  • 对外 PushWorkBranch / PushExistingWorkBranch / PullWorkBranch / PullExistingWorkBranch 签名与行为不变,调用方无需改动。

验证方式

  • 新增 verifyOffWorkBranch 单元测试:非严格模式放行;严格模式远端分支不存在报错;严格模式远端存在但未检出报「工作区未检出已有工作分支」;已检出时通过;
  • gofmt、go vet ./internal/gitwork/、go test ./internal/gitwork/ ./internal/runner/ 通过。
Lingua principale
Go
Stelle
141
Fork
28
Merge medio
1g 15h
PR unite (30g)
113

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Tencent/LoopForge

Tutte le issue di Tencent/LoopForge

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.