File: lfstest-testutils.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (87 lines) | stat: -rw-r--r-- 2,181 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//go:build testtools
// +build testtools

package main

import (
	"encoding/json"
	"fmt"
	"io"
	"os"
	"path/filepath"

	. "github.com/git-lfs/git-lfs/v3/t/cmd/util"
)

type TestUtilRepoCallback struct{}

func (*TestUtilRepoCallback) Fatalf(format string, args ...interface{}) {
	fmt.Fprintf(os.Stderr, format, args...)
	os.Exit(4)
}
func (*TestUtilRepoCallback) Errorf(format string, args ...interface{}) {
	fmt.Fprintf(os.Stderr, format, args...)
}

func main() {
	commandMap := map[string]func(*Repo){
		"addcommits": AddCommits,
	}
	if len(os.Args) < 2 {
		fmt.Fprintf(os.Stderr, "Command required (e.g. addcommits)\n")
		os.Exit(2)
	}

	f, ok := commandMap[os.Args[1]]
	if !ok {
		fmt.Fprintf(os.Stderr, "Unknown command: %v\n", os.Args[1])
		os.Exit(2)
	}
	// Construct test repo context (note: no Cleanup() call since managed outside)
	// also assume we're in the same folder
	wd, err := os.Getwd()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Problem getting working dir: %v\n", err)
		os.Exit(2)
	}
	// Make sure we're directly inside directory which contains .git
	// don't want to accidentally end up committing to some other parent git
	_, err = os.Stat(filepath.Join(wd, ".git"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "You're in the wrong directory, should be in root of a test repo: %v\n", err)
		os.Exit(2)
	}

	repo := WrapRepo(&TestUtilRepoCallback{}, wd)
	f(repo)
}

func AddCommits(repo *Repo) {
	// Read stdin as JSON []*CommitInput
	in, err := io.ReadAll(os.Stdin)
	if err != nil {
		fmt.Fprintf(os.Stderr, "addcommits: Unable to read input data: %v\n", err)
		os.Exit(3)
	}
	inputs := make([]*CommitInput, 0)
	err = json.Unmarshal(in, &inputs)
	if err != nil {
		fmt.Fprintf(os.Stderr, "addcommits: Unable to unmarshal JSON: %v\n%v\n", string(in), err)
		os.Exit(3)
	}
	outputs := repo.AddCommits(inputs)

	by, err := json.Marshal(outputs)
	if err != nil {
		fmt.Fprintf(os.Stderr, "addcommits: Unable to marshal output JSON: %v\n", err)
		os.Exit(3)
	}
	// Write response to stdout
	_, err = os.Stdout.Write(by)
	if err != nil {
		fmt.Fprintf(os.Stderr, "addcommits: Error writing JSON to stdout: %v\n", err)
		os.Exit(3)
	}
	os.Stdout.WriteString("\n")

}