33package tools
44
55import (
6- "bufio"
76 "encoding/hex"
87 "fmt"
98 "io"
@@ -239,7 +238,7 @@ func VerifyFileHash(oid, path string) error {
239238// FastWalkCallback is the signature for the callback given to FastWalkGitRepo()
240239type FastWalkCallback func (parentDir string , info os.FileInfo , err error )
241240
242- // FastWalkGitRepo is a more optimal implementation of filepath.Walk for a Git
241+ // FastWalkDir is a more optimal implementation of filepath.Walk for a Git
243242// repo. The callback guaranteed to be called sequentially. The function returns
244243// once all files and errors have triggered callbacks.
245244// It differs in the following ways:
@@ -248,18 +247,10 @@ type FastWalkCallback func(parentDir string, info os.FileInfo, err error)
248247// there are no other guarantees. Use parentDir argument in the callback to
249248// determine absolute path rather than tracking it yourself
250249// * Automatically ignores any .git directories
251- // * Respects .gitignore contents and skips ignored files/dirs
252250//
253251// rootDir - Absolute path to the top of the repository working directory
254- func FastWalkGitRepo (rootDir string , cb FastWalkCallback ) {
255- fastWalkCallback (fastWalkWithExcludeFiles (rootDir , ".gitignore" ), cb )
256- }
257-
258- // FastWalkGitRepoAll behaves as FastWalkGitRepo, with the additional caveat
259- // that it does not ignore paths and directories found in .gitignore file(s)
260- // throughout the repository.
261- func FastWalkGitRepoAll (rootDir string , cb FastWalkCallback ) {
262- fastWalkCallback (fastWalkWithExcludeFiles (rootDir , "" ), cb )
252+ func FastWalkDir (rootDir string , cb FastWalkCallback ) {
253+ fastWalkCallback (fastWalkWithExcludeFiles (rootDir ), cb )
263254}
264255
265256// fastWalkCallback calls the FastWalkCallback "cb" for all files found by the
@@ -281,19 +272,17 @@ type fastWalkInfo struct {
281272
282273type fastWalker struct {
283274 rootDir string
284- excludeFilename string
285275 ch chan fastWalkInfo
286276 limit int32
287277 cur * int32
288278 wg * sync.WaitGroup
289279}
290280
291281// fastWalkWithExcludeFiles walks the contents of a dir, respecting
292- // include/exclude patterns and also loading new exlude patterns from files
293- // named excludeFilename in directories walked
282+ // include/exclude patterns.
294283//
295284// rootDir - Absolute path to the top of the repository working directory
296- func fastWalkWithExcludeFiles (rootDir , excludeFilename string ) * fastWalker {
285+ func fastWalkWithExcludeFiles (rootDir string ) * fastWalker {
297286 excludePaths := []filepathfilter.Pattern {
298287 filepathfilter .NewPattern (".git" ),
299288 filepathfilter .NewPattern ("**/.git" ),
@@ -307,7 +296,6 @@ func fastWalkWithExcludeFiles(rootDir, excludeFilename string) *fastWalker {
307296 c := int32 (0 )
308297 w := & fastWalker {
309298 rootDir : rootDir ,
310- excludeFilename : excludeFilename ,
311299 limit : int32 (limit ),
312300 cur : & c ,
313301 ch : make (chan fastWalkInfo , 256 ),
@@ -327,12 +315,9 @@ func fastWalkWithExcludeFiles(rootDir, excludeFilename string) *fastWalker {
327315 return w
328316}
329317
330- // Walk is the main recursive implementation of fast walk.
331- // Sends the file/dir and any contents to the channel so long as it passes the
332- // include/exclude filter. If a dir, parses any excludeFilename found and updates
333- // the excludePaths with its content before (parallel) recursing into contents
334- // Also splits large directories into multiple goroutines.
335- // Increments waitg.Add(1) for each new goroutine launched internally
318+ // Walk is the main recursive implementation of fast walk. Sends the file/dir
319+ // and any contents to the channel so long as it passes the include/exclude
320+ // filter. Increments waitg.Add(1) for each new goroutine launched internally
336321//
337322// workDir - Relative path inside the repository
338323func (w * fastWalker ) Walk (isRoot bool , workDir string , itemFi os.FileInfo ,
@@ -373,15 +358,6 @@ func (w *fastWalker) Walk(isRoot bool, workDir string, itemFi os.FileInfo,
373358 childWorkDir = join (workDir , itemFi .Name ())
374359 }
375360
376- if len (w .excludeFilename ) > 0 {
377- possibleExcludeFile := join (fullPath , w .excludeFilename )
378- var err error
379- excludePaths , err = loadExcludeFilename (possibleExcludeFile , childWorkDir , excludePaths )
380- if err != nil {
381- w .ch <- fastWalkInfo {Err : err }
382- }
383- }
384-
385361 // The absolute optimal way to scan would be File.Readdirnames but we
386362 // still need the Stat() to know whether something is a dir, so use
387363 // File.Readdir instead. Means we can provide os.FileInfo to callers like
@@ -430,51 +406,6 @@ func (w *fastWalker) Wait() {
430406 close (w .ch )
431407}
432408
433- // loadExcludeFilename reads the given file in gitignore format and returns a
434- // revised array of exclude paths if there are any changes.
435- // If any changes are made a copy of the array is taken so the original is not
436- // modified
437- func loadExcludeFilename (filename , workDir string , excludePaths []filepathfilter.Pattern ) ([]filepathfilter.Pattern , error ) {
438- f , err := os .OpenFile (filename , os .O_RDONLY , 0644 )
439- if err != nil {
440- if os .IsNotExist (err ) {
441- return excludePaths , nil
442- }
443- return excludePaths , err
444- }
445- defer f .Close ()
446-
447- retPaths := excludePaths
448- modified := false
449-
450- scanner := bufio .NewScanner (f )
451- for scanner .Scan () {
452- line := strings .TrimSpace (scanner .Text ())
453- // Skip blanks, comments and negations (not supported right now)
454- if len (line ) == 0 || strings .HasPrefix (line , "#" ) || strings .HasPrefix (line , "!" ) {
455- continue
456- }
457-
458- if ! modified {
459- // copy on write
460- retPaths = make ([]filepathfilter.Pattern , len (excludePaths ))
461- copy (retPaths , excludePaths )
462- modified = true
463- }
464-
465- path := line
466- // Add pattern in context if exclude has separator, or no wildcard
467- // Allow for both styles of separator at this point
468- if strings .ContainsAny (path , "/\\ " ) ||
469- ! strings .Contains (path , "*" ) {
470- path = join (workDir , line )
471- }
472- retPaths = append (retPaths , filepathfilter .NewPattern (path ))
473- }
474-
475- return retPaths , nil
476- }
477-
478409func join (paths ... string ) string {
479410 ne := make ([]string , 0 , len (paths ))
480411
0 commit comments