IndexTime fields are private, how to I create IndexTime{}? #304

Closed
opened 2016-04-22 14:12:14 -05:00 by chr4 · 5 comments
chr4 commented 2016-04-22 14:12:14 -05:00 (Migrated from github.com)

I'm trying to add an Entry to an Index:

        // Create git blob from file
        blobOid, err := repo.CreateBlobFromBuffer(file.Content)
        if err != nil {
            return err
        }

        // Create git index entry
        indexEntry := &git.IndexEntry{
            // Since v23 IndexTime{} is required. Which is not possible to create from here, as all
            // fields are private. See https://github.com/libgit2/git2go/blob/master/index.go
            // 
            // Ctime: git.IndexTime{}.
            // Mtime: git.IndexTime{},
            Mode:  git.FilemodeBlob,
            Uid:   uint32(os.Getuid()),
            Gid:   uint32(os.Getgid()),
            Size:  uint32(len(file.Content)),
            Id:    blobOid,
            Path:  file.Name,
        }

        // Add index to git tree
        if err := index.Add(indexEntry); err != nil {
            return err
        }

Unfortunately, since git2go v23, I cannot set Ctime and Mtime, ad they are requiring an IndexTime element, which I cannot fill with e.g. the current time, as the fields of IndexTime are private.
Is this an bug, or am I supposed to create the IndexEntry in another way? I couldn't find a NewIndexEntry() function or any of the sort.

I'd apprechiate a hint!

I'm trying to add an Entry to an Index: ``` golang // Create git blob from file blobOid, err := repo.CreateBlobFromBuffer(file.Content) if err != nil { return err } // Create git index entry indexEntry := &git.IndexEntry{ // Since v23 IndexTime{} is required. Which is not possible to create from here, as all // fields are private. See https://github.com/libgit2/git2go/blob/master/index.go // // Ctime: git.IndexTime{}. // Mtime: git.IndexTime{}, Mode: git.FilemodeBlob, Uid: uint32(os.Getuid()), Gid: uint32(os.Getgid()), Size: uint32(len(file.Content)), Id: blobOid, Path: file.Name, } // Add index to git tree if err := index.Add(indexEntry); err != nil { return err } ``` Unfortunately, since git2go v23, I cannot set `Ctime` and `Mtime`, ad they are requiring an IndexTime element, which I cannot fill with e.g. the current time, as the fields of `IndexTime` are private. Is this an bug, or am I supposed to create the `IndexEntry` in another way? I couldn't find a `NewIndexEntry()` function or any of the sort. I'd apprechiate a hint!
carlosmn commented 2016-04-23 09:22:50 -05:00 (Migrated from github.com)

That looks like an oversight, they should be public members. However since you're creating a blob from memory rather than from a file on the filesystem, those fields should be left to zero to avoid confusing the status/diff checks.

That looks like an oversight, they should be public members. However since you're creating a blob from memory rather than from a file on the filesystem, those fields should be left to zero to avoid confusing the status/diff checks.
carlosmn commented 2019-01-04 07:15:14 -06:00 (Migrated from github.com)

I don't see that these fields were ever private, but either way they are public so this was fixed quite some time ago.

I don't see that these fields were ever private, but either way they are public so this was fixed quite some time ago.
carlosmn commented 2019-01-04 07:18:05 -06:00 (Migrated from github.com)

It looks like I misread and the issue is the fields of IndexTime itself so it's actually not resolved.

It looks like I misread and the issue is the fields of `IndexTime` itself so it's actually not resolved.
mbfr commented 2021-02-15 04:57:39 -06:00 (Migrated from github.com)

I've run into this issue as well, if I make a PR to fix this should I just add a method to read the index time and a function to create a new index time? Changing the fields to be public would be a breaking API change and might be a bit annoying for other users.

I've run into this issue as well, if I make a PR to fix this should I just add a method to read the index time and a function to create a new index time? Changing the fields to be public would be a breaking API change and might be a bit annoying for other users.
lhchavez commented 2021-02-15 07:39:52 -06:00 (Migrated from github.com)

per the Go module compatibility doc , adding fields and removing unexported fields is safe. since we're also only adding comparable fields, we should be okay for making 'em all public with no need for methods!

you can try using https://pkg.go.dev/golang.org/x/exp/cmd/gorelease to double check that this is the case.

per [the Go module compatibility doc](https://blog.golang.org/module-compatibility#TOC_5.) , adding fields and removing unexported fields is safe. since we're also only adding comparable fields, we should be okay for making 'em all public with no need for methods! you can try using https://pkg.go.dev/golang.org/x/exp/cmd/gorelease to double check that this is the case.
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: jcarr/git2go#304
No description provided.