-
-
Couldn't load subscription status.
- Fork 6.2k
Move some functions to gitrepo package #35503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
66fc22d
Move some functions to gitrepo package
lunny ec62dfb
improvements
lunny be4c551
Merge branch 'main' into lunny/refactor_gitrepo
lunny 0b0ccfd
Merge branch 'main' into lunny/refactor_gitrepo
lunny 67aa256
Merge branch 'main' into lunny/refactor_gitrepo
lunny ab22ecd
Merge branch 'main' into lunny/refactor_gitrepo
lunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Copyright 2025 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package gitrepo | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "code.gitea.io/gitea/modules/git/gitcmd" | ||
| ) | ||
|
|
||
| func LineBlame(ctx context.Context, repo Repository, revision, file string, line uint) (string, error) { | ||
| res, _, err := runCmdString(ctx, repo, | ||
| gitcmd.NewCommand("blame"). | ||
| AddOptionFormat("-L %d,%d", line, line). | ||
| AddOptionValues("-p", revision). | ||
| AddDashesAndList(file)) | ||
| return res, err | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Copyright 2025 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package gitrepo | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "code.gitea.io/gitea/modules/git/gitcmd" | ||
| ) | ||
|
|
||
| func runCmdString(ctx context.Context, repo Repository, cmd *gitcmd.Command) (string, string, error) { //nolint:unparam // the second return parameter maybe used in the future | ||
| return cmd.RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)}) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright 2025 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package gitrepo | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "regexp" | ||
| "strconv" | ||
|
|
||
| "code.gitea.io/gitea/modules/git/gitcmd" | ||
| ) | ||
|
|
||
| // GetDiffShortStatByCmdArgs counts number of changed files, number of additions and deletions | ||
| // TODO: it can be merged with another "GetDiffShortStat" in the future | ||
| func GetDiffShortStatByCmdArgs(ctx context.Context, repo Repository, trustedArgs gitcmd.TrustedCmdArgs, dynamicArgs ...string) (numFiles, totalAdditions, totalDeletions int, err error) { | ||
| // Now if we call: | ||
| // $ git diff --shortstat 1ebb35b98889ff77299f24d82da426b434b0cca0...788b8b1440462d477f45b0088875 | ||
| // we get: | ||
| // " 9902 files changed, 2034198 insertions(+), 298800 deletions(-)\n" | ||
| cmd := gitcmd.NewCommand("diff", "--shortstat").AddArguments(trustedArgs...).AddDynamicArguments(dynamicArgs...) | ||
| stdout, _, err := runCmdString(ctx, repo, cmd) | ||
| if err != nil { | ||
| return 0, 0, 0, err | ||
| } | ||
|
|
||
| return parseDiffStat(stdout) | ||
| } | ||
|
|
||
| var shortStatFormat = regexp.MustCompile( | ||
| `\s*(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?`) | ||
|
|
||
| func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int, err error) { | ||
| if len(stdout) == 0 || stdout == "\n" { | ||
| return 0, 0, 0, nil | ||
| } | ||
| groups := shortStatFormat.FindStringSubmatch(stdout) | ||
| if len(groups) != 4 { | ||
| return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s groups: %s", stdout, groups) | ||
| } | ||
|
|
||
| numFiles, err = strconv.Atoi(groups[1]) | ||
| if err != nil { | ||
| return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumFiles %w", stdout, err) | ||
| } | ||
|
|
||
| if len(groups[2]) != 0 { | ||
| totalAdditions, err = strconv.Atoi(groups[2]) | ||
| if err != nil { | ||
| return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumAdditions %w", stdout, err) | ||
| } | ||
| } | ||
|
|
||
| if len(groups[3]) != 0 { | ||
| totalDeletions, err = strconv.Atoi(groups[3]) | ||
| if err != nil { | ||
| return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumDeletions %w", stdout, err) | ||
| } | ||
| } | ||
| return numFiles, totalAdditions, totalDeletions, err | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.