Skip to content

Commit 7c78077

Browse files
authored
Merge pull request #40 from github/windows-fd
Correct file descriptors on Windows
2 parents a493c57 + efb92bc commit 7c78077

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

status.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,37 @@ const (
110110
)
111111

112112
var (
113-
setupStatus sync.Once
114-
statusFile *os.File
113+
_setupStatus sync.Once
114+
statusFile *os.File
115115
)
116116

117-
func (s status) emitf(format string, args ...interface{}) {
118-
setupStatus.Do(func() {
119-
if *statusFdOpt > 0 {
117+
func setupStatus() {
118+
_setupStatus.Do(func() {
119+
if *statusFdOpt <= 0 {
120+
return
121+
}
122+
123+
const (
124+
unixStdout = 1
125+
unixStderr = 2
126+
)
127+
128+
// Even though Windows uses different numbers, we always equate 1/2 with
129+
// stdout/stderr because Git always passes `--status-fd=1`.
130+
switch *statusFdOpt {
131+
case unixStdout:
132+
statusFile = os.Stdout
133+
case unixStderr:
134+
statusFile = os.Stderr
135+
default:
120136
// TODO: debugging output if this fails
121137
statusFile = os.NewFile(uintptr(*statusFdOpt), "status")
122138
}
123139
})
140+
}
141+
142+
func (s status) emitf(format string, args ...interface{}) {
143+
setupStatus()
124144

125145
if statusFile == nil {
126146
return
@@ -133,12 +153,7 @@ func (s status) emitf(format string, args ...interface{}) {
133153
}
134154

135155
func (s status) emit() {
136-
setupStatus.Do(func() {
137-
if *statusFdOpt > 0 {
138-
// TODO: debugging output if this fails
139-
statusFile = os.NewFile(uintptr(*statusFdOpt), "status")
140-
}
141-
})
156+
setupStatus()
142157

143158
if statusFile == nil {
144159
return

0 commit comments

Comments
 (0)