More diff functionality #629

Merged
mbfr merged 24 commits from feature-diff-extra into master 2020-08-18 08:14:03 -05:00
2 changed files with 67 additions and 3 deletions
Showing only changes of commit 1352980b36 - Show all commits

60
diff.go
View File

@ -849,10 +849,63 @@ func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string,
return nil
}
type ApplyHunkCallback func(*DiffHunk) (apply bool, err error)
type ApplyDeltaCallback func(*DiffDelta) (apply bool, err error)
// ApplyOptions has 2 callbacks that are called for hunks or deltas
// If these functions return an error, abort the apply process immediately.
// If the first resutnr value is true, the delta/hunk will be applied. If it is false, the delta/hunk will not be applied. In either case, the rest of the apply process will continue.
type ApplyOptions struct {
Version uint
ApplyHunkCallback
ApplyDeltaCallback
Flags uint
// TODO: there are some more flags, not currently used
}
//export hunkApplyCallback
func hunkApplyCallback(_hunk *C.git_diff_hunk, _payload unsafe.Pointer) C.int {
opts, ok := pointerHandles.Get(_payload).(ApplyOptions)
if !ok {
panic("invalid apply options payload")
}
if opts.ApplyHunkCallback == nil {
return 0
}
hunk := diffHunkFromC(_hunk)
apply, err := opts.ApplyHunkCallback(&hunk)
if err != nil {
return -1
} else if apply {
return 0
} else {
return 1
}
}
//export deltaApplyCallback
func deltaApplyCallback(_delta *C.git_diff_delta, _payload unsafe.Pointer) C.int {
opts, ok := pointerHandles.Get(_payload).(ApplyOptions)
if !ok {
panic("invalid apply options payload")
}
if opts.ApplyDeltaCallback == nil {
return 0
}
delta := diffDeltaFromC(_delta)
apply, err := opts.ApplyDeltaCallback(&delta)
if err != nil {
return -1
lhchavez commented 2020-08-14 19:38:58 -05:00 (Migrated from github.com)
Review

can this also use the same pattern as the other change?

		if gitError, ok := err.(*GitError); ok {
			return C.int(gitError.Code)
		}
		return -1
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 ```
mbfr commented 2020-08-17 02:20:53 -05:00 (Migrated from github.com)
Review

Fixed

Fixed
} else if apply {
return 0
} else {
return 1
}
}
func DefaultApplyOptions() (*ApplyOptions, error) {
@ -876,6 +929,11 @@ func (a *ApplyOptions) toC() *C.git_apply_options {
flags: C.uint(a.Flags),
}
if a.ApplyDeltaCallback != nil || a.ApplyHunkCallback != nil {
C._go_git_populate_apply_cb(opts)
opts.payload = pointerHandles.Track(*a)
}
return opts
}

View File

@ -12,6 +12,12 @@ void _go_git_apply_init_options(git_apply_options *options)
*options = opts;
}
void _go_git_populate_apply_cb(git_apply_options *options)
{
opts->delta_cb = (git_apply_delta_cb)deltaApplyCallback;
opts->hunk_cb = (git_apply_hunk_cb)hunkApplyCallback;
}
void _go_git_populate_remote_cb(git_clone_options *opts)
{
opts->remote_cb = (git_remote_create_cb)remoteCreateCallback;