More diff functionality #629
74
diff.go
|
@ -3,6 +3,7 @@ package git
|
||||||
/*
|
/*
|
||||||
#include <git2.h>
|
#include <git2.h>
|
||||||
|
|
||||||
|
extern void _go_git_apply_init_options(git_apply_options *options);
|
||||||
extern int _go_git_diff_foreach(git_diff *diff, int eachFile, int eachHunk, int eachLine, void *payload);
|
extern int _go_git_diff_foreach(git_diff *diff, int eachFile, int eachHunk, int eachLine, void *payload);
|
||||||
extern void _go_git_setup_diff_notify_callbacks(git_diff_options* opts);
|
extern void _go_git_setup_diff_notify_callbacks(git_diff_options* opts);
|
||||||
extern int _go_git_diff_blobs(git_blob *old, const char *old_path, git_blob *new, const char *new_path, git_diff_options *opts, int eachFile, int eachHunk, int eachLine, void *payload);
|
extern int _go_git_diff_blobs(git_blob *old, const char *old_path, git_blob *new, const char *new_path, git_diff_options *opts, int eachFile, int eachHunk, int eachLine, void *payload);
|
||||||
|
@ -847,3 +848,76 @@ func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string,
|
||||||
|
|
||||||
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ApplyOptions struct {
|
||||||
|
Version uint
|
||||||
|
Flags uint
|
||||||
|
// TODO: there are some more flags, not currently used
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultApplyOptions() (*ApplyOptions, error) {
|
||||||
|
opts := C.git_apply_options{}
|
||||||
|
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
|
C._go_git_apply_init_options(&opts)
|
||||||
|
|
||||||
|
return applyOptionsFromC(&opts), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ApplyOptions) toC() *C.git_apply_options {
|
||||||
|
if a == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := &C.git_apply_options{
|
||||||
|
version: C.uint(a.Version),
|
||||||
|
flags: C.uint(a.Flags),
|
||||||
|
}
|
||||||
|
|
||||||
|
return opts
|
||||||
|
}
|
||||||
|
|
||||||
|
func applyOptionsFromC(opts *C.git_apply_options) *ApplyOptions {
|
||||||
|
return &ApplyOptions{
|
||||||
|
Version: uint(opts.version),
|
||||||
|
Flags: uint(opts.flags),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type GitApplyLocation int
|
||||||
|
|
||||||
|
const (
|
||||||
|
GitApplyLocationWorkdir GitApplyLocation = C.GIT_APPLY_LOCATION_WORKDIR
|
||||||
|
GitApplyLocationIndex GitApplyLocation = C.GIT_APPLY_LOCATION_INDEX
|
||||||
|
GitApplyLocationBoth GitApplyLocation = C.GIT_APPLY_LOCATION_BOTH
|
||||||
|
)
|
||||||
|
|
||||||
|
func (v *Repository) ApplyDiff(diff *Diff, location GitApplyLocation, opts *ApplyOptions) error {
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
|
ecode := C.git_apply(v.ptr, diff.ptr, C.git_apply_location_t(location), opts.toC())
|
||||||
|
runtime.KeepAlive(v)
|
||||||
|
if ecode < 0 {
|
||||||
![]() can this also use the same pattern as the other change?
can this also use the same pattern as the other change?
```go
if gitError, ok := err.(*GitError); ok {
return C.int(gitError.Code)
}
return -1
```
![]() Fixed Fixed
|
|||||||
|
return MakeGitError(ecode)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DiffFromBuffer(buffer []byte, repo *Repository) (*Diff, error) {
|
||||||
|
var diff *C.git_diff
|
||||||
|
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
|
ecode := C.git_diff_from_buffer(&diff, C.CString(string(buffer)), C.size_t(len(buffer)))
|
||||||
![]() Can these be returned to their old place? The reason why they are needed is that when We may need to tweak Can these be returned to their old place? The reason why they are needed is that when [`runtime.LockOsThread()`](https://golang.org/pkg/runtime/#LockOSThread)/`runtime.UnlockOSThread()` combo is called, it asks Go to guarantee that _only_ this code executes on that thread (since Go is completely free to move stuff around when needed, at any point in time). That's the only way in which it can be guaranteed that if `git_apply_options_init` happens to place any error information in the Thread-local storage, `MakeGitError()` below can still find it.
We may need to tweak `script/check-MakeGitError-thread-lock.go` to _also_ complain if these functions don't happen before any cgo calls in the function to make this less error-prone.
![]() Fixed Fixed
|
|||||||
|
if ecode < 0 {
|
||||||
|
return nil, MakeGitError(ecode)
|
||||||
|
}
|
||||||
|
runtime.KeepAlive(diff)
|
||||||
|
|
||||||
|
return newDiffFromC(diff, repo), nil
|
||||||
|
}
|
||||||
|
|
62
diff_test.go
|
@ -236,3 +236,65 @@ func TestDiffBlobs(t *testing.T) {
|
||||||
t.Fatalf("Bad number of lines iterated")
|
t.Fatalf("Bad number of lines iterated")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
![]()
so far, no tests have underscores in their names. ```suggestion
func TestApplyDiffAddfile(t *testing.T) {
```
so far, no tests have underscores in their names.
|
|||||||
|
|
||||||
|
func Test_ApplyDiff_Addfile(t *testing.T) {
|
||||||
|
repo := createTestRepo(t)
|
||||||
|
defer cleanupTestRepo(t, repo)
|
||||||
|
|
||||||
|
seedTestRepo(t, repo)
|
||||||
|
|
||||||
|
addFirstFileCommit, addFileTree := addAndGetTree(t, repo, "file1", `hello`)
|
||||||
|
addSecondFileCommit, addSecondFileTree := addAndGetTree(t, repo, "file2", `hello2`)
|
||||||
|
|
||||||
|
diff, err := repo.DiffTreeToTree(addFileTree, addSecondFileTree, nil)
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
t.Run("check does not apply to current tree because file exists", func(t *testing.T) {
|
||||||
|
err = repo.ResetToCommit(addSecondFileCommit, ResetHard, &CheckoutOpts{})
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
err = repo.ApplyDiff(diff, GitApplyLocationBoth, nil)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expecting applying patch to current repo to fail")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("check apply to correct commit", func(t *testing.T) {
|
||||||
|
err = repo.ResetToCommit(addFirstFileCommit, ResetHard, &CheckoutOpts{})
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
err = repo.ApplyDiff(diff, GitApplyLocationBoth, nil)
|
||||||
|
checkFatal(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("check convert to raw buffer and apply", func(t *testing.T) {
|
||||||
|
err = repo.ResetToCommit(addFirstFileCommit, ResetHard, &CheckoutOpts{})
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
raw, err := diff.ToBuf(DiffFormatPatch)
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
if len(raw) == 0 {
|
||||||
|
t.Error("empty diff created")
|
||||||
|
}
|
||||||
|
|
||||||
|
diff2, err := DiffFromBuffer(raw, repo)
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
err = repo.ApplyDiff(diff2, GitApplyLocationBoth, nil)
|
||||||
|
checkFatal(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func addAndGetTree(t *testing.T, repo *Repository, filename string, content string) (*Commit, *Tree) {
|
||||||
|
commitId, err := commitSomething(repo, filename, content)
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
commit, err := repo.LookupCommit(commitId)
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
tree, err := commit.Tree()
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
return commit, tree
|
||||||
|
}
|
||||||
|
|
the
C.CString()
needs to beC.free()
d. Although it might be better to useC.CBytes()
since the buffer should be able to contain NULL bytes.Is it possible to avoid embedding the callbacks into this struct? otherwise it gives the appearance of there being an 'is-a' relationship between
ApplyOptions
andApplyHunkCallback
.can the
pointerHandles
only store the pointer to theApplyOptions
?other places (like
fc6eaf3638/diff.go (L514)
) use the function initialization rather than the initialization constant to avoid having to create a wrapper:can the
.toC()
call be outlined? all other places use thepattern (except for
Oid
, since those are just cast to unsafe pointers).the version is not needed to be exposed. it's only needed internally by libgit2 to ensure that the size of the struct is what it expects.
I exposed it because it is exposed in some of the other structs (For example, CherrypickOptions, RebaseOptions, etc). Should I just convert this to a non-exported field instead?
Fixed
I didn't realise that, I suppose that's why it passes the size as a separate parameter (so it can do a binary diff?) I'm not sure about the CString, I wasn't able to find out how Go behaves if theres a null byte in the middle of a string so I converted it to a CBytes as recommended
Fixed
I thought there was a reason I did it this way but after changing it to call this it works fine 🤔
I'd rather remove it altogether since that version number is just there to account for version drift in C, where there is no way to differentiate between different versions of the same struct. As long as there is a layer that accounts for this (e.g. git2go), it's an implementation detail that's best to hide from the end user.
RevertOptions was recently de-Versionified, so we should probably make the other Options follow suit:
4bca045e5a (diff-30188d21ac9afa73021c8dd3ae818448)
I'll probably do that at v31 branch creation time to avoid breaking the interface for older folks.the older name was better for consistency:
in L860-L861, the same name can be used for the field and the type, which is the way to express that something is not to be embedded.
(as mentioned above, this is possible)
Fixed
Removed and hardcoded like the other options