Class: Git::Trifle::PatchPatcher

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/git-patch-patch/patch_patcher.rb,
lib/git-patch-patch/version.rb

Overview

not too bright though

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ PatchPatcher

open the repo in path set the root path from where the patches are retrieved



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/git-patch-patch/patch_patcher.rb', line 32

def initialize(options)
  options = {branch: 'master', patch_dir: '/tmp'}.merge options

  # git handler
  @t = Git::Trifle.new options[:repo]
  # where the patch file will be stored
  @patch_dir = Pathstring.new options[:patch_dir]
  # branch from which the commits will be reviewed
  @branch = options[:branch]
  # list of reviewed commits
  @patcher_commits = commits branch: @branch
end

Instance Attribute Details

#patchObject (readonly)

current patch generated by the last diff performed



28
29
30
# File 'lib/git-patch-patch/patch_patcher.rb', line 28

def patch
  @patch
end

Instance Method Details

#checkout_work_branchObject



113
114
115
116
# File 'lib/git-patch-patch/patch_patcher.rb', line 113

def checkout_work_branch
  # checkout -b barbaric_name sha
  checkout "__patch_patcher_#{Time.now.to_f}", commit: @patcher_commits.first
end

#commit_patchObject

plain as plain :

  • patch from file

  • apply and commit with a reuse of the commit message from the second sha of the diff which generated the patch



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/git-patch-patch/patch_patcher.rb', line 94

def commit_patch
  patch.error = nil

  @t.apply patch.file
  @t.add '.'
  @t.commit '', reuse_message: patch.second_commit
rescue => error
  # Houston we have a problem
  patch.error = error
  # first we yield to allow a fix
  yield patch if block_given?

  # if a fix was made, retry
  if patch.changed?
    patch.save
    retry
  end
end

#diff(first_commit, second_commit) ⇒ Object

this class base operation a diff between two sha’s stored in @patch



82
83
84
85
86
87
88
# File 'lib/git-patch-patch/patch_patcher.rb', line 82

def diff(first_commit, second_commit)
  @patch = PatchPatch.new @t.diff(first_commit, second_commit),
                          first_commit: first_commit,
                          second_commit: second_commit,
                          file: patch_file_for(second_commit),
                          work: patch_file_for(second_commit).exist?
end

#patch_file_for(commit) ⇒ Object

unique patch filename for a given sha and a given local repo’



119
120
121
122
123
124
125
126
127
# File 'lib/git-patch-patch/patch_patcher.rb', line 119

def patch_file_for(commit)
  @patch_dir.join 'git-patch-patch',
                  # horrendous but necessary to ensure unicity
                  # of path, without having an absolute path
                  # that 'join' doesn't like
                  Pathname(@t.directory).realpath.to_s.sub('/',''),
                  commit.to_s,
                  'patch'
end

#patch_work(pattern, replacement, *options, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/git-patch-patch/patch_patcher.rb', line 45

def patch_work(pattern, replacement, *options, &block)
  # work branch
  checkout_work_branch

  # main workflow
  @patcher_commits.each_with_index do |c, id|
    # exit on the last commit
    break unless c_next = @patcher_commits[id + 1]

    # current diff sets the current patch
    diff c, c_next

    # even if work is done we yield the patch
    # to allow modification
    patch.work == :done &&
      yield(patch)

    # if a patch file is found, we commit and
    # jump to next iteration
    patch.file.exist? &&
      commit_patch(&block) &&
      next

    # patch filenames and / or patch content
    options.each do |work|
      patch.send "patch_#{work}", pattern, replacement
      yield patch
    end

    # save to file and commit
    patch.save
    commit_patch &block
  end
end