go-git报告文件为“未跟踪",而应将其修改为"Untracked" [英] go-git reports a file as Untracked while it should be Unmodified

查看:74
本文介绍了go-git报告文件为“未跟踪",而应将其修改为"Untracked"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 go-git 这是我尝试运行的代码:

This is the code I am attempting to run:

    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

即我正在尝试使用完整路径以及我要检查的文件的相关(至回购)路径.

i.e. I am attempting to use both the full as also the relevant (to the repo) path to the file I want to check.

在两种情况下,打印出的内容如下:

In both cases what is printed out is the following:

`?`
`?`

根据

According to the documentation however, this corresponds to an Untracked file.

已正确提交并检入了特定文件.

The specific file is properly committed and checked in.

为什么我输入了错误的状态码?

Why am I getting the wrong status code?

推荐答案

解决方法:

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()

NOTE: s.IsUntracked() called before s.File() on purpose

这篇关于go-git报告文件为“未跟踪",而应将其修改为"Untracked"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆