Skip to content

Commit 5752410

Browse files
committed
Modify Diff View FileTree to show all files
== Changes * removes Show Status button on diff * uses `git diff-tree` to generate the file tree for the diff
1 parent a1f1bcc commit 5752410

File tree

13 files changed

+290
-181
lines changed

13 files changed

+290
-181
lines changed

routers/web/repo/commit.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,18 +330,30 @@ func Diff(ctx *context.Context) {
330330
ctx.Data["Reponame"] = repoName
331331

332332
var parentCommit *git.Commit
333+
var parentCommitID string
333334
if commit.ParentCount() > 0 {
334335
parentCommit, err = gitRepo.GetCommit(parents[0])
335336
if err != nil {
336337
ctx.NotFound("GetParentCommit", err)
337338
return
338339
}
340+
parentCommitID = parentCommit.ID.String()
339341
}
340342
setCompareContext(ctx, parentCommit, commit, userName, repoName)
341343
ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
342344
ctx.Data["Commit"] = commit
343345
ctx.Data["Diff"] = diff
344346

347+
if !fileOnly {
348+
diffTree, err := gitdiff.GetDiffTree(ctx, gitRepo, false, parentCommitID, commitID)
349+
if err != nil {
350+
ctx.ServerError("GetDiffTree", err)
351+
return
352+
}
353+
354+
ctx.Data["DiffTree"] = transformDiffTreeForUI(diffTree, nil)
355+
}
356+
345357
statuses, _, err := git_model.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, commitID, db.ListOptionsAll)
346358
if err != nil {
347359
log.Error("GetLatestCommitStatus: %v", err)

routers/web/repo/compare.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,16 @@ func PrepareCompareDiff(
633633
ctx.Data["Diff"] = diff
634634
ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
635635

636+
if !fileOnly {
637+
diffTree, err := gitdiff.GetDiffTree(ctx, ci.HeadGitRepo, false, beforeCommitID, headCommitID)
638+
if err != nil {
639+
ctx.ServerError("GetDiffTree", err)
640+
return false
641+
}
642+
643+
ctx.Data["DiffTree"] = transformDiffTreeForUI(diffTree, nil)
644+
}
645+
636646
headCommit, err := ci.HeadGitRepo.GetCommit(headCommitID)
637647
if err != nil {
638648
ctx.ServerError("GetCommit", err)

routers/web/repo/pull.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
757757

758758
var methodWithError string
759759
var diff *gitdiff.Diff
760+
shouldGetUserSpecificDiff := false
760761

761762
// if we're not logged in or only a single commit (or commit range) is shown we
762763
// have to load only the diff and not get the viewed information
@@ -768,6 +769,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
768769
} else {
769770
diff, err = gitdiff.SyncAndGetUserSpecificDiff(ctx, ctx.Doer.ID, pull, gitRepo, diffOptions, files...)
770771
methodWithError = "SyncAndGetUserSpecificDiff"
772+
shouldGetUserSpecificDiff = true
771773
}
772774
if err != nil {
773775
ctx.ServerError(methodWithError, err)
@@ -812,6 +814,27 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
812814
}
813815
}
814816

817+
if !fileOnly {
818+
// note: use mergeBase is set to false because we already have the merge base from the pull request info
819+
diffTree, err := gitdiff.GetDiffTree(ctx, gitRepo, false, pull.MergeBase, headCommitID)
820+
if err != nil {
821+
ctx.ServerError("GetDiffTree", err)
822+
return
823+
}
824+
825+
filesViewedState := make(map[string]pull_model.ViewedState)
826+
if shouldGetUserSpecificDiff {
827+
// This sort of sucks because we already fetch this when getting the diff
828+
review, err := pull_model.GetNewestReviewState(ctx, ctx.Doer.ID, issue.ID)
829+
if !(err != nil || review == nil || review.UpdatedFiles == nil) {
830+
// If there wasn't an error and we have a review with updated files, use that
831+
filesViewedState = review.UpdatedFiles
832+
}
833+
}
834+
835+
ctx.Data["DiffFiles"] = transformDiffTreeForUI(diffTree, filesViewedState)
836+
}
837+
815838
ctx.Data["Diff"] = diff
816839
ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
817840

routers/web/repo/treelist.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ package repo
66
import (
77
"net/http"
88

9+
pull_model "code.gitea.io/gitea/models/pull"
910
"code.gitea.io/gitea/modules/base"
1011
"code.gitea.io/gitea/modules/git"
1112
"code.gitea.io/gitea/services/context"
13+
"code.gitea.io/gitea/services/gitdiff"
1214

1315
"github.com/go-enry/go-enry/v2"
1416
)
@@ -52,3 +54,40 @@ func isExcludedEntry(entry *git.TreeEntry) bool {
5254

5355
return false
5456
}
57+
58+
type FileDiffFile struct {
59+
Name string
60+
NameHash string
61+
IsSubmodule bool
62+
IsBinary bool
63+
IsViewed bool
64+
Status string
65+
}
66+
67+
// transformDiffTreeForUI transforms a DiffTree into a slice of FileDiffFile for UI rendering
68+
// it also takes a map of file names to their viewed state, which is used to mark files as viewed
69+
func transformDiffTreeForUI(diffTree *gitdiff.DiffTree, filesViewedState map[string]pull_model.ViewedState) []FileDiffFile {
70+
files := make([]FileDiffFile, 0, len(diffTree.Files))
71+
72+
for _, file := range diffTree.Files {
73+
nameHash := git.HashFilePathForWebUI(file.HeadPath)
74+
isSubmodule := file.HeadMode == git.EntryModeCommit
75+
isBinary := file.HeadMode == git.EntryModeExec
76+
77+
isViewed := false
78+
if fileViewedState, ok := filesViewedState[file.Path()]; ok {
79+
isViewed = (fileViewedState == pull_model.Viewed)
80+
}
81+
82+
files = append(files, FileDiffFile{
83+
Name: file.HeadPath,
84+
NameHash: nameHash,
85+
IsSubmodule: isSubmodule,
86+
IsBinary: isBinary,
87+
IsViewed: isViewed,
88+
Status: file.Status,
89+
})
90+
}
91+
92+
return files
93+
}

services/gitdiff/git_diff_tree.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ type DiffTreeRecord struct {
3434
BaseBlobID string
3535
}
3636

37+
func (d *DiffTreeRecord) Path() string {
38+
if d.HeadPath != "" {
39+
return d.HeadPath
40+
}
41+
return d.BasePath
42+
}
43+
3744
// GetDiffTree returns the list of path of the files that have changed between the two commits.
3845
// If useMergeBase is true, the diff will be calculated using the merge base of the two commits.
3946
// This is the same behavior as using a three-dot diff in git diff.

templates/repo/diff/box.tmpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
</div>
5959
{{end}}
6060
<script id="diff-data-script" type="module">
61-
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}}];
61+
const diffDataFiles = [{{range $i, $file := .DiffFiles}}
62+
{Name:"{{$file.Name}}",NameHash:"{{$file.NameHash}}",Status:{{$file.Status}},IsSubmodule:{{$file.IsSubmodule}},IsViewed:{{$file.IsViewed}}},{{end}}];
6263
const diffData = {
6364
isIncomplete: {{.Diff.IsIncomplete}},
6465
tooManyFilesMessage: "{{ctx.Locale.Tr "repo.diff.too_many_files"}}",

templates/repo/diff/options_dropdown.tmpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<div class="ui dropdown tiny basic button" data-tooltip-content="{{ctx.Locale.Tr "repo.diff.options_button"}}">
22
{{svg "octicon-kebab-horizontal"}}
33
<div class="menu">
4-
<a class="item" id="show-file-list-btn">{{ctx.Locale.Tr "repo.diff.show_diff_stats"}}</a>
54
{{if .Issue.Index}}
65
<a class="item" href="{{$.RepoLink}}/pulls/{{.Issue.Index}}.patch" download="{{.Issue.Index}}.patch">{{ctx.Locale.Tr "repo.diff.download_patch"}}</a>
76
<a class="item" href="{{$.RepoLink}}/pulls/{{.Issue.Index}}.diff" download="{{.Issue.Index}}.diff">{{ctx.Locale.Tr "repo.diff.download_diff"}}</a>

web_src/js/components/DiffFileList.vue

Lines changed: 0 additions & 60 deletions
This file was deleted.

web_src/js/components/DiffFileTree.vue

Lines changed: 6 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,18 @@
11
<script lang="ts" setup>
2-
import DiffFileTreeItem, {type Item} from './DiffFileTreeItem.vue';
3-
import {loadMoreFiles} from '../features/repo-diff.ts';
2+
import DiffFileTreeItem from './DiffFileTreeItem.vue';
43
import {toggleElem} from '../utils/dom.ts';
54
import {diffTreeStore} from '../modules/stores.ts';
65
import {setFileFolding} from '../features/file-fold.ts';
7-
import {computed, onMounted, onUnmounted} from 'vue';
6+
import {computed, nextTick, onMounted, onUnmounted} from 'vue';
7+
import {pathListToTree, mergeChildIfOnlyOneDir} from './file_tree.ts';
88
99
const LOCAL_STORAGE_KEY = 'diff_file_tree_visible';
1010
1111
const store = diffTreeStore();
1212
1313
const fileTree = computed(() => {
14-
const result: Array<Item> = [];
15-
for (const file of store.files) {
16-
// Split file into directories
17-
const splits = file.Name.split('/');
18-
let index = 0;
19-
let parent = null;
20-
let isFile = false;
21-
for (const split of splits) {
22-
index += 1;
23-
// reached the end
24-
if (index === splits.length) {
25-
isFile = true;
26-
}
27-
let newParent: Item = {
28-
name: split,
29-
children: [],
30-
isFile,
31-
};
32-
33-
if (isFile === true) {
34-
newParent.file = file;
35-
}
36-
37-
if (parent) {
38-
// check if the folder already exists
39-
const existingFolder = parent.children.find(
40-
(x) => x.name === split,
41-
);
42-
if (existingFolder) {
43-
newParent = existingFolder;
44-
} else {
45-
parent.children.push(newParent);
46-
}
47-
} else {
48-
const existingFolder = result.find((x) => x.name === split);
49-
if (existingFolder) {
50-
newParent = existingFolder;
51-
} else {
52-
result.push(newParent);
53-
}
54-
}
55-
parent = newParent;
56-
}
57-
}
58-
const mergeChildIfOnlyOneDir = (entries: Array<Record<string, any>>) => {
59-
for (const entry of entries) {
60-
if (entry.children) {
61-
mergeChildIfOnlyOneDir(entry.children);
62-
}
63-
if (entry.children.length === 1 && entry.children[0].isFile === false) {
64-
// Merge it to the parent
65-
entry.name = `${entry.name}/${entry.children[0].name}`;
66-
entry.children = entry.children[0].children;
67-
}
68-
}
69-
};
70-
// Merge folders with just a folder as children in order to
71-
// reduce the depth of our tree.
72-
mergeChildIfOnlyOneDir(result);
14+
const result = pathListToTree(store.files);
15+
mergeChildIfOnlyOneDir(result); // mutation
7316
return result;
7417
});
7518
@@ -78,8 +21,8 @@ onMounted(() => {
7821
store.fileTreeIsVisible = localStorage.getItem(LOCAL_STORAGE_KEY) !== 'false';
7922
document.querySelector('.diff-toggle-file-tree-button').addEventListener('click', toggleVisibility);
8023
81-
hashChangeListener();
8224
window.addEventListener('hashchange', hashChangeListener);
25+
nextTick(hashChangeListener);
8326
});
8427
8528
onUnmounted(() => {
@@ -121,19 +64,12 @@ function updateState(visible: boolean) {
12164
toggleElem(toShow, !visible);
12265
toggleElem(toHide, visible);
12366
}
124-
125-
function loadMoreData() {
126-
loadMoreFiles(store.linkLoadMore);
127-
}
12867
</script>
12968

13069
<template>
13170
<div v-if="store.fileTreeIsVisible" class="diff-file-tree-items">
13271
<!-- only render the tree if we're visible. in many cases this is something that doesn't change very often -->
13372
<DiffFileTreeItem v-for="item in fileTree" :key="item.name" :item="item"/>
134-
<div v-if="store.isIncomplete" class="tw-pt-1">
135-
<a :class="['ui', 'basic', 'tiny', 'button', store.isLoadingNewData ? 'disabled' : '']" @click.stop="loadMoreData">{{ store.showMoreMessage }}</a>
136-
</div>
13773
</div>
13874
</template>
13975

0 commit comments

Comments
 (0)