github.com/denormal/go-gitignore@v0.0.0-20180930084346-ae8ad1d07817/exclude.go (about)

     1  package gitignore
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  )
     7  
     8  // exclude attempts to return the GitIgnore instance for the
     9  // $GIT_DIR/info/exclude from the working copy to which path belongs.
    10  func exclude(path string) (GitIgnore, error) {
    11  	// attempt to locate GIT_DIR
    12  	_gitdir := os.Getenv("GIT_DIR")
    13  	if _gitdir == "" {
    14  		_gitdir = filepath.Join(path, ".git")
    15  	}
    16  	_info, _err := os.Stat(_gitdir)
    17  	if _err != nil {
    18  		if os.IsNotExist(_err) {
    19  			return nil, nil
    20  		} else {
    21  			return nil, _err
    22  		}
    23  	} else if !_info.IsDir() {
    24  		return nil, nil
    25  	}
    26  
    27  	// is there an info/exclude file within this directory?
    28  	_file := filepath.Join(_gitdir, "info", "exclude")
    29  	_, _err = os.Stat(_file)
    30  	if _err != nil {
    31  		if os.IsNotExist(_err) {
    32  			return nil, nil
    33  		} else {
    34  			return nil, _err
    35  		}
    36  	}
    37  
    38  	// attempt to load the exclude file
    39  	return NewFromFile(_file)
    40  } // exclude()