repo
stringlengths
5
58
path
stringlengths
9
168
func_name
stringlengths
9
130
original_string
stringlengths
66
10.5k
language
stringclasses
1 value
code
stringlengths
66
10.5k
code_tokens
sequence
docstring
stringlengths
8
16k
docstring_tokens
sequence
sha
stringlengths
40
40
url
stringlengths
94
266
partition
stringclasses
1 value
sds/overcommit
lib/overcommit/hook/pre_commit/yard_coverage.rb
Overcommit::Hook::PreCommit.YardCoverage.check_yard_coverage
def check_yard_coverage(stat_lines) if config['min_coverage_percentage'] match = stat_lines.last.match(/^\s*([\d.]+)%\s+documented\s*$/) unless match return :warn end yard_coverage = match.captures[0].to_f if yard_coverage >= config['min_coverage_percentage'].to_f return :pass end yard_coverage end end
ruby
def check_yard_coverage(stat_lines) if config['min_coverage_percentage'] match = stat_lines.last.match(/^\s*([\d.]+)%\s+documented\s*$/) unless match return :warn end yard_coverage = match.captures[0].to_f if yard_coverage >= config['min_coverage_percentage'].to_f return :pass end yard_coverage end end
[ "def", "check_yard_coverage", "(", "stat_lines", ")", "if", "config", "[", "'min_coverage_percentage'", "]", "match", "=", "stat_lines", ".", "last", ".", "match", "(", "/", "\\s", "\\d", "\\s", "\\s", "/", ")", "unless", "match", "return", ":warn", "end", "yard_coverage", "=", "match", ".", "captures", "[", "0", "]", ".", "to_f", "if", "yard_coverage", ">=", "config", "[", "'min_coverage_percentage'", "]", ".", "to_f", "return", ":pass", "end", "yard_coverage", "end", "end" ]
Check the yard coverage Return a :pass if the coverage is enough, :warn if it couldn't be read, otherwise, it has been read successfully.
[ "Check", "the", "yard", "coverage" ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/hook/pre_commit/yard_coverage.rb#L49-L63
train
sds/overcommit
lib/overcommit/hook/pre_commit/yard_coverage.rb
Overcommit::Hook::PreCommit.YardCoverage.error_messages
def error_messages(yard_coverage, error_text) first_message = "You have a #{yard_coverage}% yard documentation coverage. "\ "#{config['min_coverage_percentage']}% is the minimum required." # Add the undocumented objects text as error messages messages = [Overcommit::Hook::Message.new(:error, nil, nil, first_message)] errors = error_text.strip.split("\n") errors.each do |undocumented_object| undocumented_object_message, file_info = undocumented_object.split(/:?\s+/) file_info_match = file_info.match(/^\(([^:]+):(\d+)\)/) # In case any compacted error does not follow the format, ignore it if file_info_match file = file_info_match.captures[0] line = file_info_match.captures[1] messages << Overcommit::Hook::Message.new( :error, file, line, "#{file}:#{line}: #{undocumented_object_message}" ) end end messages end
ruby
def error_messages(yard_coverage, error_text) first_message = "You have a #{yard_coverage}% yard documentation coverage. "\ "#{config['min_coverage_percentage']}% is the minimum required." # Add the undocumented objects text as error messages messages = [Overcommit::Hook::Message.new(:error, nil, nil, first_message)] errors = error_text.strip.split("\n") errors.each do |undocumented_object| undocumented_object_message, file_info = undocumented_object.split(/:?\s+/) file_info_match = file_info.match(/^\(([^:]+):(\d+)\)/) # In case any compacted error does not follow the format, ignore it if file_info_match file = file_info_match.captures[0] line = file_info_match.captures[1] messages << Overcommit::Hook::Message.new( :error, file, line, "#{file}:#{line}: #{undocumented_object_message}" ) end end messages end
[ "def", "error_messages", "(", "yard_coverage", ",", "error_text", ")", "first_message", "=", "\"You have a #{yard_coverage}% yard documentation coverage. \"", "\"#{config['min_coverage_percentage']}% is the minimum required.\"", "messages", "=", "[", "Overcommit", "::", "Hook", "::", "Message", ".", "new", "(", ":error", ",", "nil", ",", "nil", ",", "first_message", ")", "]", "errors", "=", "error_text", ".", "strip", ".", "split", "(", "\"\\n\"", ")", "errors", ".", "each", "do", "|", "undocumented_object", "|", "undocumented_object_message", ",", "file_info", "=", "undocumented_object", ".", "split", "(", "/", "\\s", "/", ")", "file_info_match", "=", "file_info", ".", "match", "(", "/", "\\(", "\\d", "\\)", "/", ")", "if", "file_info_match", "file", "=", "file_info_match", ".", "captures", "[", "0", "]", "line", "=", "file_info_match", ".", "captures", "[", "1", "]", "messages", "<<", "Overcommit", "::", "Hook", "::", "Message", ".", "new", "(", ":error", ",", "file", ",", "line", ",", "\"#{file}:#{line}: #{undocumented_object_message}\"", ")", "end", "end", "messages", "end" ]
Create the error messages
[ "Create", "the", "error", "messages" ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/hook/pre_commit/yard_coverage.rb#L66-L88
train
sds/overcommit
lib/overcommit/hook_context/post_merge.rb
Overcommit::HookContext.PostMerge.modified_files
def modified_files staged = squash? refs = 'HEAD^ HEAD' if merge_commit? @modified_files ||= Overcommit::GitRepo.modified_files(staged: staged, refs: refs) end
ruby
def modified_files staged = squash? refs = 'HEAD^ HEAD' if merge_commit? @modified_files ||= Overcommit::GitRepo.modified_files(staged: staged, refs: refs) end
[ "def", "modified_files", "staged", "=", "squash?", "refs", "=", "'HEAD^ HEAD'", "if", "merge_commit?", "@modified_files", "||=", "Overcommit", "::", "GitRepo", ".", "modified_files", "(", "staged", ":", "staged", ",", "refs", ":", "refs", ")", "end" ]
Get a list of files that were added, copied, or modified in the merge commit. Renames and deletions are ignored, since there should be nothing to check.
[ "Get", "a", "list", "of", "files", "that", "were", "added", "copied", "or", "modified", "in", "the", "merge", "commit", ".", "Renames", "and", "deletions", "are", "ignored", "since", "there", "should", "be", "nothing", "to", "check", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/hook_context/post_merge.rb#L11-L15
train
sds/overcommit
lib/overcommit/hook_context/post_rewrite.rb
Overcommit::HookContext.PostRewrite.modified_files
def modified_files @modified_files ||= begin @modified_files = [] rewritten_commits.each do |rewritten_commit| refs = "#{rewritten_commit.old_hash} #{rewritten_commit.new_hash}" @modified_files |= Overcommit::GitRepo.modified_files(refs: refs) end filter_modified_files(@modified_files) end end
ruby
def modified_files @modified_files ||= begin @modified_files = [] rewritten_commits.each do |rewritten_commit| refs = "#{rewritten_commit.old_hash} #{rewritten_commit.new_hash}" @modified_files |= Overcommit::GitRepo.modified_files(refs: refs) end filter_modified_files(@modified_files) end end
[ "def", "modified_files", "@modified_files", "||=", "begin", "@modified_files", "=", "[", "]", "rewritten_commits", ".", "each", "do", "|", "rewritten_commit", "|", "refs", "=", "\"#{rewritten_commit.old_hash} #{rewritten_commit.new_hash}\"", "@modified_files", "|=", "Overcommit", "::", "GitRepo", ".", "modified_files", "(", "refs", ":", "refs", ")", "end", "filter_modified_files", "(", "@modified_files", ")", "end", "end" ]
Get a list of files that have been added or modified as part of a rewritten commit. Renames and deletions are ignored, since there should be nothing to check.
[ "Get", "a", "list", "of", "files", "that", "have", "been", "added", "or", "modified", "as", "part", "of", "a", "rewritten", "commit", ".", "Renames", "and", "deletions", "are", "ignored", "since", "there", "should", "be", "nothing", "to", "check", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/hook_context/post_rewrite.rb#L33-L44
train
sds/overcommit
lib/overcommit/message_processor.rb
Overcommit.MessageProcessor.basic_status_and_output
def basic_status_and_output(messages) status = if messages.any? { |message| message.type == :error } :fail elsif messages.any? { |message| message.type == :warning } :warn else :pass end output = '' if messages.any? output += messages.join("\n") + "\n" end [status, output] end
ruby
def basic_status_and_output(messages) status = if messages.any? { |message| message.type == :error } :fail elsif messages.any? { |message| message.type == :warning } :warn else :pass end output = '' if messages.any? output += messages.join("\n") + "\n" end [status, output] end
[ "def", "basic_status_and_output", "(", "messages", ")", "status", "=", "if", "messages", ".", "any?", "{", "|", "message", "|", "message", ".", "type", "==", ":error", "}", ":fail", "elsif", "messages", ".", "any?", "{", "|", "message", "|", "message", ".", "type", "==", ":warning", "}", ":warn", "else", ":pass", "end", "output", "=", "''", "if", "messages", ".", "any?", "output", "+=", "messages", ".", "join", "(", "\"\\n\"", ")", "+", "\"\\n\"", "end", "[", "status", ",", "output", "]", "end" ]
Returns status and output for messages assuming no special treatment of messages occurring on unmodified lines.
[ "Returns", "status", "and", "output", "for", "messages", "assuming", "no", "special", "treatment", "of", "messages", "occurring", "on", "unmodified", "lines", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/message_processor.rb#L101-L117
train
sds/overcommit
lib/overcommit/hook/base.rb
Overcommit::Hook.Base.run_and_transform
def run_and_transform if output = check_for_requirements status = :fail else result = Overcommit::Utils.with_environment(@config.fetch('env') { {} }) { run } status, output = process_hook_return_value(result) end [transform_status(status), output] end
ruby
def run_and_transform if output = check_for_requirements status = :fail else result = Overcommit::Utils.with_environment(@config.fetch('env') { {} }) { run } status, output = process_hook_return_value(result) end [transform_status(status), output] end
[ "def", "run_and_transform", "if", "output", "=", "check_for_requirements", "status", "=", ":fail", "else", "result", "=", "Overcommit", "::", "Utils", ".", "with_environment", "(", "@config", ".", "fetch", "(", "'env'", ")", "{", "{", "}", "}", ")", "{", "run", "}", "status", ",", "output", "=", "process_hook_return_value", "(", "result", ")", "end", "[", "transform_status", "(", "status", ")", ",", "output", "]", "end" ]
Runs the hook and transforms the status returned based on the hook's configuration. Poorly named because we already have a bunch of hooks in the wild that implement `#run`, and we needed a wrapper step to transform the status based on any custom configuration.
[ "Runs", "the", "hook", "and", "transforms", "the", "status", "returned", "based", "on", "the", "hook", "s", "configuration", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/hook/base.rb#L43-L52
train
sds/overcommit
lib/overcommit/hook/base.rb
Overcommit::Hook.Base.check_for_libraries
def check_for_libraries output = [] required_libraries.each do |library| begin require library rescue LoadError install_command = @config['install_command'] install_command = " -- install via #{install_command}" if install_command output << "Unable to load '#{library}'#{install_command}" end end return if output.empty? output.join("\n") end
ruby
def check_for_libraries output = [] required_libraries.each do |library| begin require library rescue LoadError install_command = @config['install_command'] install_command = " -- install via #{install_command}" if install_command output << "Unable to load '#{library}'#{install_command}" end end return if output.empty? output.join("\n") end
[ "def", "check_for_libraries", "output", "=", "[", "]", "required_libraries", ".", "each", "do", "|", "library", "|", "begin", "require", "library", "rescue", "LoadError", "install_command", "=", "@config", "[", "'install_command'", "]", "install_command", "=", "\" -- install via #{install_command}\"", "if", "install_command", "output", "<<", "\"Unable to load '#{library}'#{install_command}\"", "end", "end", "return", "if", "output", ".", "empty?", "output", ".", "join", "(", "\"\\n\"", ")", "end" ]
If the hook defines required library paths that it wants to load, attempt to load them.
[ "If", "the", "hook", "defines", "required", "library", "paths", "that", "it", "wants", "to", "load", "attempt", "to", "load", "them", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/hook/base.rb#L221-L238
train
sds/overcommit
lib/overcommit/hook_context/pre_commit.rb
Overcommit::HookContext.PreCommit.setup_environment
def setup_environment store_modified_times Overcommit::GitRepo.store_merge_state Overcommit::GitRepo.store_cherry_pick_state if !initial_commit? && any_changes? @stash_attempted = true stash_message = "Overcommit: Stash of repo state before hook run at #{Time.now}" result = Overcommit::Utils.execute( %w[git -c commit.gpgsign=false stash save --keep-index --quiet] + [stash_message] ) unless result.success? # Failure to stash in this case is likely due to a configuration # issue (e.g. author/email not set or GPG signing key incorrect) raise Overcommit::Exceptions::HookSetupFailed, "Unable to setup environment for #{hook_script_name} hook run:" \ "\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}" end @changes_stashed = `git stash list -1`.include?(stash_message) end # While running the hooks make it appear as if nothing changed restore_modified_times end
ruby
def setup_environment store_modified_times Overcommit::GitRepo.store_merge_state Overcommit::GitRepo.store_cherry_pick_state if !initial_commit? && any_changes? @stash_attempted = true stash_message = "Overcommit: Stash of repo state before hook run at #{Time.now}" result = Overcommit::Utils.execute( %w[git -c commit.gpgsign=false stash save --keep-index --quiet] + [stash_message] ) unless result.success? # Failure to stash in this case is likely due to a configuration # issue (e.g. author/email not set or GPG signing key incorrect) raise Overcommit::Exceptions::HookSetupFailed, "Unable to setup environment for #{hook_script_name} hook run:" \ "\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}" end @changes_stashed = `git stash list -1`.include?(stash_message) end # While running the hooks make it appear as if nothing changed restore_modified_times end
[ "def", "setup_environment", "store_modified_times", "Overcommit", "::", "GitRepo", ".", "store_merge_state", "Overcommit", "::", "GitRepo", ".", "store_cherry_pick_state", "if", "!", "initial_commit?", "&&", "any_changes?", "@stash_attempted", "=", "true", "stash_message", "=", "\"Overcommit: Stash of repo state before hook run at #{Time.now}\"", "result", "=", "Overcommit", "::", "Utils", ".", "execute", "(", "%w[", "git", "-c", "commit.gpgsign=false", "stash", "save", "--keep-index", "--quiet", "]", "+", "[", "stash_message", "]", ")", "unless", "result", ".", "success?", "raise", "Overcommit", "::", "Exceptions", "::", "HookSetupFailed", ",", "\"Unable to setup environment for #{hook_script_name} hook run:\"", "\"\\nSTDOUT:#{result.stdout}\\nSTDERR:#{result.stderr}\"", "end", "@changes_stashed", "=", "`", "`", ".", "include?", "(", "stash_message", ")", "end", "restore_modified_times", "end" ]
Stash unstaged contents of files so hooks don't see changes that aren't about to be committed.
[ "Stash", "unstaged", "contents", "of", "files", "so", "hooks", "don", "t", "see", "changes", "that", "aren", "t", "about", "to", "be", "committed", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/hook_context/pre_commit.rb#L46-L72
train
sds/overcommit
lib/overcommit/hook_context/pre_commit.rb
Overcommit::HookContext.PreCommit.cleanup_environment
def cleanup_environment unless initial_commit? || (@stash_attempted && !@changes_stashed) clear_working_tree # Ensure working tree is clean before restoring it restore_modified_times end if @changes_stashed restore_working_tree restore_modified_times end Overcommit::GitRepo.restore_merge_state Overcommit::GitRepo.restore_cherry_pick_state restore_modified_times end
ruby
def cleanup_environment unless initial_commit? || (@stash_attempted && !@changes_stashed) clear_working_tree # Ensure working tree is clean before restoring it restore_modified_times end if @changes_stashed restore_working_tree restore_modified_times end Overcommit::GitRepo.restore_merge_state Overcommit::GitRepo.restore_cherry_pick_state restore_modified_times end
[ "def", "cleanup_environment", "unless", "initial_commit?", "||", "(", "@stash_attempted", "&&", "!", "@changes_stashed", ")", "clear_working_tree", "restore_modified_times", "end", "if", "@changes_stashed", "restore_working_tree", "restore_modified_times", "end", "Overcommit", "::", "GitRepo", ".", "restore_merge_state", "Overcommit", "::", "GitRepo", ".", "restore_cherry_pick_state", "restore_modified_times", "end" ]
Restore unstaged changes and reset file modification times so it appears as if nothing ever changed. We want to restore the modification times for each of the files after every step to ensure as little time as possible has passed while the modification time on the file was newer. This helps us play more nicely with file watchers.
[ "Restore", "unstaged", "changes", "and", "reset", "file", "modification", "times", "so", "it", "appears", "as", "if", "nothing", "ever", "changed", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/hook_context/pre_commit.rb#L81-L95
train
sds/overcommit
lib/overcommit/hook_context/pre_commit.rb
Overcommit::HookContext.PreCommit.modified_files
def modified_files unless @modified_files currently_staged = Overcommit::GitRepo.modified_files(staged: true) @modified_files = currently_staged # Include files modified in last commit if amending if amendment? subcmd = 'show --format=%n' previously_modified = Overcommit::GitRepo.modified_files(subcmd: subcmd) @modified_files |= filter_modified_files(previously_modified) end end @modified_files end
ruby
def modified_files unless @modified_files currently_staged = Overcommit::GitRepo.modified_files(staged: true) @modified_files = currently_staged # Include files modified in last commit if amending if amendment? subcmd = 'show --format=%n' previously_modified = Overcommit::GitRepo.modified_files(subcmd: subcmd) @modified_files |= filter_modified_files(previously_modified) end end @modified_files end
[ "def", "modified_files", "unless", "@modified_files", "currently_staged", "=", "Overcommit", "::", "GitRepo", ".", "modified_files", "(", "staged", ":", "true", ")", "@modified_files", "=", "currently_staged", "if", "amendment?", "subcmd", "=", "'show --format=%n'", "previously_modified", "=", "Overcommit", "::", "GitRepo", ".", "modified_files", "(", "subcmd", ":", "subcmd", ")", "@modified_files", "|=", "filter_modified_files", "(", "previously_modified", ")", "end", "end", "@modified_files", "end" ]
Get a list of added, copied, or modified files that have been staged. Renames and deletions are ignored, since there should be nothing to check.
[ "Get", "a", "list", "of", "added", "copied", "or", "modified", "files", "that", "have", "been", "staged", ".", "Renames", "and", "deletions", "are", "ignored", "since", "there", "should", "be", "nothing", "to", "check", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/hook_context/pre_commit.rb#L99-L112
train
sds/overcommit
lib/overcommit/hook_context/pre_commit.rb
Overcommit::HookContext.PreCommit.clear_working_tree
def clear_working_tree removed_submodules = Overcommit::GitRepo.staged_submodule_removals result = Overcommit::Utils.execute(%w[git reset --hard]) unless result.success? raise Overcommit::Exceptions::HookCleanupFailed, "Unable to cleanup working tree after #{hook_script_name} hooks run:" \ "\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}" end # Hard-resetting a staged submodule removal results in the index being # reset but the submodule being restored as an empty directory. This empty # directory prevents us from stashing on a subsequent run if a hook fails. # # Work around this by removing these empty submodule directories as there # doesn't appear any reason to keep them around. removed_submodules.each do |submodule| FileUtils.rmdir(submodule.path) end end
ruby
def clear_working_tree removed_submodules = Overcommit::GitRepo.staged_submodule_removals result = Overcommit::Utils.execute(%w[git reset --hard]) unless result.success? raise Overcommit::Exceptions::HookCleanupFailed, "Unable to cleanup working tree after #{hook_script_name} hooks run:" \ "\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}" end # Hard-resetting a staged submodule removal results in the index being # reset but the submodule being restored as an empty directory. This empty # directory prevents us from stashing on a subsequent run if a hook fails. # # Work around this by removing these empty submodule directories as there # doesn't appear any reason to keep them around. removed_submodules.each do |submodule| FileUtils.rmdir(submodule.path) end end
[ "def", "clear_working_tree", "removed_submodules", "=", "Overcommit", "::", "GitRepo", ".", "staged_submodule_removals", "result", "=", "Overcommit", "::", "Utils", ".", "execute", "(", "%w[", "git", "reset", "--hard", "]", ")", "unless", "result", ".", "success?", "raise", "Overcommit", "::", "Exceptions", "::", "HookCleanupFailed", ",", "\"Unable to cleanup working tree after #{hook_script_name} hooks run:\"", "\"\\nSTDOUT:#{result.stdout}\\nSTDERR:#{result.stderr}\"", "end", "removed_submodules", ".", "each", "do", "|", "submodule", "|", "FileUtils", ".", "rmdir", "(", "submodule", ".", "path", ")", "end", "end" ]
Clears the working tree so that the stash can be applied.
[ "Clears", "the", "working", "tree", "so", "that", "the", "stash", "can", "be", "applied", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/hook_context/pre_commit.rb#L141-L160
train
sds/overcommit
lib/overcommit/hook_context/pre_commit.rb
Overcommit::HookContext.PreCommit.restore_working_tree
def restore_working_tree result = Overcommit::Utils.execute(%w[git stash pop --index --quiet]) unless result.success? raise Overcommit::Exceptions::HookCleanupFailed, "Unable to restore working tree after #{hook_script_name} hooks run:" \ "\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}" end end
ruby
def restore_working_tree result = Overcommit::Utils.execute(%w[git stash pop --index --quiet]) unless result.success? raise Overcommit::Exceptions::HookCleanupFailed, "Unable to restore working tree after #{hook_script_name} hooks run:" \ "\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}" end end
[ "def", "restore_working_tree", "result", "=", "Overcommit", "::", "Utils", ".", "execute", "(", "%w[", "git", "stash", "pop", "--index", "--quiet", "]", ")", "unless", "result", ".", "success?", "raise", "Overcommit", "::", "Exceptions", "::", "HookCleanupFailed", ",", "\"Unable to restore working tree after #{hook_script_name} hooks run:\"", "\"\\nSTDOUT:#{result.stdout}\\nSTDERR:#{result.stderr}\"", "end", "end" ]
Applies the stash to the working tree to restore the user's state.
[ "Applies", "the", "stash", "to", "the", "working", "tree", "to", "restore", "the", "user", "s", "state", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/hook_context/pre_commit.rb#L163-L170
train
sds/overcommit
lib/overcommit/hook_context/pre_commit.rb
Overcommit::HookContext.PreCommit.store_modified_times
def store_modified_times @modified_times = {} staged_files = modified_files unstaged_files = Overcommit::GitRepo.modified_files(staged: false) (staged_files + unstaged_files).each do |file| next if Overcommit::Utils.broken_symlink?(file) next unless File.exist?(file) # Ignore renamed files (old file no longer exists) @modified_times[file] = File.mtime(file) end end
ruby
def store_modified_times @modified_times = {} staged_files = modified_files unstaged_files = Overcommit::GitRepo.modified_files(staged: false) (staged_files + unstaged_files).each do |file| next if Overcommit::Utils.broken_symlink?(file) next unless File.exist?(file) # Ignore renamed files (old file no longer exists) @modified_times[file] = File.mtime(file) end end
[ "def", "store_modified_times", "@modified_times", "=", "{", "}", "staged_files", "=", "modified_files", "unstaged_files", "=", "Overcommit", "::", "GitRepo", ".", "modified_files", "(", "staged", ":", "false", ")", "(", "staged_files", "+", "unstaged_files", ")", ".", "each", "do", "|", "file", "|", "next", "if", "Overcommit", "::", "Utils", ".", "broken_symlink?", "(", "file", ")", "next", "unless", "File", ".", "exist?", "(", "file", ")", "@modified_times", "[", "file", "]", "=", "File", ".", "mtime", "(", "file", ")", "end", "end" ]
Stores the modification times for all modified files to make it appear like they never changed. This prevents (some) editors from complaining about files changing when we stash changes before running the hooks.
[ "Stores", "the", "modification", "times", "for", "all", "modified", "files", "to", "make", "it", "appear", "like", "they", "never", "changed", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/hook_context/pre_commit.rb#L187-L198
train
sds/overcommit
lib/overcommit/hook_context/pre_commit.rb
Overcommit::HookContext.PreCommit.restore_modified_times
def restore_modified_times @modified_times.each do |file, time| next if Overcommit::Utils.broken_symlink?(file) next unless File.exist?(file) File.utime(time, time, file) end end
ruby
def restore_modified_times @modified_times.each do |file, time| next if Overcommit::Utils.broken_symlink?(file) next unless File.exist?(file) File.utime(time, time, file) end end
[ "def", "restore_modified_times", "@modified_times", ".", "each", "do", "|", "file", ",", "time", "|", "next", "if", "Overcommit", "::", "Utils", ".", "broken_symlink?", "(", "file", ")", "next", "unless", "File", ".", "exist?", "(", "file", ")", "File", ".", "utime", "(", "time", ",", "time", ",", "file", ")", "end", "end" ]
Restores the file modification times for all modified files to make it appear like they never changed.
[ "Restores", "the", "file", "modification", "times", "for", "all", "modified", "files", "to", "make", "it", "appear", "like", "they", "never", "changed", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/hook_context/pre_commit.rb#L202-L208
train
sds/overcommit
lib/overcommit/configuration_validator.rb
Overcommit.ConfigurationValidator.validate
def validate(config, hash, options) @options = options.dup @log = options[:logger] hash = convert_nils_to_empty_hashes(hash) ensure_hook_type_sections_exist(hash) check_hook_name_format(hash) check_hook_env(hash) check_for_missing_enabled_option(hash) unless @options[:default] check_for_too_many_processors(config, hash) check_for_verify_plugin_signatures_option(hash) hash end
ruby
def validate(config, hash, options) @options = options.dup @log = options[:logger] hash = convert_nils_to_empty_hashes(hash) ensure_hook_type_sections_exist(hash) check_hook_name_format(hash) check_hook_env(hash) check_for_missing_enabled_option(hash) unless @options[:default] check_for_too_many_processors(config, hash) check_for_verify_plugin_signatures_option(hash) hash end
[ "def", "validate", "(", "config", ",", "hash", ",", "options", ")", "@options", "=", "options", ".", "dup", "@log", "=", "options", "[", ":logger", "]", "hash", "=", "convert_nils_to_empty_hashes", "(", "hash", ")", "ensure_hook_type_sections_exist", "(", "hash", ")", "check_hook_name_format", "(", "hash", ")", "check_hook_env", "(", "hash", ")", "check_for_missing_enabled_option", "(", "hash", ")", "unless", "@options", "[", ":default", "]", "check_for_too_many_processors", "(", "config", ",", "hash", ")", "check_for_verify_plugin_signatures_option", "(", "hash", ")", "hash", "end" ]
Validates hash for any invalid options, normalizing where possible. @param config [Overcommit::Configuration] @param hash [Hash] hash representation of YAML config @param options[Hash] @option default [Boolean] whether hash represents the default built-in config @option logger [Overcommit::Logger] logger to output warnings to @return [Hash] validated hash (potentially modified)
[ "Validates", "hash", "for", "any", "invalid", "options", "normalizing", "where", "possible", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/configuration_validator.rb#L15-L28
train
sds/overcommit
lib/overcommit/configuration_validator.rb
Overcommit.ConfigurationValidator.convert_nils_to_empty_hashes
def convert_nils_to_empty_hashes(hash) hash.each_with_object({}) do |(key, value), h| h[key] = case value when nil then {} when Hash then convert_nils_to_empty_hashes(value) else value end end end
ruby
def convert_nils_to_empty_hashes(hash) hash.each_with_object({}) do |(key, value), h| h[key] = case value when nil then {} when Hash then convert_nils_to_empty_hashes(value) else value end end end
[ "def", "convert_nils_to_empty_hashes", "(", "hash", ")", "hash", ".", "each_with_object", "(", "{", "}", ")", "do", "|", "(", "key", ",", "value", ")", ",", "h", "|", "h", "[", "key", "]", "=", "case", "value", "when", "nil", "then", "{", "}", "when", "Hash", "then", "convert_nils_to_empty_hashes", "(", "value", ")", "else", "value", "end", "end", "end" ]
Normalizes `nil` values to empty hashes. This is useful for when we want to merge two configuration hashes together, since it's easier to merge two hashes than to have to check if one of the values is nil.
[ "Normalizes", "nil", "values", "to", "empty", "hashes", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/configuration_validator.rb#L46-L56
train
sds/overcommit
lib/overcommit/configuration_validator.rb
Overcommit.ConfigurationValidator.check_hook_name_format
def check_hook_name_format(hash) errors = [] Overcommit::Utils.supported_hook_type_classes.each do |hook_type| hash.fetch(hook_type) { {} }.each_key do |hook_name| next if hook_name == 'ALL' unless hook_name.match?(/\A[A-Za-z0-9]+\z/) errors << "#{hook_type}::#{hook_name} has an invalid name " \ "#{hook_name}. It must contain only alphanumeric " \ 'characters (no underscores or dashes, etc.)' end end end if errors.any? if @log @log.error errors.join("\n") @log.newline end raise Overcommit::Exceptions::ConfigurationError, 'One or more hooks had invalid names' end end
ruby
def check_hook_name_format(hash) errors = [] Overcommit::Utils.supported_hook_type_classes.each do |hook_type| hash.fetch(hook_type) { {} }.each_key do |hook_name| next if hook_name == 'ALL' unless hook_name.match?(/\A[A-Za-z0-9]+\z/) errors << "#{hook_type}::#{hook_name} has an invalid name " \ "#{hook_name}. It must contain only alphanumeric " \ 'characters (no underscores or dashes, etc.)' end end end if errors.any? if @log @log.error errors.join("\n") @log.newline end raise Overcommit::Exceptions::ConfigurationError, 'One or more hooks had invalid names' end end
[ "def", "check_hook_name_format", "(", "hash", ")", "errors", "=", "[", "]", "Overcommit", "::", "Utils", ".", "supported_hook_type_classes", ".", "each", "do", "|", "hook_type", "|", "hash", ".", "fetch", "(", "hook_type", ")", "{", "{", "}", "}", ".", "each_key", "do", "|", "hook_name", "|", "next", "if", "hook_name", "==", "'ALL'", "unless", "hook_name", ".", "match?", "(", "/", "\\A", "\\z", "/", ")", "errors", "<<", "\"#{hook_type}::#{hook_name} has an invalid name \"", "\"#{hook_name}. It must contain only alphanumeric \"", "'characters (no underscores or dashes, etc.)'", "end", "end", "end", "if", "errors", ".", "any?", "if", "@log", "@log", ".", "error", "errors", ".", "join", "(", "\"\\n\"", ")", "@log", ".", "newline", "end", "raise", "Overcommit", "::", "Exceptions", "::", "ConfigurationError", ",", "'One or more hooks had invalid names'", "end", "end" ]
Prints an error message and raises an exception if a hook has an invalid name, since this can result in strange errors elsewhere.
[ "Prints", "an", "error", "message", "and", "raises", "an", "exception", "if", "a", "hook", "has", "an", "invalid", "name", "since", "this", "can", "result", "in", "strange", "errors", "elsewhere", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/configuration_validator.rb#L98-L121
train
sds/overcommit
lib/overcommit/configuration_validator.rb
Overcommit.ConfigurationValidator.check_for_missing_enabled_option
def check_for_missing_enabled_option(hash) return unless @log any_warnings = false Overcommit::Utils.supported_hook_type_classes.each do |hook_type| hash.fetch(hook_type) { {} }.each do |hook_name, hook_config| next if hook_name == 'ALL' if hook_config['enabled'].nil? @log.warning "#{hook_type}::#{hook_name} hook does not explicitly " \ 'set `enabled` option in .overcommit.yml' any_warnings = true end end end @log.newline if any_warnings end
ruby
def check_for_missing_enabled_option(hash) return unless @log any_warnings = false Overcommit::Utils.supported_hook_type_classes.each do |hook_type| hash.fetch(hook_type) { {} }.each do |hook_name, hook_config| next if hook_name == 'ALL' if hook_config['enabled'].nil? @log.warning "#{hook_type}::#{hook_name} hook does not explicitly " \ 'set `enabled` option in .overcommit.yml' any_warnings = true end end end @log.newline if any_warnings end
[ "def", "check_for_missing_enabled_option", "(", "hash", ")", "return", "unless", "@log", "any_warnings", "=", "false", "Overcommit", "::", "Utils", ".", "supported_hook_type_classes", ".", "each", "do", "|", "hook_type", "|", "hash", ".", "fetch", "(", "hook_type", ")", "{", "{", "}", "}", ".", "each", "do", "|", "hook_name", ",", "hook_config", "|", "next", "if", "hook_name", "==", "'ALL'", "if", "hook_config", "[", "'enabled'", "]", ".", "nil?", "@log", ".", "warning", "\"#{hook_type}::#{hook_name} hook does not explicitly \"", "'set `enabled` option in .overcommit.yml'", "any_warnings", "=", "true", "end", "end", "end", "@log", ".", "newline", "if", "any_warnings", "end" ]
Prints a warning if there are any hooks listed in the configuration without `enabled` explicitly set.
[ "Prints", "a", "warning", "if", "there", "are", "any", "hooks", "listed", "in", "the", "configuration", "without", "enabled", "explicitly", "set", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/configuration_validator.rb#L125-L143
train
sds/overcommit
lib/overcommit/configuration_validator.rb
Overcommit.ConfigurationValidator.check_for_too_many_processors
def check_for_too_many_processors(config, hash) concurrency = config.concurrency errors = [] Overcommit::Utils.supported_hook_type_classes.each do |hook_type| hash.fetch(hook_type) { {} }.each do |hook_name, hook_config| processors = hook_config.fetch('processors') { 1 } if processors > concurrency errors << "#{hook_type}::#{hook_name} `processors` value " \ "(#{processors}) is larger than the global `concurrency` " \ "option (#{concurrency})" end end end if errors.any? if @log @log.error errors.join("\n") @log.newline end raise Overcommit::Exceptions::ConfigurationError, 'One or more hooks had invalid `processor` value configured' end end
ruby
def check_for_too_many_processors(config, hash) concurrency = config.concurrency errors = [] Overcommit::Utils.supported_hook_type_classes.each do |hook_type| hash.fetch(hook_type) { {} }.each do |hook_name, hook_config| processors = hook_config.fetch('processors') { 1 } if processors > concurrency errors << "#{hook_type}::#{hook_name} `processors` value " \ "(#{processors}) is larger than the global `concurrency` " \ "option (#{concurrency})" end end end if errors.any? if @log @log.error errors.join("\n") @log.newline end raise Overcommit::Exceptions::ConfigurationError, 'One or more hooks had invalid `processor` value configured' end end
[ "def", "check_for_too_many_processors", "(", "config", ",", "hash", ")", "concurrency", "=", "config", ".", "concurrency", "errors", "=", "[", "]", "Overcommit", "::", "Utils", ".", "supported_hook_type_classes", ".", "each", "do", "|", "hook_type", "|", "hash", ".", "fetch", "(", "hook_type", ")", "{", "{", "}", "}", ".", "each", "do", "|", "hook_name", ",", "hook_config", "|", "processors", "=", "hook_config", ".", "fetch", "(", "'processors'", ")", "{", "1", "}", "if", "processors", ">", "concurrency", "errors", "<<", "\"#{hook_type}::#{hook_name} `processors` value \"", "\"(#{processors}) is larger than the global `concurrency` \"", "\"option (#{concurrency})\"", "end", "end", "end", "if", "errors", ".", "any?", "if", "@log", "@log", ".", "error", "errors", ".", "join", "(", "\"\\n\"", ")", "@log", ".", "newline", "end", "raise", "Overcommit", "::", "Exceptions", "::", "ConfigurationError", ",", "'One or more hooks had invalid `processor` value configured'", "end", "end" ]
Prints a warning if any hook has a number of processors larger than the global `concurrency` setting.
[ "Prints", "a", "warning", "if", "any", "hook", "has", "a", "number", "of", "processors", "larger", "than", "the", "global", "concurrency", "setting", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/configuration_validator.rb#L147-L170
train
sds/overcommit
lib/overcommit/configuration.rb
Overcommit.Configuration.all_builtin_hook_configs
def all_builtin_hook_configs hook_configs = {} Overcommit::Utils.supported_hook_type_classes.each do |hook_type| hook_names = @hash[hook_type].keys.reject { |name| name == 'ALL' } hook_configs[hook_type] = Hash[ hook_names.map do |hook_name| [hook_name, for_hook(hook_name, hook_type)] end ] end hook_configs end
ruby
def all_builtin_hook_configs hook_configs = {} Overcommit::Utils.supported_hook_type_classes.each do |hook_type| hook_names = @hash[hook_type].keys.reject { |name| name == 'ALL' } hook_configs[hook_type] = Hash[ hook_names.map do |hook_name| [hook_name, for_hook(hook_name, hook_type)] end ] end hook_configs end
[ "def", "all_builtin_hook_configs", "hook_configs", "=", "{", "}", "Overcommit", "::", "Utils", ".", "supported_hook_type_classes", ".", "each", "do", "|", "hook_type", "|", "hook_names", "=", "@hash", "[", "hook_type", "]", ".", "keys", ".", "reject", "{", "|", "name", "|", "name", "==", "'ALL'", "}", "hook_configs", "[", "hook_type", "]", "=", "Hash", "[", "hook_names", ".", "map", "do", "|", "hook_name", "|", "[", "hook_name", ",", "for_hook", "(", "hook_name", ",", "hook_type", ")", "]", "end", "]", "end", "hook_configs", "end" ]
Returns configuration for all built-in hooks in each hook type. @return [Hash]
[ "Returns", "configuration", "for", "all", "built", "-", "in", "hooks", "in", "each", "hook", "type", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/configuration.rb#L72-L86
train
sds/overcommit
lib/overcommit/configuration.rb
Overcommit.Configuration.all_plugin_hook_configs
def all_plugin_hook_configs hook_configs = {} Overcommit::Utils.supported_hook_types.each do |hook_type| hook_type_class_name = Overcommit::Utils.camel_case(hook_type) directory = File.join(plugin_directory, hook_type.tr('-', '_')) plugin_paths = Dir[File.join(directory, '*.rb')].sort hook_names = plugin_paths.map do |path| Overcommit::Utils.camel_case(File.basename(path, '.rb')) end hook_configs[hook_type_class_name] = Hash[ hook_names.map do |hook_name| [hook_name, for_hook(hook_name, Overcommit::Utils.camel_case(hook_type))] end ] end hook_configs end
ruby
def all_plugin_hook_configs hook_configs = {} Overcommit::Utils.supported_hook_types.each do |hook_type| hook_type_class_name = Overcommit::Utils.camel_case(hook_type) directory = File.join(plugin_directory, hook_type.tr('-', '_')) plugin_paths = Dir[File.join(directory, '*.rb')].sort hook_names = plugin_paths.map do |path| Overcommit::Utils.camel_case(File.basename(path, '.rb')) end hook_configs[hook_type_class_name] = Hash[ hook_names.map do |hook_name| [hook_name, for_hook(hook_name, Overcommit::Utils.camel_case(hook_type))] end ] end hook_configs end
[ "def", "all_plugin_hook_configs", "hook_configs", "=", "{", "}", "Overcommit", "::", "Utils", ".", "supported_hook_types", ".", "each", "do", "|", "hook_type", "|", "hook_type_class_name", "=", "Overcommit", "::", "Utils", ".", "camel_case", "(", "hook_type", ")", "directory", "=", "File", ".", "join", "(", "plugin_directory", ",", "hook_type", ".", "tr", "(", "'-'", ",", "'_'", ")", ")", "plugin_paths", "=", "Dir", "[", "File", ".", "join", "(", "directory", ",", "'*.rb'", ")", "]", ".", "sort", "hook_names", "=", "plugin_paths", ".", "map", "do", "|", "path", "|", "Overcommit", "::", "Utils", ".", "camel_case", "(", "File", ".", "basename", "(", "path", ",", "'.rb'", ")", ")", "end", "hook_configs", "[", "hook_type_class_name", "]", "=", "Hash", "[", "hook_names", ".", "map", "do", "|", "hook_name", "|", "[", "hook_name", ",", "for_hook", "(", "hook_name", ",", "Overcommit", "::", "Utils", ".", "camel_case", "(", "hook_type", ")", ")", "]", "end", "]", "end", "hook_configs", "end" ]
Returns configuration for all plugin hooks in each hook type. @return [Hash]
[ "Returns", "configuration", "for", "all", "plugin", "hooks", "in", "each", "hook", "type", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/configuration.rb#L91-L112
train
sds/overcommit
lib/overcommit/configuration.rb
Overcommit.Configuration.enabled_builtin_hooks
def enabled_builtin_hooks(hook_context) @hash[hook_context.hook_class_name].keys. reject { |hook_name| hook_name == 'ALL' }. select { |hook_name| built_in_hook?(hook_context, hook_name) }. select { |hook_name| hook_enabled?(hook_context, hook_name) } end
ruby
def enabled_builtin_hooks(hook_context) @hash[hook_context.hook_class_name].keys. reject { |hook_name| hook_name == 'ALL' }. select { |hook_name| built_in_hook?(hook_context, hook_name) }. select { |hook_name| hook_enabled?(hook_context, hook_name) } end
[ "def", "enabled_builtin_hooks", "(", "hook_context", ")", "@hash", "[", "hook_context", ".", "hook_class_name", "]", ".", "keys", ".", "reject", "{", "|", "hook_name", "|", "hook_name", "==", "'ALL'", "}", ".", "select", "{", "|", "hook_name", "|", "built_in_hook?", "(", "hook_context", ",", "hook_name", ")", "}", ".", "select", "{", "|", "hook_name", "|", "hook_enabled?", "(", "hook_context", ",", "hook_name", ")", "}", "end" ]
Returns the built-in hooks that have been enabled for a hook type.
[ "Returns", "the", "built", "-", "in", "hooks", "that", "have", "been", "enabled", "for", "a", "hook", "type", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/configuration.rb#L115-L120
train
sds/overcommit
lib/overcommit/configuration.rb
Overcommit.Configuration.enabled_ad_hoc_hooks
def enabled_ad_hoc_hooks(hook_context) @hash[hook_context.hook_class_name].keys. reject { |hook_name| hook_name == 'ALL' }. select { |hook_name| ad_hoc_hook?(hook_context, hook_name) }. select { |hook_name| hook_enabled?(hook_context, hook_name) } end
ruby
def enabled_ad_hoc_hooks(hook_context) @hash[hook_context.hook_class_name].keys. reject { |hook_name| hook_name == 'ALL' }. select { |hook_name| ad_hoc_hook?(hook_context, hook_name) }. select { |hook_name| hook_enabled?(hook_context, hook_name) } end
[ "def", "enabled_ad_hoc_hooks", "(", "hook_context", ")", "@hash", "[", "hook_context", ".", "hook_class_name", "]", ".", "keys", ".", "reject", "{", "|", "hook_name", "|", "hook_name", "==", "'ALL'", "}", ".", "select", "{", "|", "hook_name", "|", "ad_hoc_hook?", "(", "hook_context", ",", "hook_name", ")", "}", ".", "select", "{", "|", "hook_name", "|", "hook_enabled?", "(", "hook_context", ",", "hook_name", ")", "}", "end" ]
Returns the ad hoc hooks that have been enabled for a hook type.
[ "Returns", "the", "ad", "hoc", "hooks", "that", "have", "been", "enabled", "for", "a", "hook", "type", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/configuration.rb#L123-L128
train
sds/overcommit
lib/overcommit/configuration.rb
Overcommit.Configuration.for_hook
def for_hook(hook, hook_type = nil) unless hook_type components = hook.class.name.split('::') hook = components.last hook_type = components[-2] end # Merge hook configuration with special 'ALL' config hook_config = smart_merge(@hash[hook_type]['ALL'], @hash[hook_type][hook] || {}) # Need to specially handle `enabled` option since not setting it does not # necessarily mean the hook is disabled hook_config['enabled'] = hook_enabled?(hook_type, hook) hook_config.freeze end
ruby
def for_hook(hook, hook_type = nil) unless hook_type components = hook.class.name.split('::') hook = components.last hook_type = components[-2] end # Merge hook configuration with special 'ALL' config hook_config = smart_merge(@hash[hook_type]['ALL'], @hash[hook_type][hook] || {}) # Need to specially handle `enabled` option since not setting it does not # necessarily mean the hook is disabled hook_config['enabled'] = hook_enabled?(hook_type, hook) hook_config.freeze end
[ "def", "for_hook", "(", "hook", ",", "hook_type", "=", "nil", ")", "unless", "hook_type", "components", "=", "hook", ".", "class", ".", "name", ".", "split", "(", "'::'", ")", "hook", "=", "components", ".", "last", "hook_type", "=", "components", "[", "-", "2", "]", "end", "hook_config", "=", "smart_merge", "(", "@hash", "[", "hook_type", "]", "[", "'ALL'", "]", ",", "@hash", "[", "hook_type", "]", "[", "hook", "]", "||", "{", "}", ")", "hook_config", "[", "'enabled'", "]", "=", "hook_enabled?", "(", "hook_type", ",", "hook", ")", "hook_config", ".", "freeze", "end" ]
Returns a non-modifiable configuration for a hook.
[ "Returns", "a", "non", "-", "modifiable", "configuration", "for", "a", "hook", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/configuration.rb#L131-L146
train
sds/overcommit
lib/overcommit/configuration.rb
Overcommit.Configuration.apply_environment!
def apply_environment!(hook_context, env) skipped_hooks = "#{env['SKIP']} #{env['SKIP_CHECKS']} #{env['SKIP_HOOKS']}".split(/[:, ]/) only_hooks = env.fetch('ONLY') { '' }.split(/[:, ]/) hook_type = hook_context.hook_class_name if only_hooks.any? || skipped_hooks.include?('all') || skipped_hooks.include?('ALL') @hash[hook_type]['ALL']['skip'] = true end only_hooks.select { |hook_name| hook_exists?(hook_context, hook_name) }. map { |hook_name| Overcommit::Utils.camel_case(hook_name) }. each do |hook_name| @hash[hook_type][hook_name] ||= {} @hash[hook_type][hook_name]['skip'] = false end skipped_hooks.select { |hook_name| hook_exists?(hook_context, hook_name) }. map { |hook_name| Overcommit::Utils.camel_case(hook_name) }. each do |hook_name| @hash[hook_type][hook_name] ||= {} @hash[hook_type][hook_name]['skip'] = true end end
ruby
def apply_environment!(hook_context, env) skipped_hooks = "#{env['SKIP']} #{env['SKIP_CHECKS']} #{env['SKIP_HOOKS']}".split(/[:, ]/) only_hooks = env.fetch('ONLY') { '' }.split(/[:, ]/) hook_type = hook_context.hook_class_name if only_hooks.any? || skipped_hooks.include?('all') || skipped_hooks.include?('ALL') @hash[hook_type]['ALL']['skip'] = true end only_hooks.select { |hook_name| hook_exists?(hook_context, hook_name) }. map { |hook_name| Overcommit::Utils.camel_case(hook_name) }. each do |hook_name| @hash[hook_type][hook_name] ||= {} @hash[hook_type][hook_name]['skip'] = false end skipped_hooks.select { |hook_name| hook_exists?(hook_context, hook_name) }. map { |hook_name| Overcommit::Utils.camel_case(hook_name) }. each do |hook_name| @hash[hook_type][hook_name] ||= {} @hash[hook_type][hook_name]['skip'] = true end end
[ "def", "apply_environment!", "(", "hook_context", ",", "env", ")", "skipped_hooks", "=", "\"#{env['SKIP']} #{env['SKIP_CHECKS']} #{env['SKIP_HOOKS']}\"", ".", "split", "(", "/", "/", ")", "only_hooks", "=", "env", ".", "fetch", "(", "'ONLY'", ")", "{", "''", "}", ".", "split", "(", "/", "/", ")", "hook_type", "=", "hook_context", ".", "hook_class_name", "if", "only_hooks", ".", "any?", "||", "skipped_hooks", ".", "include?", "(", "'all'", ")", "||", "skipped_hooks", ".", "include?", "(", "'ALL'", ")", "@hash", "[", "hook_type", "]", "[", "'ALL'", "]", "[", "'skip'", "]", "=", "true", "end", "only_hooks", ".", "select", "{", "|", "hook_name", "|", "hook_exists?", "(", "hook_context", ",", "hook_name", ")", "}", ".", "map", "{", "|", "hook_name", "|", "Overcommit", "::", "Utils", ".", "camel_case", "(", "hook_name", ")", "}", ".", "each", "do", "|", "hook_name", "|", "@hash", "[", "hook_type", "]", "[", "hook_name", "]", "||=", "{", "}", "@hash", "[", "hook_type", "]", "[", "hook_name", "]", "[", "'skip'", "]", "=", "false", "end", "skipped_hooks", ".", "select", "{", "|", "hook_name", "|", "hook_exists?", "(", "hook_context", ",", "hook_name", ")", "}", ".", "map", "{", "|", "hook_name", "|", "Overcommit", "::", "Utils", ".", "camel_case", "(", "hook_name", ")", "}", ".", "each", "do", "|", "hook_name", "|", "@hash", "[", "hook_type", "]", "[", "hook_name", "]", "||=", "{", "}", "@hash", "[", "hook_type", "]", "[", "hook_name", "]", "[", "'skip'", "]", "=", "true", "end", "end" ]
Applies additional configuration settings based on the provided environment variables.
[ "Applies", "additional", "configuration", "settings", "based", "on", "the", "provided", "environment", "variables", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/configuration.rb#L157-L179
train
sds/overcommit
lib/overcommit/configuration.rb
Overcommit.Configuration.stored_signature
def stored_signature result = Overcommit::Utils.execute( %w[git config --local --get] + [signature_config_key] ) if result.status == 1 # Key doesn't exist return '' elsif result.status != 0 raise Overcommit::Exceptions::GitConfigError, "Unable to read from local repo git config: #{result.stderr}" end result.stdout.chomp end
ruby
def stored_signature result = Overcommit::Utils.execute( %w[git config --local --get] + [signature_config_key] ) if result.status == 1 # Key doesn't exist return '' elsif result.status != 0 raise Overcommit::Exceptions::GitConfigError, "Unable to read from local repo git config: #{result.stderr}" end result.stdout.chomp end
[ "def", "stored_signature", "result", "=", "Overcommit", "::", "Utils", ".", "execute", "(", "%w[", "git", "config", "--local", "--get", "]", "+", "[", "signature_config_key", "]", ")", "if", "result", ".", "status", "==", "1", "return", "''", "elsif", "result", ".", "status", "!=", "0", "raise", "Overcommit", "::", "Exceptions", "::", "GitConfigError", ",", "\"Unable to read from local repo git config: #{result.stderr}\"", "end", "result", ".", "stdout", ".", "chomp", "end" ]
Returns the stored signature of this repo's Overcommit configuration. This is intended to be compared against the current signature of this configuration object. @return [String]
[ "Returns", "the", "stored", "signature", "of", "this", "repo", "s", "Overcommit", "configuration", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/configuration.rb#L327-L340
train
sds/overcommit
lib/overcommit/git_repo.rb
Overcommit.GitRepo.all_files
def all_files `git ls-files`. split(/\n/). map { |relative_file| File.expand_path(relative_file) }. reject { |file| File.directory?(file) } # Exclude submodule directories end
ruby
def all_files `git ls-files`. split(/\n/). map { |relative_file| File.expand_path(relative_file) }. reject { |file| File.directory?(file) } # Exclude submodule directories end
[ "def", "all_files", "`", "`", ".", "split", "(", "/", "\\n", "/", ")", ".", "map", "{", "|", "relative_file", "|", "File", ".", "expand_path", "(", "relative_file", ")", "}", ".", "reject", "{", "|", "file", "|", "File", ".", "directory?", "(", "file", ")", "}", "end" ]
Returns the names of all files that are tracked by git. @return [Array<String>] list of absolute file paths
[ "Returns", "the", "names", "of", "all", "files", "that", "are", "tracked", "by", "git", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/git_repo.rb#L129-L134
train
sds/overcommit
lib/overcommit/git_repo.rb
Overcommit.GitRepo.restore_merge_state
def restore_merge_state if @merge_head FileUtils.touch(File.expand_path('MERGE_MODE', Overcommit::Utils.git_dir)) File.open(File.expand_path('MERGE_HEAD', Overcommit::Utils.git_dir), 'w') do |f| f.write(@merge_head) end @merge_head = nil end if @merge_msg File.open(File.expand_path('MERGE_MSG', Overcommit::Utils.git_dir), 'w') do |f| f.write("#{@merge_msg}\n") end @merge_msg = nil end end
ruby
def restore_merge_state if @merge_head FileUtils.touch(File.expand_path('MERGE_MODE', Overcommit::Utils.git_dir)) File.open(File.expand_path('MERGE_HEAD', Overcommit::Utils.git_dir), 'w') do |f| f.write(@merge_head) end @merge_head = nil end if @merge_msg File.open(File.expand_path('MERGE_MSG', Overcommit::Utils.git_dir), 'w') do |f| f.write("#{@merge_msg}\n") end @merge_msg = nil end end
[ "def", "restore_merge_state", "if", "@merge_head", "FileUtils", ".", "touch", "(", "File", ".", "expand_path", "(", "'MERGE_MODE'", ",", "Overcommit", "::", "Utils", ".", "git_dir", ")", ")", "File", ".", "open", "(", "File", ".", "expand_path", "(", "'MERGE_HEAD'", ",", "Overcommit", "::", "Utils", ".", "git_dir", ")", ",", "'w'", ")", "do", "|", "f", "|", "f", ".", "write", "(", "@merge_head", ")", "end", "@merge_head", "=", "nil", "end", "if", "@merge_msg", "File", ".", "open", "(", "File", ".", "expand_path", "(", "'MERGE_MSG'", ",", "Overcommit", "::", "Utils", ".", "git_dir", ")", ",", "'w'", ")", "do", "|", "f", "|", "f", ".", "write", "(", "\"#{@merge_msg}\\n\"", ")", "end", "@merge_msg", "=", "nil", "end", "end" ]
Restore any relevant files that were present when repo was in the middle of a merge.
[ "Restore", "any", "relevant", "files", "that", "were", "present", "when", "repo", "was", "in", "the", "middle", "of", "a", "merge", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/git_repo.rb#L175-L191
train
sds/overcommit
lib/overcommit/git_repo.rb
Overcommit.GitRepo.restore_cherry_pick_state
def restore_cherry_pick_state if @cherry_head File.open(File.expand_path('CHERRY_PICK_HEAD', Overcommit::Utils.git_dir), 'w') do |f| f.write(@cherry_head) end @cherry_head = nil end end
ruby
def restore_cherry_pick_state if @cherry_head File.open(File.expand_path('CHERRY_PICK_HEAD', Overcommit::Utils.git_dir), 'w') do |f| f.write(@cherry_head) end @cherry_head = nil end end
[ "def", "restore_cherry_pick_state", "if", "@cherry_head", "File", ".", "open", "(", "File", ".", "expand_path", "(", "'CHERRY_PICK_HEAD'", ",", "Overcommit", "::", "Utils", ".", "git_dir", ")", ",", "'w'", ")", "do", "|", "f", "|", "f", ".", "write", "(", "@cherry_head", ")", "end", "@cherry_head", "=", "nil", "end", "end" ]
Restore any relevant files that were present when repo was in the middle of a cherry-pick.
[ "Restore", "any", "relevant", "files", "that", "were", "present", "when", "repo", "was", "in", "the", "middle", "of", "a", "cherry", "-", "pick", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/git_repo.rb#L195-L203
train
sds/overcommit
lib/overcommit/hook_signer.rb
Overcommit.HookSigner.update_signature!
def update_signature! result = Overcommit::Utils.execute( %w[git config --local] + [signature_config_key, signature] ) unless result.success? raise Overcommit::Exceptions::GitConfigError, "Unable to write to local repo git config: #{result.stderr}" end end
ruby
def update_signature! result = Overcommit::Utils.execute( %w[git config --local] + [signature_config_key, signature] ) unless result.success? raise Overcommit::Exceptions::GitConfigError, "Unable to write to local repo git config: #{result.stderr}" end end
[ "def", "update_signature!", "result", "=", "Overcommit", "::", "Utils", ".", "execute", "(", "%w[", "git", "config", "--local", "]", "+", "[", "signature_config_key", ",", "signature", "]", ")", "unless", "result", ".", "success?", "raise", "Overcommit", "::", "Exceptions", "::", "GitConfigError", ",", "\"Unable to write to local repo git config: #{result.stderr}\"", "end", "end" ]
Update the current stored signature for this hook.
[ "Update", "the", "current", "stored", "signature", "for", "this", "hook", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/hook_signer.rb#L69-L78
train
sds/overcommit
lib/overcommit/hook_signer.rb
Overcommit.HookSigner.signature
def signature hook_config = @config.for_hook(@hook_name, @context.hook_class_name). dup. tap { |config| IGNORED_CONFIG_KEYS.each { |k| config.delete(k) } } content_to_sign = if signable_file?(hook_path) && Overcommit::GitRepo.tracked?(hook_path) hook_contents end Digest::SHA256.hexdigest(content_to_sign.to_s + hook_config.to_s) end
ruby
def signature hook_config = @config.for_hook(@hook_name, @context.hook_class_name). dup. tap { |config| IGNORED_CONFIG_KEYS.each { |k| config.delete(k) } } content_to_sign = if signable_file?(hook_path) && Overcommit::GitRepo.tracked?(hook_path) hook_contents end Digest::SHA256.hexdigest(content_to_sign.to_s + hook_config.to_s) end
[ "def", "signature", "hook_config", "=", "@config", ".", "for_hook", "(", "@hook_name", ",", "@context", ".", "hook_class_name", ")", ".", "dup", ".", "tap", "{", "|", "config", "|", "IGNORED_CONFIG_KEYS", ".", "each", "{", "|", "k", "|", "config", ".", "delete", "(", "k", ")", "}", "}", "content_to_sign", "=", "if", "signable_file?", "(", "hook_path", ")", "&&", "Overcommit", "::", "GitRepo", ".", "tracked?", "(", "hook_path", ")", "hook_contents", "end", "Digest", "::", "SHA256", ".", "hexdigest", "(", "content_to_sign", ".", "to_s", "+", "hook_config", ".", "to_s", ")", "end" ]
Calculates a hash of a hook using a combination of its configuration and file contents. This way, if either the plugin code changes or its configuration changes, the hash will change and we can alert the user to this change.
[ "Calculates", "a", "hash", "of", "a", "hook", "using", "a", "combination", "of", "its", "configuration", "and", "file", "contents", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/hook_signer.rb#L87-L98
train
sds/overcommit
lib/overcommit/printer.rb
Overcommit.Printer.end_hook
def end_hook(hook, status, output) # Want to print the header for quiet hooks only if the result wasn't good # so that the user knows what failed print_header(hook) if (!hook.quiet? && !@config['quiet']) || status != :pass print_result(hook, status, output) end
ruby
def end_hook(hook, status, output) # Want to print the header for quiet hooks only if the result wasn't good # so that the user knows what failed print_header(hook) if (!hook.quiet? && !@config['quiet']) || status != :pass print_result(hook, status, output) end
[ "def", "end_hook", "(", "hook", ",", "status", ",", "output", ")", "print_header", "(", "hook", ")", "if", "(", "!", "hook", ".", "quiet?", "&&", "!", "@config", "[", "'quiet'", "]", ")", "||", "status", "!=", ":pass", "print_result", "(", "hook", ",", "status", ",", "output", ")", "end" ]
Executed at the end of an individual hook run.
[ "Executed", "at", "the", "end", "of", "an", "individual", "hook", "run", "." ]
35d60adb41da942178b789560968e3ad030b0ac7
https://github.com/sds/overcommit/blob/35d60adb41da942178b789560968e3ad030b0ac7/lib/overcommit/printer.rb#L37-L43
train
ruby/rake
lib/rake/loaders/makefile.rb
Rake.MakefileLoader.process_line
def process_line(line) # :nodoc: file_tasks, args = line.split(":", 2) return if args.nil? dependents = args.split.map { |d| respace(d) } file_tasks.scan(/\S+/) do |file_task| file_task = respace(file_task) file file_task => dependents end end
ruby
def process_line(line) # :nodoc: file_tasks, args = line.split(":", 2) return if args.nil? dependents = args.split.map { |d| respace(d) } file_tasks.scan(/\S+/) do |file_task| file_task = respace(file_task) file file_task => dependents end end
[ "def", "process_line", "(", "line", ")", "file_tasks", ",", "args", "=", "line", ".", "split", "(", "\":\"", ",", "2", ")", "return", "if", "args", ".", "nil?", "dependents", "=", "args", ".", "split", ".", "map", "{", "|", "d", "|", "respace", "(", "d", ")", "}", "file_tasks", ".", "scan", "(", "/", "\\S", "/", ")", "do", "|", "file_task", "|", "file_task", "=", "respace", "(", "file_task", ")", "file", "file_task", "=>", "dependents", "end", "end" ]
Process one logical line of makefile data.
[ "Process", "one", "logical", "line", "of", "makefile", "data", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/loaders/makefile.rb#L37-L45
train
ruby/rake
lib/rake/task.rb
Rake.Task.invoke
def invoke(*args) task_args = TaskArguments.new(arg_names, args) invoke_with_call_chain(task_args, InvocationChain::EMPTY) end
ruby
def invoke(*args) task_args = TaskArguments.new(arg_names, args) invoke_with_call_chain(task_args, InvocationChain::EMPTY) end
[ "def", "invoke", "(", "*", "args", ")", "task_args", "=", "TaskArguments", ".", "new", "(", "arg_names", ",", "args", ")", "invoke_with_call_chain", "(", "task_args", ",", "InvocationChain", "::", "EMPTY", ")", "end" ]
Invoke the task if it is needed. Prerequisites are invoked first.
[ "Invoke", "the", "task", "if", "it", "is", "needed", ".", "Prerequisites", "are", "invoked", "first", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/task.rb#L181-L184
train
ruby/rake
lib/rake/task.rb
Rake.Task.transform_comments
def transform_comments(separator, &block) if @comments.empty? nil else block ||= lambda { |c| c } @comments.map(&block).join(separator) end end
ruby
def transform_comments(separator, &block) if @comments.empty? nil else block ||= lambda { |c| c } @comments.map(&block).join(separator) end end
[ "def", "transform_comments", "(", "separator", ",", "&", "block", ")", "if", "@comments", ".", "empty?", "nil", "else", "block", "||=", "lambda", "{", "|", "c", "|", "c", "}", "@comments", ".", "map", "(", "&", "block", ")", ".", "join", "(", "separator", ")", "end", "end" ]
Transform the list of comments as specified by the block and join with the separator.
[ "Transform", "the", "list", "of", "comments", "as", "specified", "by", "the", "block", "and", "join", "with", "the", "separator", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/task.rb#L319-L326
train
ruby/rake
lib/rake/file_utils_ext.rb
Rake.FileUtilsExt.rake_merge_option
def rake_merge_option(args, defaults) if Hash === args.last defaults.update(args.last) args.pop end args.push defaults args end
ruby
def rake_merge_option(args, defaults) if Hash === args.last defaults.update(args.last) args.pop end args.push defaults args end
[ "def", "rake_merge_option", "(", "args", ",", "defaults", ")", "if", "Hash", "===", "args", ".", "last", "defaults", ".", "update", "(", "args", ".", "last", ")", "args", ".", "pop", "end", "args", ".", "push", "defaults", "args", "end" ]
Merge the given options with the default values.
[ "Merge", "the", "given", "options", "with", "the", "default", "values", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/file_utils_ext.rb#L117-L124
train
ruby/rake
lib/rake/file_utils_ext.rb
Rake.FileUtilsExt.rake_check_options
def rake_check_options(options, *optdecl) h = options.dup optdecl.each do |name| h.delete name end raise ArgumentError, "no such option: #{h.keys.join(' ')}" unless h.empty? end
ruby
def rake_check_options(options, *optdecl) h = options.dup optdecl.each do |name| h.delete name end raise ArgumentError, "no such option: #{h.keys.join(' ')}" unless h.empty? end
[ "def", "rake_check_options", "(", "options", ",", "*", "optdecl", ")", "h", "=", "options", ".", "dup", "optdecl", ".", "each", "do", "|", "name", "|", "h", ".", "delete", "name", "end", "raise", "ArgumentError", ",", "\"no such option: #{h.keys.join(' ')}\"", "unless", "h", ".", "empty?", "end" ]
Check that the options do not contain options not listed in +optdecl+. An ArgumentError exception is thrown if non-declared options are found.
[ "Check", "that", "the", "options", "do", "not", "contain", "options", "not", "listed", "in", "+", "optdecl", "+", ".", "An", "ArgumentError", "exception", "is", "thrown", "if", "non", "-", "declared", "options", "are", "found", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/file_utils_ext.rb#L134-L141
train
ruby/rake
lib/rake/scope.rb
Rake.Scope.trim
def trim(n) result = self while n > 0 && !result.empty? result = result.tail n -= 1 end result end
ruby
def trim(n) result = self while n > 0 && !result.empty? result = result.tail n -= 1 end result end
[ "def", "trim", "(", "n", ")", "result", "=", "self", "while", "n", ">", "0", "&&", "!", "result", ".", "empty?", "result", "=", "result", ".", "tail", "n", "-=", "1", "end", "result", "end" ]
Trim +n+ innermost scope levels from the scope. In no case will this trim beyond the toplevel scope.
[ "Trim", "+", "n", "+", "innermost", "scope", "levels", "from", "the", "scope", ".", "In", "no", "case", "will", "this", "trim", "beyond", "the", "toplevel", "scope", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/scope.rb#L17-L24
train
ruby/rake
lib/rake/application.rb
Rake.Application.init
def init(app_name="rake", argv = ARGV) standard_exception_handling do @name = app_name begin args = handle_options argv rescue ArgumentError # Backward compatibility for capistrano args = handle_options end collect_command_line_tasks(args) end end
ruby
def init(app_name="rake", argv = ARGV) standard_exception_handling do @name = app_name begin args = handle_options argv rescue ArgumentError # Backward compatibility for capistrano args = handle_options end collect_command_line_tasks(args) end end
[ "def", "init", "(", "app_name", "=", "\"rake\"", ",", "argv", "=", "ARGV", ")", "standard_exception_handling", "do", "@name", "=", "app_name", "begin", "args", "=", "handle_options", "argv", "rescue", "ArgumentError", "args", "=", "handle_options", "end", "collect_command_line_tasks", "(", "args", ")", "end", "end" ]
Initialize the command line parameters and app name.
[ "Initialize", "the", "command", "line", "parameters", "and", "app", "name", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/application.rb#L88-L99
train
ruby/rake
lib/rake/application.rb
Rake.Application.have_rakefile
def have_rakefile # :nodoc: @rakefiles.each do |fn| if File.exist?(fn) others = FileList.glob(fn, File::FNM_CASEFOLD) return others.size == 1 ? others.first : fn elsif fn == "" return fn end end return nil end
ruby
def have_rakefile # :nodoc: @rakefiles.each do |fn| if File.exist?(fn) others = FileList.glob(fn, File::FNM_CASEFOLD) return others.size == 1 ? others.first : fn elsif fn == "" return fn end end return nil end
[ "def", "have_rakefile", "@rakefiles", ".", "each", "do", "|", "fn", "|", "if", "File", ".", "exist?", "(", "fn", ")", "others", "=", "FileList", ".", "glob", "(", "fn", ",", "File", "::", "FNM_CASEFOLD", ")", "return", "others", ".", "size", "==", "1", "?", "others", ".", "first", ":", "fn", "elsif", "fn", "==", "\"\"", "return", "fn", "end", "end", "return", "nil", "end" ]
True if one of the files in RAKEFILES is in the current directory. If a match is found, it is copied into @rakefile.
[ "True", "if", "one", "of", "the", "files", "in", "RAKEFILES", "is", "in", "the", "current", "directory", ".", "If", "a", "match", "is", "found", "it", "is", "copied", "into" ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/application.rb#L274-L284
train
ruby/rake
lib/rake/file_list.rb
Rake.FileList.sub
def sub(pat, rep) inject(self.class.new) { |res, fn| res << fn.sub(pat, rep) } end
ruby
def sub(pat, rep) inject(self.class.new) { |res, fn| res << fn.sub(pat, rep) } end
[ "def", "sub", "(", "pat", ",", "rep", ")", "inject", "(", "self", ".", "class", ".", "new", ")", "{", "|", "res", ",", "fn", "|", "res", "<<", "fn", ".", "sub", "(", "pat", ",", "rep", ")", "}", "end" ]
Return a new FileList with the results of running +sub+ against each element of the original list. Example: FileList['a.c', 'b.c'].sub(/\.c$/, '.o') => ['a.o', 'b.o']
[ "Return", "a", "new", "FileList", "with", "the", "results", "of", "running", "+", "sub", "+", "against", "each", "element", "of", "the", "original", "list", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/file_list.rb#L242-L244
train
ruby/rake
lib/rake/file_list.rb
Rake.FileList.gsub
def gsub(pat, rep) inject(self.class.new) { |res, fn| res << fn.gsub(pat, rep) } end
ruby
def gsub(pat, rep) inject(self.class.new) { |res, fn| res << fn.gsub(pat, rep) } end
[ "def", "gsub", "(", "pat", ",", "rep", ")", "inject", "(", "self", ".", "class", ".", "new", ")", "{", "|", "res", ",", "fn", "|", "res", "<<", "fn", ".", "gsub", "(", "pat", ",", "rep", ")", "}", "end" ]
Return a new FileList with the results of running +gsub+ against each element of the original list. Example: FileList['lib/test/file', 'x/y'].gsub(/\//, "\\") => ['lib\\test\\file', 'x\\y']
[ "Return", "a", "new", "FileList", "with", "the", "results", "of", "running", "+", "gsub", "+", "against", "each", "element", "of", "the", "original", "list", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/file_list.rb#L253-L255
train
ruby/rake
lib/rake/file_list.rb
Rake.FileList.sub!
def sub!(pat, rep) each_with_index { |fn, i| self[i] = fn.sub(pat, rep) } self end
ruby
def sub!(pat, rep) each_with_index { |fn, i| self[i] = fn.sub(pat, rep) } self end
[ "def", "sub!", "(", "pat", ",", "rep", ")", "each_with_index", "{", "|", "fn", ",", "i", "|", "self", "[", "i", "]", "=", "fn", ".", "sub", "(", "pat", ",", "rep", ")", "}", "self", "end" ]
Same as +sub+ except that the original file list is modified.
[ "Same", "as", "+", "sub", "+", "except", "that", "the", "original", "file", "list", "is", "modified", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/file_list.rb#L258-L261
train
ruby/rake
lib/rake/file_list.rb
Rake.FileList.gsub!
def gsub!(pat, rep) each_with_index { |fn, i| self[i] = fn.gsub(pat, rep) } self end
ruby
def gsub!(pat, rep) each_with_index { |fn, i| self[i] = fn.gsub(pat, rep) } self end
[ "def", "gsub!", "(", "pat", ",", "rep", ")", "each_with_index", "{", "|", "fn", ",", "i", "|", "self", "[", "i", "]", "=", "fn", ".", "gsub", "(", "pat", ",", "rep", ")", "}", "self", "end" ]
Same as +gsub+ except that the original file list is modified.
[ "Same", "as", "+", "gsub", "+", "except", "that", "the", "original", "file", "list", "is", "modified", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/file_list.rb#L264-L267
train
ruby/rake
lib/rake/packagetask.rb
Rake.PackageTask.define
def define fail "Version required (or :noversion)" if @version.nil? @version = nil if :noversion == @version desc "Build all the packages" task :package desc "Force a rebuild of the package files" task repackage: [:clobber_package, :package] desc "Remove package products" task :clobber_package do rm_r package_dir rescue nil end task clobber: [:clobber_package] [ [need_tar, tgz_file, "z"], [need_tar_gz, tar_gz_file, "z"], [need_tar_bz2, tar_bz2_file, "j"], [need_tar_xz, tar_xz_file, "J"] ].each do |need, file, flag| if need task package: ["#{package_dir}/#{file}"] file "#{package_dir}/#{file}" => [package_dir_path] + package_files do chdir(package_dir) { sh @tar_command, "#{flag}cvf", file, package_name } end end end if need_zip task package: ["#{package_dir}/#{zip_file}"] file "#{package_dir}/#{zip_file}" => [package_dir_path] + package_files do chdir(package_dir) { sh @zip_command, "-r", zip_file, package_name } end end directory package_dir_path => @package_files do @package_files.each do |fn| f = File.join(package_dir_path, fn) fdir = File.dirname(f) mkdir_p(fdir) unless File.exist?(fdir) if File.directory?(fn) mkdir_p(f) else rm_f f safe_ln(fn, f) end end end self end
ruby
def define fail "Version required (or :noversion)" if @version.nil? @version = nil if :noversion == @version desc "Build all the packages" task :package desc "Force a rebuild of the package files" task repackage: [:clobber_package, :package] desc "Remove package products" task :clobber_package do rm_r package_dir rescue nil end task clobber: [:clobber_package] [ [need_tar, tgz_file, "z"], [need_tar_gz, tar_gz_file, "z"], [need_tar_bz2, tar_bz2_file, "j"], [need_tar_xz, tar_xz_file, "J"] ].each do |need, file, flag| if need task package: ["#{package_dir}/#{file}"] file "#{package_dir}/#{file}" => [package_dir_path] + package_files do chdir(package_dir) { sh @tar_command, "#{flag}cvf", file, package_name } end end end if need_zip task package: ["#{package_dir}/#{zip_file}"] file "#{package_dir}/#{zip_file}" => [package_dir_path] + package_files do chdir(package_dir) { sh @zip_command, "-r", zip_file, package_name } end end directory package_dir_path => @package_files do @package_files.each do |fn| f = File.join(package_dir_path, fn) fdir = File.dirname(f) mkdir_p(fdir) unless File.exist?(fdir) if File.directory?(fn) mkdir_p(f) else rm_f f safe_ln(fn, f) end end end self end
[ "def", "define", "fail", "\"Version required (or :noversion)\"", "if", "@version", ".", "nil?", "@version", "=", "nil", "if", ":noversion", "==", "@version", "desc", "\"Build all the packages\"", "task", ":package", "desc", "\"Force a rebuild of the package files\"", "task", "repackage", ":", "[", ":clobber_package", ",", ":package", "]", "desc", "\"Remove package products\"", "task", ":clobber_package", "do", "rm_r", "package_dir", "rescue", "nil", "end", "task", "clobber", ":", "[", ":clobber_package", "]", "[", "[", "need_tar", ",", "tgz_file", ",", "\"z\"", "]", ",", "[", "need_tar_gz", ",", "tar_gz_file", ",", "\"z\"", "]", ",", "[", "need_tar_bz2", ",", "tar_bz2_file", ",", "\"j\"", "]", ",", "[", "need_tar_xz", ",", "tar_xz_file", ",", "\"J\"", "]", "]", ".", "each", "do", "|", "need", ",", "file", ",", "flag", "|", "if", "need", "task", "package", ":", "[", "\"#{package_dir}/#{file}\"", "]", "file", "\"#{package_dir}/#{file}\"", "=>", "[", "package_dir_path", "]", "+", "package_files", "do", "chdir", "(", "package_dir", ")", "{", "sh", "@tar_command", ",", "\"#{flag}cvf\"", ",", "file", ",", "package_name", "}", "end", "end", "end", "if", "need_zip", "task", "package", ":", "[", "\"#{package_dir}/#{zip_file}\"", "]", "file", "\"#{package_dir}/#{zip_file}\"", "=>", "[", "package_dir_path", "]", "+", "package_files", "do", "chdir", "(", "package_dir", ")", "{", "sh", "@zip_command", ",", "\"-r\"", ",", "zip_file", ",", "package_name", "}", "end", "end", "directory", "package_dir_path", "=>", "@package_files", "do", "@package_files", ".", "each", "do", "|", "fn", "|", "f", "=", "File", ".", "join", "(", "package_dir_path", ",", "fn", ")", "fdir", "=", "File", ".", "dirname", "(", "f", ")", "mkdir_p", "(", "fdir", ")", "unless", "File", ".", "exist?", "(", "fdir", ")", "if", "File", ".", "directory?", "(", "fn", ")", "mkdir_p", "(", "f", ")", "else", "rm_f", "f", "safe_ln", "(", "fn", ",", "f", ")", "end", "end", "end", "self", "end" ]
Create the tasks defined by this task library.
[ "Create", "the", "tasks", "defined", "by", "this", "task", "library", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/packagetask.rb#L108-L162
train
ruby/rake
lib/rake/thread_pool.rb
Rake.ThreadPool.join
def join @threads_mon.synchronize do begin stat :joining @join_cond.wait unless @threads.empty? stat :joined rescue Exception => e stat :joined $stderr.puts e $stderr.print "Queue contains #{@queue.size} items. " + "Thread pool contains #{@threads.count} threads\n" $stderr.print "Current Thread #{Thread.current} status = " + "#{Thread.current.status}\n" $stderr.puts e.backtrace.join("\n") @threads.each do |t| $stderr.print "Thread #{t} status = #{t.status}\n" $stderr.puts t.backtrace.join("\n") end raise e end end end
ruby
def join @threads_mon.synchronize do begin stat :joining @join_cond.wait unless @threads.empty? stat :joined rescue Exception => e stat :joined $stderr.puts e $stderr.print "Queue contains #{@queue.size} items. " + "Thread pool contains #{@threads.count} threads\n" $stderr.print "Current Thread #{Thread.current} status = " + "#{Thread.current.status}\n" $stderr.puts e.backtrace.join("\n") @threads.each do |t| $stderr.print "Thread #{t} status = #{t.status}\n" $stderr.puts t.backtrace.join("\n") end raise e end end end
[ "def", "join", "@threads_mon", ".", "synchronize", "do", "begin", "stat", ":joining", "@join_cond", ".", "wait", "unless", "@threads", ".", "empty?", "stat", ":joined", "rescue", "Exception", "=>", "e", "stat", ":joined", "$stderr", ".", "puts", "e", "$stderr", ".", "print", "\"Queue contains #{@queue.size} items. \"", "+", "\"Thread pool contains #{@threads.count} threads\\n\"", "$stderr", ".", "print", "\"Current Thread #{Thread.current} status = \"", "+", "\"#{Thread.current.status}\\n\"", "$stderr", ".", "puts", "e", ".", "backtrace", ".", "join", "(", "\"\\n\"", ")", "@threads", ".", "each", "do", "|", "t", "|", "$stderr", ".", "print", "\"Thread #{t} status = #{t.status}\\n\"", "$stderr", ".", "puts", "t", ".", "backtrace", ".", "join", "(", "\"\\n\"", ")", "end", "raise", "e", "end", "end", "end" ]
Waits until the queue of futures is empty and all threads have exited.
[ "Waits", "until", "the", "queue", "of", "futures", "is", "empty", "and", "all", "threads", "have", "exited", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/thread_pool.rb#L44-L65
train
ruby/rake
lib/rake/thread_pool.rb
Rake.ThreadPool.process_queue_item
def process_queue_item #:nodoc: return false if @queue.empty? # Even though we just asked if the queue was empty, it # still could have had an item which by this statement # is now gone. For this reason we pass true to Queue#deq # because we will sleep indefinitely if it is empty. promise = @queue.deq(true) stat :dequeued, item_id: promise.object_id promise.work return true rescue ThreadError # this means the queue is empty false end
ruby
def process_queue_item #:nodoc: return false if @queue.empty? # Even though we just asked if the queue was empty, it # still could have had an item which by this statement # is now gone. For this reason we pass true to Queue#deq # because we will sleep indefinitely if it is empty. promise = @queue.deq(true) stat :dequeued, item_id: promise.object_id promise.work return true rescue ThreadError # this means the queue is empty false end
[ "def", "process_queue_item", "return", "false", "if", "@queue", ".", "empty?", "promise", "=", "@queue", ".", "deq", "(", "true", ")", "stat", ":dequeued", ",", "item_id", ":", "promise", ".", "object_id", "promise", ".", "work", "return", "true", "rescue", "ThreadError", "false", "end" ]
processes one item on the queue. Returns true if there was an item to process, false if there was no item
[ "processes", "one", "item", "on", "the", "queue", ".", "Returns", "true", "if", "there", "was", "an", "item", "to", "process", "false", "if", "there", "was", "no", "item" ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/thread_pool.rb#L95-L109
train
ruby/rake
lib/rake/task_manager.rb
Rake.TaskManager.resolve_args_without_dependencies
def resolve_args_without_dependencies(args) task_name = args.shift if args.size == 1 && args.first.respond_to?(:to_ary) arg_names = args.first.to_ary else arg_names = args end [task_name, arg_names, []] end
ruby
def resolve_args_without_dependencies(args) task_name = args.shift if args.size == 1 && args.first.respond_to?(:to_ary) arg_names = args.first.to_ary else arg_names = args end [task_name, arg_names, []] end
[ "def", "resolve_args_without_dependencies", "(", "args", ")", "task_name", "=", "args", ".", "shift", "if", "args", ".", "size", "==", "1", "&&", "args", ".", "first", ".", "respond_to?", "(", ":to_ary", ")", "arg_names", "=", "args", ".", "first", ".", "to_ary", "else", "arg_names", "=", "args", "end", "[", "task_name", ",", "arg_names", ",", "[", "]", "]", "end" ]
Resolve task arguments for a task or rule when there are no dependencies declared. The patterns recognized by this argument resolving function are: task :t task :t, [:a]
[ "Resolve", "task", "arguments", "for", "a", "task", "or", "rule", "when", "there", "are", "no", "dependencies", "declared", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/task_manager.rb#L105-L113
train
ruby/rake
lib/rake/task_manager.rb
Rake.TaskManager.enhance_with_matching_rule
def enhance_with_matching_rule(task_name, level=0) fail Rake::RuleRecursionOverflowError, "Rule Recursion Too Deep" if level >= 16 @rules.each do |pattern, args, extensions, block| if pattern && pattern.match(task_name) task = attempt_rule(task_name, pattern, args, extensions, block, level) return task if task end end nil rescue Rake::RuleRecursionOverflowError => ex ex.add_target(task_name) fail ex end
ruby
def enhance_with_matching_rule(task_name, level=0) fail Rake::RuleRecursionOverflowError, "Rule Recursion Too Deep" if level >= 16 @rules.each do |pattern, args, extensions, block| if pattern && pattern.match(task_name) task = attempt_rule(task_name, pattern, args, extensions, block, level) return task if task end end nil rescue Rake::RuleRecursionOverflowError => ex ex.add_target(task_name) fail ex end
[ "def", "enhance_with_matching_rule", "(", "task_name", ",", "level", "=", "0", ")", "fail", "Rake", "::", "RuleRecursionOverflowError", ",", "\"Rule Recursion Too Deep\"", "if", "level", ">=", "16", "@rules", ".", "each", "do", "|", "pattern", ",", "args", ",", "extensions", ",", "block", "|", "if", "pattern", "&&", "pattern", ".", "match", "(", "task_name", ")", "task", "=", "attempt_rule", "(", "task_name", ",", "pattern", ",", "args", ",", "extensions", ",", "block", ",", "level", ")", "return", "task", "if", "task", "end", "end", "nil", "rescue", "Rake", "::", "RuleRecursionOverflowError", "=>", "ex", "ex", ".", "add_target", "(", "task_name", ")", "fail", "ex", "end" ]
If a rule can be found that matches the task name, enhance the task with the prerequisites and actions from the rule. Set the source attribute of the task appropriately for the rule. Return the enhanced task or nil of no rule was found.
[ "If", "a", "rule", "can", "be", "found", "that", "matches", "the", "task", "name", "enhance", "the", "task", "with", "the", "prerequisites", "and", "actions", "from", "the", "rule", ".", "Set", "the", "source", "attribute", "of", "the", "task", "appropriately", "for", "the", "rule", ".", "Return", "the", "enhanced", "task", "or", "nil", "of", "no", "rule", "was", "found", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/task_manager.rb#L145-L158
train
ruby/rake
lib/rake/task_manager.rb
Rake.TaskManager.find_location
def find_location locations = caller i = 0 while locations[i] return locations[i + 1] if locations[i] =~ /rake\/dsl_definition.rb/ i += 1 end nil end
ruby
def find_location locations = caller i = 0 while locations[i] return locations[i + 1] if locations[i] =~ /rake\/dsl_definition.rb/ i += 1 end nil end
[ "def", "find_location", "locations", "=", "caller", "i", "=", "0", "while", "locations", "[", "i", "]", "return", "locations", "[", "i", "+", "1", "]", "if", "locations", "[", "i", "]", "=~", "/", "\\/", "/", "i", "+=", "1", "end", "nil", "end" ]
Find the location that called into the dsl layer.
[ "Find", "the", "location", "that", "called", "into", "the", "dsl", "layer", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/task_manager.rb#L241-L249
train
ruby/rake
lib/rake/task_manager.rb
Rake.TaskManager.attempt_rule
def attempt_rule(task_name, task_pattern, args, extensions, block, level) sources = make_sources(task_name, task_pattern, extensions) prereqs = sources.map { |source| trace_rule level, "Attempting Rule #{task_name} => #{source}" if File.exist?(source) || Rake::Task.task_defined?(source) trace_rule level, "(#{task_name} => #{source} ... EXIST)" source elsif parent = enhance_with_matching_rule(source, level + 1) trace_rule level, "(#{task_name} => #{source} ... ENHANCE)" parent.name else trace_rule level, "(#{task_name} => #{source} ... FAIL)" return nil end } task = FileTask.define_task(task_name, { args => prereqs }, &block) task.sources = prereqs task end
ruby
def attempt_rule(task_name, task_pattern, args, extensions, block, level) sources = make_sources(task_name, task_pattern, extensions) prereqs = sources.map { |source| trace_rule level, "Attempting Rule #{task_name} => #{source}" if File.exist?(source) || Rake::Task.task_defined?(source) trace_rule level, "(#{task_name} => #{source} ... EXIST)" source elsif parent = enhance_with_matching_rule(source, level + 1) trace_rule level, "(#{task_name} => #{source} ... ENHANCE)" parent.name else trace_rule level, "(#{task_name} => #{source} ... FAIL)" return nil end } task = FileTask.define_task(task_name, { args => prereqs }, &block) task.sources = prereqs task end
[ "def", "attempt_rule", "(", "task_name", ",", "task_pattern", ",", "args", ",", "extensions", ",", "block", ",", "level", ")", "sources", "=", "make_sources", "(", "task_name", ",", "task_pattern", ",", "extensions", ")", "prereqs", "=", "sources", ".", "map", "{", "|", "source", "|", "trace_rule", "level", ",", "\"Attempting Rule #{task_name} => #{source}\"", "if", "File", ".", "exist?", "(", "source", ")", "||", "Rake", "::", "Task", ".", "task_defined?", "(", "source", ")", "trace_rule", "level", ",", "\"(#{task_name} => #{source} ... EXIST)\"", "source", "elsif", "parent", "=", "enhance_with_matching_rule", "(", "source", ",", "level", "+", "1", ")", "trace_rule", "level", ",", "\"(#{task_name} => #{source} ... ENHANCE)\"", "parent", ".", "name", "else", "trace_rule", "level", ",", "\"(#{task_name} => #{source} ... FAIL)\"", "return", "nil", "end", "}", "task", "=", "FileTask", ".", "define_task", "(", "task_name", ",", "{", "args", "=>", "prereqs", "}", ",", "&", "block", ")", "task", ".", "sources", "=", "prereqs", "task", "end" ]
Attempt to create a rule given the list of prerequisites.
[ "Attempt", "to", "create", "a", "rule", "given", "the", "list", "of", "prerequisites", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/task_manager.rb#L264-L282
train
ruby/rake
lib/rake/file_task.rb
Rake.FileTask.out_of_date?
def out_of_date?(stamp) all_prerequisite_tasks.any? { |prereq| prereq_task = application[prereq, @scope] if prereq_task.instance_of?(Rake::FileTask) prereq_task.timestamp > stamp || @application.options.build_all else prereq_task.timestamp > stamp end } end
ruby
def out_of_date?(stamp) all_prerequisite_tasks.any? { |prereq| prereq_task = application[prereq, @scope] if prereq_task.instance_of?(Rake::FileTask) prereq_task.timestamp > stamp || @application.options.build_all else prereq_task.timestamp > stamp end } end
[ "def", "out_of_date?", "(", "stamp", ")", "all_prerequisite_tasks", ".", "any?", "{", "|", "prereq", "|", "prereq_task", "=", "application", "[", "prereq", ",", "@scope", "]", "if", "prereq_task", ".", "instance_of?", "(", "Rake", "::", "FileTask", ")", "prereq_task", ".", "timestamp", ">", "stamp", "||", "@application", ".", "options", ".", "build_all", "else", "prereq_task", ".", "timestamp", ">", "stamp", "end", "}", "end" ]
Are there any prerequisites with a later time than the given time stamp?
[ "Are", "there", "any", "prerequisites", "with", "a", "later", "time", "than", "the", "given", "time", "stamp?" ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/file_task.rb#L32-L41
train
ruby/rake
lib/rake/task_arguments.rb
Rake.TaskArguments.new_scope
def new_scope(names) values = names.map { |n| self[n] } self.class.new(names, values + extras, self) end
ruby
def new_scope(names) values = names.map { |n| self[n] } self.class.new(names, values + extras, self) end
[ "def", "new_scope", "(", "names", ")", "values", "=", "names", ".", "map", "{", "|", "n", "|", "self", "[", "n", "]", "}", "self", ".", "class", ".", "new", "(", "names", ",", "values", "+", "extras", ",", "self", ")", "end" ]
Create a new argument scope using the prerequisite argument names.
[ "Create", "a", "new", "argument", "scope", "using", "the", "prerequisite", "argument", "names", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/task_arguments.rb#L38-L41
train
ruby/rake
lib/rake/promise.rb
Rake.Promise.work
def work stat :attempting_lock_on, item_id: object_id if @mutex.try_lock stat :has_lock_on, item_id: object_id chore stat :releasing_lock_on, item_id: object_id @mutex.unlock else stat :bailed_on, item_id: object_id end end
ruby
def work stat :attempting_lock_on, item_id: object_id if @mutex.try_lock stat :has_lock_on, item_id: object_id chore stat :releasing_lock_on, item_id: object_id @mutex.unlock else stat :bailed_on, item_id: object_id end end
[ "def", "work", "stat", ":attempting_lock_on", ",", "item_id", ":", "object_id", "if", "@mutex", ".", "try_lock", "stat", ":has_lock_on", ",", "item_id", ":", "object_id", "chore", "stat", ":releasing_lock_on", ",", "item_id", ":", "object_id", "@mutex", ".", "unlock", "else", "stat", ":bailed_on", ",", "item_id", ":", "object_id", "end", "end" ]
If no one else is working this promise, go ahead and do the chore.
[ "If", "no", "one", "else", "is", "working", "this", "promise", "go", "ahead", "and", "do", "the", "chore", "." ]
1c22b490ee6cb8bd614fa8d0d6145f671466206b
https://github.com/ruby/rake/blob/1c22b490ee6cb8bd614fa8d0d6145f671466206b/lib/rake/promise.rb#L42-L52
train
cloudfoundry/bosh
src/bosh_common/lib/common/retryable.rb
Bosh.Retryable.retryer
def retryer(&blk) loop do @try_count += 1 y = blk.call(@try_count, @retry_exception) @retry_exception = nil # no exception was raised in the block return y if y raise Bosh::Common::RetryCountExceeded if @try_count >= @retry_limit wait end rescue Exception => exception raise unless @matchers.any? { |m| m.matches?(exception) } raise unless exception.message =~ @matching raise if @try_count >= @retry_limit @retry_exception = exception wait retry ensure @ensure_callback.call(@try_count) end
ruby
def retryer(&blk) loop do @try_count += 1 y = blk.call(@try_count, @retry_exception) @retry_exception = nil # no exception was raised in the block return y if y raise Bosh::Common::RetryCountExceeded if @try_count >= @retry_limit wait end rescue Exception => exception raise unless @matchers.any? { |m| m.matches?(exception) } raise unless exception.message =~ @matching raise if @try_count >= @retry_limit @retry_exception = exception wait retry ensure @ensure_callback.call(@try_count) end
[ "def", "retryer", "(", "&", "blk", ")", "loop", "do", "@try_count", "+=", "1", "y", "=", "blk", ".", "call", "(", "@try_count", ",", "@retry_exception", ")", "@retry_exception", "=", "nil", "return", "y", "if", "y", "raise", "Bosh", "::", "Common", "::", "RetryCountExceeded", "if", "@try_count", ">=", "@retry_limit", "wait", "end", "rescue", "Exception", "=>", "exception", "raise", "unless", "@matchers", ".", "any?", "{", "|", "m", "|", "m", ".", "matches?", "(", "exception", ")", "}", "raise", "unless", "exception", ".", "message", "=~", "@matching", "raise", "if", "@try_count", ">=", "@retry_limit", "@retry_exception", "=", "exception", "wait", "retry", "ensure", "@ensure_callback", ".", "call", "(", "@try_count", ")", "end" ]
Loops until the block returns a true value
[ "Loops", "until", "the", "block", "returns", "a", "true", "value" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh_common/lib/common/retryable.rb#L25-L44
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/dns/power_dns_manager.rb
Bosh::Director.PowerDnsManager.flush_dns_cache
def flush_dns_cache if @flush_command && !@flush_command.empty? stdout, stderr, status = Open3.capture3(@flush_command) if status == 0 @logger.debug("Flushed #{stdout.chomp} records from DNS cache") else @logger.warn("Failed to flush DNS cache: #{stderr.chomp}") end end end
ruby
def flush_dns_cache if @flush_command && !@flush_command.empty? stdout, stderr, status = Open3.capture3(@flush_command) if status == 0 @logger.debug("Flushed #{stdout.chomp} records from DNS cache") else @logger.warn("Failed to flush DNS cache: #{stderr.chomp}") end end end
[ "def", "flush_dns_cache", "if", "@flush_command", "&&", "!", "@flush_command", ".", "empty?", "stdout", ",", "stderr", ",", "status", "=", "Open3", ".", "capture3", "(", "@flush_command", ")", "if", "status", "==", "0", "@logger", ".", "debug", "(", "\"Flushed #{stdout.chomp} records from DNS cache\"", ")", "else", "@logger", ".", "warn", "(", "\"Failed to flush DNS cache: #{stderr.chomp}\"", ")", "end", "end", "end" ]
Purge cached DNS records
[ "Purge", "cached", "DNS", "records" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/dns/power_dns_manager.rb#L101-L110
train
cloudfoundry/bosh
src/spec/support/director.rb
Bosh::Spec.Director.instance
def instance(instance_group_name, index_or_id, options = { deployment_name: Deployments::DEFAULT_DEPLOYMENT_NAME }) find_instance(instances(options), instance_group_name, index_or_id) end
ruby
def instance(instance_group_name, index_or_id, options = { deployment_name: Deployments::DEFAULT_DEPLOYMENT_NAME }) find_instance(instances(options), instance_group_name, index_or_id) end
[ "def", "instance", "(", "instance_group_name", ",", "index_or_id", ",", "options", "=", "{", "deployment_name", ":", "Deployments", "::", "DEFAULT_DEPLOYMENT_NAME", "}", ")", "find_instance", "(", "instances", "(", "options", ")", ",", "instance_group_name", ",", "index_or_id", ")", "end" ]
vm always returns a vm
[ "vm", "always", "returns", "a", "vm" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/spec/support/director.rb#L61-L63
train
cloudfoundry/bosh
src/bosh-monitor/lib/bosh/monitor/deployment.rb
Bosh::Monitor.Deployment.upsert_agent
def upsert_agent(instance) @logger.info("Adding agent #{instance.agent_id} (#{instance.job}/#{instance.id}) to #{name}...") agent_id = instance.agent_id if agent_id.nil? @logger.warn("No agent id for instance #{instance.job}/#{instance.id} in deployment #{name}") #count agents for instances with deleted vm, which expect to have vm if instance.expects_vm? && !instance.has_vm? agent = Agent.new("agent_with_no_vm", deployment: name) @instance_id_to_agent[instance.id] = agent agent.update_instance(instance) end return false end # Idle VMs, we don't care about them, but we still want to track them if instance.job.nil? @logger.debug("VM with no job found: #{agent_id}") end agent = @agent_id_to_agent[agent_id] if agent.nil? @logger.debug("Discovered agent #{agent_id}") agent = Agent.new(agent_id, deployment: name) @agent_id_to_agent[agent_id] = agent @instance_id_to_agent.delete(instance.id) if @instance_id_to_agent[instance.id] end agent.update_instance(instance) true end
ruby
def upsert_agent(instance) @logger.info("Adding agent #{instance.agent_id} (#{instance.job}/#{instance.id}) to #{name}...") agent_id = instance.agent_id if agent_id.nil? @logger.warn("No agent id for instance #{instance.job}/#{instance.id} in deployment #{name}") #count agents for instances with deleted vm, which expect to have vm if instance.expects_vm? && !instance.has_vm? agent = Agent.new("agent_with_no_vm", deployment: name) @instance_id_to_agent[instance.id] = agent agent.update_instance(instance) end return false end # Idle VMs, we don't care about them, but we still want to track them if instance.job.nil? @logger.debug("VM with no job found: #{agent_id}") end agent = @agent_id_to_agent[agent_id] if agent.nil? @logger.debug("Discovered agent #{agent_id}") agent = Agent.new(agent_id, deployment: name) @agent_id_to_agent[agent_id] = agent @instance_id_to_agent.delete(instance.id) if @instance_id_to_agent[instance.id] end agent.update_instance(instance) true end
[ "def", "upsert_agent", "(", "instance", ")", "@logger", ".", "info", "(", "\"Adding agent #{instance.agent_id} (#{instance.job}/#{instance.id}) to #{name}...\"", ")", "agent_id", "=", "instance", ".", "agent_id", "if", "agent_id", ".", "nil?", "@logger", ".", "warn", "(", "\"No agent id for instance #{instance.job}/#{instance.id} in deployment #{name}\"", ")", "if", "instance", ".", "expects_vm?", "&&", "!", "instance", ".", "has_vm?", "agent", "=", "Agent", ".", "new", "(", "\"agent_with_no_vm\"", ",", "deployment", ":", "name", ")", "@instance_id_to_agent", "[", "instance", ".", "id", "]", "=", "agent", "agent", ".", "update_instance", "(", "instance", ")", "end", "return", "false", "end", "if", "instance", ".", "job", ".", "nil?", "@logger", ".", "debug", "(", "\"VM with no job found: #{agent_id}\"", ")", "end", "agent", "=", "@agent_id_to_agent", "[", "agent_id", "]", "if", "agent", ".", "nil?", "@logger", ".", "debug", "(", "\"Discovered agent #{agent_id}\"", ")", "agent", "=", "Agent", ".", "new", "(", "agent_id", ",", "deployment", ":", "name", ")", "@agent_id_to_agent", "[", "agent_id", "]", "=", "agent", "@instance_id_to_agent", ".", "delete", "(", "instance", ".", "id", ")", "if", "@instance_id_to_agent", "[", "instance", ".", "id", "]", "end", "agent", ".", "update_instance", "(", "instance", ")", "true", "end" ]
Processes VM data from BOSH Director, extracts relevant agent data, wraps it into Agent object and adds it to a list of managed agents.
[ "Processes", "VM", "data", "from", "BOSH", "Director", "extracts", "relevant", "agent", "data", "wraps", "it", "into", "Agent", "object", "and", "adds", "it", "to", "a", "list", "of", "managed", "agents", "." ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-monitor/lib/bosh/monitor/deployment.rb#L63-L96
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/nats_rpc.rb
Bosh::Director.NatsRpc.subscribe_inbox
def subscribe_inbox # double-check locking to reduce synchronization if @subject_id.nil? # nats lazy-load needs to be outside the synchronized block client = nats @lock.synchronize do if @subject_id.nil? @subject_id = client.subscribe("#{@inbox_name}.>") do |message, _, subject| @handled_response = true handle_response(message, subject) end end end end end
ruby
def subscribe_inbox # double-check locking to reduce synchronization if @subject_id.nil? # nats lazy-load needs to be outside the synchronized block client = nats @lock.synchronize do if @subject_id.nil? @subject_id = client.subscribe("#{@inbox_name}.>") do |message, _, subject| @handled_response = true handle_response(message, subject) end end end end end
[ "def", "subscribe_inbox", "if", "@subject_id", ".", "nil?", "client", "=", "nats", "@lock", ".", "synchronize", "do", "if", "@subject_id", ".", "nil?", "@subject_id", "=", "client", ".", "subscribe", "(", "\"#{@inbox_name}.>\"", ")", "do", "|", "message", ",", "_", ",", "subject", "|", "@handled_response", "=", "true", "handle_response", "(", "message", ",", "subject", ")", "end", "end", "end", "end", "end" ]
subscribe to an inbox, if not already subscribed
[ "subscribe", "to", "an", "inbox", "if", "not", "already", "subscribed" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/nats_rpc.rb#L114-L128
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/compiled_package_requirement.rb
Bosh::Director.CompiledPackageRequirement.dependency_spec
def dependency_spec spec = {} @dependencies.each do |dependency| unless dependency.compiled? raise DirectorError, 'Cannot generate package dependency spec ' \ "for '#{@package.name}', " \ "'#{dependency.package.name}' hasn't been compiled yet" end compiled_package = dependency.compiled_package spec[compiled_package.name] = { 'name' => compiled_package.name, 'version' => "#{compiled_package.version}.#{compiled_package.build}", 'sha1' => compiled_package.sha1, 'blobstore_id' => compiled_package.blobstore_id, } end spec end
ruby
def dependency_spec spec = {} @dependencies.each do |dependency| unless dependency.compiled? raise DirectorError, 'Cannot generate package dependency spec ' \ "for '#{@package.name}', " \ "'#{dependency.package.name}' hasn't been compiled yet" end compiled_package = dependency.compiled_package spec[compiled_package.name] = { 'name' => compiled_package.name, 'version' => "#{compiled_package.version}.#{compiled_package.build}", 'sha1' => compiled_package.sha1, 'blobstore_id' => compiled_package.blobstore_id, } end spec end
[ "def", "dependency_spec", "spec", "=", "{", "}", "@dependencies", ".", "each", "do", "|", "dependency", "|", "unless", "dependency", ".", "compiled?", "raise", "DirectorError", ",", "'Cannot generate package dependency spec '", "\"for '#{@package.name}', \"", "\"'#{dependency.package.name}' hasn't been compiled yet\"", "end", "compiled_package", "=", "dependency", ".", "compiled_package", "spec", "[", "compiled_package", ".", "name", "]", "=", "{", "'name'", "=>", "compiled_package", ".", "name", ",", "'version'", "=>", "\"#{compiled_package.version}.#{compiled_package.build}\"", ",", "'sha1'", "=>", "compiled_package", ".", "sha1", ",", "'blobstore_id'", "=>", "compiled_package", ".", "blobstore_id", ",", "}", "end", "spec", "end" ]
This call only makes sense if all dependencies have already been compiled, otherwise it raises an exception @return [Hash] Hash representation of all package dependencies. Agent uses that to download package dependencies before compiling the package on a compilation VM.
[ "This", "call", "only", "makes", "sense", "if", "all", "dependencies", "have", "already", "been", "compiled", "otherwise", "it", "raises", "an", "exception" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/compiled_package_requirement.rb#L107-L129
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/agent_client.rb
Bosh::Director.AgentClient.format_exception
def format_exception(exception) return exception.to_s unless exception.is_a?(Hash) msg = exception['message'].to_s if exception['backtrace'] msg += "\n" msg += Array(exception['backtrace']).join("\n") end if exception['blobstore_id'] blob = download_and_delete_blob(exception['blobstore_id']) msg += "\n" msg += blob.to_s end msg end
ruby
def format_exception(exception) return exception.to_s unless exception.is_a?(Hash) msg = exception['message'].to_s if exception['backtrace'] msg += "\n" msg += Array(exception['backtrace']).join("\n") end if exception['blobstore_id'] blob = download_and_delete_blob(exception['blobstore_id']) msg += "\n" msg += blob.to_s end msg end
[ "def", "format_exception", "(", "exception", ")", "return", "exception", ".", "to_s", "unless", "exception", ".", "is_a?", "(", "Hash", ")", "msg", "=", "exception", "[", "'message'", "]", ".", "to_s", "if", "exception", "[", "'backtrace'", "]", "msg", "+=", "\"\\n\"", "msg", "+=", "Array", "(", "exception", "[", "'backtrace'", "]", ")", ".", "join", "(", "\"\\n\"", ")", "end", "if", "exception", "[", "'blobstore_id'", "]", "blob", "=", "download_and_delete_blob", "(", "exception", "[", "'blobstore_id'", "]", ")", "msg", "+=", "\"\\n\"", "msg", "+=", "blob", ".", "to_s", "end", "msg", "end" ]
Returns formatted exception information @param [Hash|#to_s] exception Serialized exception @return [String]
[ "Returns", "formatted", "exception", "information" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/agent_client.rb#L287-L304
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/agent_client.rb
Bosh::Director.AgentClient.inject_compile_log
def inject_compile_log(response) if response['value'] && response['value'].is_a?(Hash) && response['value']['result'].is_a?(Hash) && blob_id = response['value']['result']['compile_log_id'] compile_log = download_and_delete_blob(blob_id) response['value']['result']['compile_log'] = compile_log end end
ruby
def inject_compile_log(response) if response['value'] && response['value'].is_a?(Hash) && response['value']['result'].is_a?(Hash) && blob_id = response['value']['result']['compile_log_id'] compile_log = download_and_delete_blob(blob_id) response['value']['result']['compile_log'] = compile_log end end
[ "def", "inject_compile_log", "(", "response", ")", "if", "response", "[", "'value'", "]", "&&", "response", "[", "'value'", "]", ".", "is_a?", "(", "Hash", ")", "&&", "response", "[", "'value'", "]", "[", "'result'", "]", ".", "is_a?", "(", "Hash", ")", "&&", "blob_id", "=", "response", "[", "'value'", "]", "[", "'result'", "]", "[", "'compile_log_id'", "]", "compile_log", "=", "download_and_delete_blob", "(", "blob_id", ")", "response", "[", "'value'", "]", "[", "'result'", "]", "[", "'compile_log'", "]", "=", "compile_log", "end", "end" ]
the blob is removed from the blobstore once we have fetched it, but if there is a crash before it is injected into the response and then logged, there is a chance that we lose it
[ "the", "blob", "is", "removed", "from", "the", "blobstore", "once", "we", "have", "fetched", "it", "but", "if", "there", "is", "a", "crash", "before", "it", "is", "injected", "into", "the", "response", "and", "then", "logged", "there", "is", "a", "chance", "that", "we", "lose", "it" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/agent_client.rb#L311-L318
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/download_helper.rb
Bosh::Director.DownloadHelper.download_remote_file
def download_remote_file(resource, remote_file, local_file, num_redirects = 0) @logger.info("Downloading remote #{resource} from #{remote_file}") if @logger uri = URI.parse(remote_file) req = Net::HTTP::Get.new(uri) if uri.user && uri.password req.basic_auth uri.user, uri.password end Net::HTTP.start(uri.host, uri.port, :ENV, :use_ssl => uri.scheme == 'https') do |http| http.request req do |response| case response when Net::HTTPSuccess File.open(local_file, 'wb') do |file| response.read_body do |chunk| file.write(chunk) end end when Net::HTTPFound, Net::HTTPMovedPermanently raise ResourceError, "Too many redirects at '#{remote_file}'." if num_redirects >= 9 location = response.header['location'] raise ResourceError, "No location header for redirect found at '#{remote_file}'." if location.nil? location = URI.join(uri, location).to_s download_remote_file(resource, location, local_file, num_redirects + 1) when Net::HTTPNotFound @logger.error("Downloading remote #{resource} from #{remote_file} failed: #{response.message}") if @logger raise ResourceNotFound, "No #{resource} found at '#{remote_file}'." else @logger.error("Downloading remote #{resource} from #{remote_file} failed: #{response.message}") if @logger raise ResourceError, "Downloading remote #{resource} failed. Check task debug log for details." end end end rescue URI::Error, SocketError, ::Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e @logger.error("Downloading remote #{resource} from #{remote_file} failed: #{e.inspect}") if @logger raise ResourceError, "Downloading remote #{resource} failed. Check task debug log for details." end
ruby
def download_remote_file(resource, remote_file, local_file, num_redirects = 0) @logger.info("Downloading remote #{resource} from #{remote_file}") if @logger uri = URI.parse(remote_file) req = Net::HTTP::Get.new(uri) if uri.user && uri.password req.basic_auth uri.user, uri.password end Net::HTTP.start(uri.host, uri.port, :ENV, :use_ssl => uri.scheme == 'https') do |http| http.request req do |response| case response when Net::HTTPSuccess File.open(local_file, 'wb') do |file| response.read_body do |chunk| file.write(chunk) end end when Net::HTTPFound, Net::HTTPMovedPermanently raise ResourceError, "Too many redirects at '#{remote_file}'." if num_redirects >= 9 location = response.header['location'] raise ResourceError, "No location header for redirect found at '#{remote_file}'." if location.nil? location = URI.join(uri, location).to_s download_remote_file(resource, location, local_file, num_redirects + 1) when Net::HTTPNotFound @logger.error("Downloading remote #{resource} from #{remote_file} failed: #{response.message}") if @logger raise ResourceNotFound, "No #{resource} found at '#{remote_file}'." else @logger.error("Downloading remote #{resource} from #{remote_file} failed: #{response.message}") if @logger raise ResourceError, "Downloading remote #{resource} failed. Check task debug log for details." end end end rescue URI::Error, SocketError, ::Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e @logger.error("Downloading remote #{resource} from #{remote_file} failed: #{e.inspect}") if @logger raise ResourceError, "Downloading remote #{resource} failed. Check task debug log for details." end
[ "def", "download_remote_file", "(", "resource", ",", "remote_file", ",", "local_file", ",", "num_redirects", "=", "0", ")", "@logger", ".", "info", "(", "\"Downloading remote #{resource} from #{remote_file}\"", ")", "if", "@logger", "uri", "=", "URI", ".", "parse", "(", "remote_file", ")", "req", "=", "Net", "::", "HTTP", "::", "Get", ".", "new", "(", "uri", ")", "if", "uri", ".", "user", "&&", "uri", ".", "password", "req", ".", "basic_auth", "uri", ".", "user", ",", "uri", ".", "password", "end", "Net", "::", "HTTP", ".", "start", "(", "uri", ".", "host", ",", "uri", ".", "port", ",", ":ENV", ",", ":use_ssl", "=>", "uri", ".", "scheme", "==", "'https'", ")", "do", "|", "http", "|", "http", ".", "request", "req", "do", "|", "response", "|", "case", "response", "when", "Net", "::", "HTTPSuccess", "File", ".", "open", "(", "local_file", ",", "'wb'", ")", "do", "|", "file", "|", "response", ".", "read_body", "do", "|", "chunk", "|", "file", ".", "write", "(", "chunk", ")", "end", "end", "when", "Net", "::", "HTTPFound", ",", "Net", "::", "HTTPMovedPermanently", "raise", "ResourceError", ",", "\"Too many redirects at '#{remote_file}'.\"", "if", "num_redirects", ">=", "9", "location", "=", "response", ".", "header", "[", "'location'", "]", "raise", "ResourceError", ",", "\"No location header for redirect found at '#{remote_file}'.\"", "if", "location", ".", "nil?", "location", "=", "URI", ".", "join", "(", "uri", ",", "location", ")", ".", "to_s", "download_remote_file", "(", "resource", ",", "location", ",", "local_file", ",", "num_redirects", "+", "1", ")", "when", "Net", "::", "HTTPNotFound", "@logger", ".", "error", "(", "\"Downloading remote #{resource} from #{remote_file} failed: #{response.message}\"", ")", "if", "@logger", "raise", "ResourceNotFound", ",", "\"No #{resource} found at '#{remote_file}'.\"", "else", "@logger", ".", "error", "(", "\"Downloading remote #{resource} from #{remote_file} failed: #{response.message}\"", ")", "if", "@logger", "raise", "ResourceError", ",", "\"Downloading remote #{resource} failed. Check task debug log for details.\"", "end", "end", "end", "rescue", "URI", "::", "Error", ",", "SocketError", ",", "::", "Timeout", "::", "Error", ",", "Errno", "::", "EINVAL", ",", "Errno", "::", "ECONNRESET", ",", "Errno", "::", "ECONNREFUSED", ",", "EOFError", ",", "Net", "::", "HTTPBadResponse", ",", "Net", "::", "HTTPHeaderSyntaxError", ",", "Net", "::", "ProtocolError", "=>", "e", "@logger", ".", "error", "(", "\"Downloading remote #{resource} from #{remote_file} failed: #{e.inspect}\"", ")", "if", "@logger", "raise", "ResourceError", ",", "\"Downloading remote #{resource} failed. Check task debug log for details.\"", "end" ]
Downloads a remote file @param [String] resource Resource name to be logged @param [String] remote_file Remote file to download @param [String] local_file Local file to store the downloaded file @raise [Bosh::Director::ResourceNotFound] If remote file is not found @raise [Bosh::Director::ResourceError] If there's a network problem
[ "Downloads", "a", "remote", "file" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/download_helper.rb#L13-L55
train
cloudfoundry/bosh
src/bosh-registry/lib/bosh/registry/instance_manager.rb
Bosh::Registry.InstanceManager.update_settings
def update_settings(instance_id, settings) params = { :instance_id => instance_id } instance = Models::RegistryInstance[params] || Models::RegistryInstance.new(params) instance.settings = settings instance.save end
ruby
def update_settings(instance_id, settings) params = { :instance_id => instance_id } instance = Models::RegistryInstance[params] || Models::RegistryInstance.new(params) instance.settings = settings instance.save end
[ "def", "update_settings", "(", "instance_id", ",", "settings", ")", "params", "=", "{", ":instance_id", "=>", "instance_id", "}", "instance", "=", "Models", "::", "RegistryInstance", "[", "params", "]", "||", "Models", "::", "RegistryInstance", ".", "new", "(", "params", ")", "instance", ".", "settings", "=", "settings", "instance", ".", "save", "end" ]
Updates instance settings @param [String] instance_id instance id (instance record will be created in DB if it doesn't already exist) @param [String] settings New settings for the instance
[ "Updates", "instance", "settings" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-registry/lib/bosh/registry/instance_manager.rb#L10-L18
train
cloudfoundry/bosh
src/bosh-registry/lib/bosh/registry/instance_manager.rb
Bosh::Registry.InstanceManager.read_settings
def read_settings(instance_id, remote_ip = nil) check_instance_ips(remote_ip, instance_id) if remote_ip get_instance(instance_id).settings end
ruby
def read_settings(instance_id, remote_ip = nil) check_instance_ips(remote_ip, instance_id) if remote_ip get_instance(instance_id).settings end
[ "def", "read_settings", "(", "instance_id", ",", "remote_ip", "=", "nil", ")", "check_instance_ips", "(", "remote_ip", ",", "instance_id", ")", "if", "remote_ip", "get_instance", "(", "instance_id", ")", ".", "settings", "end" ]
Reads instance settings @param [String] instance_id instance id @param [optional, String] remote_ip If this IP is provided, check will be performed to see if it instance id actually has this IP address according to the IaaS.
[ "Reads", "instance", "settings" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-registry/lib/bosh/registry/instance_manager.rb#L26-L30
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/links/links_manager.rb
Bosh::Director::Links.LinksManager.find_or_create_provider_intent
def find_or_create_provider_intent(link_provider:, link_original_name:, link_type:) intent = Bosh::Director::Models::Links::LinkProviderIntent.find( link_provider: link_provider, original_name: link_original_name, ) if intent.nil? intent = Bosh::Director::Models::Links::LinkProviderIntent.create( link_provider: link_provider, original_name: link_original_name, type: link_type, ) else intent.type = link_type end intent.serial_id = @serial_id if intent.serial_id != @serial_id intent.save end
ruby
def find_or_create_provider_intent(link_provider:, link_original_name:, link_type:) intent = Bosh::Director::Models::Links::LinkProviderIntent.find( link_provider: link_provider, original_name: link_original_name, ) if intent.nil? intent = Bosh::Director::Models::Links::LinkProviderIntent.create( link_provider: link_provider, original_name: link_original_name, type: link_type, ) else intent.type = link_type end intent.serial_id = @serial_id if intent.serial_id != @serial_id intent.save end
[ "def", "find_or_create_provider_intent", "(", "link_provider", ":", ",", "link_original_name", ":", ",", "link_type", ":", ")", "intent", "=", "Bosh", "::", "Director", "::", "Models", "::", "Links", "::", "LinkProviderIntent", ".", "find", "(", "link_provider", ":", "link_provider", ",", "original_name", ":", "link_original_name", ",", ")", "if", "intent", ".", "nil?", "intent", "=", "Bosh", "::", "Director", "::", "Models", "::", "Links", "::", "LinkProviderIntent", ".", "create", "(", "link_provider", ":", "link_provider", ",", "original_name", ":", "link_original_name", ",", "type", ":", "link_type", ",", ")", "else", "intent", ".", "type", "=", "link_type", "end", "intent", ".", "serial_id", "=", "@serial_id", "if", "intent", ".", "serial_id", "!=", "@serial_id", "intent", ".", "save", "end" ]
Used by provider, not using alias because want to update existing provider intent when alias changes
[ "Used", "by", "provider", "not", "using", "alias", "because", "want", "to", "update", "existing", "provider", "intent", "when", "alias", "changes" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/links/links_manager.rb#L46-L64
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/links/links_manager.rb
Bosh::Director::Links.LinksManager.consumer?
def consumer?(provider_intent, deployment_plan) return true if provider_intent.shared link_consumers = deployment_plan.model.link_consumers link_consumers = link_consumers.select do |consumer| consumer.serial_id == @serial_id end link_consumers.any? do |consumer| consumer.intents.any? do |consumer_intent| can_be_consumed?(consumer, provider_intent, consumer_intent, @serial_id) end end end
ruby
def consumer?(provider_intent, deployment_plan) return true if provider_intent.shared link_consumers = deployment_plan.model.link_consumers link_consumers = link_consumers.select do |consumer| consumer.serial_id == @serial_id end link_consumers.any? do |consumer| consumer.intents.any? do |consumer_intent| can_be_consumed?(consumer, provider_intent, consumer_intent, @serial_id) end end end
[ "def", "consumer?", "(", "provider_intent", ",", "deployment_plan", ")", "return", "true", "if", "provider_intent", ".", "shared", "link_consumers", "=", "deployment_plan", ".", "model", ".", "link_consumers", "link_consumers", "=", "link_consumers", ".", "select", "do", "|", "consumer", "|", "consumer", ".", "serial_id", "==", "@serial_id", "end", "link_consumers", ".", "any?", "do", "|", "consumer", "|", "consumer", ".", "intents", ".", "any?", "do", "|", "consumer_intent", "|", "can_be_consumed?", "(", "consumer", ",", "provider_intent", ",", "consumer_intent", ",", "@serial_id", ")", "end", "end", "end" ]
A consumer which is within the same deployment
[ "A", "consumer", "which", "is", "within", "the", "same", "deployment" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/links/links_manager.rb#L466-L479
train
cloudfoundry/bosh
src/spec/support/wait.rb
Bosh::Spec.Waiter.wait
def wait(retries_left, &blk) blk.call rescue Exception => e retries_left -= 1 if retries_left > 0 sleep(0.5) retry else raise end end
ruby
def wait(retries_left, &blk) blk.call rescue Exception => e retries_left -= 1 if retries_left > 0 sleep(0.5) retry else raise end end
[ "def", "wait", "(", "retries_left", ",", "&", "blk", ")", "blk", ".", "call", "rescue", "Exception", "=>", "e", "retries_left", "-=", "1", "if", "retries_left", ">", "0", "sleep", "(", "0.5", ")", "retry", "else", "raise", "end", "end" ]
Do not add retries_left default value
[ "Do", "not", "add", "retries_left", "default", "value" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/spec/support/wait.rb#L8-L18
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/lock.rb
Bosh::Director.Lock.lock
def lock acquire @refresh_thread = Thread.new do renew_interval = [1.0, @expiration/2].max begin done_refreshing = false until @unlock || done_refreshing @refresh_mutex.synchronize do @refresh_signal.wait(@refresh_mutex, renew_interval) break if @unlock @logger.debug("Renewing lock: #@name") lock_expiration = Time.now.to_f + @expiration + 1 if Models::Lock.where(name: @name, uid: @uid).update(expired_at: Time.at(lock_expiration)) == 0 done_refreshing = true end end end ensure if !@unlock Models::Event.create( user: Config.current_job.username, action: 'lost', object_type: 'lock', object_name: @name, task: @task_id, deployment: @deployment_name, error: 'Lock renewal thread exiting', timestamp: Time.now, ) Models::Task[@task_id].update(state: 'cancelling') @logger.debug('Lock renewal thread exiting') end end end if block_given? begin yield ensure release end end end
ruby
def lock acquire @refresh_thread = Thread.new do renew_interval = [1.0, @expiration/2].max begin done_refreshing = false until @unlock || done_refreshing @refresh_mutex.synchronize do @refresh_signal.wait(@refresh_mutex, renew_interval) break if @unlock @logger.debug("Renewing lock: #@name") lock_expiration = Time.now.to_f + @expiration + 1 if Models::Lock.where(name: @name, uid: @uid).update(expired_at: Time.at(lock_expiration)) == 0 done_refreshing = true end end end ensure if !@unlock Models::Event.create( user: Config.current_job.username, action: 'lost', object_type: 'lock', object_name: @name, task: @task_id, deployment: @deployment_name, error: 'Lock renewal thread exiting', timestamp: Time.now, ) Models::Task[@task_id].update(state: 'cancelling') @logger.debug('Lock renewal thread exiting') end end end if block_given? begin yield ensure release end end end
[ "def", "lock", "acquire", "@refresh_thread", "=", "Thread", ".", "new", "do", "renew_interval", "=", "[", "1.0", ",", "@expiration", "/", "2", "]", ".", "max", "begin", "done_refreshing", "=", "false", "until", "@unlock", "||", "done_refreshing", "@refresh_mutex", ".", "synchronize", "do", "@refresh_signal", ".", "wait", "(", "@refresh_mutex", ",", "renew_interval", ")", "break", "if", "@unlock", "@logger", ".", "debug", "(", "\"Renewing lock: #@name\"", ")", "lock_expiration", "=", "Time", ".", "now", ".", "to_f", "+", "@expiration", "+", "1", "if", "Models", "::", "Lock", ".", "where", "(", "name", ":", "@name", ",", "uid", ":", "@uid", ")", ".", "update", "(", "expired_at", ":", "Time", ".", "at", "(", "lock_expiration", ")", ")", "==", "0", "done_refreshing", "=", "true", "end", "end", "end", "ensure", "if", "!", "@unlock", "Models", "::", "Event", ".", "create", "(", "user", ":", "Config", ".", "current_job", ".", "username", ",", "action", ":", "'lost'", ",", "object_type", ":", "'lock'", ",", "object_name", ":", "@name", ",", "task", ":", "@task_id", ",", "deployment", ":", "@deployment_name", ",", "error", ":", "'Lock renewal thread exiting'", ",", "timestamp", ":", "Time", ".", "now", ",", ")", "Models", "::", "Task", "[", "@task_id", "]", ".", "update", "(", "state", ":", "'cancelling'", ")", "@logger", ".", "debug", "(", "'Lock renewal thread exiting'", ")", "end", "end", "end", "if", "block_given?", "begin", "yield", "ensure", "release", "end", "end", "end" ]
Creates new lock with the given name. @param name lock name @option opts [Number] timeout how long to wait before giving up @option opts [Number] expiration how long to wait before expiring an old lock Acquire a lock. @yield [void] optional block to do work before automatically releasing the lock. @return [void]
[ "Creates", "new", "lock", "with", "the", "given", "name", "." ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/lock.rb#L36-L84
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/lock.rb
Bosh::Director.Lock.release
def release @refresh_mutex.synchronize { @unlock = true delete @refresh_signal.signal } @refresh_thread.join if @refresh_thread @event_manager.create_event( { user: Config.current_job.username, action: 'release', object_type: 'lock', object_name: @name, task: @task_id, deployment: @deployment_name, } ) end
ruby
def release @refresh_mutex.synchronize { @unlock = true delete @refresh_signal.signal } @refresh_thread.join if @refresh_thread @event_manager.create_event( { user: Config.current_job.username, action: 'release', object_type: 'lock', object_name: @name, task: @task_id, deployment: @deployment_name, } ) end
[ "def", "release", "@refresh_mutex", ".", "synchronize", "{", "@unlock", "=", "true", "delete", "@refresh_signal", ".", "signal", "}", "@refresh_thread", ".", "join", "if", "@refresh_thread", "@event_manager", ".", "create_event", "(", "{", "user", ":", "Config", ".", "current_job", ".", "username", ",", "action", ":", "'release'", ",", "object_type", ":", "'lock'", ",", "object_name", ":", "@name", ",", "task", ":", "@task_id", ",", "deployment", ":", "@deployment_name", ",", "}", ")", "end" ]
Release a lock that was not auto released by the lock method. @return [void]
[ "Release", "a", "lock", "that", "was", "not", "auto", "released", "by", "the", "lock", "method", "." ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/lock.rb#L89-L110
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/orphan_network_manager.rb
Bosh::Director.OrphanNetworkManager.list_orphan_networks
def list_orphan_networks Models::Network.where(orphaned: true).map do |network| { 'name' => network.name, 'type' => network.type, 'created_at' => network.created_at.to_s, 'orphaned_at' => network.orphaned_at.to_s, } end end
ruby
def list_orphan_networks Models::Network.where(orphaned: true).map do |network| { 'name' => network.name, 'type' => network.type, 'created_at' => network.created_at.to_s, 'orphaned_at' => network.orphaned_at.to_s, } end end
[ "def", "list_orphan_networks", "Models", "::", "Network", ".", "where", "(", "orphaned", ":", "true", ")", ".", "map", "do", "|", "network", "|", "{", "'name'", "=>", "network", ".", "name", ",", "'type'", "=>", "network", ".", "type", ",", "'created_at'", "=>", "network", ".", "created_at", ".", "to_s", ",", "'orphaned_at'", "=>", "network", ".", "orphaned_at", ".", "to_s", ",", "}", "end", "end" ]
returns a list of orphaned networks
[ "returns", "a", "list", "of", "orphaned", "networks" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/orphan_network_manager.rb#L38-L47
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/job_runner.rb
Bosh::Director.JobRunner.perform_job
def perform_job(*args) @task_logger.info('Creating job') job = @job_class.new(*args) Config.current_job = job job.task_id = @task_id job.task_checkpoint # cancelled in the queue? run_checkpointing @task_logger.info("Performing task: #{@task.inspect}") @task.timestamp = Time.now @task.started_at = Time.now @task.checkpoint_time = Time.now @task.save result = job.perform @task_logger.info('Done') finish_task(:done, result) rescue Bosh::Director::TaskCancelled => e log_exception(e) @task_logger.info("Task #{@task.id} cancelled") finish_task(:cancelled, 'task cancelled') rescue Exception => e log_exception(e) @task_logger.error("#{e}\n#{e.backtrace.join("\n")}") finish_task(:error, e) end
ruby
def perform_job(*args) @task_logger.info('Creating job') job = @job_class.new(*args) Config.current_job = job job.task_id = @task_id job.task_checkpoint # cancelled in the queue? run_checkpointing @task_logger.info("Performing task: #{@task.inspect}") @task.timestamp = Time.now @task.started_at = Time.now @task.checkpoint_time = Time.now @task.save result = job.perform @task_logger.info('Done') finish_task(:done, result) rescue Bosh::Director::TaskCancelled => e log_exception(e) @task_logger.info("Task #{@task.id} cancelled") finish_task(:cancelled, 'task cancelled') rescue Exception => e log_exception(e) @task_logger.error("#{e}\n#{e.backtrace.join("\n")}") finish_task(:error, e) end
[ "def", "perform_job", "(", "*", "args", ")", "@task_logger", ".", "info", "(", "'Creating job'", ")", "job", "=", "@job_class", ".", "new", "(", "*", "args", ")", "Config", ".", "current_job", "=", "job", "job", ".", "task_id", "=", "@task_id", "job", ".", "task_checkpoint", "run_checkpointing", "@task_logger", ".", "info", "(", "\"Performing task: #{@task.inspect}\"", ")", "@task", ".", "timestamp", "=", "Time", ".", "now", "@task", ".", "started_at", "=", "Time", ".", "now", "@task", ".", "checkpoint_time", "=", "Time", ".", "now", "@task", ".", "save", "result", "=", "job", ".", "perform", "@task_logger", ".", "info", "(", "'Done'", ")", "finish_task", "(", ":done", ",", "result", ")", "rescue", "Bosh", "::", "Director", "::", "TaskCancelled", "=>", "e", "log_exception", "(", "e", ")", "@task_logger", ".", "info", "(", "\"Task #{@task.id} cancelled\"", ")", "finish_task", "(", ":cancelled", ",", "'task cancelled'", ")", "rescue", "Exception", "=>", "e", "log_exception", "(", "e", ")", "@task_logger", ".", "error", "(", "\"#{e}\\n#{e.backtrace.join(\"\\n\")}\"", ")", "finish_task", "(", ":error", ",", "e", ")", "end" ]
Instantiates and performs director job. @param [Array] args Opaque list of job arguments that will be used to instantiate the new job object. @return [void]
[ "Instantiates", "and", "performs", "director", "job", "." ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/job_runner.rb#L81-L112
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/job_runner.rb
Bosh::Director.JobRunner.run_checkpointing
def run_checkpointing # task check pointer is scoped to separate class to avoid # the secondary thread and main thread modifying the same @task # variable (and accidentally clobbering it in the process) task_checkpointer = TaskCheckPointer.new(@task.id) Thread.new do with_thread_name("task:#{@task.id}-checkpoint") do while true sleep(Config.task_checkpoint_interval) task_checkpointer.checkpoint end end end end
ruby
def run_checkpointing # task check pointer is scoped to separate class to avoid # the secondary thread and main thread modifying the same @task # variable (and accidentally clobbering it in the process) task_checkpointer = TaskCheckPointer.new(@task.id) Thread.new do with_thread_name("task:#{@task.id}-checkpoint") do while true sleep(Config.task_checkpoint_interval) task_checkpointer.checkpoint end end end end
[ "def", "run_checkpointing", "task_checkpointer", "=", "TaskCheckPointer", ".", "new", "(", "@task", ".", "id", ")", "Thread", ".", "new", "do", "with_thread_name", "(", "\"task:#{@task.id}-checkpoint\"", ")", "do", "while", "true", "sleep", "(", "Config", ".", "task_checkpoint_interval", ")", "task_checkpointer", ".", "checkpoint", "end", "end", "end", "end" ]
Spawns a thread that periodically updates task checkpoint time. There is no need to kill this thread as job execution lifetime is the same as worker process lifetime. @return [Thread] Checkpoint thread
[ "Spawns", "a", "thread", "that", "periodically", "updates", "task", "checkpoint", "time", ".", "There", "is", "no", "need", "to", "kill", "this", "thread", "as", "job", "execution", "lifetime", "is", "the", "same", "as", "worker", "process", "lifetime", "." ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/job_runner.rb#L118-L131
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/job_runner.rb
Bosh::Director.JobRunner.truncate
def truncate(string, len = 128) stripped = string.strip[0..len] if stripped.length > len stripped.gsub(/\s+?(\S+)?$/, "") + "..." else stripped end end
ruby
def truncate(string, len = 128) stripped = string.strip[0..len] if stripped.length > len stripped.gsub(/\s+?(\S+)?$/, "") + "..." else stripped end end
[ "def", "truncate", "(", "string", ",", "len", "=", "128", ")", "stripped", "=", "string", ".", "strip", "[", "0", "..", "len", "]", "if", "stripped", ".", "length", ">", "len", "stripped", ".", "gsub", "(", "/", "\\s", "\\S", "/", ",", "\"\"", ")", "+", "\"...\"", "else", "stripped", "end", "end" ]
Truncates string to fit task result length @param [String] string The original string @param [Integer] len Desired string length @return [String] Truncated string
[ "Truncates", "string", "to", "fit", "task", "result", "length" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/job_runner.rb#L137-L144
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/job_runner.rb
Bosh::Director.JobRunner.finish_task
def finish_task(state, result) @task.refresh @task.state = state @task.result = truncate(result.to_s) @task.timestamp = Time.now @task.save end
ruby
def finish_task(state, result) @task.refresh @task.state = state @task.result = truncate(result.to_s) @task.timestamp = Time.now @task.save end
[ "def", "finish_task", "(", "state", ",", "result", ")", "@task", ".", "refresh", "@task", ".", "state", "=", "state", "@task", ".", "result", "=", "truncate", "(", "result", ".", "to_s", ")", "@task", ".", "timestamp", "=", "Time", ".", "now", "@task", ".", "save", "end" ]
Marks task completion @param [Symbol] state Task completion state @param [#to_s] result
[ "Marks", "task", "completion" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/job_runner.rb#L149-L155
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/job_runner.rb
Bosh::Director.JobRunner.log_exception
def log_exception(exception) # Event log is being used here to propagate the error. # It's up to event log renderer to find the error and # signal it properly. director_error = DirectorError.create_from_exception(exception) Config.event_log.log_error(director_error) end
ruby
def log_exception(exception) # Event log is being used here to propagate the error. # It's up to event log renderer to find the error and # signal it properly. director_error = DirectorError.create_from_exception(exception) Config.event_log.log_error(director_error) end
[ "def", "log_exception", "(", "exception", ")", "director_error", "=", "DirectorError", ".", "create_from_exception", "(", "exception", ")", "Config", ".", "event_log", ".", "log_error", "(", "director_error", ")", "end" ]
Logs the exception in the event log @param [Exception] exception
[ "Logs", "the", "exception", "in", "the", "event", "log" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/job_runner.rb#L159-L165
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/compiled_package_requirement_generator.rb
Bosh::Director.CompiledPackageRequirementGenerator.generate!
def generate!(requirements, instance_group, job, package, stemcell) # Our assumption here is that package dependency graph # has no cycles: this is being enforced on release upload. # Other than that it's a vanilla Depth-First Search (DFS). @logger.info("Checking whether package '#{package.desc}' needs to be compiled for stemcell '#{stemcell.desc}'") requirement_key = [package.id, "#{stemcell.os}/#{stemcell.version}"] requirement = requirements[requirement_key] if requirement # We already visited this and its dependencies requirement.add_instance_group(instance_group) # But we still need to register with this instance group return requirement end package_dependency_manager = PackageDependenciesManager.new(job.release.model) requirement = create_requirement(instance_group, job, package, stemcell, package_dependency_manager) @logger.info("Processing package '#{package.desc}' dependencies") dependencies = package_dependency_manager.dependencies(package) dependencies.each do |dependency| @logger.info("Package '#{package.desc}' depends on package '#{dependency.desc}'") dependency_requirement = generate!(requirements, instance_group, job, dependency, stemcell) requirement.add_dependency(dependency_requirement) end requirements[requirement_key] = requirement requirement end
ruby
def generate!(requirements, instance_group, job, package, stemcell) # Our assumption here is that package dependency graph # has no cycles: this is being enforced on release upload. # Other than that it's a vanilla Depth-First Search (DFS). @logger.info("Checking whether package '#{package.desc}' needs to be compiled for stemcell '#{stemcell.desc}'") requirement_key = [package.id, "#{stemcell.os}/#{stemcell.version}"] requirement = requirements[requirement_key] if requirement # We already visited this and its dependencies requirement.add_instance_group(instance_group) # But we still need to register with this instance group return requirement end package_dependency_manager = PackageDependenciesManager.new(job.release.model) requirement = create_requirement(instance_group, job, package, stemcell, package_dependency_manager) @logger.info("Processing package '#{package.desc}' dependencies") dependencies = package_dependency_manager.dependencies(package) dependencies.each do |dependency| @logger.info("Package '#{package.desc}' depends on package '#{dependency.desc}'") dependency_requirement = generate!(requirements, instance_group, job, dependency, stemcell) requirement.add_dependency(dependency_requirement) end requirements[requirement_key] = requirement requirement end
[ "def", "generate!", "(", "requirements", ",", "instance_group", ",", "job", ",", "package", ",", "stemcell", ")", "@logger", ".", "info", "(", "\"Checking whether package '#{package.desc}' needs to be compiled for stemcell '#{stemcell.desc}'\"", ")", "requirement_key", "=", "[", "package", ".", "id", ",", "\"#{stemcell.os}/#{stemcell.version}\"", "]", "requirement", "=", "requirements", "[", "requirement_key", "]", "if", "requirement", "requirement", ".", "add_instance_group", "(", "instance_group", ")", "return", "requirement", "end", "package_dependency_manager", "=", "PackageDependenciesManager", ".", "new", "(", "job", ".", "release", ".", "model", ")", "requirement", "=", "create_requirement", "(", "instance_group", ",", "job", ",", "package", ",", "stemcell", ",", "package_dependency_manager", ")", "@logger", ".", "info", "(", "\"Processing package '#{package.desc}' dependencies\"", ")", "dependencies", "=", "package_dependency_manager", ".", "dependencies", "(", "package", ")", "dependencies", ".", "each", "do", "|", "dependency", "|", "@logger", ".", "info", "(", "\"Package '#{package.desc}' depends on package '#{dependency.desc}'\"", ")", "dependency_requirement", "=", "generate!", "(", "requirements", ",", "instance_group", ",", "job", ",", "dependency", ",", "stemcell", ")", "requirement", ".", "add_dependency", "(", "dependency_requirement", ")", "end", "requirements", "[", "requirement_key", "]", "=", "requirement", "requirement", "end" ]
The rquirements hash passed in by the caller will be populated with CompiledPackageRequirement objects
[ "The", "rquirements", "hash", "passed", "in", "by", "the", "caller", "will", "be", "populated", "with", "CompiledPackageRequirement", "objects" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/compiled_package_requirement_generator.rb#L12-L40
train
cloudfoundry/bosh
src/bosh-director/lib/bosh/director/deployment_plan/assembler.rb
Bosh::Director.DeploymentPlan::Assembler.bind_jobs
def bind_jobs @deployment_plan.releases.each do |release| release.bind_jobs end @deployment_plan.instance_groups.each(&:validate_package_names_do_not_collide!) @deployment_plan.instance_groups.each(&:validate_exported_from_matches_stemcell!) end
ruby
def bind_jobs @deployment_plan.releases.each do |release| release.bind_jobs end @deployment_plan.instance_groups.each(&:validate_package_names_do_not_collide!) @deployment_plan.instance_groups.each(&:validate_exported_from_matches_stemcell!) end
[ "def", "bind_jobs", "@deployment_plan", ".", "releases", ".", "each", "do", "|", "release", "|", "release", ".", "bind_jobs", "end", "@deployment_plan", ".", "instance_groups", ".", "each", "(", "&", ":validate_package_names_do_not_collide!", ")", "@deployment_plan", ".", "instance_groups", ".", "each", "(", "&", ":validate_exported_from_matches_stemcell!", ")", "end" ]
Binds template models for each release spec in the deployment plan @return [void]
[ "Binds", "template", "models", "for", "each", "release", "spec", "in", "the", "deployment", "plan" ]
2eaa7100879ddd20cd909cd698514746195e28b7
https://github.com/cloudfoundry/bosh/blob/2eaa7100879ddd20cd909cd698514746195e28b7/src/bosh-director/lib/bosh/director/deployment_plan/assembler.rb#L190-L197
train
GeorgeKaraszi/ActiveRecordExtended
lib/active_record_extended/utilities.rb
ActiveRecordExtended.Utilities.double_quote
def double_quote(value) return if value.nil? case value.to_s # Ignore keys that contain double quotes or a Arel.star (*)[all columns] # or if a table has already been explicitly declared (ex: users.id) when "*", /((^".+"$)|(^[[:alpha:]]+\.[[:alnum:]]+))/ value else PG::Connection.quote_ident(value.to_s) end end
ruby
def double_quote(value) return if value.nil? case value.to_s # Ignore keys that contain double quotes or a Arel.star (*)[all columns] # or if a table has already been explicitly declared (ex: users.id) when "*", /((^".+"$)|(^[[:alpha:]]+\.[[:alnum:]]+))/ value else PG::Connection.quote_ident(value.to_s) end end
[ "def", "double_quote", "(", "value", ")", "return", "if", "value", ".", "nil?", "case", "value", ".", "to_s", "when", "\"*\"", ",", "/", "\\.", "/", "value", "else", "PG", "::", "Connection", ".", "quote_ident", "(", "value", ".", "to_s", ")", "end", "end" ]
Ensures the given value is properly double quoted. This also ensures we don't have conflicts with reversed keywords. IE: `user` is a reserved keyword in PG. But `"user"` is allowed and works the same when used as an column/tbl alias.
[ "Ensures", "the", "given", "value", "is", "properly", "double", "quoted", ".", "This", "also", "ensures", "we", "don", "t", "have", "conflicts", "with", "reversed", "keywords", "." ]
aca74eebb64b9957a2c8765bef6e43c7d5736fd8
https://github.com/GeorgeKaraszi/ActiveRecordExtended/blob/aca74eebb64b9957a2c8765bef6e43c7d5736fd8/lib/active_record_extended/utilities.rb#L108-L119
train
GeorgeKaraszi/ActiveRecordExtended
lib/active_record_extended/utilities.rb
ActiveRecordExtended.Utilities.literal_key
def literal_key(key) case key when TrueClass then "'t'" when FalseClass then "'f'" when Numeric then key else key = key.to_s key.start_with?("'") && key.end_with?("'") ? key : "'#{key}'" end end
ruby
def literal_key(key) case key when TrueClass then "'t'" when FalseClass then "'f'" when Numeric then key else key = key.to_s key.start_with?("'") && key.end_with?("'") ? key : "'#{key}'" end end
[ "def", "literal_key", "(", "key", ")", "case", "key", "when", "TrueClass", "then", "\"'t'\"", "when", "FalseClass", "then", "\"'f'\"", "when", "Numeric", "then", "key", "else", "key", "=", "key", ".", "to_s", "key", ".", "start_with?", "(", "\"'\"", ")", "&&", "key", ".", "end_with?", "(", "\"'\"", ")", "?", "key", ":", "\"'#{key}'\"", "end", "end" ]
Ensures the key is properly single quoted and treated as a actual PG key reference.
[ "Ensures", "the", "key", "is", "properly", "single", "quoted", "and", "treated", "as", "a", "actual", "PG", "key", "reference", "." ]
aca74eebb64b9957a2c8765bef6e43c7d5736fd8
https://github.com/GeorgeKaraszi/ActiveRecordExtended/blob/aca74eebb64b9957a2c8765bef6e43c7d5736fd8/lib/active_record_extended/utilities.rb#L122-L131
train
GeorgeKaraszi/ActiveRecordExtended
lib/active_record_extended/utilities.rb
ActiveRecordExtended.Utilities.to_arel_sql
def to_arel_sql(value) case value when Arel::Node, Arel::Nodes::SqlLiteral, nil value when ActiveRecord::Relation Arel.sql(value.spawn.to_sql) else Arel.sql(value.respond_to?(:to_sql) ? value.to_sql : value.to_s) end end
ruby
def to_arel_sql(value) case value when Arel::Node, Arel::Nodes::SqlLiteral, nil value when ActiveRecord::Relation Arel.sql(value.spawn.to_sql) else Arel.sql(value.respond_to?(:to_sql) ? value.to_sql : value.to_s) end end
[ "def", "to_arel_sql", "(", "value", ")", "case", "value", "when", "Arel", "::", "Node", ",", "Arel", "::", "Nodes", "::", "SqlLiteral", ",", "nil", "value", "when", "ActiveRecord", "::", "Relation", "Arel", ".", "sql", "(", "value", ".", "spawn", ".", "to_sql", ")", "else", "Arel", ".", "sql", "(", "value", ".", "respond_to?", "(", ":to_sql", ")", "?", "value", ".", "to_sql", ":", "value", ".", "to_s", ")", "end", "end" ]
Converts a potential subquery into a compatible Arel SQL node. Note: We convert relations to SQL to maintain compatibility with Rails 5.[0/1]. Only Rails 5.2+ maintains bound attributes in Arel, so its better to be safe then sorry. When we drop support for Rails 5.[0/1], we then can then drop the '.to_sql' conversation
[ "Converts", "a", "potential", "subquery", "into", "a", "compatible", "Arel", "SQL", "node", "." ]
aca74eebb64b9957a2c8765bef6e43c7d5736fd8
https://github.com/GeorgeKaraszi/ActiveRecordExtended/blob/aca74eebb64b9957a2c8765bef6e43c7d5736fd8/lib/active_record_extended/utilities.rb#L140-L149
train
GeorgeKaraszi/ActiveRecordExtended
lib/active_record_extended/query_methods/where_chain.rb
ActiveRecordExtended.WhereChain.contains
def contains(opts, *rest) build_where_chain(opts, rest) do |arel| case arel when Arel::Nodes::In, Arel::Nodes::Equality column = left_column(arel) || column_from_association(arel) if [:hstore, :jsonb].include?(column.type) Arel::Nodes::ContainsHStore.new(arel.left, arel.right) elsif column.try(:array) Arel::Nodes::ContainsArray.new(arel.left, arel.right) else raise ArgumentError, "Invalid argument for .where.contains(), got #{arel.class}" end else raise ArgumentError, "Invalid argument for .where.contains(), got #{arel.class}" end end end
ruby
def contains(opts, *rest) build_where_chain(opts, rest) do |arel| case arel when Arel::Nodes::In, Arel::Nodes::Equality column = left_column(arel) || column_from_association(arel) if [:hstore, :jsonb].include?(column.type) Arel::Nodes::ContainsHStore.new(arel.left, arel.right) elsif column.try(:array) Arel::Nodes::ContainsArray.new(arel.left, arel.right) else raise ArgumentError, "Invalid argument for .where.contains(), got #{arel.class}" end else raise ArgumentError, "Invalid argument for .where.contains(), got #{arel.class}" end end end
[ "def", "contains", "(", "opts", ",", "*", "rest", ")", "build_where_chain", "(", "opts", ",", "rest", ")", "do", "|", "arel", "|", "case", "arel", "when", "Arel", "::", "Nodes", "::", "In", ",", "Arel", "::", "Nodes", "::", "Equality", "column", "=", "left_column", "(", "arel", ")", "||", "column_from_association", "(", "arel", ")", "if", "[", ":hstore", ",", ":jsonb", "]", ".", "include?", "(", "column", ".", "type", ")", "Arel", "::", "Nodes", "::", "ContainsHStore", ".", "new", "(", "arel", ".", "left", ",", "arel", ".", "right", ")", "elsif", "column", ".", "try", "(", ":array", ")", "Arel", "::", "Nodes", "::", "ContainsArray", ".", "new", "(", "arel", ".", "left", ",", "arel", ".", "right", ")", "else", "raise", "ArgumentError", ",", "\"Invalid argument for .where.contains(), got #{arel.class}\"", "end", "else", "raise", "ArgumentError", ",", "\"Invalid argument for .where.contains(), got #{arel.class}\"", "end", "end", "end" ]
Finds Records that contains a nested set elements Array Column Type: User.where.contains(tags: [1, 3]) # SELECT user.* FROM user WHERE user.tags @> {1,3} HStore Column Type: User.where.contains(data: { nickname: 'chainer' }) # SELECT user.* FROM user WHERE user.data @> 'nickname' => 'chainer' JSONB Column Type: User.where.contains(data: { nickname: 'chainer' }) # SELECT user.* FROM user WHERE user.data @> {'nickname': 'chainer'} This can also be used along side joined tables JSONB Column Type Example: Tag.joins(:user).where.contains(user: { data: { nickname: 'chainer' } }) # SELECT tags.* FROM tags INNER JOIN user on user.id = tags.user_id WHERE user.data @> { nickname: 'chainer' }
[ "Finds", "Records", "that", "contains", "a", "nested", "set", "elements" ]
aca74eebb64b9957a2c8765bef6e43c7d5736fd8
https://github.com/GeorgeKaraszi/ActiveRecordExtended/blob/aca74eebb64b9957a2c8765bef6e43c7d5736fd8/lib/active_record_extended/query_methods/where_chain.rb#L46-L63
train
jekyll/jekyll-admin
lib/jekyll-admin/urlable.rb
JekyllAdmin.URLable.api_url
def api_url @api_url ||= Addressable::URI.new( :scheme => scheme, :host => host, :port => port, :path => path_with_base("/_api", resource_path) ).normalize.to_s end
ruby
def api_url @api_url ||= Addressable::URI.new( :scheme => scheme, :host => host, :port => port, :path => path_with_base("/_api", resource_path) ).normalize.to_s end
[ "def", "api_url", "@api_url", "||=", "Addressable", "::", "URI", ".", "new", "(", ":scheme", "=>", "scheme", ",", ":host", "=>", "host", ",", ":port", "=>", "port", ",", ":path", "=>", "path_with_base", "(", "\"/_api\"", ",", "resource_path", ")", ")", ".", "normalize", ".", "to_s", "end" ]
Absolute URL to the API representation of this resource
[ "Absolute", "URL", "to", "the", "API", "representation", "of", "this", "resource" ]
bc053b3b93faba679e8666091c42c47970e3bb5e
https://github.com/jekyll/jekyll-admin/blob/bc053b3b93faba679e8666091c42c47970e3bb5e/lib/jekyll-admin/urlable.rb#L17-L22
train
jekyll/jekyll-admin
lib/jekyll-admin/apiable.rb
JekyllAdmin.APIable.to_api
def to_api(include_content: false) output = hash_for_api output = output.merge(url_fields) # Include content, if requested, otherwise remove it if include_content output = output.merge(content_fields) else CONTENT_FIELDS.each { |field| output.delete(field) } end # Documents have duplicate output and content fields, Pages do not # Since it's an API, use `content` in both for consistency output.delete("output") # By default, calling to_liquid on a collection will return a docs # array with each rendered document, which we don't want. if is_a?(Jekyll::Collection) output.delete("docs") output["entries_url"] = entries_url end if is_a?(Jekyll::Document) output["relative_path"] = relative_path.sub("_drafts/", "") if draft? output["name"] = basename end if is_a?(Jekyll::StaticFile) output["from_theme"] = from_theme_gem? end output end
ruby
def to_api(include_content: false) output = hash_for_api output = output.merge(url_fields) # Include content, if requested, otherwise remove it if include_content output = output.merge(content_fields) else CONTENT_FIELDS.each { |field| output.delete(field) } end # Documents have duplicate output and content fields, Pages do not # Since it's an API, use `content` in both for consistency output.delete("output") # By default, calling to_liquid on a collection will return a docs # array with each rendered document, which we don't want. if is_a?(Jekyll::Collection) output.delete("docs") output["entries_url"] = entries_url end if is_a?(Jekyll::Document) output["relative_path"] = relative_path.sub("_drafts/", "") if draft? output["name"] = basename end if is_a?(Jekyll::StaticFile) output["from_theme"] = from_theme_gem? end output end
[ "def", "to_api", "(", "include_content", ":", "false", ")", "output", "=", "hash_for_api", "output", "=", "output", ".", "merge", "(", "url_fields", ")", "if", "include_content", "output", "=", "output", ".", "merge", "(", "content_fields", ")", "else", "CONTENT_FIELDS", ".", "each", "{", "|", "field", "|", "output", ".", "delete", "(", "field", ")", "}", "end", "output", ".", "delete", "(", "\"output\"", ")", "if", "is_a?", "(", "Jekyll", "::", "Collection", ")", "output", ".", "delete", "(", "\"docs\"", ")", "output", "[", "\"entries_url\"", "]", "=", "entries_url", "end", "if", "is_a?", "(", "Jekyll", "::", "Document", ")", "output", "[", "\"relative_path\"", "]", "=", "relative_path", ".", "sub", "(", "\"_drafts/\"", ",", "\"\"", ")", "if", "draft?", "output", "[", "\"name\"", "]", "=", "basename", "end", "if", "is_a?", "(", "Jekyll", "::", "StaticFile", ")", "output", "[", "\"from_theme\"", "]", "=", "from_theme_gem?", "end", "output", "end" ]
Returns a hash suitable for use as an API response. For Documents and Pages: 1. Adds the file's raw content to the `raw_content` field 2. Adds the file's raw YAML front matter to the `front_matter` field For Static Files it addes the Base64 `encoded_content` field Options: include_content - if true, includes the content in the respond, false by default to support mapping on indexes where we only want metadata Returns a hash (which can then be to_json'd)
[ "Returns", "a", "hash", "suitable", "for", "use", "as", "an", "API", "response", "." ]
bc053b3b93faba679e8666091c42c47970e3bb5e
https://github.com/jekyll/jekyll-admin/blob/bc053b3b93faba679e8666091c42c47970e3bb5e/lib/jekyll-admin/apiable.rb#L24-L56
train
jekyll/jekyll-admin
lib/jekyll-admin/apiable.rb
JekyllAdmin.APIable.content_fields
def content_fields output = {} # Include file content-related fields if is_a?(Jekyll::StaticFile) output["encoded_content"] = encoded_content elsif is_a?(JekyllAdmin::DataFile) output["content"] = content output["raw_content"] = raw_content else output["raw_content"] = raw_content output["front_matter"] = front_matter end # Include next and previous documents non-recursively if is_a?(Jekyll::Document) %w(next previous).each do |direction| method = "#{direction}_doc".to_sym doc = public_send(method) output[direction] = doc.to_api if doc end end output end
ruby
def content_fields output = {} # Include file content-related fields if is_a?(Jekyll::StaticFile) output["encoded_content"] = encoded_content elsif is_a?(JekyllAdmin::DataFile) output["content"] = content output["raw_content"] = raw_content else output["raw_content"] = raw_content output["front_matter"] = front_matter end # Include next and previous documents non-recursively if is_a?(Jekyll::Document) %w(next previous).each do |direction| method = "#{direction}_doc".to_sym doc = public_send(method) output[direction] = doc.to_api if doc end end output end
[ "def", "content_fields", "output", "=", "{", "}", "if", "is_a?", "(", "Jekyll", "::", "StaticFile", ")", "output", "[", "\"encoded_content\"", "]", "=", "encoded_content", "elsif", "is_a?", "(", "JekyllAdmin", "::", "DataFile", ")", "output", "[", "\"content\"", "]", "=", "content", "output", "[", "\"raw_content\"", "]", "=", "raw_content", "else", "output", "[", "\"raw_content\"", "]", "=", "raw_content", "output", "[", "\"front_matter\"", "]", "=", "front_matter", "end", "if", "is_a?", "(", "Jekyll", "::", "Document", ")", "%w(", "next", "previous", ")", ".", "each", "do", "|", "direction", "|", "method", "=", "\"#{direction}_doc\"", ".", "to_sym", "doc", "=", "public_send", "(", "method", ")", "output", "[", "direction", "]", "=", "doc", ".", "to_api", "if", "doc", "end", "end", "output", "end" ]
Returns a hash of content fields for inclusion in the API output
[ "Returns", "a", "hash", "of", "content", "fields", "for", "inclusion", "in", "the", "API", "output" ]
bc053b3b93faba679e8666091c42c47970e3bb5e
https://github.com/jekyll/jekyll-admin/blob/bc053b3b93faba679e8666091c42c47970e3bb5e/lib/jekyll-admin/apiable.rb#L128-L152
train
jekyll/jekyll-admin
lib/jekyll-admin/path_helper.rb
JekyllAdmin.PathHelper.directory_path
def directory_path sanitized_path( case namespace when "collections" File.join(collection.relative_directory, params["splat"].first) when "data" File.join(DataFile.data_dir, params["splat"].first) when "drafts" File.join("_drafts", params["splat"].first) else params["splat"].first end ) end
ruby
def directory_path sanitized_path( case namespace when "collections" File.join(collection.relative_directory, params["splat"].first) when "data" File.join(DataFile.data_dir, params["splat"].first) when "drafts" File.join("_drafts", params["splat"].first) else params["splat"].first end ) end
[ "def", "directory_path", "sanitized_path", "(", "case", "namespace", "when", "\"collections\"", "File", ".", "join", "(", "collection", ".", "relative_directory", ",", "params", "[", "\"splat\"", "]", ".", "first", ")", "when", "\"data\"", "File", ".", "join", "(", "DataFile", ".", "data_dir", ",", "params", "[", "\"splat\"", "]", ".", "first", ")", "when", "\"drafts\"", "File", ".", "join", "(", "\"_drafts\"", ",", "params", "[", "\"splat\"", "]", ".", "first", ")", "else", "params", "[", "\"splat\"", "]", ".", "first", "end", ")", "end" ]
Returns the path to the requested file's containing directory
[ "Returns", "the", "path", "to", "the", "requested", "file", "s", "containing", "directory" ]
bc053b3b93faba679e8666091c42c47970e3bb5e
https://github.com/jekyll/jekyll-admin/blob/bc053b3b93faba679e8666091c42c47970e3bb5e/lib/jekyll-admin/path_helper.rb#L54-L67
train
jekyll/jekyll-admin
lib/jekyll-admin/file_helper.rb
JekyllAdmin.FileHelper.write_file
def write_file(path, content) Jekyll.logger.debug "WRITING:", path path = sanitized_path(path) FileUtils.mkdir_p File.dirname(path) File.open(path, "wb") do |file| file.write(content) end # we should fully process in dev mode for tests to pass if ENV["RACK_ENV"] == "production" site.read else site.process end end
ruby
def write_file(path, content) Jekyll.logger.debug "WRITING:", path path = sanitized_path(path) FileUtils.mkdir_p File.dirname(path) File.open(path, "wb") do |file| file.write(content) end # we should fully process in dev mode for tests to pass if ENV["RACK_ENV"] == "production" site.read else site.process end end
[ "def", "write_file", "(", "path", ",", "content", ")", "Jekyll", ".", "logger", ".", "debug", "\"WRITING:\"", ",", "path", "path", "=", "sanitized_path", "(", "path", ")", "FileUtils", ".", "mkdir_p", "File", ".", "dirname", "(", "path", ")", "File", ".", "open", "(", "path", ",", "\"wb\"", ")", "do", "|", "file", "|", "file", ".", "write", "(", "content", ")", "end", "if", "ENV", "[", "\"RACK_ENV\"", "]", "==", "\"production\"", "site", ".", "read", "else", "site", ".", "process", "end", "end" ]
Write a file to disk with the given content
[ "Write", "a", "file", "to", "disk", "with", "the", "given", "content" ]
bc053b3b93faba679e8666091c42c47970e3bb5e
https://github.com/jekyll/jekyll-admin/blob/bc053b3b93faba679e8666091c42c47970e3bb5e/lib/jekyll-admin/file_helper.rb#L17-L30
train
jekyll/jekyll-admin
lib/jekyll-admin/file_helper.rb
JekyllAdmin.FileHelper.delete_file
def delete_file(path) Jekyll.logger.debug "DELETING:", path FileUtils.rm_f sanitized_path(path) site.process end
ruby
def delete_file(path) Jekyll.logger.debug "DELETING:", path FileUtils.rm_f sanitized_path(path) site.process end
[ "def", "delete_file", "(", "path", ")", "Jekyll", ".", "logger", ".", "debug", "\"DELETING:\"", ",", "path", "FileUtils", ".", "rm_f", "sanitized_path", "(", "path", ")", "site", ".", "process", "end" ]
Delete the file at the given path
[ "Delete", "the", "file", "at", "the", "given", "path" ]
bc053b3b93faba679e8666091c42c47970e3bb5e
https://github.com/jekyll/jekyll-admin/blob/bc053b3b93faba679e8666091c42c47970e3bb5e/lib/jekyll-admin/file_helper.rb#L33-L37
train
jekyll/jekyll-admin
lib/jekyll-admin/server.rb
JekyllAdmin.Server.restored_front_matter
def restored_front_matter front_matter.map do |key, value| value = "null" if value.nil? [key, value] end.to_h end
ruby
def restored_front_matter front_matter.map do |key, value| value = "null" if value.nil? [key, value] end.to_h end
[ "def", "restored_front_matter", "front_matter", ".", "map", "do", "|", "key", ",", "value", "|", "value", "=", "\"null\"", "if", "value", ".", "nil?", "[", "key", ",", "value", "]", "end", ".", "to_h", "end" ]
verbose 'null' values in front matter
[ "verbose", "null", "values", "in", "front", "matter" ]
bc053b3b93faba679e8666091c42c47970e3bb5e
https://github.com/jekyll/jekyll-admin/blob/bc053b3b93faba679e8666091c42c47970e3bb5e/lib/jekyll-admin/server.rb#L87-L92
train
sds/scss-lint
lib/scss_lint/linter/single_line_per_property.rb
SCSSLint.Linter::SingleLinePerProperty.single_line_rule_set?
def single_line_rule_set?(rule) rule.children.all? { |child| child.line == rule.source_range.end_pos.line } end
ruby
def single_line_rule_set?(rule) rule.children.all? { |child| child.line == rule.source_range.end_pos.line } end
[ "def", "single_line_rule_set?", "(", "rule", ")", "rule", ".", "children", ".", "all?", "{", "|", "child", "|", "child", ".", "line", "==", "rule", ".", "source_range", ".", "end_pos", ".", "line", "}", "end" ]
Return whether this rule set occupies a single line. Note that this allows: a, b, i { margin: 0; padding: 0; } and: p { margin: 0; padding: 0; } In other words, the line of the opening curly brace is the line that the rule set is considered to occupy.
[ "Return", "whether", "this", "rule", "set", "occupies", "a", "single", "line", "." ]
e99afe4ede041a431a06e585c12ce82f6ad50116
https://github.com/sds/scss-lint/blob/e99afe4ede041a431a06e585c12ce82f6ad50116/lib/scss_lint/linter/single_line_per_property.rb#L41-L43
train
sds/scss-lint
lib/scss_lint/linter/single_line_per_property.rb
SCSSLint.Linter::SingleLinePerProperty.check_adjacent_properties
def check_adjacent_properties(properties) properties[0..-2].zip(properties[1..-1]).each do |first, second| next unless first.line == second.line add_lint(second, "Property '#{second.name.join}' should be placed on own line") end end
ruby
def check_adjacent_properties(properties) properties[0..-2].zip(properties[1..-1]).each do |first, second| next unless first.line == second.line add_lint(second, "Property '#{second.name.join}' should be placed on own line") end end
[ "def", "check_adjacent_properties", "(", "properties", ")", "properties", "[", "0", "..", "-", "2", "]", ".", "zip", "(", "properties", "[", "1", "..", "-", "1", "]", ")", ".", "each", "do", "|", "first", ",", "second", "|", "next", "unless", "first", ".", "line", "==", "second", ".", "line", "add_lint", "(", "second", ",", "\"Property '#{second.name.join}' should be placed on own line\"", ")", "end", "end" ]
Compare each property against the next property to see if they are on the same line. @param properties [Array<Sass::Tree::PropNode>]
[ "Compare", "each", "property", "against", "the", "next", "property", "to", "see", "if", "they", "are", "on", "the", "same", "line", "." ]
e99afe4ede041a431a06e585c12ce82f6ad50116
https://github.com/sds/scss-lint/blob/e99afe4ede041a431a06e585c12ce82f6ad50116/lib/scss_lint/linter/single_line_per_property.rb#L53-L59
train
sds/scss-lint
lib/scss_lint/control_comment_processor.rb
SCSSLint.ControlCommentProcessor.before_node_visit
def before_node_visit(node) return unless (commands = Array(extract_commands(node))).any? commands.each do |command| linters = command[:linters] next unless linters.include?('all') || linters.include?(@linter.name) process_command(command, node) # Is the control comment the only thing on this line? next if node.is_a?(Sass::Tree::RuleNode) || %r{^\s*(//|/\*)}.match(@linter.engine.lines[command[:line] - 1]) # Otherwise, pop since we only want comment to apply to the single line pop_control_comment_stack(node) end end
ruby
def before_node_visit(node) return unless (commands = Array(extract_commands(node))).any? commands.each do |command| linters = command[:linters] next unless linters.include?('all') || linters.include?(@linter.name) process_command(command, node) # Is the control comment the only thing on this line? next if node.is_a?(Sass::Tree::RuleNode) || %r{^\s*(//|/\*)}.match(@linter.engine.lines[command[:line] - 1]) # Otherwise, pop since we only want comment to apply to the single line pop_control_comment_stack(node) end end
[ "def", "before_node_visit", "(", "node", ")", "return", "unless", "(", "commands", "=", "Array", "(", "extract_commands", "(", "node", ")", ")", ")", ".", "any?", "commands", ".", "each", "do", "|", "command", "|", "linters", "=", "command", "[", ":linters", "]", "next", "unless", "linters", ".", "include?", "(", "'all'", ")", "||", "linters", ".", "include?", "(", "@linter", ".", "name", ")", "process_command", "(", "command", ",", "node", ")", "next", "if", "node", ".", "is_a?", "(", "Sass", "::", "Tree", "::", "RuleNode", ")", "||", "%r{", "\\s", "\\*", "}", ".", "match", "(", "@linter", ".", "engine", ".", "lines", "[", "command", "[", ":line", "]", "-", "1", "]", ")", "pop_control_comment_stack", "(", "node", ")", "end", "end" ]
Executed before a node has been visited. @param node [Sass::Tree::Node]
[ "Executed", "before", "a", "node", "has", "been", "visited", "." ]
e99afe4ede041a431a06e585c12ce82f6ad50116
https://github.com/sds/scss-lint/blob/e99afe4ede041a431a06e585c12ce82f6ad50116/lib/scss_lint/control_comment_processor.rb#L22-L38
train
sds/scss-lint
lib/scss_lint/control_comment_processor.rb
SCSSLint.ControlCommentProcessor.last_child
def last_child(node) last = node.children.inject(node) do |lowest, child| return lowest unless child.respond_to?(:line) lowest.line < child.line ? child : lowest end # In this case, none of the children have associated line numbers or the # node has no children at all, so return `nil`. return if last == node last end
ruby
def last_child(node) last = node.children.inject(node) do |lowest, child| return lowest unless child.respond_to?(:line) lowest.line < child.line ? child : lowest end # In this case, none of the children have associated line numbers or the # node has no children at all, so return `nil`. return if last == node last end
[ "def", "last_child", "(", "node", ")", "last", "=", "node", ".", "children", ".", "inject", "(", "node", ")", "do", "|", "lowest", ",", "child", "|", "return", "lowest", "unless", "child", ".", "respond_to?", "(", ":line", ")", "lowest", ".", "line", "<", "child", ".", "line", "?", "child", ":", "lowest", "end", "return", "if", "last", "==", "node", "last", "end" ]
Gets the child of the node that resides on the lowest line in the file. This is necessary due to the fact that our monkey patching of the parse tree's {#children} method does not return nodes sorted by their line number. Returns `nil` if node has no children or no children with associated line numbers. @param node [Sass::Tree::Node, Sass::Script::Tree::Node] @return [Sass::Tree::Node, Sass::Script::Tree::Node]
[ "Gets", "the", "child", "of", "the", "node", "that", "resides", "on", "the", "lowest", "line", "in", "the", "file", "." ]
e99afe4ede041a431a06e585c12ce82f6ad50116
https://github.com/sds/scss-lint/blob/e99afe4ede041a431a06e585c12ce82f6ad50116/lib/scss_lint/control_comment_processor.rb#L137-L148
train
sds/scss-lint
lib/scss_lint/linter/qualifying_element.rb
SCSSLint.Linter::QualifyingElement.seq_contains_sel_class?
def seq_contains_sel_class?(seq, selector_class) seq.members.any? do |simple| simple.is_a?(selector_class) end end
ruby
def seq_contains_sel_class?(seq, selector_class) seq.members.any? do |simple| simple.is_a?(selector_class) end end
[ "def", "seq_contains_sel_class?", "(", "seq", ",", "selector_class", ")", "seq", ".", "members", ".", "any?", "do", "|", "simple", "|", "simple", ".", "is_a?", "(", "selector_class", ")", "end", "end" ]
Checks if a simple sequence contains a simple selector of a certain class. @param seq [Sass::Selector::SimpleSequence] @param selector_class [Sass::Selector::Simple] @returns [Boolean]
[ "Checks", "if", "a", "simple", "sequence", "contains", "a", "simple", "selector", "of", "a", "certain", "class", "." ]
e99afe4ede041a431a06e585c12ce82f6ad50116
https://github.com/sds/scss-lint/blob/e99afe4ede041a431a06e585c12ce82f6ad50116/lib/scss_lint/linter/qualifying_element.rb#L21-L25
train
sds/scss-lint
lib/scss_lint/linter/selector_depth.rb
SCSSLint.Linter::SelectorDepth.max_sequence_depth
def max_sequence_depth(comma_sequence, current_depth) # Sequence contains interpolation; assume a depth of 1 return current_depth + 1 unless comma_sequence comma_sequence.members.map { |sequence| sequence_depth(sequence, current_depth) }.max end
ruby
def max_sequence_depth(comma_sequence, current_depth) # Sequence contains interpolation; assume a depth of 1 return current_depth + 1 unless comma_sequence comma_sequence.members.map { |sequence| sequence_depth(sequence, current_depth) }.max end
[ "def", "max_sequence_depth", "(", "comma_sequence", ",", "current_depth", ")", "return", "current_depth", "+", "1", "unless", "comma_sequence", "comma_sequence", ".", "members", ".", "map", "{", "|", "sequence", "|", "sequence_depth", "(", "sequence", ",", "current_depth", ")", "}", ".", "max", "end" ]
Find the maximum depth of all sequences in a comma sequence.
[ "Find", "the", "maximum", "depth", "of", "all", "sequences", "in", "a", "comma", "sequence", "." ]
e99afe4ede041a431a06e585c12ce82f6ad50116
https://github.com/sds/scss-lint/blob/e99afe4ede041a431a06e585c12ce82f6ad50116/lib/scss_lint/linter/selector_depth.rb#L29-L34
train
sds/scss-lint
lib/scss_lint/linter/indentation.rb
SCSSLint.Linter::Indentation.check_root_ruleset_indent
def check_root_ruleset_indent(node, actual_indent) # Whether node is a ruleset not nested within any other ruleset. if @indent == 0 && node.is_a?(Sass::Tree::RuleNode) unless actual_indent % @indent_width == 0 add_lint(node.line, lint_message("a multiple of #{@indent_width}", actual_indent)) return true end end false end
ruby
def check_root_ruleset_indent(node, actual_indent) # Whether node is a ruleset not nested within any other ruleset. if @indent == 0 && node.is_a?(Sass::Tree::RuleNode) unless actual_indent % @indent_width == 0 add_lint(node.line, lint_message("a multiple of #{@indent_width}", actual_indent)) return true end end false end
[ "def", "check_root_ruleset_indent", "(", "node", ",", "actual_indent", ")", "if", "@indent", "==", "0", "&&", "node", ".", "is_a?", "(", "Sass", "::", "Tree", "::", "RuleNode", ")", "unless", "actual_indent", "%", "@indent_width", "==", "0", "add_lint", "(", "node", ".", "line", ",", "lint_message", "(", "\"a multiple of #{@indent_width}\"", ",", "actual_indent", ")", ")", "return", "true", "end", "end", "false", "end" ]
Allow rulesets to be indented any amount when the indent is zero, as long as it's a multiple of the indent width
[ "Allow", "rulesets", "to", "be", "indented", "any", "amount", "when", "the", "indent", "is", "zero", "as", "long", "as", "it", "s", "a", "multiple", "of", "the", "indent", "width" ]
e99afe4ede041a431a06e585c12ce82f6ad50116
https://github.com/sds/scss-lint/blob/e99afe4ede041a431a06e585c12ce82f6ad50116/lib/scss_lint/linter/indentation.rb#L166-L176
train
sds/scss-lint
lib/scss_lint/linter/indentation.rb
SCSSLint.Linter::Indentation.one_shift_greater_than_parent?
def one_shift_greater_than_parent?(node, actual_indent) parent_indent = node_indent(node_indent_parent(node)).length expected_indent = parent_indent + @indent_width expected_indent == actual_indent end
ruby
def one_shift_greater_than_parent?(node, actual_indent) parent_indent = node_indent(node_indent_parent(node)).length expected_indent = parent_indent + @indent_width expected_indent == actual_indent end
[ "def", "one_shift_greater_than_parent?", "(", "node", ",", "actual_indent", ")", "parent_indent", "=", "node_indent", "(", "node_indent_parent", "(", "node", ")", ")", ".", "length", "expected_indent", "=", "parent_indent", "+", "@indent_width", "expected_indent", "==", "actual_indent", "end" ]
Returns whether node is indented exactly one indent width greater than its parent. @param node [Sass::Tree::Node] @return [true,false]
[ "Returns", "whether", "node", "is", "indented", "exactly", "one", "indent", "width", "greater", "than", "its", "parent", "." ]
e99afe4ede041a431a06e585c12ce82f6ad50116
https://github.com/sds/scss-lint/blob/e99afe4ede041a431a06e585c12ce82f6ad50116/lib/scss_lint/linter/indentation.rb#L183-L187
train
sds/scss-lint
lib/scss_lint/linter/space_between_parens.rb
SCSSLint.Linter::SpaceBetweenParens.feel_for_enclosing_parens
def feel_for_enclosing_parens(node) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity range = node.source_range original_source = source_from_range(range) left_offset = -1 right_offset = 0 if original_source[-1] != ')' right_offset += 1 while character_at(range.end_pos, right_offset) =~ /\s/ return original_source if character_at(range.end_pos, right_offset) != ')' end # At this point, we know that we're wrapped on the right by a ')'. # Are we wrapped on the left by a '('? left_offset -= 1 while character_at(range.start_pos, left_offset) =~ /\s/ return original_source if character_at(range.start_pos, left_offset) != '(' # At this point, we know we're wrapped on both sides by parens. However, # those parens may be part of a parent function call. We don't care about # such parens. This depends on whether the preceding character is part of # a function name. return original_source if character_at(range.start_pos, left_offset - 1).match?(/[A-Za-z0-9_]/) range.start_pos.offset += left_offset range.end_pos.offset += right_offset source_from_range(range) end
ruby
def feel_for_enclosing_parens(node) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity range = node.source_range original_source = source_from_range(range) left_offset = -1 right_offset = 0 if original_source[-1] != ')' right_offset += 1 while character_at(range.end_pos, right_offset) =~ /\s/ return original_source if character_at(range.end_pos, right_offset) != ')' end # At this point, we know that we're wrapped on the right by a ')'. # Are we wrapped on the left by a '('? left_offset -= 1 while character_at(range.start_pos, left_offset) =~ /\s/ return original_source if character_at(range.start_pos, left_offset) != '(' # At this point, we know we're wrapped on both sides by parens. However, # those parens may be part of a parent function call. We don't care about # such parens. This depends on whether the preceding character is part of # a function name. return original_source if character_at(range.start_pos, left_offset - 1).match?(/[A-Za-z0-9_]/) range.start_pos.offset += left_offset range.end_pos.offset += right_offset source_from_range(range) end
[ "def", "feel_for_enclosing_parens", "(", "node", ")", "range", "=", "node", ".", "source_range", "original_source", "=", "source_from_range", "(", "range", ")", "left_offset", "=", "-", "1", "right_offset", "=", "0", "if", "original_source", "[", "-", "1", "]", "!=", "')'", "right_offset", "+=", "1", "while", "character_at", "(", "range", ".", "end_pos", ",", "right_offset", ")", "=~", "/", "\\s", "/", "return", "original_source", "if", "character_at", "(", "range", ".", "end_pos", ",", "right_offset", ")", "!=", "')'", "end", "left_offset", "-=", "1", "while", "character_at", "(", "range", ".", "start_pos", ",", "left_offset", ")", "=~", "/", "\\s", "/", "return", "original_source", "if", "character_at", "(", "range", ".", "start_pos", ",", "left_offset", ")", "!=", "'('", "return", "original_source", "if", "character_at", "(", "range", ".", "start_pos", ",", "left_offset", "-", "1", ")", ".", "match?", "(", "/", "/", ")", "range", ".", "start_pos", ".", "offset", "+=", "left_offset", "range", ".", "end_pos", ".", "offset", "+=", "right_offset", "source_from_range", "(", "range", ")", "end" ]
An expression enclosed in parens will include or not include each paren, depending on whitespace. Here we feel out for enclosing parens, and return them as the new source for the node.
[ "An", "expression", "enclosed", "in", "parens", "will", "include", "or", "not", "include", "each", "paren", "depending", "on", "whitespace", ".", "Here", "we", "feel", "out", "for", "enclosing", "parens", "and", "return", "them", "as", "the", "new", "source", "for", "the", "node", "." ]
e99afe4ede041a431a06e585c12ce82f6ad50116
https://github.com/sds/scss-lint/blob/e99afe4ede041a431a06e585c12ce82f6ad50116/lib/scss_lint/linter/space_between_parens.rb#L72-L98
train
sds/scss-lint
lib/scss_lint/linter/empty_line_between_blocks.rb
SCSSLint.Linter::EmptyLineBetweenBlocks.check_preceding_node
def check_preceding_node(node, type) case prev_node(node) when nil, Sass::Tree::FunctionNode, Sass::Tree::MixinNode, Sass::Tree::MixinDefNode, Sass::Tree::RuleNode, Sass::Tree::CommentNode # Ignore nil else unless engine.lines[node.line - 2].strip.empty? add_lint(node.line, MESSAGE_FORMAT % [type, 'preceded']) end end end
ruby
def check_preceding_node(node, type) case prev_node(node) when nil, Sass::Tree::FunctionNode, Sass::Tree::MixinNode, Sass::Tree::MixinDefNode, Sass::Tree::RuleNode, Sass::Tree::CommentNode # Ignore nil else unless engine.lines[node.line - 2].strip.empty? add_lint(node.line, MESSAGE_FORMAT % [type, 'preceded']) end end end
[ "def", "check_preceding_node", "(", "node", ",", "type", ")", "case", "prev_node", "(", "node", ")", "when", "nil", ",", "Sass", "::", "Tree", "::", "FunctionNode", ",", "Sass", "::", "Tree", "::", "MixinNode", ",", "Sass", "::", "Tree", "::", "MixinDefNode", ",", "Sass", "::", "Tree", "::", "RuleNode", ",", "Sass", "::", "Tree", "::", "CommentNode", "nil", "else", "unless", "engine", ".", "lines", "[", "node", ".", "line", "-", "2", "]", ".", "strip", ".", "empty?", "add_lint", "(", "node", ".", "line", ",", "MESSAGE_FORMAT", "%", "[", "type", ",", "'preceded'", "]", ")", "end", "end", "end" ]
In cases where the previous node is not a block declaration, we won't have run any checks against it, so we need to check here if the previous line is an empty line
[ "In", "cases", "where", "the", "previous", "node", "is", "not", "a", "block", "declaration", "we", "won", "t", "have", "run", "any", "checks", "against", "it", "so", "we", "need", "to", "check", "here", "if", "the", "previous", "line", "is", "an", "empty", "line" ]
e99afe4ede041a431a06e585c12ce82f6ad50116
https://github.com/sds/scss-lint/blob/e99afe4ede041a431a06e585c12ce82f6ad50116/lib/scss_lint/linter/empty_line_between_blocks.rb#L80-L96
train
sds/scss-lint
lib/scss_lint/linter/space_after_property_colon.rb
SCSSLint.Linter::SpaceAfterPropertyColon.value_offset
def value_offset(prop) src_range = prop.name_source_range src_range.start_pos.offset + (src_range.end_pos.offset - src_range.start_pos.offset) + whitespace_after_colon(prop).take_while { |w| w == ' ' }.size end
ruby
def value_offset(prop) src_range = prop.name_source_range src_range.start_pos.offset + (src_range.end_pos.offset - src_range.start_pos.offset) + whitespace_after_colon(prop).take_while { |w| w == ' ' }.size end
[ "def", "value_offset", "(", "prop", ")", "src_range", "=", "prop", ".", "name_source_range", "src_range", ".", "start_pos", ".", "offset", "+", "(", "src_range", ".", "end_pos", ".", "offset", "-", "src_range", ".", "start_pos", ".", "offset", ")", "+", "whitespace_after_colon", "(", "prop", ")", ".", "take_while", "{", "|", "w", "|", "w", "==", "' '", "}", ".", "size", "end" ]
Offset of value for property
[ "Offset", "of", "value", "for", "property" ]
e99afe4ede041a431a06e585c12ce82f6ad50116
https://github.com/sds/scss-lint/blob/e99afe4ede041a431a06e585c12ce82f6ad50116/lib/scss_lint/linter/space_after_property_colon.rb#L75-L80
train