Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 30, 2025

Migrates internal/git from shelling out via exec.Command to using the go-git/go-git/v5 library where practical.

Migrated to go-git

  • CmdCurrentBranch() - uses repo.Head()
  • CmdHasLocalChanges() - uses worktree.Status().IsClean()
  • hasRemote() - uses repo.Remotes()
  • CmdHasRemoteUnfetchedTags() - uses repo.Tags() for local tag enumeration
  • CmdCreateTag() - uses repo.CreateTag()
  • CmdRemoveTag() - uses repo.DeleteTag()

Kept as exec.Command

Remote operations remain as shell commands since go-git requires complex auth setup:

  • CmdPushTag(), CmdRemoveRemoteTag() - push operations
  • CmdGetTag() - for-each-ref with timestamp sorting (no clean go-git equivalent)
  • Fetch/log comparison in CmdHasRemoteChanges()/CmdHasUnpushedChanges()

Example

// Before
func CmdCreateTag(tag string) error {
    cmd := exec.Command("git", "tag", tag)
    output, err := cmd.CombinedOutput()
    // ...
}

// After
func CmdCreateTag(tag string) error {
    repo, err := openRepository()
    if err != nil {
        return err
    }
    head, err := repo.Head()
    if err != nil {
        return fmt.Errorf("failed to get HEAD: %w", err)
    }
    _, err = repo.CreateTag(tag, head.Hash(), nil)
    // ...
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • dario.cat
    • Triggering command: /update-job-proxy /update-job-proxy (dns block)
  • go.googlesource.com
    • Triggering command: /update-job-proxy /update-job-proxy (dns block)
  • gopkg.in
    • Triggering command: /update-job-proxy /update-job-proxy (dns block)
  • https://api.github.com//advisories
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (http block)
  • https://api.github.com/repos/go-git/go-git/releases/latest
    • Triggering command: /usr/bin/curl curl -s REDACTED (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Prepare a plan/implementation to mitigate to latest version of go-git


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: flaticols <7881854+flaticols@users.noreply.github.com>
@flaticols flaticols marked this pull request as ready for review November 30, 2025 09:30
Copilot AI review requested due to automatic review settings November 30, 2025 09:30
Copilot AI changed the title [WIP] Update go-git to latest version Migrate git operations to go-git library v5.16.4 Nov 30, 2025
Copilot AI requested a review from flaticols November 30, 2025 09:30
Copy link

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

This PR migrates the codebase from using exec.Command("git", ...) for git operations to using the go-git library (v5.16.4) where practical. The migration improves code maintainability by replacing shell command executions with native Go library calls for several core git operations, while strategically keeping exec.Command for operations that require complex authentication or are simpler with direct command execution.

Key Changes

  • Migrated core git operations (CmdCurrentBranch, CmdHasLocalChanges, CmdCreateTag, CmdRemoveTag, hasRemote, CmdHasRemoteUnfetchedTags) to use go-git library functions
  • Added go-git v5.16.4 dependency and its transitive dependencies to go.mod
  • Retained exec.Command for operations requiring authentication (push/fetch) or specific formatting (tag creation dates)

Reviewed changes

Copilot reviewed 3 out of 747 changed files in this pull request and generated 1 comment.

File Description
internal/git/git.go Core migration implementing go-git library for repository operations, with new openRepository() helper and updated functions to use native go-git APIs
internal/git/git_test.go Formatting improvement splitting multi-line test data for better readability
go.mod Added go-git v5.16.4 dependency with all required transitive dependencies

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


// Fallback to symbolic-ref (works for repos without commits)
// Detached HEAD - return short hash
return head.Hash().String()[:7], nil
Copy link

Copilot AI Nov 30, 2025

Choose a reason for hiding this comment

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

Slicing head.Hash().String()[:7] will panic if the hash string is shorter than 7 characters. While git hashes are typically 40 characters, add a length check to prevent potential runtime panics in edge cases.

Suggested change
return head.Hash().String()[:7], nil
hashStr := head.Hash().String()
if len(hashStr) >= 7 {
return hashStr[:7], nil
}
return hashStr, nil

Copilot uses AI. Check for mistakes.
@flaticols flaticols closed this Jan 24, 2026
@flaticols flaticols deleted the copilot/update-to-latest-go-git-version branch January 24, 2026 20:03
@flaticols flaticols restored the copilot/update-to-latest-go-git-version branch January 24, 2026 20:04
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.

2 participants