-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add go wrapper around git diff-tree --raw -r -M #33369
base: main
Are you sure you want to change the base?
Conversation
McRaeAlex
commented
Jan 23, 2025
- Implemented calling git diff-tree
- Ensures wrapper function is called with valid arguments
- Parses output into go struct, using strong typing when possible
a1fb10e
to
bdfd379
Compare
Context for this PR: This was split out of a larger PR I hope to upstream to gitea for showing all files in a file tree at once rather than having a show more button at the bottom. Note that this wouldn't change the behaviour of the files list to the right of the file tree. (Lets have that discussion in the PR which modifies the frontend) |
bdfd379
to
da24f58
Compare
And if it's for the left tree of git comparing, I don't think the structure is enough. Maybe it should like type TreeViewNode struct {
Name string `json:"name"`
Type string `json:"type"`
Path string `json:"path"`
SubModuleURL string `json:"sub_module_url,omitempty"`
Children []*TreeViewNode `json:"children,omitempty"`
} This will be introduced in https://github.com/go-gitea/gitea/pull/32721/files#diff-ab9647487f3912999527f743b0fcada9b0bf1858b95e0bb7a684cc9fa383b157 |
d6e1065
to
a02be75
Compare
The way I did this for allspice was the same structure as this with a function for transforming from output of Heres the function we are using in allspice. type FileDiffFile struct {
Name string
NameHash string
IsSubmodule bool
IsBinary bool
IsViewed bool
Status string
}
// transformDiffTreeForUI transforms a DiffTree into a slice of FileDiffFile for UI rendering
// it also takes a map of file names to their viewed state, which is used to mark files as viewed
func transformDiffTreeForUI(diffTree *gitdiff.DiffTree, filesViewedState map[string]pull_model.ViewedState) []FileDiffFile {
files := make([]FileDiffFile, 0, len(diffTree.Files))
for _, file := range diffTree.Files {
nameHash := git.HashFilePathForWebUI(file.HeadPath)
isSubmodule := file.HeadMode == git.EntryModeCommit
isBinary := file.HeadMode == git.EntryModeExec
isViewed := false
if fileViewedState, ok := filesViewedState[file.Path()]; ok {
isViewed = (fileViewedState == pull_model.Viewed)
}
files = append(files, FileDiffFile{
Name: file.HeadPath,
NameHash: nameHash,
IsSubmodule: isSubmodule,
IsBinary: isBinary,
IsViewed: isViewed,
Status: file.Status,
})
}
return files
} This allows it to be used in gitea/templates/repo/diff/box.tmpl Line 61 in f88dbf8
|
}, nil | ||
} | ||
|
||
func runGitDiffTree(ctx context.Context, gitRepo *git.Repository, useMergeBase bool, baseSha, headSha string) ([]*DiffTreeRecord, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inherently, this method belongs into modules/git
.
However, I'm also fine with declaring it here as it is directly below its usage.
Moving it would be important if we wanted the same information elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am somewhat unfamiliar with what goes where in gitea. I put it here because the wrapper around git diff
used by pull requests go here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally:
- routers: routing logic
- services: logic
- models: database access
- modules: low-level stuff that doesn't depend on anything else, i.e. calling
git
.
But as I said, it's fine as it is for now.
* Implemented calling git diff-tree * Ensures wrapper function is called with valid arguments * Parses output into go struct, using strong typing when possible * Modifies services/gitdiff/testdata/acedemic-module * Makes it a bare repo * Adds a branch which updates readme * Adds a branch which updates the webpack config
a02be75
to
6e7924c
Compare
Since we are rewriting the logic, maybe we can consider the performance more. Perhaps we can avoid traversing twice. Using a callback in the runGitDiffTree function might be more efficient, eliminating the need to create a slice of DiffTreeRecord before transforming it into another slice. |