github.com/BenLubar/git-last-modified@v0.1.1-0.20210215221858-9b8031919630/walk.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"sync"
     7  )
     8  
     9  type walker struct {
    10  	ch chan string
    11  	wg sync.WaitGroup
    12  }
    13  
    14  func (w *walker) start(concurrency int) {
    15  	w.wg.Add(concurrency)
    16  	w.ch = make(chan string, concurrency*2)
    17  	for i := 0; i < concurrency; i++ {
    18  		go w.worker()
    19  	}
    20  }
    21  
    22  func (w *walker) callback(path string, info os.FileInfo, err error) error {
    23  	if err != nil {
    24  		return err
    25  	}
    26  
    27  	if path == "." {
    28  		// Don't modify working directory.
    29  		return nil
    30  	}
    31  
    32  	if info.Name() == ".git" && info.IsDir() {
    33  		return filepath.SkipDir
    34  	}
    35  
    36  	w.ch <- path
    37  	return nil
    38  }
    39  
    40  func (w *walker) worker() {
    41  	defer w.wg.Done()
    42  
    43  	for path := range w.ch {
    44  		checkError(setModTime(path))
    45  	}
    46  }
    47  
    48  func (w *walker) finish() {
    49  	close(w.ch)
    50  	w.wg.Wait()
    51  }