File: lfs-ssh-echo.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 (154 lines) | stat: -rw-r--r-- 3,839 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//go:build testtools
// +build testtools

package main

import (
	"encoding/json"
	"fmt"
	"os"
	"os/exec"
	"runtime"
	"strings"
	"syscall"
	"time"
)

type sshResponse struct {
	Href      string            `json:"href"`
	Header    map[string]string `json:"header"`
	ExpiresAt time.Time         `json:"expires_at,omitempty"`
	ExpiresIn int               `json:"expires_in,omitempty"`
}

func shell() string {
	if runtime.GOOS == "windows" {
		return "bash"
	}
	return "sh"
}

func spawnCommand(command string) error {
	cmd := exec.Command(shell(), "-c", command)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err := cmd.Run()
	if e, ok := err.(*exec.ExitError); ok {
		var ws syscall.WaitStatus
		ws, ok = e.ProcessState.Sys().(syscall.WaitStatus)
		if ok {
			os.Exit(ws.ExitStatus())
		}
	}
	return err
}

func checkSufficientArgs(offset int) {
	if len(os.Args) < offset+2 {
		fmt.Fprintf(os.Stderr, "got %d args: %v", len(os.Args), os.Args)
		os.Exit(1)
	}
}

func main() {
	// expect args:
	//   lfs-ssh-echo [-p PORT [--]] git@127.0.0.1 "git-lfs-authenticate REPO OPERATION"
	//   lfs-ssh-echo [-p PORT [--]] git@127.0.0.1 "git-lfs-transfer REPO OPERATION"
	//   lfs-ssh-echo git@127.0.0.1 "git-upload-pack REPO"
	//   lfs-ssh-echo git@127.0.0.1 "git-receive-pack REPO"
	offset := 1

	checkSufficientArgs(offset)
	if masterArg, found := strings.CutPrefix(os.Args[offset], "-oControlMaster="); found {
		var master bool
		switch masterArg {
		case "yes":
			master = true
		case "no":
			master = false
		default:
			fmt.Fprintf(os.Stderr, "expected \"-oControlMaster=yes\" or \"-oControlMaster=no\", got %q", os.Args[offset])
			os.Exit(1)
		}
		if pathArg, found := strings.CutPrefix(os.Args[offset+1], "-oControlPath="); found {
			if master {
				if file, err := os.OpenFile(pathArg, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666); err != nil {
					if os.IsExist(err) {
						fmt.Fprintf(os.Stderr, "expected %q to not exist", pathArg)
					} else {
						fmt.Fprintf(os.Stderr, "unable to create %q: %s", pathArg, err)
					}
					os.Exit(1)
				} else {
					file.Close()
					defer os.Remove(pathArg)
				}
			} else {
				if file, err := os.OpenFile(pathArg, os.O_RDONLY, 0); err != nil {
					if os.IsNotExist(err) {
						fmt.Fprintf(os.Stderr, "expected %q to exist", pathArg)
					} else {
						fmt.Fprintf(os.Stderr, "unable to open %q: %s", pathArg, err)
					}
					os.Exit(1)
				} else {
					file.Close()
				}
			}
		} else {
			fmt.Fprintf(os.Stderr, "expected \"-oControlPath\"")
			os.Exit(1)
		}
		offset += 2
	}

	checkSufficientArgs(offset)
	if os.Args[offset] == "-p" {
		offset += 2
	}

	checkSufficientArgs(offset)
	if os.Args[offset] == "--" {
		offset += 1
	}

	checkSufficientArgs(offset)
	if os.Args[offset] != "git@127.0.0.1" {
		fmt.Fprintf(os.Stderr, "expected \"git@127.0.0.1\", got %q", os.Args[offset])
		os.Exit(1)
	}

	// just "git-lfs-(authenticate|transfer) REPO OPERATION" or "git-(upload|receive)-pack REPO"
	remoteCmd := strings.Split(os.Args[offset+1], " ")
	if len(remoteCmd) < 2 {
		fmt.Fprintf(os.Stderr, "bad command line: %s\nargs: %v", remoteCmd, os.Args)
		os.Exit(1)
	}

	if remoteCmd[0] == "git-lfs-transfer" || remoteCmd[0] == "git-upload-pack" || remoteCmd[0] == "git-receive-pack" {
		err := spawnCommand(os.Args[offset+1])
		if err != nil {
			fmt.Fprintf(os.Stderr, "error running command %q: %v", remoteCmd[0], err)
			os.Exit(1)
		}
		return
	}

	repo := remoteCmd[1]

	r := &sshResponse{
		Href: fmt.Sprintf("http://127.0.0.1:%s/%s.git/info/lfs", os.Args[2], repo),
	}
	switch repo {
	case "/ssh-expired-absolute":
		r.ExpiresAt = time.Now().Add(-5 * time.Minute)
	case "/ssh-expired-relative":
		r.ExpiresIn = -5
	case "/ssh-expired-both":
		r.ExpiresAt = time.Now().Add(-5 * time.Minute)
		r.ExpiresIn = -5
	}

	json.NewEncoder(os.Stdout).Encode(r)
}