package main import ( "bufio" "fmt" "io" "net/http" "os" "path/filepath" "strings" "time" ) type PendingSync struct { Files map[string]File `json:"files"` } func (s *Server) createDedicated(w http.ResponseWriter, req WorkspaceRequest) { if req.Git == nil || !gitObjectID.MatchString(req.Git.Head) { http.Error(w, "专用工作区需要有效Git提交", 400) return } path, e := s.validateWorkspace(req.WorkspacePath) if e != nil { http.Error(w, e.Error(), 400) return } if pr := s.projects[req.ProjectID]; pr != nil && pr.Dedicated { if !sameAccessValue(pr.WorkspacePath, path) { http.Error(w, "工程绑定目录不匹配", 409) return } jsonOut(w, map[string]string{"id": req.ProjectID, "workspacePath": path, "buildWorkspace": s.work(req.ProjectID)}) return } p := id() pr := &Project{WorkspacePath: path, Dedicated: true, Files: map[string]File{}} if previous := s.projects[req.ProjectID]; previous != nil && !previous.Dedicated && sameAccessValue(previous.WorkspacePath, path) { pr.LegacyProject = req.ProjectID } s.projects[p] = pr work := s.work(p) if e = os.MkdirAll(work, 0700); e == nil { args := []string{"init", "-q"} if len(req.Git.Head) == 64 { args = append(args, "--object-format=sha256") } _, e = gitCommand(work, nil, args...) } if e == nil { e = s.seedDedicated(p, path) } if e == nil { var cfg BuildEnvironment cfg, e = s.buildEnvironment() if e == nil { e = s.prepareWorkspaceFiles(p, cfg) } } if e == nil { e = s.save(p) } if e != nil { delete(s.projects, p) http.Error(w, e.Error(), 400) return } jsonOut(w, map[string]string{"id": p, "workspacePath": path, "buildWorkspace": work}) } // 只读初始化普通源码,凭据及缓存继续遵循现有排除规则;不改写原仓库索引。 func (s *Server) seedDedicated(p, source string) error { if _, e := os.Stat(filepath.Join(source, ".git")); os.IsNotExist(e) { return nil } files, e := sourceInventory(source) if e != nil { return e } for path := range files { dest := filepath.Join(s.work(p), filepath.FromSlash(path)) if e = os.MkdirAll(filepath.Dir(dest), 0700); e != nil { return e } input, err := os.Open(filepath.Join(source, filepath.FromSlash(path))) if err != nil { return err } info, err := input.Stat() if err != nil || !info.Mode().IsRegular() { input.Close() return fmt.Errorf("初始化来源不是普通文件:%s", path) } output, err := os.OpenFile(dest, os.O_CREATE|os.O_EXCL|os.O_WRONLY, info.Mode().Perm()) if err != nil { input.Close() return err } _, err = io.Copy(output, input) input.Close() closed := output.Close() if err == nil { err = closed } if err != nil { return err } value, err := comparisonFile(dest) if err != nil { return err } s.projects[p].Files[path] = value } return nil } func (s *Server) checkDedicated(p string, local *GitSnapshot) error { pr := s.projects[p] if local == nil || !gitObjectID.MatchString(local.Head) { return fmt.Errorf("专用工作区需要有效的本机 Git 提交") } if pr.Git != nil && pr.Pending == nil { head, err := gitCommand(s.work(p), nil, "rev-parse", "HEAD") if err != nil || strings.TrimSpace(string(head)) != pr.Git.Head { return fmt.Errorf("专用工作区 Git 基线被外部修改,请先检查") } if pr.Git.Branch != "" { branch, err := gitCommand(s.work(p), nil, "symbolic-ref", "--short", "-q", "HEAD") if err != nil || strings.TrimSpace(string(branch)) != pr.Git.Branch { return fmt.Errorf("专用工作区Git分支被外部修改") } } if _, err := gitCommand(s.work(p), nil, "diff-index", "--cached", "--quiet", "--no-renames", pr.Git.Head, "--"); err != nil { return fmt.Errorf("专用工作区 Git 索引被外部修改,请先处理暂存内容") } } actual, e := s.projectInventory(p) if e != nil { return e } for path, f := range actual { before, old := pr.Files[path] if old && (before.Hash == f.Hash || before.TextHash != "" && func() bool { current, e := comparisonFile(filepath.Join(s.work(p), filepath.FromSlash(path))) return e == nil && current.TextHash == before.TextHash }()) { continue } if pr.Pending != nil && pr.Pending.Files[path].Hash == f.Hash { continue } return fmt.Errorf("专用编译工作区存在额外修改:%s;请先检查该文件,服务不会覆盖人工修改", path) } for path := range pr.Files { if _, exists := actual[path]; !exists { if pr.Pending != nil { continue } return fmt.Errorf("专用编译工作区文件被删除:%s", path) } } return nil } func (s *Server) importBundle(w http.ResponseWriter, r *http.Request, p string) { pr := s.projects[p] if !pr.Dedicated { http.Error(w, "请使用专用编译工作区", 400) return } if pr.Busy { http.Error(w, "workspace busy", 409) return } head := r.URL.Query().Get("head") if !gitObjectID.MatchString(head) { http.Error(w, "invalid Git commit", 400) return } if e := s.checkDedicated(p, &GitSnapshot{Head: head}); e != nil { http.Error(w, e.Error(), 409) return } file, e := os.CreateTemp(filepath.Join(s.root, "projects", p), "bundle-*") if e != nil { http.Error(w, e.Error(), 400) return } defer os.Remove(file.Name()) _, e = io.Copy(file, http.MaxBytesReader(w, r.Body, 1<<30)) closeErr := file.Close() if e == nil { e = closeErr } if e == nil { _, e = gitCommandFor(s.work(p), nil, 5*time.Minute, "bundle", "verify", file.Name()) } if e == nil { header, openErr := os.Open(file.Name()) if openErr != nil { e = openErr } else { scanner := bufio.NewScanner(io.LimitReader(header, 1<<20)) scanner.Buffer(make([]byte, 4096), 1<<20) filtered := false for scanner.Scan() { line := scanner.Text() if line == "" { break } if line == "@filter=blob:none" { filtered = true } } if !filtered { e = fmt.Errorf("只接受blob:none过滤bundle,禁止历史文件内容绕过源码排除规则") } header.Close() } } if e == nil { var refs []byte refs, e = gitCommand(s.work(p), nil, "bundle", "list-heads", file.Name(), "HEAD") fields := strings.Fields(string(refs)) if e == nil && (len(fields) != 2 || fields[0] != head || fields[1] != "HEAD") { e = fmt.Errorf("bundle HEAD 与请求提交不一致") } } if e == nil { _, e = gitCommandFor(s.work(p), nil, 5*time.Minute, "bundle", "unbundle", file.Name()) } if e != nil { http.Error(w, "Git bundle 导入失败或缺少前置提交,请重试完整 bundle:"+e.Error(), 400) return } jsonOut(w, map[string]string{"head": head}) } func (s *Server) advanceDedicated(p string, g *GitSnapshot) error { if g == nil || !gitObjectID.MatchString(g.Head) { return fmt.Errorf("本机提交无效") } kind, e := gitCommand(s.work(p), nil, "cat-file", "-t", g.Head) if e != nil || strings.TrimSpace(string(kind)) != "commit" { return fmt.Errorf("请先上传本机提交的 Git bundle") } if _, e = gitCommand(s.work(p), nil, "read-tree", g.Head); e != nil { return e } if g.Branch != "" { ref := "refs/heads/" + g.Branch if _, e = gitCommand(s.work(p), nil, "check-ref-format", ref); e != nil { return fmt.Errorf("本机Git分支名无效") } if _, e = gitCommand(s.work(p), nil, "update-ref", ref, g.Head); e != nil { return e } _, e = gitCommand(s.work(p), nil, "symbolic-ref", "HEAD", ref) } else { _, e = gitCommand(s.work(p), nil, "update-ref", "--no-deref", "HEAD", g.Head) } return e }