lib/abak-flow/request.rb in abak-flow-0.3.1 vs lib/abak-flow/request.rb in abak-flow-0.3.2

- old
+ new

@@ -171,6 +171,64 @@ end say color('Хм ... кажется у вас все готово к работе', :debug).to_s if request.valid? end end + + command :garbage do |c| + c.syntax = 'git request status' + c.description = 'Проверить пригодность удаленных (origin) веток и возможность их уничтожения (ветки master, develop игнорируются)' + + c.action do |args, options| + config = Abak::Flow::Config.current + github_client = Abak::Flow::GithubClient.connect(config) + request = Abak::Flow::PullRequest.new(config, :strategy => :status) + + exit unless request.valid? + + messages = {unused: ["отсутствует в upstream репозитории", :notice], + differ: ["отличается от origin репозитория", :warning], + missing: ["отсутствует в локальном репозитории", :warning]} + + say "=> Обновляю данные о репозитории upstream" + %w(origin upstream).each { |remote| Hub::Runner.execute('fetch', remote, '-p') } + + say "=> Загружаю список веток для origin" + branches = github_client.branches(request.origin_project). + reject { |branch| %w(master develop).include? branch.name } + + say "=> На origin найдено веток: #{branches.count}\n\n" + branches.each_with_index do |branch, index| + index += 1 + + base = Abak::Flow::PullRequest.branch_by_prefix branch.name.split('/').first + + upstream_branch = %x(git branch -r --contain #{branch.commit.sha} | grep upstream/#{base} 2> /dev/null).strip + local_sha = %x(git show #{branch.name} --format=%H --no-notes 2> /dev/null | head -n 1).strip + + statuses = { + unused: upstream_branch.empty?, + differ: !local_sha.empty? && local_sha != branch.commit.sha, + missing: local_sha.empty? + } + + unless statuses.values.inject &:| + say color("#{index}) #{branch.name} → можно удалить", :debug).to_s + say "\n" + next + end + + diagnoses = statuses.select { |_,bool| bool }. + map { |name,_| messages[name].first }. + map { |msg| "#{' ' * (index.to_s.length + 2)} ↪ #{msg}" }. + join("\n") + + if statuses.select { |_,bool| bool }.keys == [:missing] + say color("#{index}) #{branch.name} → потенциально можно удалить", :warning).to_s + say "#{diagnoses}\n\n" + else + say "#{index}) #{branch.name}\n#{diagnoses}\n\n" + end + end + end + end end \ No newline at end of file