Skip to content

Commit

Permalink
Merge pull request #17 from utilitywarehouse/as-PR
Browse files Browse the repository at this point in the history
added helper function to get PR info
  • Loading branch information
asiyani committed Jul 2, 2024
2 parents 3b79a1d + 11ea1cf commit df9edf4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/mirror/example_noworktree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ repositories:

// perform 1st mirror to ensure all repositories
// initial mirror might take longer
if err := repos.Mirror(ctx, 5*time.Minute); err != nil {
if err := repos.MirrorAll(ctx, 5*time.Minute); err != nil {
panic(err)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/mirror/example_worktree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ repositories:

// perform 1st mirror to ensure all repositories
// initial mirror might take longer
if err := repos.Mirror(ctx, 5*time.Minute); err != nil {
if err := repos.MirrorAll(ctx, 5*time.Minute); err != nil {
panic(err)
}

Expand Down
37 changes: 34 additions & 3 deletions pkg/mirror/repo_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ func (rp *RepoPool) AddRepository(repo *Repository) error {
return nil
}

// Mirror will trigger mirror on every repo in foreground with given timeout.
// MirrorAll will trigger mirror on every repo in foreground with given timeout.
// It will error out if any of the repository mirror errors.
// Ideally Mirror should be used for the first mirror cycle to ensure repositories are
// Ideally MirrorAll should be used for the first mirror cycle to ensure repositories are
// successfully mirrored
func (rp *RepoPool) Mirror(ctx context.Context, timeout time.Duration) error {
func (rp *RepoPool) MirrorAll(ctx context.Context, timeout time.Duration) error {
for _, repo := range rp.repos {
mCtx, cancel := context.WithTimeout(ctx, timeout)
err := repo.Mirror(mCtx)
Expand All @@ -85,6 +85,16 @@ func (rp *RepoPool) Mirror(ctx context.Context, timeout time.Duration) error {
return nil
}

// Mirror is wrapper around repositories Mirror method
func (rp *RepoPool) Mirror(ctx context.Context, remote string) error {
repo, err := rp.Repository(remote)
if err != nil {
return err
}

return repo.Mirror(ctx)
}

// StartLoop will start mirror loop on all repositories
// if its not already started
func (rp *RepoPool) StartLoop() {
Expand Down Expand Up @@ -175,6 +185,27 @@ func (rp *RepoPool) ChangedFiles(ctx context.Context, remote, hash string) ([]st
return repo.ChangedFiles(ctx, hash)
}

// ChangedFilesBetweenRefs is wrapper around repositories ChangedFilesBetweenRefs method
// git diff --name-only HEAD...refs/pull/13179/head
// git diff --name-only --merge-base HEAD refs/pull/13179/head
func (rp *RepoPool) ChangedFilesBetweenRefs(ctx context.Context, remote, ref1, ref2 string) ([]string, error) {
repo, err := rp.Repository(remote)
if err != nil {
return nil, err
}
return repo.ChangedFilesBetweenRefs(ctx, ref1, ref2)
}

// CommitsBetweenRefs is wrapper around repositories CommitsBetweenRefs method
// git rev-list ^HEAD refs/pull/13179/head
func (rp *RepoPool) CommitsBetweenRefs(ctx context.Context, remote, ref1, ref2 string) ([]string, error) {
repo, err := rp.Repository(remote)
if err != nil {
return nil, err
}
return repo.CommitsBetweenRefs(ctx, ref1, ref2)
}

// ObjectExists is wrapper around repositories ObjectExists method
func (rp *RepoPool) ObjectExists(ctx context.Context, remote, obj string) error {
repo, err := rp.Repository(remote)
Expand Down
46 changes: 46 additions & 0 deletions pkg/mirror/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,52 @@ func (r *Repository) ChangedFiles(ctx context.Context, hash string) ([]string, e
return strings.Split(msg, "\n"), nil
}

// ChangedFilesBetweenRefs returns path of the changed files between ref1 and ref2,
// using the merge base of the two commits for the "before" side.
// git diff --name-only HEAD...refs/pull/13179/head
// git diff --name-only --merge-base HEAD refs/pull/13179/head
func (r *Repository) ChangedFilesBetweenRefs(ctx context.Context, ref1, ref2 string) ([]string, error) {
r.lock.RLock()
defer r.lock.RUnlock()

if err := r.ObjectExists(ctx, ref1); err != nil {
return nil, err
}
if err := r.ObjectExists(ctx, ref2); err != nil {
return nil, err
}

// merge-base finds the best common ancestor(s) between two commits to use in a three-way merge.
args := []string{"diff", `--name-only`, `--merge-base`, ref1, ref2}
msg, err := runGitCommand(ctx, r.log, r.envs, r.dir, args...)
if err != nil {
return nil, err
}
return strings.Split(msg, "\n"), nil
}

// ChangedFiles returns list of all the commits which are reachable from ref2, but not from ref1".
// list is in reverse order ie old commit -> new commit
// git rev-list ^HEAD refs/pull/13179/head
func (r *Repository) CommitsBetweenRefs(ctx context.Context, ref1, ref2 string) ([]string, error) {
r.lock.RLock()
defer r.lock.RUnlock()

if err := r.ObjectExists(ctx, ref1); err != nil {
return nil, err
}
if err := r.ObjectExists(ctx, ref2); err != nil {
return nil, err
}

args := []string{"rev-list", `--reverse`, "^" + ref1, ref2}
msg, err := runGitCommand(ctx, r.log, r.envs, r.dir, args...)
if err != nil {
return nil, err
}
return strings.Split(msg, "\n"), nil
}

// ObjectExists returns error is given object is not valid or if it doesn't exists
func (r *Repository) ObjectExists(ctx context.Context, obj string) error {
r.lock.RLock()
Expand Down
2 changes: 1 addition & 1 deletion pkg/mirror/z_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ func Test_RepoPool_Success(t *testing.T) {
}

// run initial mirror
if err := rp.Mirror(context.TODO(), testTimeout); err != nil {
if err := rp.MirrorAll(context.TODO(), testTimeout); err != nil {
t.Fatalf("unexpected err:%s", err)
}

Expand Down

0 comments on commit df9edf4

Please sign in to comment.