Class: RakeCommit::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/rake_commit/git.rb

Instance Method Summary collapse

Constructor Details

#initialize(collapse_commits = true, rebase_only = false, incremental = false, prompt_exclusions = [], precommit = nil) ⇒ Git



6
7
8
9
10
11
12
# File 'lib/rake_commit/git.rb', line 6

def initialize(collapse_commits = true, rebase_only = false, incremental = false, prompt_exclusions = [], precommit = nil)
  @collapse_commits = collapse_commits
  @rebase_only = rebase_only
  @incremental = incremental
  @prompt_exclusions = prompt_exclusions
  @precommit = precommit
end

Instance Method Details

#addObject



70
71
72
# File 'lib/rake_commit/git.rb', line 70

def add
  RakeCommit::Shell.system "git add -A ."
end

#collapse_git_commitsObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rake_commit/git.rb', line 54

def collapse_git_commits
  RakeCommit::Shell.system(@precommit) unless @precommit.nil?
  add
  temp_commit
  reset_soft
  status
  return if nothing_to_commit?
  incremental_commit
  pull_rebase rescue return false
  return true
end

#collapse_git_commits?Boolean



38
39
40
41
42
43
44
# File 'lib/rake_commit/git.rb', line 38

def collapse_git_commits?
  return false unless @collapse_commits
  return true unless merge_commits?
  status
  input = Readline.readline("Do you want to collapse merge commits? (y/n): ").chomp
  input == "y"
end

#commitObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rake_commit/git.rb', line 14

def commit
  if @incremental
    incremental_commit
  elsif rebase_in_progress?
    rebase_continue
    RakeCommit::Shell.system("rake")
    push
  else
    if collapse_git_commits?
      return unless collapse_git_commits
    elsif rebase_only?
      add
      incremental_commit unless nothing_to_commit?
      pull_rebase rescue return false
    end
    RakeCommit::Shell.system("rake")
    push
  end
end

#git_branchObject



106
107
108
109
110
111
# File 'lib/rake_commit/git.rb', line 106

def git_branch
  @git_branch ||= begin
    output = RakeCommit::Shell.backtick("git symbolic-ref HEAD")
    output.gsub('refs/heads/', '').strip
  end
end

#incremental_commitObject



74
75
76
77
78
79
80
81
# File 'lib/rake_commit/git.rb', line 74

def incremental_commit
  commit_message = RakeCommit::CommitMessage.new(@prompt_exclusions)
  unless commit_message.author.nil?
    RakeCommit::Shell.system("git config user.name #{Shellwords.shellescape(commit_message.author)}")
  end
  message = [commit_message.feature, commit_message.message].compact.join(" - ")
  RakeCommit::Shell.system("git commit -m #{Shellwords.shellescape(message)}")
end

#merge_baseObject



117
118
119
# File 'lib/rake_commit/git.rb', line 117

def merge_base
  @merge_base ||= RakeCommit::Shell.backtick("git merge-base #{git_branch} origin/#{git_branch}").strip
end

#merge_commits?Boolean



113
114
115
# File 'lib/rake_commit/git.rb', line 113

def merge_commits?
  RakeCommit::Shell.backtick("git log #{merge_base}..HEAD") != RakeCommit::Shell.backtick("git log --no-merges #{merge_base}..HEAD")
end

#nothing_to_commit?Boolean



101
102
103
104
# File 'lib/rake_commit/git.rb', line 101

def nothing_to_commit?
  status = RakeCommit::Shell.backtick("git status", false)
  status.empty? || status =~ /nothing to commit/m
end

#pull_rebaseObject



88
89
90
# File 'lib/rake_commit/git.rb', line 88

def pull_rebase
  RakeCommit::Shell.system "git pull --rebase --stat"
end

#pushObject



92
93
94
# File 'lib/rake_commit/git.rb', line 92

def push
  RakeCommit::Shell.system "git push origin #{git_branch}"
end

#rebase_continueObject



50
51
52
# File 'lib/rake_commit/git.rb', line 50

def rebase_continue
  RakeCommit::Shell.system("git rebase --continue")
end

#rebase_in_progress?Boolean



34
35
36
# File 'lib/rake_commit/git.rb', line 34

def rebase_in_progress?
  File.directory?(".git/rebase-merge") || File.directory?(".git/rebase-apply")
end

#rebase_only?Boolean



46
47
48
# File 'lib/rake_commit/git.rb', line 46

def rebase_only?
  !!@rebase_only
end

#reset_softObject



83
84
85
86
# File 'lib/rake_commit/git.rb', line 83

def reset_soft
  raise "Could not determine branch" unless git_branch
  RakeCommit::Shell.system "git reset --soft #{merge_base}"
end

#statusObject



66
67
68
# File 'lib/rake_commit/git.rb', line 66

def status
  RakeCommit::Shell.system("git status", false)
end

#temp_commitObject



96
97
98
99
# File 'lib/rake_commit/git.rb', line 96

def temp_commit
  return if nothing_to_commit?
  RakeCommit::Shell.system "git commit -m 'rake_commit backup commit'"
end