Skip to content

Commit df9edf4

Browse files
authored
Merge pull request #17 from utilitywarehouse/as-PR
added helper function to get PR info
2 parents 3b79a1d + 11ea1cf commit df9edf4

File tree

5 files changed

+83
-6
lines changed

5 files changed

+83
-6
lines changed

pkg/mirror/example_noworktree_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ repositories:
4848

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

pkg/mirror/example_worktree_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ repositories:
5151

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

pkg/mirror/repo_pool.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ func (rp *RepoPool) AddRepository(repo *Repository) error {
6868
return nil
6969
}
7070

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

88+
// Mirror is wrapper around repositories Mirror method
89+
func (rp *RepoPool) Mirror(ctx context.Context, remote string) error {
90+
repo, err := rp.Repository(remote)
91+
if err != nil {
92+
return err
93+
}
94+
95+
return repo.Mirror(ctx)
96+
}
97+
8898
// StartLoop will start mirror loop on all repositories
8999
// if its not already started
90100
func (rp *RepoPool) StartLoop() {
@@ -175,6 +185,27 @@ func (rp *RepoPool) ChangedFiles(ctx context.Context, remote, hash string) ([]st
175185
return repo.ChangedFiles(ctx, hash)
176186
}
177187

188+
// ChangedFilesBetweenRefs is wrapper around repositories ChangedFilesBetweenRefs method
189+
// git diff --name-only HEAD...refs/pull/13179/head
190+
// git diff --name-only --merge-base HEAD refs/pull/13179/head
191+
func (rp *RepoPool) ChangedFilesBetweenRefs(ctx context.Context, remote, ref1, ref2 string) ([]string, error) {
192+
repo, err := rp.Repository(remote)
193+
if err != nil {
194+
return nil, err
195+
}
196+
return repo.ChangedFilesBetweenRefs(ctx, ref1, ref2)
197+
}
198+
199+
// CommitsBetweenRefs is wrapper around repositories CommitsBetweenRefs method
200+
// git rev-list ^HEAD refs/pull/13179/head
201+
func (rp *RepoPool) CommitsBetweenRefs(ctx context.Context, remote, ref1, ref2 string) ([]string, error) {
202+
repo, err := rp.Repository(remote)
203+
if err != nil {
204+
return nil, err
205+
}
206+
return repo.CommitsBetweenRefs(ctx, ref1, ref2)
207+
}
208+
178209
// ObjectExists is wrapper around repositories ObjectExists method
179210
func (rp *RepoPool) ObjectExists(ctx context.Context, remote, obj string) error {
180211
repo, err := rp.Repository(remote)

pkg/mirror/repository.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,52 @@ func (r *Repository) ChangedFiles(ctx context.Context, hash string) ([]string, e
220220
return strings.Split(msg, "\n"), nil
221221
}
222222

223+
// ChangedFilesBetweenRefs returns path of the changed files between ref1 and ref2,
224+
// using the merge base of the two commits for the "before" side.
225+
// git diff --name-only HEAD...refs/pull/13179/head
226+
// git diff --name-only --merge-base HEAD refs/pull/13179/head
227+
func (r *Repository) ChangedFilesBetweenRefs(ctx context.Context, ref1, ref2 string) ([]string, error) {
228+
r.lock.RLock()
229+
defer r.lock.RUnlock()
230+
231+
if err := r.ObjectExists(ctx, ref1); err != nil {
232+
return nil, err
233+
}
234+
if err := r.ObjectExists(ctx, ref2); err != nil {
235+
return nil, err
236+
}
237+
238+
// merge-base finds the best common ancestor(s) between two commits to use in a three-way merge.
239+
args := []string{"diff", `--name-only`, `--merge-base`, ref1, ref2}
240+
msg, err := runGitCommand(ctx, r.log, r.envs, r.dir, args...)
241+
if err != nil {
242+
return nil, err
243+
}
244+
return strings.Split(msg, "\n"), nil
245+
}
246+
247+
// ChangedFiles returns list of all the commits which are reachable from ref2, but not from ref1".
248+
// list is in reverse order ie old commit -> new commit
249+
// git rev-list ^HEAD refs/pull/13179/head
250+
func (r *Repository) CommitsBetweenRefs(ctx context.Context, ref1, ref2 string) ([]string, error) {
251+
r.lock.RLock()
252+
defer r.lock.RUnlock()
253+
254+
if err := r.ObjectExists(ctx, ref1); err != nil {
255+
return nil, err
256+
}
257+
if err := r.ObjectExists(ctx, ref2); err != nil {
258+
return nil, err
259+
}
260+
261+
args := []string{"rev-list", `--reverse`, "^" + ref1, ref2}
262+
msg, err := runGitCommand(ctx, r.log, r.envs, r.dir, args...)
263+
if err != nil {
264+
return nil, err
265+
}
266+
return strings.Split(msg, "\n"), nil
267+
}
268+
223269
// ObjectExists returns error is given object is not valid or if it doesn't exists
224270
func (r *Repository) ObjectExists(ctx context.Context, obj string) error {
225271
r.lock.RLock()

pkg/mirror/z_e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ func Test_RepoPool_Success(t *testing.T) {
11651165
}
11661166

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

0 commit comments

Comments
 (0)