Skip to content

Commit

Permalink
Improve handling of line length errors. See #49
Browse files Browse the repository at this point in the history
  • Loading branch information
svent committed Dec 25, 2015
1 parent d5638db commit fc60949
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Options struct {
Cores int `short:"j" long:"cores" description:"limit used CPU Cores (default: 0 = all)" default-mask:"-"`
Count bool `short:"c" long:"count" description:"print count of matches per file" json:"-"`
IncludeDirs []string `long:"dirs" description:"recurse only into directories whose name matches GLOB" value-name:"GLOB" default-mask:"-"`
ErrShowLineLength bool `long:"err-show-line-length" description:"show all line length errors"`
ErrSkipLineLength bool `long:"err-skip-line-length" description:"skip line length errors"`
ExcludeDirs []string `long:"exclude-dirs" description:"do not recurse into directories whose name matches GLOB" value-name:"GLOB" default-mask:"-"`
IncludeExtensions string `short:"x" long:"ext" description:"limit search to specific file extensions (comma-separated)" default-mask:"-"`
Expand Down Expand Up @@ -554,6 +555,10 @@ func (o *Options) checkCompatibility(targets []string) error {
return errors.New("options 'binary-skip' and 'binary-text' cannot be used together")
}

if o.ErrSkipLineLength && o.ErrShowLineLength {
return errors.New("options 'err-skip-line-length' and 'err-show-line-length' cannot be used together")
}

if len(global.conditions) == 0 {
global.streamingAllowed = true

Expand Down
9 changes: 8 additions & 1 deletion sift.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ var global = struct {
termHighlightLineno string
termHighlightMatch string
termHighlightReset string
totalLineLengthErrors int64
totalMatchCount int64
totalResultCount int64
totalTargetCount int64
Expand Down Expand Up @@ -413,7 +414,8 @@ func processFileTargets() {
}
if err != nil {
if err == errLineTooLong {
if !options.ErrSkipLineLength {
global.totalLineLengthErrors += 1
if options.ErrShowLineLength {
errmsg := fmt.Sprintf("file contains very long lines (>= %d bytes). See options --blocksize and --err-skip-line-length.", InputBlockSize)
errorLogger.Printf("cannot process data from file '%s': %s\n", filepath, errmsg)
}
Expand Down Expand Up @@ -478,6 +480,7 @@ func executeSearch(targets []string) (ret int, err error) {
global.resultsDoneChan = make(chan struct{})
global.gitignoreCache = gitignore.NewGitIgnoreCache()
global.totalTargetCount = 0
global.totalLineLengthErrors = 0
global.totalMatchCount = 0
global.totalResultCount = 0

Expand Down Expand Up @@ -531,6 +534,10 @@ func executeSearch(targets []string) (ret int, err error) {
retVal = 1
}

if !options.ErrSkipLineLength && !options.ErrShowLineLength && global.totalLineLengthErrors > 0 {
errorLogger.Printf("%d files skipped due to very long lines (>= %d bytes). See options --blocksize, --err-show-line-length and --err-skip-line-length.", global.totalLineLengthErrors, InputBlockSize)
}

if options.Stats {
tend := time.Now()
fmt.Fprintln(os.Stderr, global.totalTargetCount, "files processed")
Expand Down

0 comments on commit fc60949

Please sign in to comment.