// Code generated by mockery v0.0.0-dev. DO NOT EDIT.

package mocks

import (
	context "context"

	mock "github.com/stretchr/testify/mock"

	provider "go.skia.org/infra/perf/go/git/provider"

	time "time"

	types "go.skia.org/infra/perf/go/types"
)

// Git is an autogenerated mock type for the Git type
type Git struct {
	mock.Mock
}

// CommitFromCommitNumber provides a mock function with given fields: ctx, commitNumber
func (_m *Git) CommitFromCommitNumber(ctx context.Context, commitNumber types.CommitNumber) (provider.Commit, error) {
	ret := _m.Called(ctx, commitNumber)

	if len(ret) == 0 {
		panic("no return value specified for CommitFromCommitNumber")
	}

	var r0 provider.Commit
	var r1 error
	if rf, ok := ret.Get(0).(func(context.Context, types.CommitNumber) (provider.Commit, error)); ok {
		return rf(ctx, commitNumber)
	}
	if rf, ok := ret.Get(0).(func(context.Context, types.CommitNumber) provider.Commit); ok {
		r0 = rf(ctx, commitNumber)
	} else {
		r0 = ret.Get(0).(provider.Commit)
	}

	if rf, ok := ret.Get(1).(func(context.Context, types.CommitNumber) error); ok {
		r1 = rf(ctx, commitNumber)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}

// CommitNumberFromGitHash provides a mock function with given fields: ctx, githash
func (_m *Git) CommitNumberFromGitHash(ctx context.Context, githash string) (types.CommitNumber, error) {
	ret := _m.Called(ctx, githash)

	if len(ret) == 0 {
		panic("no return value specified for CommitNumberFromGitHash")
	}

	var r0 types.CommitNumber
	var r1 error
	if rf, ok := ret.Get(0).(func(context.Context, string) (types.CommitNumber, error)); ok {
		return rf(ctx, githash)
	}
	if rf, ok := ret.Get(0).(func(context.Context, string) types.CommitNumber); ok {
		r0 = rf(ctx, githash)
	} else {
		r0 = ret.Get(0).(types.CommitNumber)
	}

	if rf, ok := ret.Get(1).(func(context.Context, string) error); ok {
		r1 = rf(ctx, githash)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}

// CommitNumberFromTime provides a mock function with given fields: ctx, t
func (_m *Git) CommitNumberFromTime(ctx context.Context, t time.Time) (types.CommitNumber, error) {
	ret := _m.Called(ctx, t)

	if len(ret) == 0 {
		panic("no return value specified for CommitNumberFromTime")
	}

	var r0 types.CommitNumber
	var r1 error
	if rf, ok := ret.Get(0).(func(context.Context, time.Time) (types.CommitNumber, error)); ok {
		return rf(ctx, t)
	}
	if rf, ok := ret.Get(0).(func(context.Context, time.Time) types.CommitNumber); ok {
		r0 = rf(ctx, t)
	} else {
		r0 = ret.Get(0).(types.CommitNumber)
	}

	if rf, ok := ret.Get(1).(func(context.Context, time.Time) error); ok {
		r1 = rf(ctx, t)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}

// CommitNumbersWhenFileChangesInCommitNumberRange provides a mock function with given fields: ctx, begin, end, filename
func (_m *Git) CommitNumbersWhenFileChangesInCommitNumberRange(ctx context.Context, begin types.CommitNumber, end types.CommitNumber, filename string) ([]types.CommitNumber, error) {
	ret := _m.Called(ctx, begin, end, filename)

	if len(ret) == 0 {
		panic("no return value specified for CommitNumbersWhenFileChangesInCommitNumberRange")
	}

	var r0 []types.CommitNumber
	var r1 error
	if rf, ok := ret.Get(0).(func(context.Context, types.CommitNumber, types.CommitNumber, string) ([]types.CommitNumber, error)); ok {
		return rf(ctx, begin, end, filename)
	}
	if rf, ok := ret.Get(0).(func(context.Context, types.CommitNumber, types.CommitNumber, string) []types.CommitNumber); ok {
		r0 = rf(ctx, begin, end, filename)
	} else {
		if ret.Get(0) != nil {
			r0 = ret.Get(0).([]types.CommitNumber)
		}
	}

	if rf, ok := ret.Get(1).(func(context.Context, types.CommitNumber, types.CommitNumber, string) error); ok {
		r1 = rf(ctx, begin, end, filename)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}

// CommitSliceFromCommitNumberRange provides a mock function with given fields: ctx, begin, end
func (_m *Git) CommitSliceFromCommitNumberRange(ctx context.Context, begin types.CommitNumber, end types.CommitNumber) ([]provider.Commit, error) {
	ret := _m.Called(ctx, begin, end)

	if len(ret) == 0 {
		panic("no return value specified for CommitSliceFromCommitNumberRange")
	}

	var r0 []provider.Commit
	var r1 error
	if rf, ok := ret.Get(0).(func(context.Context, types.CommitNumber, types.CommitNumber) ([]provider.Commit, error)); ok {
		return rf(ctx, begin, end)
	}
	if rf, ok := ret.Get(0).(func(context.Context, types.CommitNumber, types.CommitNumber) []provider.Commit); ok {
		r0 = rf(ctx, begin, end)
	} else {
		if ret.Get(0) != nil {
			r0 = ret.Get(0).([]provider.Commit)
		}
	}

	if rf, ok := ret.Get(1).(func(context.Context, types.CommitNumber, types.CommitNumber) error); ok {
		r1 = rf(ctx, begin, end)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}

// CommitSliceFromCommitNumberSlice provides a mock function with given fields: ctx, commitNumberSlice
func (_m *Git) CommitSliceFromCommitNumberSlice(ctx context.Context, commitNumberSlice []types.CommitNumber) ([]provider.Commit, error) {
	ret := _m.Called(ctx, commitNumberSlice)

	if len(ret) == 0 {
		panic("no return value specified for CommitSliceFromCommitNumberSlice")
	}

	var r0 []provider.Commit
	var r1 error
	if rf, ok := ret.Get(0).(func(context.Context, []types.CommitNumber) ([]provider.Commit, error)); ok {
		return rf(ctx, commitNumberSlice)
	}
	if rf, ok := ret.Get(0).(func(context.Context, []types.CommitNumber) []provider.Commit); ok {
		r0 = rf(ctx, commitNumberSlice)
	} else {
		if ret.Get(0) != nil {
			r0 = ret.Get(0).([]provider.Commit)
		}
	}

	if rf, ok := ret.Get(1).(func(context.Context, []types.CommitNumber) error); ok {
		r1 = rf(ctx, commitNumberSlice)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}

// CommitSliceFromTimeRange provides a mock function with given fields: ctx, begin, end
func (_m *Git) CommitSliceFromTimeRange(ctx context.Context, begin time.Time, end time.Time) ([]provider.Commit, error) {
	ret := _m.Called(ctx, begin, end)

	if len(ret) == 0 {
		panic("no return value specified for CommitSliceFromTimeRange")
	}

	var r0 []provider.Commit
	var r1 error
	if rf, ok := ret.Get(0).(func(context.Context, time.Time, time.Time) ([]provider.Commit, error)); ok {
		return rf(ctx, begin, end)
	}
	if rf, ok := ret.Get(0).(func(context.Context, time.Time, time.Time) []provider.Commit); ok {
		r0 = rf(ctx, begin, end)
	} else {
		if ret.Get(0) != nil {
			r0 = ret.Get(0).([]provider.Commit)
		}
	}

	if rf, ok := ret.Get(1).(func(context.Context, time.Time, time.Time) error); ok {
		r1 = rf(ctx, begin, end)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}

// GetCommitNumber provides a mock function with given fields: ctx, githash, commitNumber
func (_m *Git) GetCommitNumber(ctx context.Context, githash string, commitNumber types.CommitNumber) (types.CommitNumber, error) {
	ret := _m.Called(ctx, githash, commitNumber)

	if len(ret) == 0 {
		panic("no return value specified for GetCommitNumber")
	}

	var r0 types.CommitNumber
	var r1 error
	if rf, ok := ret.Get(0).(func(context.Context, string, types.CommitNumber) (types.CommitNumber, error)); ok {
		return rf(ctx, githash, commitNumber)
	}
	if rf, ok := ret.Get(0).(func(context.Context, string, types.CommitNumber) types.CommitNumber); ok {
		r0 = rf(ctx, githash, commitNumber)
	} else {
		r0 = ret.Get(0).(types.CommitNumber)
	}

	if rf, ok := ret.Get(1).(func(context.Context, string, types.CommitNumber) error); ok {
		r1 = rf(ctx, githash, commitNumber)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}

// GitHashFromCommitNumber provides a mock function with given fields: ctx, commitNumber
func (_m *Git) GitHashFromCommitNumber(ctx context.Context, commitNumber types.CommitNumber) (string, error) {
	ret := _m.Called(ctx, commitNumber)

	if len(ret) == 0 {
		panic("no return value specified for GitHashFromCommitNumber")
	}

	var r0 string
	var r1 error
	if rf, ok := ret.Get(0).(func(context.Context, types.CommitNumber) (string, error)); ok {
		return rf(ctx, commitNumber)
	}
	if rf, ok := ret.Get(0).(func(context.Context, types.CommitNumber) string); ok {
		r0 = rf(ctx, commitNumber)
	} else {
		r0 = ret.Get(0).(string)
	}

	if rf, ok := ret.Get(1).(func(context.Context, types.CommitNumber) error); ok {
		r1 = rf(ctx, commitNumber)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}

// LogEntry provides a mock function with given fields: ctx, commit
func (_m *Git) LogEntry(ctx context.Context, commit types.CommitNumber) (string, error) {
	ret := _m.Called(ctx, commit)

	if len(ret) == 0 {
		panic("no return value specified for LogEntry")
	}

	var r0 string
	var r1 error
	if rf, ok := ret.Get(0).(func(context.Context, types.CommitNumber) (string, error)); ok {
		return rf(ctx, commit)
	}
	if rf, ok := ret.Get(0).(func(context.Context, types.CommitNumber) string); ok {
		r0 = rf(ctx, commit)
	} else {
		r0 = ret.Get(0).(string)
	}

	if rf, ok := ret.Get(1).(func(context.Context, types.CommitNumber) error); ok {
		r1 = rf(ctx, commit)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}

// PreviousCommitNumberFromCommitNumber provides a mock function with given fields: ctx, commitNumber
func (_m *Git) PreviousCommitNumberFromCommitNumber(ctx context.Context, commitNumber types.CommitNumber) (types.CommitNumber, error) {
	ret := _m.Called(ctx, commitNumber)

	if len(ret) == 0 {
		panic("no return value specified for PreviousCommitNumberFromCommitNumber")
	}

	var r0 types.CommitNumber
	var r1 error
	if rf, ok := ret.Get(0).(func(context.Context, types.CommitNumber) (types.CommitNumber, error)); ok {
		return rf(ctx, commitNumber)
	}
	if rf, ok := ret.Get(0).(func(context.Context, types.CommitNumber) types.CommitNumber); ok {
		r0 = rf(ctx, commitNumber)
	} else {
		r0 = ret.Get(0).(types.CommitNumber)
	}

	if rf, ok := ret.Get(1).(func(context.Context, types.CommitNumber) error); ok {
		r1 = rf(ctx, commitNumber)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}

// PreviousGitHashFromCommitNumber provides a mock function with given fields: ctx, commitNumber
func (_m *Git) PreviousGitHashFromCommitNumber(ctx context.Context, commitNumber types.CommitNumber) (string, error) {
	ret := _m.Called(ctx, commitNumber)

	if len(ret) == 0 {
		panic("no return value specified for PreviousGitHashFromCommitNumber")
	}

	var r0 string
	var r1 error
	if rf, ok := ret.Get(0).(func(context.Context, types.CommitNumber) (string, error)); ok {
		return rf(ctx, commitNumber)
	}
	if rf, ok := ret.Get(0).(func(context.Context, types.CommitNumber) string); ok {
		r0 = rf(ctx, commitNumber)
	} else {
		r0 = ret.Get(0).(string)
	}

	if rf, ok := ret.Get(1).(func(context.Context, types.CommitNumber) error); ok {
		r1 = rf(ctx, commitNumber)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}

// RepoSuppliedCommitNumber provides a mock function with given fields:
func (_m *Git) RepoSuppliedCommitNumber() bool {
	ret := _m.Called()

	if len(ret) == 0 {
		panic("no return value specified for RepoSuppliedCommitNumber")
	}

	var r0 bool
	if rf, ok := ret.Get(0).(func() bool); ok {
		r0 = rf()
	} else {
		r0 = ret.Get(0).(bool)
	}

	return r0
}

// StartBackgroundPolling provides a mock function with given fields: ctx, duration
func (_m *Git) StartBackgroundPolling(ctx context.Context, duration time.Duration) {
	_m.Called(ctx, duration)
}

// Update provides a mock function with given fields: ctx
func (_m *Git) Update(ctx context.Context) error {
	ret := _m.Called(ctx)

	if len(ret) == 0 {
		panic("no return value specified for Update")
	}

	var r0 error
	if rf, ok := ret.Get(0).(func(context.Context) error); ok {
		r0 = rf(ctx)
	} else {
		r0 = ret.Error(0)
	}

	return r0
}

// NewGit creates a new instance of Git. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewGit(t interface {
	mock.TestingT
	Cleanup(func())
}) *Git {
	mock := &Git{}
	mock.Mock.Test(t)

	t.Cleanup(func() { mock.AssertExpectations(t) })

	return mock
}
