我正在尝试使用go-git library检查git存储库中特定文件的状态。
这是我试图运行的代码:
repo, err := git.PlainOpen(fullPathToRepo)
if err != nil {
return false, fmt.Errorf("ERROR: Unable to open repository %s\n%s", fullPathToRepo, err)
}
workTree, err := repo.Worktree()
if err != nil {
return false, fmt.Errorf("ERROR: Unable to open worktree for repository %s\n%s", fullPathToRepo, err)
}
workTreeStatus, err := workTree.Status()
if err != nil {
return false, fmt.Errorf("ERROR: Unable to retrieve worktree status for repository %s\n%s", fullPathToRepo, err)
}
fmt.Printf("%q\n", workTreeStatus.File("releases/filename").Worktree)
fmt.Printf("%q\n", workTreeStatus.File("/Users/panteliskaramolegkos/myrepo/filename/releases/faros.yaml").Worktree)
return workTreeStatus.IsClean(), nil即,我试图既使用完整的,也使用到我想要检查的文件的相关(到repo)路径。
在这两种情况下,打印输出的内容如下:
`?`
`?`然而,根据documentation,这对应于一个Untracked文件。
特定文件已正确提交并检入。
为什么我得到了错误的状态代码?
发布于 2021-04-19 14:31:07
解决方法:
var fileStatusMapping = map[git.StatusCode]string{
git.Unmodified: "",
git.Untracked: "Untracked",
git.Modified: "Modified",
git.Added: "Added",
git.Deleted: "Deleted",
git.Renamed: "Renamed",
git.Copied: "Copied",
git.UpdatedButUnmerged: "Updated",
}
func (r *Repo) FileStatus(filename string) (string, string, error) {
w, err := r.worktree()
if err != nil {
return "", "", err
}
s, err := w.Status()
if err != nil {
return "", "", err
}
if s != nil {
var untracked bool
if s.IsUntracked(filename) {
untracked = true
}
fileStatus := s.File(filename)
if !untracked && fileStatus.Staging == git.Untracked &&
fileStatus.Worktree == git.Untracked {
fileStatus.Staging = git.Unmodified
fileStatus.Worktree = git.Unmodified
}
return fileStatusMapping[fileStatus.Staging], fileStatusMapping[fileStatus.Worktree], nil
}
return "", "", nil
}注意:故意在s.File()之前调用s.IsUntracked()
https://stackoverflow.com/questions/62738651
复制相似问题