lib/gitlab_exporter/git.rb in gitlab-exporter-8.0.0 vs lib/gitlab_exporter/git.rb in gitlab-exporter-9.0.0
- old
+ new
@@ -11,10 +11,11 @@
# Both methods return a CommandResult which includes the output of the execution
# plus the tracked execution time.
class Git
def initialize(repo)
fail "Repository #{repo} does not exists" unless Dir.exist? repo
+
@repo = repo
@tracker = TimeTracker.new
end
def pull
@@ -33,10 +34,11 @@
private
def execute(command)
result = CommandResult.new(*Open3.capture2e(command, chdir: @repo))
fail "Command #{command} failed with status #{result.status}\n#{result.stdout}" if result.failed?
+
result
end
end
# Result of a command
@@ -94,11 +96,11 @@
counts = Hash.new(0)
Utils.pgrep("^git ").each do |pid|
process_cmd = begin
File.read("/proc/#{pid}/cmdline")
- rescue
+ rescue StandardError
"" # Process file is gone (race condition)
end
subcommand = self.class.extract_subcommand(process_cmd)
next unless subcommand # Unlikely, but just to be safe
@@ -129,9 +131,10 @@
target.write(@metrics.to_s)
end
def self.extract_subcommand(cmd)
return if cmd.empty?
+
cmd_splitted = cmd.split("\u0000") # cmdline does not return it space-separated
cmd_splitted.shift # Because it's "git"
cmd_splitted.shift while cmd_splitted.first &&
(cmd_splitted.first.empty? || cmd_splitted.first !~ /^[^-][a-z\-]*$/)