1

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?

Walter
  • 1,290
  • 2
  • 21
  • 46
  • 2
    I don't have an answer, but midnight January 1 1970 is the zero value of [Unix time](https://en.wikipedia.org/wiki/Unix_time), a widely used measure of time in software. – jkr Mar 26 '20 at 02:46

1 Answers1

2

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(),
    },
})
jkr
  • 17,119
  • 2
  • 42
  • 68