src-d / go-git

Project has been moved to: https://github.com/go-git/go-git
https://github.com/go-git/go-git
Apache License 2.0
4.91k stars 542 forks source link

Patch: Fetch changes #1102

Closed dmigwi closed 5 years ago

dmigwi commented 5 years ago

How can I be able to implement git log -p --reverse using this tool?

dmigwi commented 5 years ago

I have made progress in implementing git log -p. The pending bit is now how to fetch changes in the first commit made i.e. without a parent commit.

This implementation has been quite helpful.

func (c *Commit) Stats() (FileStats, error) {
    // Get the previous commit.
    ci := c.Parents()
    parentCommit, err := ci.Next()
    if err != nil {
        if err == io.EOF {
            emptyNoder := treeNoder{}
            parentCommit = &Commit{
                Hash: emptyNoder.hash,
                // TreeHash: emptyNoder.parent.Hash,
                s: c.s,
            }
        } else {
            return nil, err
        }
    }

    patch, err := parentCommit.Patch(c)
    if err != nil {
        return nil, err
    }

    return getFileStatsFromFilePatches(patch.FilePatches()), nil
}

Sadly stats for the first commit (without a parent) returns Object not found error.

Also creating a dummy parent commit outside the package is quite difficult because storer.EncodedObjectStorer Commit field is unexported.

So how can I fetch stats/changes in a commit without a parent?

chappjc commented 5 years ago

It's interesting that (*Commit).Stats does not work for the initial commit. I can't work out how to get the patch for the initial commit either.

chappjc commented 5 years ago

@mcuadros Sorry for the direct mention, but it seems like there must be a simple way to get the initial commit patch diff that I'm just not seeing. How would you suggest handling this?

mcuadros commented 5 years ago

This was solved here: https://github.com/src-d/go-git/pull/1115