Class: Maximus::GitControl

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/maximus/git_control.rb

Overview

Since:

  • 0.1.0

Instance Method Summary collapse

Methods included from Helper

#check_default_config_path, #edit_yaml, #file_count, #file_list, #is_rails?, #lines_added_to_range, #node_module_exists, #path_exists?, #prompt, #reporter_path, #root_dir, #truthy?

Constructor Details

#initialize(opts = {}) ⇒ GitControl

Git management

Inherits settings from Config#initialize

Parameters:

  • opts (Hash) (defaults to: {})

    options passed directly to config

Options Hash (opts):

  • :config (Config object)

    custom Maximus::Config object

  • :commit (String)

    accepts sha, “working”, “last”, or “master”.

Since:

  • 0.1.0



15
16
17
18
19
20
21
22
23
# File 'lib/maximus/git_control.rb', line 15

def initialize(opts = {})
  opts[:config] ||= Maximus::Config.new({ commit: opts[:commit] })
  @config = opts[:config]

  @settings = @config.settings
  @psuedo_commit = ( !@settings[:commit].blank? && %w(working last master).include?(@settings[:commit]) )

  @g = Git.open(@settings[:root_dir])
end

Instance Method Details

#associationsHash

Define associations to linters based on file extension

Returns:

  • (Hash)

    linters and extension arrays

Since:

  • 0.1.0



193
194
195
196
197
198
199
200
# File 'lib/maximus/git_control.rb', line 193

def associations
  {
    scss:   ['scss', 'sass'],
    js:     ['js'],
    ruby:   ['rb', 'Gemfile', 'lock', 'yml', 'Rakefile', 'ru', 'rdoc', 'rake', 'Capfile'],
    rails:  ['slim', 'haml', 'jbuilder', 'erb']
  }
end

#commit_export(commit_sha = head_sha) ⇒ Hash

30,000 foot view of a commit

Parameters:

  • commit_sha (String) (defaults to: head_sha)

    (head_sha) the sha of the commit

Returns:

  • (Hash)

    commit data

Since:

  • 0.1.0



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/maximus/git_control.rb', line 28

def commit_export(commit_sha = head_sha)
  commit_sha = commit_sha.to_s

  ce_commit = @g.gcommit(commit_sha)

  if first_commit == commit_sha
    ce_diff = diff_initial(first_commit)
  else
    last_commit = @g.gcommit(previous_commit(commit_sha))
    ce_diff = diff(last_commit, ce_commit)
  end

  {
    commit_sha: commit_sha,
    branch: branch,
    message: ce_commit.message,
    remote_repo: remote,
    git_author: ce_commit.author.name,
    git_author_email: ce_commit.author.email,
    commit_date: ce_commit.author.date.to_s,
    diff: ce_diff
  }
end

#compare(sha1 = master_commit.sha, sha2 = head_sha) ⇒ Hash

Compare two commits and get line number ranges of changed patches

Examples:

output from the method

{
  'sha': {
    rb: {
      filename: 'file.rb',
      changes: {
        ['0..4'],
        ['10..20']
      }
    }
  }
}

Parameters:

  • sha1 (String) (defaults to: master_commit.sha)
  • sha2 (String) (defaults to: head_sha)

Returns:

  • (Hash)

    diff_return files changed grouped by file extension and line number

Since:

  • 0.1.0



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/maximus/git_control.rb', line 69

def compare(sha1 = master_commit.sha, sha2 = head_sha)
  diff_return = {}

  sha1 = set_psuedo_commit if @settings[:commit]
  # Reverse so that we go in chronological order
  git_spread = commit_range(sha1, sha2).reverse

  git_spread.each do |git_sha|

    # Grab all files in that commit and group them by extension
    #   If working copy, just give the diff names of the files changed
    files = @psuedo_commit ? `git -C #{@settings[:root_dir]} diff --name-only` : `git -C #{@settings[:root_dir]} show --pretty="format:" --name-only #{git_sha}`

    diff_return[git_sha.to_s] = match_associations(git_sha, files)
  end
  diff_return
end

#first_commitString

Find first commit

Returns:

  • (String)

Since:

  • 0.1.5



178
179
180
# File 'lib/maximus/git_control.rb', line 178

def first_commit
  `git -C #{@settings[:root_dir]} rev-list --max-parents=0 HEAD`.strip!
end

#lints_and_stats(lint_by_path = false, git_shas = compare, nuclear = false) ⇒ Hash

Run appropriate lint for every sha in commit history. For each sha a new branch is created then deleted

Examples:

sample output

{
  'sha': {
    lints: {
      scsslint: {
        files_inspec...
      },
    },
    statisti...
  },
  'sha'...
}

Parameters:

  • lint_by_path (Boolean) (defaults to: false)

    only lint by files in git commit and not the commit as a whole

  • git_shas (Hash) (defaults to: compare)

    (#compare) a hash of gitcommit shas and relevant file types in the commit

  • nuclear (Boolean) (defaults to: false)

    do everything regardless of what’s in the commit

Returns:

  • (Hash)

    data all data grouped by task

See Also:

Since:

  • 0.1.0



110
111
112
113
114
115
116
117
118
119
120
121
122
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
169
170
171
172
173
# File 'lib/maximus/git_control.rb', line 110

def lints_and_stats(lint_by_path = false, git_shas = compare, nuclear = false)
  return false if git_shas.blank?
  base_branch = branch
  git_output = {}
  git_shas.each do |sha, exts|
    create_branch(sha) unless @psuedo_commit
    sha = sha.to_s
    puts sha.color(:blue)
    git_output[sha] = {lints: {}, statistics: {}}
    lints = git_output[sha][:lints]
    statistics = git_output[sha][:statistics]

    # This is where everything goes down
    exts.each do |ext, files|

      # For relevant_lines data
      lint_opts = {
        git_files: files,
        config: @config,
        file_paths: (lint_file_paths(files, ext) if lint_by_path)
      }

      if nuclear
        lints[:scsslint] = Maximus::Scsslint.new(lint_opts).result
        lints[:jshint] = Maximus::Jshint.new(lint_opts).result
        lints[:rubocop] = Maximus::Rubocop.new(lint_opts).result
        lints[:railsbp] = Maximus::Railsbp.new(lint_opts).result
        lints[:brakeman] = Maximus::Brakeman.new(lint_opts).result
        statistics[:stylestat] = Maximus::Stylestats.new({config: @config}).result
        statistics[:phantomas] = Maximus::Phantomas.new({config: @config}).result
        statistics[:wraith] = Maximus::Wraith.new({config: @config}).result
      else
        case ext
          when :scss
            lints[:scsslint] = Maximus::Scsslint.new(lint_opts).result

            # @todo stylestat is singular here because model name in Rails is singular.
            #   But adding a .classify when it's converted to a model chops off the end s on 'phantomas',
            #   which breaks the model name.
            statistics[:stylestat] = Maximus::Stylestats.new({config: @config}).result

            # @todo double pipe here is best way to say, if it's already run, don't run again, right?
            statistics[:phantomas] ||= Maximus::Phantomas.new({config: @config}).result
            statistics[:wraith] ||= Maximus::Wraith.new({config: @config}).result
          when :js
            lints[:jshint] = Maximus::Jshint.new(lint_opts).result

            statistics[:phantomas] ||= Maximus::Phantomas.new({config: @config}).result

            # @todo double pipe here is best way to say, if it's already run, don't run again, right?
            statistics[:wraith] ||= Maximus::Wraith.new({config: @config}).result
          when :ruby
            lints[:rubocop] = Maximus::Rubocop.new(lint_opts).result
            lints[:railsbp] ||= Maximus::Railsbp.new(lint_opts).result
            lints[:brakeman] = Maximus::Brakeman.new(lint_opts).result
          when :rails
            lints[:railsbp] ||= Maximus::Railsbp.new(lint_opts).result
        end
      end
    end
    destroy_branch(base_branch, sha) unless @psuedo_commit
  end
  git_output
end

#previous_commit(current_commit = head_sha, previous_by = 1) ⇒ String

Get commit before current

Parameters:

  • current_commit (String) (defaults to: head_sha)

    (head_sha) commit to start at

  • previous_by (Integer) (defaults to: 1)

    (1) commit n commits ago

Returns:

  • (String)

Since:

  • 0.1.5



187
188
189
# File 'lib/maximus/git_control.rb', line 187

def previous_commit(current_commit = head_sha, previous_by = 1)
  `git -C #{@settings[:root_dir]} rev-list --max-count=#{previous_by + 1} #{current_commit} --reverse | head -n1`.strip!
end