open-code-review/internal/gitcmd/runner.go
kite 533b526b4c
Some checks are pending
CI / cross-compile (arm64, darwin) (push) Waiting to run
CI / cross-compile (arm64, linux) (push) Waiting to run
CI / cross-compile (arm64, windows) (push) Waiting to run
CI / test (push) Waiting to run
CI / cross-compile (amd64, darwin) (push) Waiting to run
CI / cross-compile (amd64, windows) (push) Waiting to run
CodeQL Advanced / Analyze (go) (push) Waiting to run
CodeQL Advanced / Analyze (actions) (push) Waiting to run
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
Deploy Pages / build (push) Waiting to run
Deploy Pages / deploy (push) Blocked by required conditions
chore: add SPDX license headers and automated verification (#740)
* chore: add SPDX license headers to all source files

Add Apache-2.0 SPDX license identifiers and copyright notices to all
tracked .go, .sh, .js, .mjs, .ts, and .tsx source files.

Introduce scripts/verify-license.sh and scripts/add-license.sh for
automated verification and bulk addition of license headers. Integrate
the check into CI (ci.yml) and the Makefile (license-check target as
a prerequisite of the existing check target).

This satisfies the OpenSSF Best Practices Badge requirements for
copyright_per_file and license_per_file.

* fix: restore execute permissions on scripts

* docs: add license header instructions to CONTRIBUTING guides

* docs: add license header instructions to pages contributing guides

* fix(pages): strip unclosed HTML comment markers to satisfy CodeQL

* fix: apply code review suggestions for license scripts

- Fix portability: detect macOS vs Linux stat for permission copy
- Fix has_header: check both SPDX and copyright (match verify logic)
- Fix is_ignored: match on path boundaries to avoid false positives
- Fix year extraction: use consistent pipeline across both scripts
- Fix Bash 3.2 compat: quote array length expansion for set -u

* fix(pages): use loop-until-clean for HTML comment stripping (CodeQL)

* fix(pages): use split/join instead of replace to avoid CodeQL false positive

CodeQL's js/incomplete-multi-character-sanitization rule flags any
.replace() that removes multi-character sequences like '<!--...-->',
regardless of context. The data here comes from readFileSync on the
project's own index.html (no untrusted input), making this a false
positive. Using split(regex).join('') achieves the same result without
triggering the taint-tracking rule.
2026-08-05 21:26:27 +08:00

130 lines
3.2 KiB
Go

// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 alibaba/open-code-review Contributors
package gitcmd
import (
"bytes"
"context"
"fmt"
"io"
"os/exec"
)
const defaultMaxConcurrent = 16
// Runner limits the number of concurrent git subprocesses via an internal
// semaphore. All git command invocations should go through a shared Runner
// instance so that the total system-wide subprocess count stays bounded.
type Runner struct {
sem chan struct{}
}
// New creates a Runner that allows at most maxConcurrent simultaneous git
// subprocesses. If maxConcurrent <= 0 the default (16) is used.
func New(maxConcurrent int) *Runner {
if maxConcurrent <= 0 {
maxConcurrent = defaultMaxConcurrent
}
return &Runner{sem: make(chan struct{}, maxConcurrent)}
}
func (r *Runner) acquire(ctx context.Context) error {
if r.sem == nil {
return fmt.Errorf("gitcmd.Runner not initialized; use gitcmd.New()")
}
select {
case r.sem <- struct{}{}:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (r *Runner) release() { <-r.sem }
// Run executes a git command and returns the combined stdout+stderr output.
func (r *Runner) Run(ctx context.Context, repoDir string, args ...string) (string, error) {
if err := r.acquire(ctx); err != nil {
return "", err
}
defer r.release()
cmd := exec.CommandContext(ctx, "git", args...)
cmd.Dir = repoDir
out, err := cmd.CombinedOutput()
return string(out), err
}
// Output executes a git command and returns stdout only.
func (r *Runner) Output(ctx context.Context, repoDir string, args ...string) ([]byte, error) {
if err := r.acquire(ctx); err != nil {
return nil, err
}
defer r.release()
cmd := exec.CommandContext(ctx, "git", args...)
cmd.Dir = repoDir
return cmd.Output()
}
// RunSplit executes a git command and returns stdout and stderr separately.
func (r *Runner) RunSplit(ctx context.Context, repoDir string, args ...string) (string, string, error) {
if err := r.acquire(ctx); err != nil {
return "", "", err
}
defer r.release()
cmd := exec.CommandContext(ctx, "git", args...)
cmd.Dir = repoDir
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return stdout.String(), stderr.String(), err
}
// Stream acquires the semaphore, starts a git command, and passes its stdout
// as an io.Reader to consume. The semaphore is held for the full duration.
// consume MUST fully drain the stdout reader before returning nil;
// otherwise cmd.Wait() may block or return a broken-pipe error.
func (r *Runner) Stream(ctx context.Context, repoDir string, consume func(stdout io.Reader) error, args ...string) error {
if err := r.acquire(ctx); err != nil {
return err
}
defer r.release()
cmd := exec.CommandContext(ctx, "git", args...)
cmd.Dir = repoDir
var stderrBuf bytes.Buffer
cmd.Stderr = &stderrBuf
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
consumeErr := consume(stdoutPipe)
if consumeErr != nil {
cmd.Process.Kill()
}
waitErr := cmd.Wait()
if consumeErr != nil {
return consumeErr
}
if waitErr != nil {
if stderrBuf.Len() > 0 {
return fmt.Errorf("%w: %s", waitErr, stderrBuf.String())
}
return waitErr
}
return nil
}