Skip to content

refactor: extract showLockFileAtCommit to public git.ShowFileAtCommit+ lockfile.ShowAtCommit#141

Open
dmcilvaney wants to merge 2 commits intomicrosoft:mainfrom
dmcilvaney:damcilva/refactor/extract-show-file-at-commit
Open

refactor: extract showLockFileAtCommit to public git.ShowFileAtCommit+ lockfile.ShowAtCommit#141
dmcilvaney wants to merge 2 commits intomicrosoft:mainfrom
dmcilvaney:damcilva/refactor/extract-show-file-at-commit

Conversation

@dmcilvaney
Copy link
Copy Markdown
Contributor

Move the private showLockFileAtCommit function from synthistory.go into two public, reusable packages:

  • git.ShowFileAtCommit: generic CLI-based 'git show commit:path' with ErrFileNotFound sentinel for missing files/directories
  • lockfile.ShowAtCommit: lockfile-specific wrapper combining ShowFileAtCommit with lockfile.Parse

Uses CLI git instead of go-git for better performance on large repositories (avoids loading full tree into memory).

Simplify readLockFileAtHEAD to pass "HEAD" directly instead of resolving via git rev-parse first.

… + lockfile.ShowAtCommit

Move the private showLockFileAtCommit function from synthistory.go into two
public, reusable packages:

- git.ShowFileAtCommit: generic CLI-based 'git show commit:path' with
  ErrFileNotFound sentinel for missing files/directories
- lockfile.ShowAtCommit: lockfile-specific wrapper combining ShowFileAtCommit
  with lockfile.Parse

Uses CLI git instead of go-git for better performance on large repositories
(avoids loading full tree into memory).

Simplify readLockFileAtHEAD to pass "HEAD" directly instead of resolving
via git rev-parse first.
Copilot AI review requested due to automatic review settings April 30, 2026 23:26
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors lockfile history inspection to use CLI git show for reading lock files at specific revisions, extracting the previous private helper into reusable utilities.

Changes:

  • Added git.ShowFileAtCommit() and git.ErrFileNotFound sentinel for git show <rev>:<path> reads.
  • Added lockfile.ShowAtCommit() wrapper that reads via git util and parses a ComponentLock.
  • Updated synthetic history code to use the new helpers and to read HEAD directly without an extra rev-parse.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
internal/utils/git/git.go Introduces ErrFileNotFound and ShowFileAtCommit() implemented via CLI git show.
internal/utils/git/showfile_test.go Adds unit tests + an E2E-style test validating stderr parsing behavior.
internal/lockfile/show.go Adds lockfile.ShowAtCommit() wrapper around git.ShowFileAtCommit() + lockfile.Parse().
internal/lockfile/show_test.go Adds tests for lockfile.ShowAtCommit() success, not-found propagation, and parse/version failures.
internal/app/azldev/core/sources/synthistory.go Switches synthetic history lockfile reads from go-git tree walking to the new CLI-based helpers.

Comment thread internal/utils/git/git.go Outdated
var stderr bytes.Buffer

objectRef := revision + ":" + relPath
rawCmd := exec.CommandContext(ctx, "git", "-C", repoDir, "show", objectRef)
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ShowFileAtCommit constructs git show without a -- end-of-options separator. If revision (branch/tag) ever begins with '-' (legal in git) it can be interpreted as a flag, changing behavior unexpectedly. Add -- before the revision:relPath argument to ensure it’s always parsed as an object spec, not options.

Suggested change
rawCmd := exec.CommandContext(ctx, "git", "-C", repoDir, "show", objectRef)
rawCmd := exec.CommandContext(ctx, "git", "-C", repoDir, "show", "--", objectRef)

Copilot uses AI. Check for mistakes.
Comment thread internal/utils/git/git.go Outdated
repoDir string,
revision string,
relPath string,
) (string, error) {
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ShowFileAtCommit shells out to git but doesn’t verify it exists via cmdFactory.CommandInSearchPath("git"). Adding an early check lets callers get a clear, deterministic error (and aligns with the repo pattern of checking tool availability through the command factory rather than exec.LookPath).

Suggested change
) (string, error) {
) (string, error) {
if _, err := cmdFactory.CommandInSearchPath("git"); err != nil {
return "", fmt.Errorf("git is not available in PATH:\n%w", err)
}

Copilot uses AI. Check for mistakes.
Comment thread internal/utils/git/showfile_test.go Outdated
Comment on lines +171 to +173
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test uses exec.LookPath("git") to probe tool availability. Elsewhere in the repo we check tool presence through the opctx command factory/context (CommandInSearchPath), e.g. internal/utils/prereqs/prereqs.go:41-66. Consider switching to cmdFactory.CommandInSearchPath("git") (or ctx.CommandInSearchPath) so the check is stub-friendly and consistent with production code.

Copilot uses AI. Check for mistakes.
Revert to go-git for lockfile.ShowAtCommit based on review feedback.
The CLI git approach was motivated by git-log path filtering performance
(which remains CLI), but single-file reads via go-git tree.File() are
adequate — the cost is two tree object lookups per call, not a full
tree load.

Go-git eliminates stderr parsing fragility (locale dependence, multiple
error message variants) and subprocess overhead.

- Remove git.ShowFileAtCommit and git.ErrFileNotFound (CLI-based)
- Remove showfile_test.go (CLI e2e tests)
- Rewrite lockfile.ShowAtCommit to use go-git tree traversal
- Rewrite lockfile tests to use in-memory go-git repos
- Restore *gogit.Repository params in FindFingerprintChanges and
  readLockFileAtHEAD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants