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"
|
2015-05-13 19:43:41 +00:00
|
|
|
|
2017-08-02 17:21:43 +00:00
|
|
|
"github.com/git-lfs/git-lfs/git"
|
2016-11-15 17:01:18 +00:00
|
|
|
"github.com/git-lfs/git-lfs/lfs"
|
2016-05-23 18:02:27 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-07-14 14:49:13 +00:00
|
|
|
ptr := lfs.NewPointer(hex.EncodeToString(oidHash.Sum(nil)), size, nil)
|
2016-03-22 09:50:40 +00:00
|
|
|
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 {
|
2017-08-22 17:33:19 +00:00
|
|
|
buildOid, err = git.HashObject(bytes.NewReader(buf.Bytes()))
|
2017-08-02 17:21:43 +00:00
|
|
|
if err != nil {
|
|
|
|
Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-03-22 09:50:40 +00:00
|
|
|
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
|
|
|
|
}
|
2016-03-22 09:50:40 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-03-22 09:50:40 +00:00
|
|
|
fmt.Fprintf(os.Stderr, buf.String())
|
2015-04-22 17:41:41 +00:00
|
|
|
if comparing {
|
2017-08-22 17:33:19 +00:00
|
|
|
compareOid, err = git.HashObject(bytes.NewReader(buf.Bytes()))
|
2017-08-02 17:21:43 +00:00
|
|
|
if err != nil {
|
|
|
|
Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-03-22 09:50:40 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "\nGit blob OID: %s\n", compareOid)
|
2015-04-22 17:41:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if comparing && buildOid != compareOid {
|
2016-03-22 09:50:40 +00:00
|
|
|
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.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Open(pointerCompare)
|
|
|
|
}
|
|
|
|
|
2015-04-24 20:24:32 +00:00
|
|
|
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() {
|
2016-09-01 16:09:38 +00:00
|
|
|
RegisterCommand("pointer", pointerCommand, func(cmd *cobra.Command) {
|
2016-08-10 15:33:25 +00:00
|
|
|
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.")
|
|
|
|
})
|
2015-04-22 17:41:41 +00:00
|
|
|
}
|