Do not traverse symlinks in heuristics (#3946)

This commit is contained in:
Ashe Connor
2017-12-12 21:53:36 +11:00
committed by GitHub
parent 0b81b21983
commit d4c2d83af9
7 changed files with 28 additions and 1 deletions

View File

@@ -11,11 +11,13 @@ module Linguist
#
# path - A path String (does not necessarily exists on the file system).
# content - Content of the file.
# symlink - Whether the file is a symlink.
#
# Returns a Blob.
def initialize(path, content)
def initialize(path, content, symlink: false)
@path = path
@content = content
@symlink = symlink
end
# Public: Filename
@@ -69,5 +71,12 @@ module Linguist
"." + segments[index..-1].join(".")
end
end
# Public: Is this a symlink?
#
# Returns true or false.
def symlink?
@symlink
end
end
end

View File

@@ -18,6 +18,8 @@ module Linguist
#
# Returns an Array of Language objects, most probable first.
def self.call(blob, possible_languages)
return [] if blob.symlink?
language_names = possible_languages.map(&:name)
classify(Samples.cache, blob.data[0...CLASSIFIER_CONSIDER_BYTES], language_names).map do |name, _|
Language[name] # Return the actual Language objects

View File

@@ -26,6 +26,11 @@ module Linguist
@mode ||= File.stat(@fullpath).mode.to_s(8)
end
def symlink?
return @symlink if !@symlink.nil?
@symlink = (File.symlink?(@fullpath) rescue false)
end
# Public: Read file contents.
#
# Returns a String.

View File

@@ -16,6 +16,8 @@ module Linguist
#
# Returns an Array of languages, or empty if none matched or were inconclusive.
def self.call(blob, candidates)
return [] if blob.symlink?
data = blob.data[0...HEURISTICS_CONSIDER_BYTES]
@heuristics.each do |heuristic|

View File

@@ -80,6 +80,11 @@ module Linguist
@size
end
def symlink?
# We don't create LazyBlobs for symlinks.
false
end
def cleanup!
@data.clear if @data
end

View File

@@ -11,6 +11,8 @@ module Linguist
# Returns an Array with one Language if the blob has a shebang with a valid
# interpreter, or empty if there is no shebang.
def self.call(blob, _ = nil)
return [] if blob.symlink?
Language.find_by_interpreter interpreter(blob.data)
end

View File

@@ -109,6 +109,8 @@ module Linguist
# Returns an Array with one Language if the blob has a Vim or Emacs modeline
# that matches a Language name or alias. Returns an empty array if no match.
def self.call(blob, _ = nil)
return [] if blob.symlink?
header = blob.first_lines(SEARCH_SCOPE).join("\n")
footer = blob.last_lines(SEARCH_SCOPE).join("\n")
Array(Language.find_by_alias(modeline(header + footer)))