diff options
| author | Carlos Martín Nieto <[email protected]> | 2015-03-11 17:05:16 +0100 |
|---|---|---|
| committer | Carlos Martín Nieto <[email protected]> | 2015-03-11 17:05:16 +0100 |
| commit | d300110b8582cd44511efc26a7a0ce04d409f8ce (patch) | |
| tree | 58652052be2848d110ff3c894517452577f44a95 /signature.go | |
| parent | 755721e68453c5d6de0681789dbe3307744fc9c4 (diff) | |
| parent | 9eae50f29a69fa47cecf3cf4dfb032414cf27a50 (diff) | |
Merge pull request #178 from schani/master
Fixes and improvements
Diffstat (limited to 'signature.go')
| -rw-r--r-- | signature.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/signature.go b/signature.go new file mode 100644 index 0000000..0518387 --- /dev/null +++ b/signature.go @@ -0,0 +1,73 @@ +package git + +/* +#include <git2.h> +*/ +import "C" +import ( + "runtime" + "time" + "unsafe" +) + +type Signature struct { + Name string + Email string + When time.Time +} + +func newSignatureFromC(sig *C.git_signature) *Signature { + // git stores minutes, go wants seconds + loc := time.FixedZone("", int(sig.when.offset)*60) + return &Signature{ + C.GoString(sig.name), + C.GoString(sig.email), + time.Unix(int64(sig.when.time), 0).In(loc), + } +} + +// the offset in mintes, which is what git wants +func (v *Signature) Offset() int { + _, offset := v.When.Zone() + return offset / 60 +} + +func (sig *Signature) toC() (*C.git_signature, error) { + if sig == nil { + return nil, nil + } + + var out *C.git_signature + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + name := C.CString(sig.Name) + defer C.free(unsafe.Pointer(name)) + + email := C.CString(sig.Email) + defer C.free(unsafe.Pointer(email)) + + ret := C.git_signature_new(&out, name, email, C.git_time_t(sig.When.Unix()), C.int(sig.Offset())) + if ret < 0 { + return nil, MakeGitError(ret) + } + + return out, nil +} + +func (repo *Repository) DefaultSignature() (*Signature, error) { + var out *C.git_signature + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + cErr := C.git_signature_default(&out, repo.ptr) + if cErr < 0 { + return nil, MakeGitError(cErr) + } + + defer C.git_signature_free(out) + + return newSignatureFromC(out), nil +} |
