github.com/q2/git-lfs@v0.5.1-0.20150410234700-03a0d4cec40e/commands/command_status.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/github/git-lfs/git"
     6  	"github.com/github/git-lfs/scanner"
     7  	"github.com/spf13/cobra"
     8  )
     9  
    10  var (
    11  	statusCmd = &cobra.Command{
    12  		Use:   "status",
    13  		Short: "Show information about Git LFS objects that would be pushed",
    14  		Run:   statusCommand,
    15  	}
    16  	porcelain = false
    17  )
    18  
    19  func statusCommand(cmd *cobra.Command, args []string) {
    20  	ref, err := git.CurrentRef()
    21  	if err != nil {
    22  		Panic(err, "Could not get the current ref")
    23  	}
    24  
    25  	stagedPointers, err := scanner.ScanIndex()
    26  	if err != nil {
    27  		Panic(err, "Could not scan staging for Git LFS objects")
    28  	}
    29  
    30  	if porcelain {
    31  		for _, p := range stagedPointers {
    32  			switch p.Status {
    33  			case "R", "C":
    34  				Print("%s  %s -> %s %d", p.Status, p.SrcName, p.Name, p.Size)
    35  			case "M":
    36  				Print(" %s %s %d", p.Status, p.Name, p.Size)
    37  			default:
    38  				Print("%s  %s %d", p.Status, p.Name, p.Size)
    39  			}
    40  		}
    41  		return
    42  	}
    43  
    44  	branch, err := git.CurrentBranch()
    45  	if err != nil {
    46  		Panic(err, "Could not get current branch")
    47  	}
    48  	Print("On branch %s", branch)
    49  
    50  	remoteRef, err := git.CurrentRemoteRef()
    51  	if err == nil {
    52  
    53  		pointers, err := scanner.Scan(ref, "^"+remoteRef)
    54  		if err != nil {
    55  			Panic(err, "Could not scan for Git LFS objects")
    56  		}
    57  
    58  		remote, err := git.CurrentRemote()
    59  		if err != nil {
    60  			Panic(err, "Could not get current remote branch")
    61  		}
    62  
    63  		Print("Git LFS objects to be pushed to %s:\n", remote)
    64  		for _, p := range pointers {
    65  			Print("\t%s (%s)", p.Name, humanizeBytes(p.Size))
    66  		}
    67  	}
    68  
    69  	Print("\nGit LFS objects to be committed:\n")
    70  	for _, p := range stagedPointers {
    71  		switch p.Status {
    72  		case "R", "C":
    73  			Print("\t%s -> %s (%s)", p.SrcName, p.Name, humanizeBytes(p.Size))
    74  		case "M":
    75  		default:
    76  			Print("\t%s (%s)", p.Name, humanizeBytes(p.Size))
    77  		}
    78  	}
    79  
    80  	Print("\nGit LFS objects not staged for commit:\n")
    81  	for _, p := range stagedPointers {
    82  		if p.Status == "M" {
    83  			Print("\t%s", p.Name)
    84  		}
    85  	}
    86  
    87  	Print("")
    88  }
    89  
    90  var byteUnits = []string{"B", "KB", "MB", "GB", "TB"}
    91  
    92  func humanizeBytes(bytes int64) string {
    93  	var output string
    94  	size := float64(bytes)
    95  
    96  	if bytes < 1024 {
    97  		return fmt.Sprintf("%d B", bytes)
    98  	}
    99  
   100  	for _, unit := range byteUnits {
   101  		if size < 1024.0 {
   102  			output = fmt.Sprintf("%3.1f %s", size, unit)
   103  			break
   104  		}
   105  		size /= 1024.0
   106  	}
   107  	return output
   108  }
   109  
   110  func init() {
   111  	statusCmd.Flags().BoolVarP(&porcelain, "porcelain", "p", false, "Give the output in an easy-to-parse format for scripts.")
   112  	RootCmd.AddCommand(statusCmd)
   113  }