Skip to content

Commit

Permalink
fixup! Get submodules working
Browse files Browse the repository at this point in the history
  • Loading branch information
robotdana committed Nov 26, 2023
1 parent 29c0719 commit c923b6f
Show file tree
Hide file tree
Showing 16 changed files with 422 additions and 277 deletions.
48 changes: 31 additions & 17 deletions lib/path_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,57 @@ def initialize
end

def gitignore(root: nil, config: true)
new_and_matcher(Gitignore.build(root: root, config: config))
new_with_matcher(Matcher::All.build([
@matcher,
Gitignore.build(root: root, config: config),
Gitignore.ignore_dot_git_matcher
]))
end

def ignore(*patterns, patterns_from_file: nil, format: :gitignore, root: nil)
new_and_matcher(
new_with_matcher(Matcher::All.build([
@matcher,
PatternParser.build(
patterns: patterns, patterns_from_file: patterns_from_file, format: format, root: root, polarity: :ignore
patterns: patterns,
patterns_from_file: patterns_from_file,
format: format,
root: root,
polarity: :ignore
)
)
]))
end

def only(*patterns, patterns_from_file: nil, format: :gitignore, root: nil)
new_and_matcher(
new_with_matcher(Matcher::All.build([
@matcher,
PatternParser.build(
patterns: patterns, patterns_from_file: patterns_from_file, format: format, root: root, polarity: :allow
patterns: patterns,
patterns_from_file: patterns_from_file,
format: format,
root: root,
polarity: :allow
)
)
]))
end

def union(path_list, *path_lists)
new_with_matcher(
Matcher::Any.build([@matcher, path_list.matcher, *path_lists.map { |l| l.matcher }]) # rubocop:disable Style/SymbolProc
)
new_with_matcher(Matcher::Any.build([
@matcher,
path_list.matcher,
*path_lists.map { |l| l.matcher } # rubocop:disable Style/SymbolProc
]))
end

def |(other)
union(other)
end

def intersection(path_list, *path_lists)
new_with_matcher(
Matcher::All.build([@matcher, path_list.matcher, path_lists.map { |l| l.matcher }]) # rubocop:disable Style/SymbolProc
)
new_with_matcher(Matcher::All.build([
@matcher,
path_list.matcher,
path_lists.map { |l| l.matcher } # rubocop:disable Style/SymbolProc
]))
end

def &(other)
Expand Down Expand Up @@ -156,10 +174,6 @@ def new_with_matcher(matcher)
path_list
end

def new_and_matcher(matcher)
new_with_matcher(Matcher::All.build([@matcher, matcher]))
end

def dir_matcher
@dir_matcher ||= @matcher.dir_matcher
end
Expand Down
6 changes: 3 additions & 3 deletions lib/path_list/candidate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def child_candidates
def children
@children ||= begin
::Dir.children(@full_path)
rescue ::SystemCallError
rescue ::IOError, ::SystemCallError
[]
end
end
Expand Down Expand Up @@ -118,7 +118,7 @@ def ftype
else
::File.ftype(@full_path)
end
rescue ::SystemCallError
rescue ::IOError, ::SystemCallError
@ftype = 'error'
end
# :nocov:
Expand All @@ -128,7 +128,7 @@ def ftype
return @ftype if @ftype

@ftype = ::File.ftype(@full_path)
rescue ::SystemCallError
rescue ::IOError, ::SystemCallError
@ftype = 'error'
end
end
Expand Down
24 changes: 12 additions & 12 deletions lib/path_list/gitconfig/core_excludesfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ module Gitconfig
# Find the configured git core.excludesFile
module CoreExcludesfile
class << self
# @param repo_root [String]
# @param git_dir [String]
# @return [String, nil]
def path(repo_root:)
ignore_path = gitconfigs_core_excludesfile_path(repo_root) ||
def path(git_dir:)
ignore_path = gitconfigs_core_excludesfile_path(git_dir) ||
default_core_excludesfile_path

ignore_path unless ignore_path.empty?
end

private

def gitconfigs_core_excludesfile_path(repo_root)
gitconfig_core_excludesfile_path(repo_config_path(repo_root)) ||
gitconfig_core_excludesfile_path(global_config_path) ||
gitconfig_core_excludesfile_path(default_user_config_path) ||
gitconfig_core_excludesfile_path(system_config_path)
def gitconfigs_core_excludesfile_path(git_dir)
gitconfig_core_excludesfile_path(repo_config_path(git_dir), git_dir) ||
gitconfig_core_excludesfile_path(global_config_path, git_dir) ||
gitconfig_core_excludesfile_path(default_user_config_path, git_dir) ||
gitconfig_core_excludesfile_path(system_config_path, git_dir)
rescue ParseError => e
::Warning.warn("PathList gitconfig parser failed\n" + e.message)

''
end

def gitconfig_core_excludesfile_path(config_path)
def gitconfig_core_excludesfile_path(config_path, git_dir)
return unless config_path
return unless ::File.readable?(config_path)

ignore_path = FileParser.parse(config_path).excludesfile
ignore_path = FileParser.parse(config_path, git_dir: git_dir).excludesfile
return unless ignore_path

ignore_path.strip!
Expand All @@ -51,8 +51,8 @@ def default_core_excludesfile_path
CanonicalPath.full_path_from('git/ignore', default_config_home)
end

def repo_config_path(root)
CanonicalPath.full_path_from('.git/config', root)
def repo_config_path(git_dir)
CanonicalPath.full_path_from('config', git_dir) if git_dir
end

def global_config_path
Expand Down
35 changes: 18 additions & 17 deletions lib/path_list/gitconfig/file_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ module Gitconfig
# Parse git config file for the core.excludesFile
class FileParser
# @param file [String]
# @param root [String]
# @param git_dir [String]
# @param nesting [Integer]
# @return [String]
# @raise [ParseError]
def self.parse(file, root: Dir.pwd, nesting: 1, find: :'core.excludesFile')
new(file, root: root, nesting: nesting, find: find).parse
def self.parse(file, git_dir: nil, nesting: 1)
new(file, git_dir: git_dir, nesting: nesting).parse
end

# @param file [String]
# @param root [String]
# @param git_dir [String]
# @param nesting [Integer]
def initialize(path, root: Dir.pwd, nesting: 1, find: :'core.excludesFile')
def initialize(path, git_dir: nil, nesting: 1)
@path = path
@root = root
@git_dir = git_dir
@nesting = nesting
@find = find
end

# @return [String]
Expand All @@ -42,7 +41,7 @@ def parse

attr_reader :nesting
attr_reader :path
attr_reader :root
attr_reader :git_dir
attr_accessor :within_quotes
attr_accessor :section

Expand Down Expand Up @@ -75,7 +74,7 @@ def read_file(path)

result = self.class.parse(
CanonicalPath.full_path_from(include_path, ::File.dirname(path)),
root: root,
git_dir: git_dir,
nesting: nesting + 1
)
self.excludesfile = result.excludesfile if result.excludesfile
Expand Down Expand Up @@ -125,22 +124,24 @@ def include_if(file)

def on_branch?(branch_pattern)
branch_pattern += '**' if branch_pattern.end_with?('/')
current_branch = ::File.readable?("#{root}/.git/HEAD") &&
::File.read("#{root}/.git/HEAD").delete_prefix('ref: refs/heads/')
current_branch = ::File.readable?("#{git_dir}/HEAD") &&
::File.read("#{git_dir}/HEAD").delete_prefix('ref: refs/heads/')
return false unless current_branch

# goddamit git what does 'a pattern with standard globbing wildcards' mean
::File.fnmatch(branch_pattern, current_branch, ::File::FNM_PATHNAME | ::File::FNM_DOTMATCH)
end

def gitdir?(gitdir, path:, case_insensitive: false)
gitdir += '**' if gitdir.end_with?('/')
gitdir.sub!(%r{\A~/}, Dir.home + '/')
gitdir.sub!(/\A\./, path + '/')
gitdir = "**/#{gitdir}" unless gitdir.start_with?('/')
def gitdir?(gitdir_value, path:, case_insensitive: false)
return unless git_dir

gitdir_value += '**' if gitdir_value.end_with?('/')
gitdir_value.sub!(%r{\A~/}, Dir.home + '/')
gitdir_value.sub!(/\A\./, path + '/')
gitdir_value = "**/#{gitdir_value}" unless gitdir_value.start_with?('/')
options = ::File::FNM_PATHNAME | ::File::FNM_DOTMATCH
options |= ::File::FNM_CASEFOLD if case_insensitive
::File.fnmatch(gitdir, ::File.join(root, '.git'), options)
::File.fnmatch(gitdir_value, git_dir, options)
end

def scan_value(file)
Expand Down
Loading

0 comments on commit c923b6f

Please sign in to comment.