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"
|
2015-04-23 16:20:36 +00:00
|
|
|
"github.com/github/git-lfs/lfs"
|
2016-05-23 18:02:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
2014-10-21 14:21:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-10-27 20:40:21 +00:00
|
|
|
porcelain = false
|
2014-10-21 14:21:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func statusCommand(cmd *cobra.Command, args []string) {
|
2015-09-08 16:23:27 +00:00
|
|
|
requireInRepo()
|
2015-09-08 15:29:53 +00:00
|
|
|
|
2016-09-26 14:24:43 +00:00
|
|
|
// tolerate errors getting ref so this works before first commit
|
|
|
|
ref, _ := git.CurrentRef()
|
2014-10-21 14:21:47 +00:00
|
|
|
|
2016-09-26 15:09:58 +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
|
|
|
|
2014-10-27 20:40:21 +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
|
|
|
|
}
|
|
|
|
|
2016-09-26 14:24:43 +00:00
|
|
|
if ref != nil {
|
2014-10-27 16:42:38 +00:00
|
|
|
|
2016-09-26 14:24:43 +00:00
|
|
|
Print("On branch %s", ref.Name)
|
2014-10-28 19:22:38 +00:00
|
|
|
|
2016-09-26 14:24:43 +00:00
|
|
|
remoteRef, err := git.CurrentRemoteRef()
|
|
|
|
if err == nil {
|
2014-10-27 19:47:07 +00:00
|
|
|
|
2016-09-26 14:24:43 +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-28 19:22:38 +00:00
|
|
|
}
|
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 {
|
2014-10-27 20:40:21 +00:00
|
|
|
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))
|
2014-10-27 20:40:21 +00:00
|
|
|
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() {
|
2016-09-01 16:09:38 +00:00
|
|
|
RegisterCommand("status", statusCommand, func(cmd *cobra.Command) {
|
2016-08-10 15:33:25 +00:00
|
|
|
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
|
|
|
}
|