Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

McRaeAlex
Copy link

  • Implemented calling git diff-tree
  • Ensures wrapper function is called with valid arguments
  • Parses output into go struct, using strong typing when possible

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jan 23, 2025
@pull-request-size pull-request-size bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Jan 23, 2025
@github-actions github-actions bot added the modifies/go Pull requests that update Go code label Jan 23, 2025
@McRaeAlex McRaeAlex marked this pull request as draft January 23, 2025 20:31
@McRaeAlex McRaeAlex marked this pull request as ready for review January 23, 2025 20:32
@McRaeAlex
Copy link
Author

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.

Screenshot 2025-01-23 at 12 36 39 PM

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)

@McRaeAlex McRaeAlex requested a review from lunny January 27, 2025 18:26
@lunny
Copy link
Member

lunny commented Jan 30, 2025

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

@McRaeAlex
Copy link
Author

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

The way I did this for allspice was the same structure as this with a function for transforming from output of git diff-tree to something more consumable by the UI.

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 box.tmpl

const diffDataFiles = [{{range $i, $file := .Diff.Files}}{Name:"{{$file.Name}}",NameHash:"{{$file.NameHash}}",Type:{{$file.Type}},IsBin:{{$file.IsBin}},IsSubmodule:{{$file.IsSubmodule}},Addition:{{$file.Addition}},Deletion:{{$file.Deletion}},IsViewed:{{$file.IsViewed}}},{{end}}];
with some other minor modifications (like removing the code which relies on Added and Deleted).

@McRaeAlex McRaeAlex requested a review from lunny January 30, 2025 16:55
services/gitdiff/git_diff_tree.go Outdated Show resolved Hide resolved
services/gitdiff/git_diff_tree.go Outdated Show resolved Hide resolved
services/gitdiff/git_diff_tree.go Outdated Show resolved Hide resolved
}, nil
}

func runGitDiffTree(ctx context.Context, gitRepo *git.Repository, useMergeBase bool, baseSha, headSha string) ([]*DiffTreeRecord, error) {
Copy link
Member

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.

Copy link
Author

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.

Copy link
Member

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
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Feb 2, 2025
@lunny
Copy link
Member

lunny commented Feb 2, 2025

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 #32721 (files)

The way I did this for allspice was the same structure as this with a function for transforming from output of git diff-tree to something more consumable by the UI.

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 box.tmpl

const diffDataFiles = [{{range $i, $file := .Diff.Files}}{Name:"{{$file.Name}}",NameHash:"{{$file.NameHash}}",Type:{{$file.Type}},IsBin:{{$file.IsBin}},IsSubmodule:{{$file.IsSubmodule}},Addition:{{$file.Addition}},Deletion:{{$file.Deletion}},IsViewed:{{$file.IsViewed}}},{{end}}];

with some other minor modifications (like removing the code which relies on Added and Deleted).

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/need 1 This PR needs approval from one additional maintainer to be merged. modifies/go Pull requests that update Go code size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants