/
blob.go
108 lines (92 loc) 路 2.13 KB
/
blob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package cmd
import (
"fmt"
"github.com/charmbracelet/soft-serve/git"
"github.com/charmbracelet/soft-serve/pkg/backend"
"github.com/charmbracelet/soft-serve/pkg/ui/common"
"github.com/charmbracelet/soft-serve/pkg/ui/styles"
"github.com/spf13/cobra"
)
// blobCommand returns a command that prints the contents of a file.
func blobCommand() *cobra.Command {
var linenumber bool
var color bool
var raw bool
styles := styles.DefaultStyles()
cmd := &cobra.Command{
Use: "blob REPOSITORY [REFERENCE] [PATH]",
Aliases: []string{"cat", "show"},
Short: "Print out the contents of file at path",
Args: cobra.RangeArgs(1, 3),
PersistentPreRunE: checkIfReadable,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
be := backend.FromContext(ctx)
rn := args[0]
ref := ""
fp := ""
switch len(args) {
case 2:
fp = args[1]
case 3:
ref = args[1]
fp = args[2]
}
repo, err := be.Repository(ctx, rn)
if err != nil {
return err
}
r, err := repo.Open()
if err != nil {
return err
}
if ref == "" {
head, err := r.HEAD()
if err != nil {
return err
}
ref = head.ID
}
tree, err := r.LsTree(ref)
if err != nil {
return err
}
te, err := tree.TreeEntry(fp)
if err != nil {
return err
}
if te.Type() != "blob" {
return git.ErrFileNotFound
}
bts, err := te.Contents()
if err != nil {
return err
}
c := string(bts)
isBin, _ := te.File().IsBinary()
if isBin {
if raw {
cmd.Println(c)
} else {
return fmt.Errorf("binary file: use --raw to print")
}
} else {
if color {
c, err = common.FormatHighlight(fp, c)
if err != nil {
return err
}
}
if linenumber {
c, _ = common.FormatLineNumber(styles, c, color)
}
cmd.Println(c)
}
return nil
},
}
cmd.Flags().BoolVarP(&raw, "raw", "r", false, "Print raw contents")
cmd.Flags().BoolVarP(&linenumber, "linenumber", "l", false, "Print line numbers")
cmd.Flags().BoolVarP(&color, "color", "c", false, "Colorize output")
return cmd
}