Why are my commits via go-git dated 1970?

301 Views Asked by At

I am committing changes via go-git:

import (
  "github.com/tcnksm/go-gitconfig"
  "github.com/walterjwhite/go-application/libraries/logging"

  "gopkg.in/src-d/go-git.v4"
  "gopkg.in/src-d/go-git.v4/plumbing/object"
)

func stackoverflowCommit(R *git.Repository, W *git.Worktree) {
  username, err := gitconfig.Username()
  logging.Panic(err)

  email, err := gitconfig.Email()
  logging.Panic(err)

  _, err = W.Commit("commit message goes here", &git.CommitOptions{Author: &object.Signature{Name: username, Email: email}})

  logging.Panic(err)
}

In my logs, I see this:

Date:   Thu Jan 1 00:00:00 1970 +0000

Is this expected behavior? I don't see anyway for me to pass in a date. A quick inspection of the source shows the Commit object doesn't have any references to date ...

Is that right, or am I missing something?

1

There are 1 best solutions below

1
On BEST ANSWER

Pass in the current time to object.Signature. The documentation for object.Signature shows that you can provide a time.Time type. This is also demonstrated in an example on GitHub. Be sure to import "time".

_, err = W.Commit("commit message goes here", &git.CommitOptions{
    Author: &object.Signature{
        Name: username, 
        Email: email, 
        When: time.Now(),
    },
})