git-lfs/commands/command_pointer.go

167 lines
3.6 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"
"io/ioutil"
2015-04-22 17:41:41 +00:00
"os"
2015-05-13 19:43:41 +00:00
"github.com/git-lfs/git-lfs/git"
2016-11-15 17:01:18 +00:00
"github.com/git-lfs/git-lfs/lfs"
"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
pointerCheck bool
2015-04-22 17:41:41 +00:00
)
func pointerCommand(cmd *cobra.Command, args []string) {
comparing := false
something := false
buildOid := ""
compareOid := ""
if pointerCheck {
var r io.ReadCloser
var err error
if len(pointerCompare) > 0 {
ExitWithError(fmt.Errorf("fatal: cannot combine --check with --compare"))
}
if len(pointerFile) > 0 {
if pointerStdin {
ExitWithError(fmt.Errorf("fatal: with --check, --file cannot be combined with --stdin"))
}
r, err = os.Open(pointerFile)
if err != nil {
ExitWithError(err)
}
} else if pointerStdin {
r = ioutil.NopCloser(os.Stdin)
} else {
ExitWithError(fmt.Errorf("fatal: must specify either --file or --stdin with --compare"))
}
_, err = lfs.DecodePointer(r)
if err != nil {
os.Exit(1)
}
r.Close()
return
}
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)
fmt.Fprintf(os.Stderr, "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, err = git.HashObject(bytes.NewReader(buf.Bytes()))
if err != nil {
Error(err.Error())
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "\nGit blob OID: %s\n\n", buildOid)
2015-04-22 17:41:41 +00:00
}
} 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.Fprintf(os.Stderr, "Pointer from %s\n\n", pointerName)
2015-04-22 17:41:41 +00:00
if err != nil {
Error(err.Error())
os.Exit(1)
}
fmt.Fprintf(os.Stderr, buf.String())
2015-04-22 17:41:41 +00:00
if comparing {
compareOid, err = git.HashObject(bytes.NewReader(buf.Bytes()))
if err != nil {
Error(err.Error())
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "\nGit blob OID: %s\n", compareOid)
2015-04-22 17:41:41 +00:00
}
}
if comparing && buildOid != compareOid {
fmt.Fprintf(os.Stderr, "\nPointers do not match\n")
2015-04-22 17:41:41 +00:00
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")
2015-04-22 19:36:16 +00:00
}
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 init() {
RegisterCommand("pointer", pointerCommand, func(cmd *cobra.Command) {
cmd.Flags().StringVarP(&pointerFile, "file", "f", "", "Path to a local file to generate the pointer from.")
cmd.Flags().StringVarP(&pointerCompare, "pointer", "p", "", "Path to a local file containing a pointer built by another Git LFS implementation.")
cmd.Flags().BoolVarP(&pointerStdin, "stdin", "", false, "Read a pointer built by another Git LFS implementation through STDIN.")
cmd.Flags().BoolVarP(&pointerCheck, "check", "", false, "Check whether the given file is a Git LFS pointer.")
})
2015-04-22 17:41:41 +00:00
}