Skip to content

Commit a36c620

Browse files
sonjeksilverwinddelvhGiteaBot
authored
Display file mode for new file and file mode changes (#24966)
This MR introduces the addition of file mode display support for both new file creation and file mode changes, following a similar approach as GitLab. GitLab: ![изображение](https://github.com/go-gitea/gitea/assets/1969460/4c0d0d74-30b2-486c-ac12-ef2355b04c96) Gitea: ![изображение](https://github.com/go-gitea/gitea/assets/1969460/8237fe99-2507-42c0-a40c-cd52ad355ab7) Replaces: #23159 Closes: #23021 --------- Co-authored-by: silverwind <[email protected]> Co-authored-by: delvh <[email protected]> Co-authored-by: Giteabot <[email protected]>
1 parent e06f3d2 commit a36c620

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

services/gitdiff/gitdiff.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ type DiffFile struct {
371371
IsViewed bool // User specific
372372
HasChangedSinceLastReview bool // User specific
373373
Language string
374+
Mode string
375+
OldMode string
374376
}
375377

376378
// GetType returns type of diff file.
@@ -501,6 +503,11 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader, ski
501503
}
502504
return diff, err
503505
}
506+
507+
prepareValue := func(s, p string) string {
508+
return strings.TrimSpace(strings.TrimPrefix(s, p))
509+
}
510+
504511
parsingLoop:
505512
for {
506513
// 1. A patch file always begins with `diff --git ` + `a/path b/path` (possibly quoted)
@@ -585,43 +592,55 @@ parsingLoop:
585592
}
586593
break parsingLoop
587594
}
595+
588596
switch {
589597
case strings.HasPrefix(line, cmdDiffHead):
590598
break curFileLoop
591599
case strings.HasPrefix(line, "old mode ") ||
592600
strings.HasPrefix(line, "new mode "):
601+
602+
if strings.HasPrefix(line, "old mode ") {
603+
curFile.OldMode = prepareValue(line, "old mode ")
604+
}
605+
if strings.HasPrefix(line, "new mode ") {
606+
curFile.Mode = prepareValue(line, "new mode ")
607+
}
608+
593609
if strings.HasSuffix(line, " 160000\n") {
594610
curFile.IsSubmodule = true
595611
}
596612
case strings.HasPrefix(line, "rename from "):
597613
curFile.IsRenamed = true
598614
curFile.Type = DiffFileRename
599615
if curFile.IsAmbiguous {
600-
curFile.OldName = line[len("rename from ") : len(line)-1]
616+
curFile.OldName = prepareValue(line, "rename from ")
601617
}
602618
case strings.HasPrefix(line, "rename to "):
603619
curFile.IsRenamed = true
604620
curFile.Type = DiffFileRename
605621
if curFile.IsAmbiguous {
606-
curFile.Name = line[len("rename to ") : len(line)-1]
622+
curFile.Name = prepareValue(line, "rename to ")
607623
curFile.IsAmbiguous = false
608624
}
609625
case strings.HasPrefix(line, "copy from "):
610626
curFile.IsRenamed = true
611627
curFile.Type = DiffFileCopy
612628
if curFile.IsAmbiguous {
613-
curFile.OldName = line[len("copy from ") : len(line)-1]
629+
curFile.OldName = prepareValue(line, "copy from ")
614630
}
615631
case strings.HasPrefix(line, "copy to "):
616632
curFile.IsRenamed = true
617633
curFile.Type = DiffFileCopy
618634
if curFile.IsAmbiguous {
619-
curFile.Name = line[len("copy to ") : len(line)-1]
635+
curFile.Name = prepareValue(line, "copy to ")
620636
curFile.IsAmbiguous = false
621637
}
622638
case strings.HasPrefix(line, "new file"):
623639
curFile.Type = DiffFileAdd
624640
curFile.IsCreated = true
641+
if strings.HasPrefix(line, "new file mode ") {
642+
curFile.Mode = prepareValue(line, "new file mode ")
643+
}
625644
if strings.HasSuffix(line, " 160000\n") {
626645
curFile.IsSubmodule = true
627646
}

templates/repo/diff/box.tmpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@
113113
{{if $file.IsVendored}}
114114
<span class="ui label gt-ml-3">{{$.locale.Tr "repo.diff.vendored"}}</span>
115115
{{end}}
116+
{{if and $file.Mode $file.OldMode}}
117+
<span class="gt-ml-4 gt-mono">{{$file.OldMode}} &rarr; {{$file.Mode}}</span>
118+
{{else if $file.Mode}}
119+
<span class="gt-ml-4 gt-mono">{{$file.Mode}}</span>
120+
{{end}}
116121
</div>
117122
<div class="diff-file-header-actions gt-df gt-ac">
118123
{{if $showFileViewToggle}}

0 commit comments

Comments
 (0)