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"
|
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-27 16:44:30 +00:00
|
|
|
scanIndexAt := "HEAD"
|
|
|
|
if ref == nil {
|
|
|
|
scanIndexAt = git.RefBeforeFirstCommit
|
|
|
|
}
|
|
|
|
|
2014-10-27 20:40:21 +00:00
|
|
|
if porcelain {
|
2016-11-29 17:02:21 +00:00
|
|
|
porcelainStagedPointers(scanIndexAt)
|
2014-10-27 20:40:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-29 20:04:05 +00:00
|
|
|
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-29 17:02:21 +00:00
|
|
|
|
2016-11-18 18:48:35 +00:00
|
|
|
var unstagedPointers []*lfs.WrappedPointer
|
2016-11-29 17:02:21 +00:00
|
|
|
indexScanner := lfs.NewGitScanner(func(p *lfs.WrappedPointer, err error) {
|
|
|
|
if err != nil {
|
|
|
|
ExitWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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":
|
2016-11-18 18:48:35 +00:00
|
|
|
unstagedPointers = append(unstagedPointers, p)
|
2014-10-27 20:40:21 +00:00
|
|
|
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
|
|
|
}
|
2016-11-29 17:02:21 +00:00
|
|
|
})
|
|
|
|
|
2016-11-29 17:11:23 +00:00
|
|
|
if err := indexScanner.ScanIndex(scanIndexAt, nil); err != nil {
|
2016-11-29 17:02:21 +00:00
|
|
|
ExitWithError(err)
|
2014-10-27 19:47:07 +00:00
|
|
|
}
|
2014-10-27 16:52:28 +00:00
|
|
|
|
2016-11-29 17:02:21 +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
|
|
|
|
2016-11-29 17:02:21 +00:00
|
|
|
Print("")
|
2014-10-27 16:52:28 +00:00
|
|
|
}
|
2014-10-27 16:42:38 +00:00
|
|
|
|
2016-11-29 20:04:05 +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() {
|
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
|
|
|
}
|