func doFsck() (bool, error) { requireInRepo() ref, err := git.CurrentRef() if err != nil { return false, err } // The LFS scanner methods return unexported *lfs.wrappedPointer objects. // All we care about is the pointer OID and file name pointerIndex := make(map[string]string) pointers, err := lfs.ScanRefs(ref.Sha, "", nil) if err != nil { return false, err } for _, p := range pointers { pointerIndex[p.Oid] = p.Name } // TODO(zeroshirts): do we want to look for LFS stuff in past commits? p2, err := lfs.ScanIndex() if err != nil { return false, err } for _, p := range p2 { pointerIndex[p.Oid] = p.Name } ok := true for oid, name := range pointerIndex { path := lfs.LocalMediaPathReadOnly(oid) Debug("Examining %v (%v)", name, path) f, err := os.Open(path) if pErr, pOk := err.(*os.PathError); pOk { Print("Object %s (%s) could not be checked: %s", name, oid, pErr.Err) ok = false continue } if err != nil { return false, err } oidHash := sha256.New() _, err = io.Copy(oidHash, f) f.Close() if err != nil { return false, err } recalculatedOid := hex.EncodeToString(oidHash.Sum(nil)) if recalculatedOid != oid { ok = false Print("Object %s (%s) is corrupt", name, oid) if fsckDryRun { continue } badDir := filepath.Join(config.LocalGitStorageDir, "lfs", "bad") if err := os.MkdirAll(badDir, 0755); err != nil { return false, err } badFile := filepath.Join(badDir, oid) if err := os.Rename(path, badFile); err != nil { return false, err } Print(" moved to %s", badFile) } } return ok, nil }
func statusCommand(cmd *cobra.Command, args []string) { requireInRepo() ref, err := git.CurrentRef() if err != nil { Panic(err, "Could not get the current ref") } stagedPointers, err := lfs.ScanIndex() if err != nil { Panic(err, "Could not scan staging for Git LFS objects") } if porcelain { for _, p := range stagedPointers { switch p.Status { case "R", "C": Print("%s %s -> %s %d", p.Status, p.SrcName, p.Name, p.Size) case "M": Print(" %s %s %d", p.Status, p.Name, p.Size) default: Print("%s %s %d", p.Status, p.Name, p.Size) } } return } Print("On branch %s", ref.Name) remoteRef, err := git.CurrentRemoteRef() if err == nil { pointers, err := lfs.ScanRefs(ref.Sha, "^"+remoteRef.Sha, nil) if err != nil { Panic(err, "Could not scan for Git LFS objects") } Print("Git LFS objects to be pushed to %s:\n", remoteRef.Name) for _, p := range pointers { Print("\t%s (%s)", p.Name, humanizeBytes(p.Size)) } } Print("\nGit LFS objects to be committed:\n") for _, p := range stagedPointers { switch p.Status { case "R", "C": Print("\t%s -> %s (%s)", p.SrcName, p.Name, humanizeBytes(p.Size)) case "M": default: Print("\t%s (%s)", p.Name, humanizeBytes(p.Size)) } } Print("\nGit LFS objects not staged for commit:\n") for _, p := range stagedPointers { if p.Status == "M" { Print("\t%s", p.Name) } } Print("") }