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



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
105
106
107
108
# File 'lib/gh-diff/cli.rb', line 70

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

  gh = init_ghdiff(opts[:repo], opts[:revision], 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 = "#{ref_format(ref)}--- #{f1}\n+++ #{f2}\n\n"
    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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/gh-diff/cli.rb', line 123

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

  gh = init_ghdiff(opts[:repo], opts[:revision], 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" }
      if opts[:save]
        added.each do |f|
          path = File.join(dir, f)
          content = gh.get(path)
          if opts[:ref]
            content = add_reference(gh, opts[:revision],
                                        opts[:repo], content)
          end
          unless content.empty?
            save(content, opts[:save_dir], path, File.extname(path))
          end
        end
      end
    end
    if removed.any?
      puts "\e[33mRemoved files:\e[0m"
      puts removed.map { |f| "  \e[31m" + f + "\e[0m" }
      if opts[:save]
        removed.each do |f|
          path = File.join(dir, f)
          content = "---\nFile: #{path}\nStatus: file deleted\n---\n"
          if opts[:ref]
            content = add_reference(gh, opts[:revision],
                                        opts[:repo], content)
          end
          save(content, opts[:save_dir], path, '.delete')
        end
      end
    end
  end
rescue GhDiff::NoDirectoryError
  puts "Directory not found: `#{dir}`"
  exit(1)
end

#get(file) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gh-diff/cli.rb', line 28

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

  gh = init_ghdiff(opts[:repo], opts[:revision], opts[:dir])
  if opts[:ref]
    ref = gh.ref(opts[:revision], repo:opts[:repo])
    print ref_format(ref)
  end
  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

#versionObject



171
172
173
# File 'lib/gh-diff/cli.rb', line 171

def version
  puts "gh-diff #{GhDiff::VERSION} (c) 2014 kyoendo"
end