repochecker: Track and report binary files found in repository

This commit is contained in:
nemunaire 2021-11-13 17:27:32 +01:00
parent 724b00b825
commit 3625af47d9
2 changed files with 62 additions and 2 deletions

View File

@ -16,6 +16,8 @@ RUN go build -v -o repochecker/repochecker ./repochecker
FROM alpine
RUN apk add --no-cache git
ENTRYPOINT ["/usr/bin/repochecker"]
COPY --from=gobuild /go/src/srs.epita.fr/fic-server/repochecker/repochecker /usr/bin/repochecker

View File

@ -1,13 +1,17 @@
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"srs.epita.fr/fic-server/admin/sync"
"srs.epita.fr/fic-server/libfic"
@ -15,6 +19,38 @@ import (
var skipFileChecks = false
func searchBinaryInGit(edir string) (ret []string) {
// Check if git exists and if we are in a git repo
err := exec.Command("git", "-C", edir, "remote").Run()
if err == nil {
cmd := exec.Command("git", "-C", edir, "log", "--all", "--numstat")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err == nil {
scanner := bufio.NewScanner(&out)
commit := ""
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "commit ") {
commit = strings.TrimPrefix(scanner.Text(), "commit ")
} else if strings.HasPrefix(scanner.Text(), "-\t-\t") {
fname := strings.TrimPrefix(scanner.Text(), "-\t-\t")
if fname == "heading.jpg" {
continue
}
ret = append(ret, fmt.Sprintf("%s (%s)", fname, commit[:7]))
}
}
}
}
return
}
func checkExercice(theme fic.Theme, edir string, dmap *map[int64]fic.Exercice) (errs []string) {
e, _, eid, _, berrs := sync.BuildExercice(sync.GlobalImporter, theme, path.Join(theme.Path, edir), dmap)
errs = append(errs, berrs...)
@ -101,7 +137,6 @@ func main() {
}
defer os.RemoveAll(fic.FilesDir)
if sync.GlobalImporter != nil {
log.Println("Using", sync.GlobalImporter.Kind())
@ -129,7 +164,7 @@ func main() {
p = path.Clean(p)
}
sync.GlobalImporter = sync.LocalImporter{
Base: path.Dir(p),
Base: path.Dir(p),
Symlink: true,
}
p = path.Base(p)
@ -161,6 +196,16 @@ func main() {
log.Printf("================================== Exercice %q treated\n", edir)
}
bfile := searchBinaryInGit(path.Join(sync.GlobalImporter.(sync.LocalImporter).Base, p))
nberr += len(bfile)
if len(bfile) > 0 {
fmt.Printf("\n")
log.Println("There are some binary files in your git repository, they HAVE TO use LFS instead:")
for _, f := range bfile {
log.Println(" -", f)
}
}
fmt.Printf("\n")
log.Printf("Theme %q treated. %d error(s) reported.\n\n", p, nberr)
} else {
@ -170,6 +215,19 @@ func main() {
nberr += 1
log.Println(err)
}
bfile := searchBinaryInGit(path.Join(sync.GlobalImporter.(sync.LocalImporter).Base, p))
if len(bfile) > 0 {
fmt.Printf("\n")
log.Println("There are some binary files in your git repository, they HAVE TO use LFS instead:")
for _, f := range bfile {
if strings.HasPrefix(f, p) {
nberr += 1
log.Println(" -", f)
}
}
}
fmt.Printf("\n")
log.Printf("Exercice %q treated. %d error(s) reported.\n", p, nberr)
}