git-lfs/commands/command_status.go

105 lines
2.1 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
2015-03-19 19:30:55 +00:00
"github.com/github/git-lfs/git"
"github.com/github/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
stagedPointers, err := lfs.ScanIndex(ref == nil)
2014-10-27 16:52:28 +00:00
if err != nil {
2015-03-19 19:30:55 +00:00
Panic(err, "Could not scan staging for Git LFS objects")
2014-10-21 14:21:47 +00:00
}
2014-10-27 16:42:38 +00:00
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
}
if ref != nil {
2014-10-27 16:42:38 +00:00
Print("On branch %s", ref.Name)
remoteRef, err := git.CurrentRemoteRef()
if err == nil {
2014-10-27 19:47:07 +00:00
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))
}
}
2014-10-27 16:42:38 +00:00
}
2014-10-21 14:21:47 +00:00
2015-03-19 19:30:55 +00:00
Print("\nGit LFS objects to be committed:\n")
2014-10-27 19:47:07 +00:00
for _, p := range stagedPointers {
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":
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
}
}
2014-10-27 16:52:28 +00:00
2015-03-19 19:30:55 +00:00
Print("\nGit LFS objects not staged for commit:\n")
2014-10-27 16:52:28 +00:00
for _, p := range stagedPointers {
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
2014-10-27 16:52:28 +00:00
Print("")
}
2014-10-27 16:42:38 +00:00
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
}