| 1 | package forge |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/base64" |
| 6 | "io" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 13 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 14 | ) |
| 15 | |
| 16 | func (p *Publisher) git(ctx context.Context, dir string, cred *Credential, args ...string) (string, error) { |
| 17 | protocol := "https" |
| 18 | if p.localRemote != nil { |
| 19 | protocol = "file" |
| 20 | if p.localReceivePack != "" && len(args) > 0 && args[0] == "push" { |
| 21 | args = append([]string{"push", "--receive-pack=" + p.localReceivePack}, args[1:]...) |
| 22 | } |
| 23 | } |
| 24 | args = append([]string{"-c", "protocol." + protocol + ".allow=always", "-c", "http.followRedirects=false", "-c", "maintenance.auto=false", "-c", "gc.auto=0"}, args...) |
| 25 | cmd := exec.CommandContext(ctx, p.cfg.GitPath, args...) |
| 26 | cmd.Dir = dir |
| 27 | env := workspace.GitEnvironment() |
| 28 | env["HOME"] = "/nonexistent" |
| 29 | env["GIT_CEILING_DIRECTORIES"] = filepath.Dir(dir) |
| 30 | if cred != nil && p.localRemote == nil { |
| 31 | env["GIT_CONFIG_COUNT"] = "10" |
| 32 | env["GIT_CONFIG_KEY_9"] = "http.extraHeader" |
| 33 | env["GIT_CONFIG_VALUE_9"] = "Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte("x:"+cred.GitPassword)) |
| 34 | } |
| 35 | for k, v := range env { |
| 36 | cmd.Env = append(cmd.Env, k+"="+v) |
| 37 | } |
| 38 | cmd.Stderr = io.Discard |
| 39 | b, e := cmd.Output() |
| 40 | if e != nil { |
| 41 | return "", team.ErrUncertain |
| 42 | } |
| 43 | return strings.TrimSpace(string(b)), nil |
| 44 | } |
| 45 | func (p *Publisher) stage(ctx context.Context, dir string, c team.GitCandidate) (string, error) { |
| 46 | exported := filepath.Join(dir, "export") |
| 47 | if e := p.cfg.Export(ctx, c, exported); e != nil { |
| 48 | return "", e |
| 49 | } |
| 50 | repo := filepath.Join(dir, "git") |
| 51 | format := "sha1" |
| 52 | if len(c.Commit) == 64 { |
| 53 | format = "sha256" |
| 54 | } |
| 55 | if _, e := p.git(ctx, dir, nil, "init", "--bare", "--template=", "--object-format="+format, repo); e != nil { |
| 56 | return "", e |
| 57 | } |
| 58 | source := filepath.Join(exported, ".git", "objects") |
| 59 | actual, e := filepath.EvalSymlinks(source) |
| 60 | if e != nil || actual != source { |
| 61 | return "", team.ErrEvidence |
| 62 | } |
| 63 | e = filepath.WalkDir(source, func(path string, d os.DirEntry, err error) error { |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | rel, e := filepath.Rel(source, path) |
| 68 | if e != nil { |
| 69 | return e |
| 70 | } |
| 71 | if rel == "." { |
| 72 | return nil |
| 73 | } |
| 74 | if d.Type()&os.ModeSymlink != 0 { |
| 75 | return team.ErrEvidence |
| 76 | } |
| 77 | if d.IsDir() { |
| 78 | if rel != "info" && rel != "pack" && !(len(rel) == 2 && strings.Trim(rel, "0123456789abcdef") == "") { |
| 79 | return team.ErrEvidence |
| 80 | } |
| 81 | return os.MkdirAll(filepath.Join(repo, "objects", rel), 0700) |
| 82 | } |
| 83 | parts := strings.Split(rel, string(filepath.Separator)) |
| 84 | if !d.Type().IsRegular() || len(parts) != 2 { |
| 85 | return team.ErrEvidence |
| 86 | } |
| 87 | valid := len(parts[0]) == 2 && object.MatchString(parts[0]+parts[1]) |
| 88 | if parts[0] == "pack" { |
| 89 | name := parts[1] |
| 90 | ext := filepath.Ext(name) |
| 91 | hash := strings.TrimSuffix(strings.TrimPrefix(name, "pack-"), ext) |
| 92 | valid = strings.HasPrefix(name, "pack-") && object.MatchString(hash) && (ext == ".pack" || ext == ".idx" || ext == ".rev") |
| 93 | } |
| 94 | if !valid { |
| 95 | return team.ErrEvidence |
| 96 | } |
| 97 | in, e := os.Open(path) |
| 98 | if e != nil { |
| 99 | return e |
| 100 | } |
| 101 | defer in.Close() |
| 102 | out, e := os.OpenFile(filepath.Join(repo, "objects", rel), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600) |
| 103 | if e != nil { |
| 104 | return e |
| 105 | } |
| 106 | _, e = io.Copy(out, in) |
| 107 | ce := out.Close() |
| 108 | if e != nil { |
| 109 | return e |
| 110 | } |
| 111 | return ce |
| 112 | }) |
| 113 | if e != nil { |
| 114 | return "", team.ErrEvidence |
| 115 | } |
| 116 | if e = p.verifyObjects(ctx, repo, c); e != nil { |
| 117 | return "", e |
| 118 | } |
| 119 | return repo, nil |
| 120 | } |
| 121 | func (p *Publisher) verifyObjects(ctx context.Context, repo string, c team.GitCandidate) error { |
| 122 | id, e := p.git(ctx, repo, nil, "rev-parse", "--verify", c.Commit+"^{commit}") |
| 123 | if e != nil || id != c.Commit { |
| 124 | return team.ErrEvidence |
| 125 | } |
| 126 | tree, e := p.git(ctx, repo, nil, "rev-parse", "--verify", c.Commit+"^{tree}") |
| 127 | if e != nil || tree != c.Tree { |
| 128 | return team.ErrEvidence |
| 129 | } |
| 130 | if _, e = p.git(ctx, repo, nil, "merge-base", "--is-ancestor", c.BaseCommit, c.Commit); e != nil { |
| 131 | return team.ErrEvidence |
| 132 | } |
| 133 | if _, e = p.git(ctx, repo, nil, "fsck", "--strict", "--no-reflogs", c.Commit); e != nil { |
| 134 | return team.ErrEvidence |
| 135 | } |
| 136 | return nil |
| 137 | } |
| 138 | |