Files
RemoteCompilation/server/git_checkout.go
T
tongtong 6f9cc7e7ea feat: 增加独立构建工作区、服务端ADB与私有文件清单
支持Git提交导入及未提交源码叠加,APK原名下载和完成通知。新增服务端设备安装与按工程维护的私有文件清单,修复Wrapper预检查、布局及IDE读锁和保存状态问题。更新协议、测试和部署记录;服务端0.7.0,插件0.7.1。
2026-11-27 11:04:58 +08:00

87 lines
2.5 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
type gitEntry struct{ mode, hash string }
type cleanCheckout struct {
work string
head, index, previous map[string]gitEntry
}
var gitObjectID = regexp.MustCompile(`^[0-9a-fA-F]{40}([0-9a-fA-F]{24})?$`)
func gitTree(work, head string) (map[string]gitEntry, error) {
if !gitObjectID.MatchString(head) {
return nil, fmt.Errorf("无法验证 Git 提交标识")
}
data, e := gitCommand(work, nil, "ls-tree", "-r", "-z", head)
if e != nil {
return nil, e
}
result := map[string]gitEntry{}
for _, record := range strings.Split(string(data), "\x00") {
meta, path, ok := strings.Cut(record, "\t")
fields := strings.Fields(meta)
if ok && len(fields) == 3 && fields[1] == "blob" {
result[path] = gitEntry{fields[0], fields[2]}
}
}
return result, nil
}
func inspectCheckout(work string, remote, previous *GitSnapshot) (*cleanCheckout, error) {
if remote == nil {
return nil, nil
}
head, e := gitTree(work, remote.Head)
if e != nil {
return nil, e
}
data, e := gitCommand(work, nil, "ls-files", "--stage", "-z")
if e != nil {
return nil, e
}
c := &cleanCheckout{work: work, head: head, index: map[string]gitEntry{}, previous: map[string]gitEntry{}}
for _, record := range strings.Split(string(data), "\x00") {
meta, path, ok := strings.Cut(record, "\t")
fields := strings.Fields(meta)
if ok && len(fields) == 3 {
if fields[2] == "0" {
c.index[path] = gitEntry{fields[0], fields[1]}
} else {
c.index[path] = gitEntry{mode: "conflict"}
}
}
}
if previous != nil && previous.Head != remote.Head && gitObjectID.MatchString(previous.Head) {
// 旧对象不可用时不放行删除,保持保守保护。
if old, e := gitTree(work, previous.Head); e == nil {
c.previous = old
}
}
return c, nil
}
func (c *cleanCheckout) matches(path string) bool {
if c == nil {
return false
}
head, tracked := c.head[path]
index, indexed := c.index[path]
if !tracked {
_, wasTracked := c.previous[path]
_, e := os.Lstat(filepath.Join(c.work, filepath.FromSlash(path)))
return wasTracked && !indexed && os.IsNotExist(e)
}
if !indexed || index != head || (head.mode != "100644" && head.mode != "100755") {
return false
}
// 实际读取文件并应用 Git 规范化,不能依赖 status 缓存或 assume-unchanged。
hash, e := gitCommand(c.work, nil, "hash-object", "--path="+path, "--", path)
return e == nil && strings.TrimSpace(string(hash)) == head.hash
}