Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

git rev-list command #757

Open
Open
@Dal-Papa

Description

@Dal-Papa

I wanted to reproduce the behavior of git rev-list HEAD my_file with the help of go-git.

As this feature was not here out of the box, I tried my best to do a clean implementation for it with the library and it looks like this :

cIter, err := g.Log(&git.LogOptions{
    From: fromHash,
})

history := make(map[string]CommitSlice) 
// Where CommitSlice is just an improved version of HashSlice which can sort by Commit date

err = cIter.ForEach(func(c *object.Commit) error {
    sts, err := c.Stats()
    if err != nil {
        if err != plumbing.ErrObjectNotFound {
            return err
        }
        files, err := c.Files()
        CheckIfErr(err)
        files.ForEach(func(f *object.File) error {
            history[f.Name] = append(history[f.Name], c.Hash)
            return nil
        })
    }
    for _, f := range sts {
        if c.NumParents() < 2 { // To get the actual commit and not the merge commit
            history[f.Name] = append(history[f.Name], c.Hash)
        }
    }
    return nil
})

// CommitSlice implementation of the sort.Interface striped from this example.

I'd like to share this with the community and potentially have it in the go-git library so I'm wondering what would be the best place for this to live in.

Thanks !

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions