Skip to content

Commit 4227a68

Browse files
committed
Put duplicated hash parsing into cleaner function
1 parent 9709e5d commit 4227a68

1 file changed

Lines changed: 22 additions & 77 deletions

File tree

go/cmd/gitter/repository.go

Lines changed: 22 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -414,63 +414,37 @@ func (r *Repository) updatePatchID(commitHash, patchID SHA1) {
414414
r.patchIDToCommits[patchID] = append(r.patchIDToCommits[patchID], commitHash)
415415
}
416416

417-
func parseHash(hash string) (SHA1, error) {
418-
hashBytes, err := hex.DecodeString(hash)
419-
if len(hashBytes) != 20 {
420-
return SHA1{}, fmt.Errorf("invalid hash length: %d", len(hashBytes))
421-
}
422-
if err != nil {
423-
return SHA1{}, fmt.Errorf("failed to decode hash: %w", err)
424-
}
425-
426-
return SHA1(hashBytes), nil
427-
}
428-
429-
// Affected returns a list of commits that are affected by the given introduced, fixed and last_affected events
430-
func (r *Repository) Affected(ctx context.Context, introStrs, fixedStrs, laStrs []string, cherrypickIntro, cherrypickFixed bool) []*Commit {
431-
introduced := make([]SHA1, 0, len(introStrs))
432-
fixed := make([]SHA1, 0, len(fixedStrs))
433-
lastAffected := make([]SHA1, 0, len(laStrs))
434-
435-
// Convert string input into SHA1
436-
// Introduced can be 0 and we'll replace it with the root commit
437-
for _, s := range introStrs {
438-
if s == "0" {
439-
introduced = append(introduced, r.rootCommits...)
417+
// parseHashes converts slice of string hashes input into slice of SHA1
418+
func (r *Repository) parseHashes(ctx context.Context, hashesStr []string, isIntroduced bool) []SHA1 {
419+
hashes := make([]SHA1, 0, len(hashesStr))
420+
for _, hash := range hashesStr {
421+
if isIntroduced && hash == "0" {
422+
hashes = append(hashes, r.rootCommits...)
440423
continue
441424
}
442425

443-
sha, err := parseHash(s)
426+
hashBytes, err := hex.DecodeString(hash)
427+
// Log error but continue with the rest of the hashes if a commit hash is invalid
444428
if err != nil {
445-
// Log error and continue if a commit hash is invalid
446-
logger.ErrorContext(ctx, "failed to parse commit hash: introduced", slog.String("hash", s), slog.Any("err", err))
429+
logger.ErrorContext(ctx, "failed to decode commit hash", slog.String("hash", hash), slog.Any("err", err))
447430
continue
448431
}
449-
450-
introduced = append(introduced, sha)
451-
}
452-
453-
for _, s := range fixedStrs {
454-
sha, err := parseHash(s)
455-
if err != nil {
456-
// Log error and continue if a commit hash is invalid
457-
logger.ErrorContext(ctx, "failed to parse commit hash: fixed", slog.String("hash", s), slog.Any("err", err))
432+
if len(hashBytes) != 20 {
433+
logger.ErrorContext(ctx, "invalid hash length", slog.String("hash", hash), slog.Int("len", len(hashBytes)))
458434
continue
459435
}
460436

461-
fixed = append(fixed, sha)
437+
hashes = append(hashes, SHA1(hashBytes))
462438
}
463439

464-
for _, s := range laStrs {
465-
sha, err := parseHash(s)
466-
if err != nil {
467-
// Log error and continue if a commit hash is invalid
468-
logger.ErrorContext(ctx, "failed to parse commit hash: last_affected", slog.String("hash", s), slog.Any("err", err))
469-
continue
470-
}
440+
return hashes
441+
}
471442

472-
lastAffected = append(lastAffected, sha)
473-
}
443+
// Affected returns a list of commits that are affected by the given introduced, fixed and last_affected events
444+
func (r *Repository) Affected(ctx context.Context, introStrs, fixedStrs, laStrs []string, cherrypickIntro, cherrypickFixed bool) []*Commit {
445+
introduced := r.parseHashes(ctx, introStrs, true)
446+
fixed := r.parseHashes(ctx, fixedStrs, false)
447+
lastAffected := r.parseHashes(ctx, laStrs, false)
474448

475449
// Expands the introduced and fixed commits to include cherrypick equivalents
476450
// lastAffected should not be expanded because it does not imply a "fix" commit that can be cherrypicked to other branches
@@ -523,7 +497,7 @@ func (r *Repository) Affected(ctx context.Context, introStrs, fixedStrs, laStrs
523497
continue
524498
}
525499

526-
// If we hit a fixed commit, its entire tree is treated as a unaffectable
500+
// If we hit a fixed commit, its entire tree is treated as unaffectable
527501
// as any downstream commit can go through this fixed commit to become unaffected
528502
if _, ok := fixedMap[curr]; ok {
529503
unaffectableMap[curr] = struct{}{}
@@ -601,37 +575,8 @@ func (r *Repository) expandByCherrypick(commits []SHA1) []SHA1 {
601575

602576
// Between walks and returns the commits that are strictly between introduced (inclusive) and limit (exclusive)
603577
func (r *Repository) Limit(ctx context.Context, introStrs, limitStrs []string) []*Commit {
604-
introduced := []SHA1{}
605-
limit := []SHA1{}
606-
607-
// Convert string input into SHA1
608-
// Introduced can be 0 and we'll replace it with the root commit
609-
for _, s := range introStrs {
610-
if s == "0" {
611-
introduced = append(introduced, r.rootCommits...)
612-
continue
613-
}
614-
615-
sha, err := parseHash(s)
616-
if err != nil {
617-
// Log error and continue if a commit hash is invalid
618-
logger.ErrorContext(ctx, "failed to parse commit hash", slog.String("hash", s), slog.Any("err", err))
619-
continue
620-
}
621-
622-
introduced = append(introduced, sha)
623-
}
624-
625-
for _, s := range limitStrs {
626-
sha, err := parseHash(s)
627-
if err != nil {
628-
// Log error and continue if a commit hash is invalid
629-
logger.ErrorContext(ctx, "failed to parse commit hash", slog.String("hash", s), slog.Any("err", err))
630-
continue
631-
}
632-
633-
limit = append(limit, sha)
634-
}
578+
introduced := r.parseHashes(ctx, introStrs, true)
579+
limit := r.parseHashes(ctx, limitStrs, false)
635580

636581
var affectedCommits []*Commit
637582

0 commit comments

Comments
 (0)