Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak file listing to omit .git directory #1722

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions pkg/util/filelisting.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,34 @@ import (
"github.com/jedib0t/go-pretty/v6/text"
)

type details struct {
name string
count int
size int64
}

// ListFiles prints all files in a given directory to the provided writer
func ListFiles(w io.Writer, dir string) error {
var totalBytes int64
var totalFiles int
var footerDetails = map[string]details{}

t := table.NewWriter()
defer func() {
t.AppendSeparator()
t.AppendRow(
table.Row{
for _, specialDir := range footerDetails {
t.AppendRow(table.Row{
"", "", "", "",
humanReadableSize(totalBytes),
fmt.Sprintf("%d files", totalFiles),
},
)
humanReadableSize(specialDir.size),
fmt.Sprintf("%d files in %s", specialDir.count, specialDir.name),
})
}

t.AppendRow(table.Row{
"", "", "", "",
humanReadableSize(totalBytes),
fmt.Sprintf("%d files in total", totalFiles),
})

t.Render()
}()
Expand All @@ -60,6 +73,24 @@ func ListFiles(w io.Writer, dir string) error {
path = l
}

// update the total count and size
totalBytes += info.Size()
totalFiles++

// special handling for the .git directory, which would otherwise
// mostly clutter the output with potentially useless information
if strings.HasPrefix(path, ".git/") || path == ".git" {
dotGitDetails, ok := footerDetails[".git"]
if !ok {
dotGitDetails = details{name: ".git"}
}

dotGitDetails.size += info.Size()
dotGitDetails.count++
footerDetails[".git"] = dotGitDetails
return nil
}

// if possible, try to obtain nlink count and user/group details
var nlink, user, group string = "?", "?", "?"
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
Expand All @@ -68,16 +99,12 @@ func ListFiles(w io.Writer, dir string) error {
nlink = strconv.FormatUint(uint64(stat.Nlink), 10)
}

var size = info.Size()
totalBytes += size
totalFiles++

t.AppendRow(table.Row{
filemode(info),
nlink,
user,
group,
humanReadableSize(size),
humanReadableSize(info.Size()),
path,
})

Expand Down