all repos — elgit @ a05244018ae02603c56a27f8f8b7006c0024f7ea

fork of legit: web frontend for git, written in go

git: check for binary files
Anirudh Oppiliappan x@icyphox.sh
Mon, 19 Dec 2022 11:36:50 +0530
commit

a05244018ae02603c56a27f8f8b7006c0024f7ea

parent

8ba1a9609adef8575a91219ef35eef97daad8d32

3 files changed, 13 insertions(+), 2 deletions(-)

jump to
M git/diff.gogit/diff.go
@@ -20,6 +20,7 @@ Old string 		New string
 	}
 	TextFragments []TextFragment
+	IsBinary      bool
 }
 
 // A nicer git diff representation.
@@ -88,6 +89,7 @@ for _, d := range diffs { 		ndiff := Diff{}
 		ndiff.Name.New = d.NewName
 		ndiff.Name.Old = d.OldName
+		ndiff.IsBinary = d.IsBinary
 
 		for _, tf := range d.TextFragments {
 			ndiff.TextFragments = append(ndiff.TextFragments, TextFragment{
M git/git.gogit/git.go
@@ -76,7 +76,13 @@ if err != nil { 		return "", err
 	}
 
-	return file.Contents()
+	isbin, _ := file.IsBinary()
+
+	if !isbin {
+		return file.Contents()
+	} else {
+		return "Not displaying binary file", nil
+	}
 }
 
 func (g *GitRepo) Tags() ([]*object.Tag, error) {
M templates/commit.htmltemplates/commit.html
@@ -55,7 +55,9 @@ {{ else }}           <a href="/{{ $repo }}/blob/{{ $this }}/{{ .Name.New }}">{{ .Name.New }}</a>
           {{- end -}}
           </div>
-
+          {{ if .IsBinary }}
+          <p>Not showing binary file.</p>
+          {{ else }}
             <pre>
             {{- range .TextFragments -}}
             <p>{{- .Header -}}</p>
@@ -71,6 +73,7 @@ <span class="diff-noop">{{ .String }}</span>               {{- end -}}
             {{- end -}}
             {{- end -}}
+          {{- end -}}
             </pre>
           </div>
         {{ end }}