package forge import ( "context" "encoding/base64" "io" "os" "os/exec" "path/filepath" "strings" "amadan.net/oficina/equipo/internal/equipo/team" "amadan.net/oficina/equipo/internal/runner/workspace" ) func (p *Publisher) git(ctx context.Context, dir string, cred *Credential, args ...string) (string, error) { protocol := "https" if p.localRemote != nil { protocol = "file" if p.localReceivePack != "" && len(args) > 0 && args[0] == "push" { args = append([]string{"push", "--receive-pack=" + p.localReceivePack}, args[1:]...) } } args = append([]string{"-c", "protocol." + protocol + ".allow=always", "-c", "http.followRedirects=false", "-c", "maintenance.auto=false", "-c", "gc.auto=0"}, args...) cmd := exec.CommandContext(ctx, p.cfg.GitPath, args...) cmd.Dir = dir env := workspace.GitEnvironment() env["HOME"] = "/nonexistent" env["GIT_CEILING_DIRECTORIES"] = filepath.Dir(dir) if cred != nil && p.localRemote == nil { env["GIT_CONFIG_COUNT"] = "10" env["GIT_CONFIG_KEY_9"] = "http.extraHeader" env["GIT_CONFIG_VALUE_9"] = "Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte("x:"+cred.GitPassword)) } for k, v := range env { cmd.Env = append(cmd.Env, k+"="+v) } cmd.Stderr = io.Discard b, e := cmd.Output() if e != nil { return "", team.ErrUncertain } return strings.TrimSpace(string(b)), nil } func (p *Publisher) stage(ctx context.Context, dir string, c team.GitCandidate) (string, error) { exported := filepath.Join(dir, "export") if e := p.cfg.Export(ctx, c, exported); e != nil { return "", e } repo := filepath.Join(dir, "git") format := "sha1" if len(c.Commit) == 64 { format = "sha256" } if _, e := p.git(ctx, dir, nil, "init", "--bare", "--template=", "--object-format="+format, repo); e != nil { return "", e } source := filepath.Join(exported, ".git", "objects") actual, e := filepath.EvalSymlinks(source) if e != nil || actual != source { return "", team.ErrEvidence } e = filepath.WalkDir(source, func(path string, d os.DirEntry, err error) error { if err != nil { return err } rel, e := filepath.Rel(source, path) if e != nil { return e } if rel == "." { return nil } if d.Type()&os.ModeSymlink != 0 { return team.ErrEvidence } if d.IsDir() { if rel != "info" && rel != "pack" && !(len(rel) == 2 && strings.Trim(rel, "0123456789abcdef") == "") { return team.ErrEvidence } return os.MkdirAll(filepath.Join(repo, "objects", rel), 0700) } parts := strings.Split(rel, string(filepath.Separator)) if !d.Type().IsRegular() || len(parts) != 2 { return team.ErrEvidence } valid := len(parts[0]) == 2 && object.MatchString(parts[0]+parts[1]) if parts[0] == "pack" { name := parts[1] ext := filepath.Ext(name) hash := strings.TrimSuffix(strings.TrimPrefix(name, "pack-"), ext) valid = strings.HasPrefix(name, "pack-") && object.MatchString(hash) && (ext == ".pack" || ext == ".idx" || ext == ".rev") } if !valid { return team.ErrEvidence } in, e := os.Open(path) if e != nil { return e } defer in.Close() out, e := os.OpenFile(filepath.Join(repo, "objects", rel), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600) if e != nil { return e } _, e = io.Copy(out, in) ce := out.Close() if e != nil { return e } return ce }) if e != nil { return "", team.ErrEvidence } if e = p.verifyObjects(ctx, repo, c); e != nil { return "", e } return repo, nil } func (p *Publisher) verifyObjects(ctx context.Context, repo string, c team.GitCandidate) error { id, e := p.git(ctx, repo, nil, "rev-parse", "--verify", c.Commit+"^{commit}") if e != nil || id != c.Commit { return team.ErrEvidence } tree, e := p.git(ctx, repo, nil, "rev-parse", "--verify", c.Commit+"^{tree}") if e != nil || tree != c.Tree { return team.ErrEvidence } if _, e = p.git(ctx, repo, nil, "merge-base", "--is-ancestor", c.BaseCommit, c.Commit); e != nil { return team.ErrEvidence } if _, e = p.git(ctx, repo, nil, "fsck", "--strict", "--no-reflogs", c.Commit); e != nil { return team.ErrEvidence } return nil }