Skip to content

Support for ignoring untracked files #43

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ usage: codeowners <path>...
-f, --file string CODEOWNERS file path
-h, --help show this help message
-o, --owner strings filter results by owner
-t, --tracked only show files tracked by git
-u, --unowned only show unowned files (can be combined with -o)

$ ls
Expand Down
61 changes: 58 additions & 3 deletions cmd/codeowners/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package main

import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"

Expand All @@ -17,11 +19,13 @@ func main() {
ownerFilters []string
showUnowned bool
codeownersPath string
trackedOnly bool
helpFlag bool
)
flag.StringSliceVarP(&ownerFilters, "owner", "o", nil, "filter results by owner")
flag.BoolVarP(&showUnowned, "unowned", "u", false, "only show unowned files (can be combined with -o)")
flag.StringVarP(&codeownersPath, "file", "f", "", "CODEOWNERS file path")
flag.BoolVarP(&trackedOnly, "tracked", "t", false, "only show files tracked by git")
flag.BoolVarP(&helpFlag, "help", "h", false, "show this help message")

flag.Usage = func() {
Expand All @@ -35,6 +39,11 @@ func main() {
os.Exit(0)
}

var trackedFiles map[string]bool
if trackedOnly {
trackedFiles = getTrackedFiles()
}

ruleset, err := loadCodeowners(codeownersPath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand All @@ -57,7 +66,12 @@ func main() {
for _, startPath := range paths {
// godirwalk only accepts directories, so we need to handle files separately
if !isDir(startPath) {
if err := printFileOwners(out, ruleset, startPath, ownerFilters, showUnowned); err != nil {
if err := printFileOwners(
out,
ruleset,
startPath,
ownerFilters,
showUnowned, trackedOnly, trackedFiles); err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err)
os.Exit(1)
}
Expand All @@ -71,7 +85,7 @@ func main() {

// Only show code owners for files, not directories
if !d.IsDir() {
return printFileOwners(out, ruleset, path, ownerFilters, showUnowned)
return printFileOwners(out, ruleset, path, ownerFilters, showUnowned, trackedOnly, trackedFiles)
}
return nil
})
Expand All @@ -83,7 +97,20 @@ func main() {
}
}

func printFileOwners(out io.Writer, ruleset codeowners.Ruleset, path string, ownerFilters []string, showUnowned bool) error {
func printFileOwners(
out io.Writer,
ruleset codeowners.Ruleset,
path string, ownerFilters []string,
showUnowned bool,
trackedOnly bool,
trackedFiles map[string]bool,
) error {
if trackedOnly {
if _, ok := trackedFiles[path]; !ok {
return nil
}
}

rule, err := ruleset.Match(path)
if err != nil {
return err
Expand Down Expand Up @@ -134,3 +161,31 @@ func isDir(path string) bool {
}
return info.IsDir()
}

func getTrackedFiles() map[string]bool {
// Ensure the script is run inside a Git repository
if _, err := os.Stat(".git"); os.IsNotExist(err) {
fmt.Fprintln(os.Stderr, "error: this is not a Git repository.")
os.Exit(1)
}

cmd := exec.Command("git", "ls-files")
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
fmt.Fprintln(os.Stderr, "Error running git ls-files:", err)
os.Exit(1)
}

var trackedFiles = make(map[string]bool)
files := strings.Split(out.String(), "\n")
for _, file := range files {
if file != "" {
trackedFiles[file] = true
}
}

return trackedFiles
}