git-lfs/commands/command_status.go

144 lines
2.9 KiB
Go
Raw Normal View History

2014-10-21 14:21:47 +00:00
package commands
import (
2014-10-27 21:14:46 +00:00
"fmt"
2015-05-13 19:43:41 +00:00
2016-11-15 17:01:18 +00:00
"github.com/git-lfs/git-lfs/git"
"github.com/git-lfs/git-lfs/lfs"
"github.com/spf13/cobra"
2014-10-21 14:21:47 +00:00
)
var (
porcelain = false
2014-10-21 14:21:47 +00:00
)
func statusCommand(cmd *cobra.Command, args []string) {
requireInRepo()
// tolerate errors getting ref so this works before first commit
ref, _ := git.CurrentRef()
2014-10-21 14:21:47 +00:00
scanIndexAt := "HEAD"
if ref == nil {
scanIndexAt = git.RefBeforeFirstCommit
}
if porcelain {
porcelainStagedPointers(scanIndexAt)
return
}
statusScanRefRange(ref)
2014-10-21 14:21:47 +00:00
2015-03-19 19:30:55 +00:00
Print("\nGit LFS objects to be committed:\n")
2016-11-18 18:48:35 +00:00
var unstagedPointers []*lfs.WrappedPointer
indexScanner := lfs.NewGitScanner(func(p *lfs.WrappedPointer, err error) {
if err != nil {
ExitWithError(err)
return
}
switch p.Status {
case "R", "C":
2014-10-27 21:14:46 +00:00
Print("\t%s -> %s (%s)", p.SrcName, p.Name, humanizeBytes(p.Size))
case "M":
2016-11-18 18:48:35 +00:00
unstagedPointers = append(unstagedPointers, p)
default:
2014-10-27 21:14:46 +00:00
Print("\t%s (%s)", p.Name, humanizeBytes(p.Size))
2014-10-27 19:47:07 +00:00
}
})
if err := indexScanner.ScanIndex(scanIndexAt, nil); err != nil {
ExitWithError(err)
2014-10-27 19:47:07 +00:00
}
2014-10-27 16:52:28 +00:00
indexScanner.Close()
2015-03-19 19:30:55 +00:00
Print("\nGit LFS objects not staged for commit:\n")
2016-11-18 18:48:35 +00:00
for _, p := range unstagedPointers {
2014-10-27 19:47:07 +00:00
if p.Status == "M" {
Print("\t%s", p.Name)
}
2014-10-27 16:52:28 +00:00
}
2014-10-27 19:47:07 +00:00
Print("")
2014-10-27 16:52:28 +00:00
}
2014-10-27 16:42:38 +00:00
func statusScanRefRange(ref *git.Ref) {
if ref == nil {
return
}
Print("On branch %s", ref.Name)
remoteRef, err := git.CurrentRemoteRef()
if err != nil {
return
}
gitscanner := lfs.NewGitScanner(func(p *lfs.WrappedPointer, err error) {
if err != nil {
Panic(err, "Could not scan for Git LFS objects")
return
}
Print("\t%s (%s)", p.Name, humanizeBytes(p.Size))
})
defer gitscanner.Close()
Print("Git LFS objects to be pushed to %s:\n", remoteRef.Name)
if err := gitscanner.ScanRefRange(ref.Sha, "^"+remoteRef.Sha, nil); err != nil {
Panic(err, "Could not scan for Git LFS objects")
}
}
func porcelainStagedPointers(ref string) {
gitscanner := lfs.NewGitScanner(func(p *lfs.WrappedPointer, err error) {
if err != nil {
ExitWithError(err)
}
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)
}
})
defer gitscanner.Close()
if err := gitscanner.ScanIndex(ref, nil); err != nil {
ExitWithError(err)
}
}
2014-10-27 21:27:40 +00:00
var byteUnits = []string{"B", "KB", "MB", "GB", "TB"}
2014-10-27 21:14:46 +00:00
func humanizeBytes(bytes int64) string {
2014-10-27 21:27:40 +00:00
var output string
2014-10-27 21:14:46 +00:00
size := float64(bytes)
if bytes < 1024 {
return fmt.Sprintf("%d B", bytes)
}
2014-10-27 21:27:40 +00:00
for _, unit := range byteUnits {
2014-10-27 21:14:46 +00:00
if size < 1024.0 {
2014-10-27 21:27:40 +00:00
output = fmt.Sprintf("%3.1f %s", size, unit)
break
2014-10-27 21:14:46 +00:00
}
size /= 1024.0
}
2014-10-27 21:27:40 +00:00
return output
2014-10-27 21:14:46 +00:00
}
2014-10-21 14:21:47 +00:00
func init() {
RegisterCommand("status", statusCommand, func(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&porcelain, "porcelain", "p", false, "Give the output in an easy-to-parse format for scripts.")
})
2014-10-21 14:21:47 +00:00
}