| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"os" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/git-lfs/pktline" |
| 11 |
) |
| 12 |
|
| 13 |
func main() { |
| 14 |
pl := pktline.NewPktline(os.Stdin, os.Stdout) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
var command, pathname string |
| 28 |
|
| 29 |
for { |
| 30 |
b, n, err := pl.ReadPacketWithLength() |
| 31 |
if err != nil { |
| 32 |
return |
| 33 |
} |
| 34 |
|
| 35 |
if n == 0 { |
| 36 |
pl.WriteFlush() |
| 37 |
} else if n == 1 { |
| 38 |
pl.WriteDelim() |
| 39 |
} else { |
| 40 |
parts := strings.SplitN(string(b), "=", 2) |
| 41 |
if len(parts) == 2 { |
| 42 |
if parts[0] == "command" { |
| 43 |
command = strings.TrimSuffix(parts[1], "\n") |
| 44 |
} else if parts[0] == "pathname" { |
| 45 |
pathname = strings.TrimSuffix(parts[1], "\n") |
| 46 |
} |
| 47 |
|
| 48 |
if command != "" && pathname != "" { |
| 49 |
if command == "clean" { |
| 50 |
if err = os.Rename(pathname, pathname+".orig"); err != nil { |
| 51 |
panic(err) |
| 52 |
} |
| 53 |
|
| 54 |
f, err := os.OpenFile(pathname, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644) |
| 55 |
if err != nil { |
| 56 |
panic(err) |
| 57 |
} |
| 58 |
f.Close() |
| 59 |
} |
| 60 |
|
| 61 |
command = "" |
| 62 |
pathname = "" |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
pl.WritePacket(b) |
| 67 |
} |
| 68 |
} |
| 69 |
} |