How to use PathFilter correctly in go-git?

87 views Asked by At

I am trying to analyze the git commit log by using go-git. But I encountered some problems and failed to solve them after many attempts.

In WSL(Ubuntu18.04), the command root@(XXXXXXX/XXXXXX):.../www/docs/docs/gmaster# git log intro.mdx will return 3 results like following text:

commit (commit hash)
Author: XXXXXX
Date:   Mon Oct 24 10:51:44 2022 +0800

    Feature/issues942

commit (commit hash)
Author: XXXXXX
Date:   Fri Oct 21 15:04:33 2022 +0800

    Feature/issues942

commit (commit hash)
Author: XXXXXX
Date:   Wed Sep 21 16:23:09 2022 +0800

    Merge branch 'master' into XXXX/XXXX

After reading the documents of go-git, I try to use the LogOptions/PathFilter to implement the git log intro.mdx. Then I write code in windows/Goland like following:

r, _ := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
        URL: "D:\\project\\gmaster",
    })
    ref, _ := r.Head()
    cIter, _ := r.Log(&git.LogOptions{
        From:  ref.Hash(),
        Order: 4,
        PathFilter: func(s string) bool {
            s = "D:\\project\\gmaster\\www\\docs\\docs\\gmaster\\intro.mdx"
            _, err := os.Stat(s)
            if err != nil {
                return false
            }
            return true
        },
    })
    err := cIter.ForEach(func(c *object.Commit) error {
        fmt.Println(c)
        return nil
    })
    if err != nil {
        return
    }

Finally, the foregoing code will return the whole commit log instead of 3 of the log. It's really confused me.

Thanks in advance for your help.

0

There are 0 answers