Skip to content

Move some functions to gitrepo package#36287

Open
lunny wants to merge 7 commits intogo-gitea:mainfrom
lunny:lunny/move_funcs2
Open

Move some functions to gitrepo package#36287
lunny wants to merge 7 commits intogo-gitea:mainfrom
lunny:lunny/move_funcs2

Conversation

@lunny
Copy link
Copy Markdown
Member

@lunny lunny commented Jan 3, 2026

  • Move ActivityStats related functions to gitrepo package
  • Move GetFilesChangedBetween, HasPreviousCommit, IsForcePush, GetBranchName, FilesCountBetween, IsCommitInBranch, GetDiff, GetDiffBinary, GetPatch functions to gitrepo package
  • Remove unused file repo_archive.go
  • Refactor CommitTree
  • Move git grep to gitrepo package

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jan 3, 2026
@github-actions github-actions bot added the modifies/go Pull requests that update Go code label Jan 3, 2026
@lunny lunny added the type/refactoring Existing code has been cleaned up. There should be no new functionality. label Jan 3, 2026
@lunny lunny marked this pull request as ready for review January 3, 2026 19:00
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 git-related helpers by moving several commit/compare/diff/grep/stats utilities into the modules/gitrepo package and updating call sites to use the new APIs.

Changes:

  • Migrate code-activity stats, diff/patch, commit ancestry/force-push checks, and file-change comparisons to modules/gitrepo.
  • Update services/modules to use gitrepo.* helpers (and adjust Actions workflow detection interfaces accordingly).
  • Refactor git.CommitTree from a git.Repository method into a package-level function that takes (ctx, repoPath, ...).

Reviewed changes

Copilot reviewed 35 out of 35 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
services/wiki/wiki.go Switch wiki commits to git.CommitTree(ctx, basePath, ...) and use request ctx for push.
services/repository/push.go Use gitrepo.IsCommitForcePush and thread ctx through.
services/repository/branch.go Use gitrepo.IsCommitForcePush / gitrepo.HasPreviousCommit for branch logic.
services/pull/pull.go Replace commit ancestry check with gitrepo.HasPreviousCommit.
services/pull/patch.go Use gitrepo.GetDiff/GetDiffBinary/GetPatch instead of git.Repository methods.
services/pull/merge.go Replace IsCommitInBranch with gitrepo.IsCommitInBranch.
services/issue/pull.go Use gitrepo.GetFilesChangedBetween for CODEOWNERS checks.
services/gitdiff/gitdiff.go Remove gitRepo parameter and use gitrepo.GetFilesChangedBetween via pull.BaseRepo.
services/git/compare.go Use gitrepo.GetDiffNumChangedFiles for compare stats.
services/convert/convert.go Use gitrepo.GetCommitBranchName instead of Commit.GetBranchName.
services/actions/notifier_helper.go Update DetectWorkflows call signature to include (ctx, repo, ...).
routers/web/repo/search.go Update git-grep search call signature to pass Repository object.
routers/web/repo/pull.go Update call to gitdiff.SyncUserSpecificDiff after signature change.
modules/indexer/code/gitgrep/gitgrep.go Move grep execution to gitrepo.GrepSearch and pass repo object (not repoID).
modules/gitrepo/stats_test.go Port stats test to gitrepo package with mock repo.
modules/gitrepo/stats.go Move code-activity stats implementation into gitrepo.
modules/gitrepo/patch_test.go Add gitrepo.GetPatch test coverage.
modules/gitrepo/patch.go Add gitrepo.GetDiff/GetDiffBinary/GetPatch helpers.
modules/gitrepo/grep_test.go Port grep tests to gitrepo package with mock repo.
modules/gitrepo/grep.go Move grep implementation into gitrepo and run via RunCmdWithStderr.
modules/gitrepo/compare_test.go Add tests for GetFilesChangedBetween in gitrepo.
modules/gitrepo/compare.go Add GetDiffNumChangedFiles and GetFilesChangedBetween to gitrepo.
modules/gitrepo/commit_test.go Add tests for HasPreviousCommit (sha1/sha256) and IsCommitInBranch.
modules/gitrepo/commit.go Add commit ancestry, force-push, branch-name, and branch-contains helpers to gitrepo.
modules/git/repo_tree.go Refactor CommitTree to a package-level function taking (ctx, repoPath, ...).
modules/git/repo_compare_test.go Remove tests moved to gitrepo.
modules/git/repo_compare.go Remove diff/patch/files-changed helpers moved to gitrepo (keep remaining methods).
modules/git/repo_commit_test.go Remove IsCommitInBranch test moved to gitrepo.
modules/git/repo_commit.go Remove FilesCountBetween and IsCommitInBranch methods moved to gitrepo.
modules/git/commit_test.go Remove HasPreviousCommit test moved to gitrepo.
modules/git/commit_sha256_test.go Remove sha256 HasPreviousCommit test moved to gitrepo.
modules/git/commit.go Remove HasPreviousCommit, IsForcePush, GetFilesChangedSinceCommit, and GetBranchName methods moved to gitrepo.
modules/actions/workflows_test.go Update detectMatched test calls for new signature.
modules/actions/workflows.go Thread (ctx, repo) into workflow detection and use gitrepo.GetFilesChangedBetween for path filters.
models/activities/repo_activity.go Use gitrepo.CodeActivityStats and gitrepo.GetCodeActivityStats.

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

// But as that does not work for all potential errors, we simply mark all files as unchanged and drop the error which always works, even if not as good as possible
if errIgnored != nil {
log.Error("Could not get changed files between %s and %s for pull request %d in repo with path %s. Assuming no changes. Error: %w", review.CommitSHA, latestCommit, pull.Index, gitRepo.Path, err)
log.Error("Could not get changed files between %s and %s for pull request %d in repo with path %s. Assuming no changes. Error: %w", review.CommitSHA, latestCommit, pull.Index, pull.BaseRepo.RelativePath(), err)
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

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

The log statement in the errIgnored != nil branch logs err (from earlier in the function) instead of the git diff error (errIgnored). This will typically log <nil> or the wrong error and makes diagnosing issues hard. Use errIgnored for the final formatted error argument (and consider keeping the variable name consistent with what’s logged).

Suggested change
log.Error("Could not get changed files between %s and %s for pull request %d in repo with path %s. Assuming no changes. Error: %w", review.CommitSHA, latestCommit, pull.Index, pull.BaseRepo.RelativePath(), err)
log.Error("Could not get changed files between %s and %s for pull request %d in repo with path %s. Assuming no changes. Error: %w", review.CommitSHA, latestCommit, pull.Index, pull.BaseRepo.RelativePath(), errIgnored)

Copilot uses AI. Check for mistakes.
Comment on lines +48 to +56
type lineCountWriter struct {
numLines int
}

// Write counts the number of newlines in the provided bytestream
func (l *lineCountWriter) Write(p []byte) (n int, err error) {
n = len(p)
l.numLines += bytes.Count(p, []byte{'\000'})
return n, err
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

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

lineCountWriter.Write counts NUL (\0) separators (from git diff -z) rather than newlines. The current comment (and numLines field name) is misleading and makes it harder to understand why this correctly counts changed files. Update the comment to refer to NUL-separated records and consider renaming numLines to something like numFiles/numRecords.

Copilot uses AI. Check for mistakes.
@wxiaoguang
Copy link
Copy Markdown
Contributor

I just don't understand why you keep moving code from git to gitrepo.

If you'd like to introduce something like "Git API", the design should be:

  1. Keep "modules/git", for command construction and data processing
  2. "modules/gitcmd", for "real git command"
  3. "modules/gitapi", for API-based git request/response

Now you just keep copying code from "git" to "gitrepo" also make a lot of code/functions redundant and more difficult to refactor in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. modifies/go Pull requests that update Go code type/refactoring Existing code has been cleaned up. There should be no new functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants