diff --git a/commands/command_pointer.go b/commands/command_pointer.go index c6595283..1270f103 100644 --- a/commands/command_pointer.go +++ b/commands/command_pointer.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io" + "io/ioutil" "os" "github.com/git-lfs/git-lfs/git" @@ -18,6 +19,7 @@ var ( pointerFile string pointerCompare string pointerStdin bool + pointerCheck bool ) func pointerCommand(cmd *cobra.Command, args []string) { @@ -26,6 +28,36 @@ func pointerCommand(cmd *cobra.Command, args []string) { 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 + } + if len(pointerCompare) > 0 || pointerStdin { comparing = true } @@ -129,5 +161,6 @@ func init() { 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.") }) } diff --git a/docs/man/git-lfs-pointer.1.ronn b/docs/man/git-lfs-pointer.1.ronn index 75eeeec0..b1b6ccce 100644 --- a/docs/man/git-lfs-pointer.1.ronn +++ b/docs/man/git-lfs-pointer.1.ronn @@ -1,11 +1,12 @@ -git-lfs-pointer(1) -- Build and compare pointers -================================================ +git-lfs-pointer(1) -- Build, compare, and check pointers +======================================================== ## SYNOPSIS `git lfs pointer --file=path/to/file`
`git lfs pointer --file=path/to/file --pointer=path/to/pointer`
`git lfs pointer --file=path/to/file --stdin` +`git lfs pointer --check --file=path/to/file` ## Description @@ -25,6 +26,12 @@ between different Git LFS implementations. Reads the pointer from STDIN to compare with the pointer generated from `--file`. +* `--check`: + Reads the pointer from STDIN (if `--stdin` is given) or the filepath (if + `--file`) is given. If neither or both of `--stdin` and `--file` are given, + the invocation is invalid. Exits 0 if the data read is a valid Git LFS + pointer. Exits 1 otherwise. + ## SEE ALSO Part of the git-lfs(1) suite. diff --git a/t/t-pointer.sh b/t/t-pointer.sh index e744bc9b..623fcea2 100755 --- a/t/t-pointer.sh +++ b/t/t-pointer.sh @@ -2,7 +2,6 @@ . "$(dirname "$0")/testlib.sh" - begin_test "pointer --file --stdin" ( set -e @@ -302,3 +301,57 @@ begin_test "pointer to console" grep "oid sha256:e96ec1bd71eea8df78b24c64a7ab9d42dd7f821c4e503f0e2288273b9bff6c16" pointer.txt ) end_test + +begin_test "pointer --check (with valid pointer)" +( + set -e + + reponame="pointer---check-valid-pointer" + git init "$reponame" + cd "$reponame" + + echo "contents" > good.txt + git lfs pointer --file good.txt > good.ptr + + cat good.ptr + + git lfs pointer --check --file good.ptr + git lfs pointer --check --stdin < good.ptr +) +end_test + +begin_test "pointer --check (with invalid pointer)" +( + set -e + + reponame="pointer---check-invalid-pointer" + git init "$reponame" + cd "$reponame" + + echo "not-a-pointer" > bad.ptr + + ! git lfs pointer --check --file bad.ptr + ! git lfs pointer --check --stdin < bad.ptr +) +end_test + +begin_test "pointer --check (with invalid arguments)" +( + set -e + + reponame="pointer---check-invalid-pointer" + git init "$reponame" + cd "$reponame" + + touch a.txt + + # git-lfs-pointer(1) --check with invalid combination --compare + ! git lfs pointer --check --compare + + # git-lfs-pointer(1) --check without --file or --stdin + ! git lfs pointer --check + + # git-lfs-pointer(1) --check with --file and --stdin + ! git lfs pointer --check --file a.txt --stdin +) +end_test