git-lfs/commands/command_pointer.go

143 lines
3.0 KiB
Go
Raw Normal View History

2015-04-22 17:41:41 +00:00
package commands
import (
"bytes"
"crypto/sha256"
"encoding/hex"
2015-04-22 19:36:16 +00:00
"errors"
2015-04-22 17:41:41 +00:00
"fmt"
"io"
"os"
"os/exec"
2015-05-13 19:43:41 +00:00
"github.com/github/git-lfs/lfs"
2015-05-25 18:20:50 +00:00
"github.com/github/git-lfs/vendor/_nuts/github.com/spf13/cobra"
2015-04-22 17:41:41 +00:00
)
var (
2015-04-22 19:17:53 +00:00
pointerFile string
2015-04-22 17:41:41 +00:00
pointerCompare string
2015-04-22 19:36:16 +00:00
pointerStdin bool
2015-04-22 17:41:41 +00:00
pointerCmd = &cobra.Command{
Use: "pointer",
Short: "Build and compare pointers between different Git LFS implementations",
Run: pointerCommand,
}
)
func pointerCommand(cmd *cobra.Command, args []string) {
comparing := false
something := false
buildOid := ""
compareOid := ""
2015-04-22 19:36:16 +00:00
if len(pointerCompare) > 0 || pointerStdin {
2015-04-22 17:41:41 +00:00
comparing = true
}
2015-04-22 19:17:53 +00:00
if len(pointerFile) > 0 {
2015-04-22 17:41:41 +00:00
something = true
2015-04-22 19:17:53 +00:00
buildFile, err := os.Open(pointerFile)
2015-04-22 17:41:41 +00:00
if err != nil {
Error(err.Error())
os.Exit(1)
}
oidHash := sha256.New()
size, err := io.Copy(oidHash, buildFile)
buildFile.Close()
if err != nil {
Error(err.Error())
os.Exit(1)
}
ptr := lfs.NewPointer(hex.EncodeToString(oidHash.Sum(nil)), size, nil)
2015-04-22 19:17:53 +00:00
fmt.Printf("Git LFS pointer for %s\n\n", pointerFile)
2015-04-22 17:41:41 +00:00
buf := &bytes.Buffer{}
2015-04-24 18:17:11 +00:00
lfs.EncodePointer(io.MultiWriter(os.Stdout, buf), ptr)
2015-04-22 17:41:41 +00:00
if comparing {
buildOid = gitHashObject(buf.Bytes())
fmt.Printf("\nGit blob OID: %s\n\n", buildOid)
}
} else {
comparing = false
}
2015-04-22 19:36:16 +00:00
if len(pointerCompare) > 0 || pointerStdin {
2015-04-22 17:41:41 +00:00
something = true
2015-04-22 19:36:16 +00:00
compFile, err := pointerReader()
2015-04-22 17:41:41 +00:00
if err != nil {
Error(err.Error())
os.Exit(1)
}
buf := &bytes.Buffer{}
tee := io.TeeReader(compFile, buf)
2015-04-24 18:17:11 +00:00
_, err = lfs.DecodePointer(tee)
2015-04-22 17:41:41 +00:00
compFile.Close()
2015-04-22 19:36:16 +00:00
pointerName := "STDIN"
if !pointerStdin {
pointerName = pointerCompare
}
fmt.Printf("Pointer from %s\n\n", pointerName)
2015-04-22 17:41:41 +00:00
if err != nil {
Error(err.Error())
os.Exit(1)
}
fmt.Printf(buf.String())
if comparing {
compareOid = gitHashObject(buf.Bytes())
fmt.Printf("\nGit blob OID: %s\n", compareOid)
}
}
if comparing && buildOid != compareOid {
fmt.Printf("\nPointers do not match\n")
os.Exit(1)
}
if !something {
Error("Nothing to do!")
os.Exit(1)
}
}
2015-04-22 19:36:16 +00:00
func pointerReader() (io.ReadCloser, error) {
if len(pointerCompare) > 0 {
if pointerStdin {
return nil, errors.New("Cannot read from STDIN and --pointer.")
}
return os.Open(pointerCompare)
}
requireStdin("The --stdin flag expects a pointer file from STDIN.")
2015-04-22 19:36:16 +00:00
return os.Stdin, nil
}
2015-04-22 17:41:41 +00:00
func gitHashObject(by []byte) string {
cmd := exec.Command("git", "hash-object", "--stdin")
cmd.Stdin = bytes.NewReader(by)
out, err := cmd.Output()
if err != nil {
Error("Error building Git blob OID: %s", err)
os.Exit(1)
}
return string(bytes.TrimSpace(out))
}
func init() {
flags := pointerCmd.Flags()
2015-04-22 19:17:53 +00:00
flags.StringVarP(&pointerFile, "file", "f", "", "Path to a local file to generate the pointer from.")
flags.StringVarP(&pointerCompare, "pointer", "p", "", "Path to a local file containing a pointer built by another Git LFS implementation.")
2015-04-22 19:36:16 +00:00
flags.BoolVarP(&pointerStdin, "stdin", "", false, "Read a pointer built by another Git LFS implementation through STDIN.")
2015-04-22 17:41:41 +00:00
RootCmd.AddCommand(pointerCmd)
}