Class: GhDiff::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/gh-diff/cli.rb

Constant Summary collapse

@@login =
nil

Instance Method Summary collapse

Instance Method Details

#diff(file1, file2 = file1) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gh-diff/cli.rb', line 61

def diff(file1, file2=file1)
  opts = Option.new(options).with_env
  github_auth(opts[:username], opts[:password], opts[:token])

  gh = Diff.new(opts[:repo], revision:opts[:revision], dir:opts[:dir])
  diffs = gh.diff(file1, file2, commentout:opts[:commentout],
                                comment_tag:opts[:comment_tag])

  ref = gh.ref(opts[:revision], repo:opts[:repo])

  diffs.each do |(f1, f2), diff|
    next if file_not_found?(f1, f2, diff)
    header = <<-EOS
Base revision: #{ref[:object][:sha]}[#{ref[:ref]}]
--- #{f1}
+++ #{f2}

    EOS
    diff_form = "#{f1} <-> #{f2} [%s:%s]" %
                [ref[:object][:sha][0,7], ref[:ref].match(/\w+$/).to_s]

    if opts[:save]
      format = opts[:format]=='color' ? :text : opts[:format]
      content = diff.to_s(format)
      unless content.empty?
        save(header + content, opts[:save_dir], f1)
      else
        print "\e[32mno Diff on\e[0m #{diff_form}\n"
      end
    else
      content = diff.to_s(:text)
      unless content.empty?
        if opts[:name_only]
          printf "\e[31mDiff found on\e[0m #{diff_form}\n"
        else
          print header
          print diff.to_s(opts[:format])
        end
      else
        print "\e[32mno Diff on\e[0m #{diff_form}\n"
      end
    end
  end
end

#dir_diff(dir) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gh-diff/cli.rb', line 107

def dir_diff(dir)
  opts = Option.new(options).with_env
  github_auth(opts[:username], opts[:password], opts[:token])

  gh = Diff.new(opts[:repo], revision:opts[:revision], dir:opts[:dir])
  added, removed = gh.dir_diff(dir)
  if [added, removed].all?(&:empty?)
    puts "\e[33mNothing changed\e[0m"
  else
    if added.any?
      puts "\e[33mNew files:\e[0m"
      puts added.map { |f| "  \e[32m" + f + "\e[0m" }
    end
    if removed.any?
      puts "\e[33mRemoved files:\e[0m"
      puts removed.map { |f| "  \e[31m" + f + "\e[0m" }
    end
  end
end

#get(file) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gh-diff/cli.rb', line 23

def get(file)
  opts = Option.new(options).with_env
  github_auth(opts[:username], opts[:password], opts[:token])

  gh = Diff.new(opts[:repo], revision:opts[:revision], dir:opts[:dir])
  print gh.get(file)
rescue ::Octokit::NotFound
  path = (dir=opts[:dir]) ? "#{dir}/#{file}" : file
  puts "File not found at remote: '#{path}'"
  exit(1)
rescue => e
  puts "something go wrong: #{e}"
  exit(1)
end