Class: Churned::Commands::Install

Inherits:
Churned::Command show all
Defined in:
lib/churned/commands/install.rb

Instance Method Summary collapse

Methods inherited from Churned::Command

#command, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which

Constructor Details

#initialize(options) ⇒ Install

Returns a new instance of Install.



8
9
10
# File 'lib/churned/commands/install.rb', line 8

def initialize(options)
  @options = options
end

Instance Method Details

#execute(input: $stdin, output: $stdout) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/churned/commands/install.rb', line 12

def execute(input: $stdin, output: $stdout)
  generator.create_dir(".churned")
  generator.remove_file ".churned/database"

  ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ".churned/database")

  ActiveRecord::Schema.define do
    create_table :commits, force: true do |t|
      t.string :sha
      t.string :author
      t.date :author_date
    end

    create_table :file_changes, force: true do |t|
      t.references :commit
      t.string :pathname
      t.integer :additions
      t.integer :deletions
    end
  end

  command.run("git log --no-merges --pretty=format:'%H%n%ad%n%ae' --numstat --since=1.years > .churned/hashes.txt")

  IO.read('.churned/hashes.txt').split("\n\n").each do |description|
    lines = description.split("\n")

    sha    = lines.shift
    date   = lines.shift
    author = lines.shift
    commit = Commit.new(sha: sha, author_date: date, author: author)

    lines.each do |numstat|
      additions, deletions, pathname = numstat.split("\t")
      commit.file_changes.build(additions: additions, deletions: deletions, pathname: pathname)
    end

    commit.save
  end
end