Class: Maximus::GitControl

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

Overview

Git management

Since:

  • 0.1.0

Instance Method Summary collapse

Methods included from GitHelper

#branch, #commit_information, #files_by_sha, #first_commit, #head_sha, #lines_by_sha, #master_commit_sha, #previous_commit, #remote, #sha_range, #working_copy_files

Methods included from Helper

#discover_path, #edit_yaml, #file_count, #file_list, #is_middleman?, #is_rails?, #node_module_exists, #path_exists?, #prompt, #reporter_path, #root_dir, #truthy?

Constructor Details

#initialize(opts = {}) ⇒ GitControl

Set up instance variables

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



18
19
20
21
22
23
24
25
26
# File 'lib/maximus/git_control.rb', line 18

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(@config.working_dir)
end

Instance Method Details

#associationsHash

Define associations to linters based on file extension

Returns:

  • (Hash)

    linters and extension arrays

Since:

  • 0.1.0



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/maximus/git_control.rb', line 145

def associations
  {
    css:    ['css'],
    scss:   ['scss', 'sass'],
    js:     ['js'],
    ruby:   ['rb', 'Gemfile', 'lock', 'yml', 'Rakefile', 'ru', 'rdoc', 'rake', 'Capfile', 'jbuilder'],
    rails:  ['slim', 'haml', 'jbuilder', 'erb'],
    images: ['png', 'jpg', 'jpeg', 'gif'],
    static: ['pdf', 'txt', 'doc', 'docx', 'csv', 'xls', 'xlsx'],
    markup: ['html', 'xml', 'xhtml'],
    markdown: ['md', 'markdown', 'mdown'],
    php:    ['php', 'ini']
  }
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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/maximus/git_control.rb', line 31

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/maximus/git_control.rb', line 72

def compare(sha1 = master_commit_sha, sha2 = head_sha)
  diff_return = {}

  sha1 = define_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 ? working_copy_files : files_by_sha(git_sha)

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

  diff_return
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



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
# File 'lib/maximus/git_control.rb', line 114

def lints_and_stats(lint_by_path = false, git_shas = compare, nuclear = false)
  return false if git_shas.blank?

  base_branch = branch
  git_ouput = {}

  git_shas.each do |sha, exts|
    create_branch(sha) unless @psuedo_commit
    sha = sha.to_s
    puts sha.color(:blue)

    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)
      }

      git_ouput[sha] = nuclear ? lints_and_stats_nuclear(lint_opts) : lints_and_stats_switch(ext, lint_opts)

    end

    destroy_branch(base_branch, sha) unless @psuedo_commit
  end

  git_ouput
end