1

I have following code

    commitIter, err := r.Log(&git.LogOptions{From: commit.Hash})
    CheckIfError(err)

    err = commitIter.ForEach(func(c *object.Commit) error {
        parent, err := c.Parent(0)

        if err != nil {
            return nil
        }

        patch, err := c.Patch(parent)
        CheckIfError(err)

        fmt.Println(patch)

        return nil
    })

it iterates over all commits and generates patch for each of them, using reference to parent. But first commit doesn't have parent then I can't generate patch. Is there any workaround like passing empty commit?

I was searching and I found open pull request on archived go-git repository.

kernelc
  • 98
  • 7
  • I've no answer but I'd try to call `c.Patch(commit.GetCommit(plumbing.ZeroHash))` – kostix Mar 31 '21 at 13:32
  • `object.GetCommit` requires two arguments, first is `storer.EncodedObjectStorer`. What I should pass? – kernelc Mar 31 '21 at 13:47
  • The examples in `go-git` suggest this is "a repository". I mean, some object which is returned by a call which "opens" a repository. – kostix Mar 31 '21 at 15:16
  • @kostix I can't solve this :( I created [new issue](https://github.com/go-git/go-git/issues/281) – kernelc Apr 03 '21 at 13:57

1 Answers1

0

Using object trees instead of commits allows you to generate a patch for the first commit.

For example:

    commitTree, err := commit.Tree()
    if err == nil {
        parentTree := &object.Tree{}
        if commit.NumParents() != 0 {
            parent, err := commit.Parents().Next()
            if err == nil {
                parentTree, err = parent.Tree()
                if err == nil {
                    parentTree.Patch(commitTree)
                }
            }
        } else {
            parentTree.Patch(commitTree)
        }
    }
Ayman
  • 11
  • 2