-
Notifications
You must be signed in to change notification settings - Fork 1
/
difffile.rb
executable file
·57 lines (48 loc) · 1.86 KB
/
difffile.rb
1
2
3
4
5
6
7
8
9
10
11
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
51
52
53
54
55
56
57
require 'rubygems'
require 'logger'
require 'rugged'
require 'git'
load 'patch.rb'
class DiffFile
attr_reader :patch, :file_name
def initialize()
@difffile = nil
@patch = nil
@stats = nil
@file_name = nil
end
def initialize(difffile)
@difffile = difffile
self.generate_patch
self.set_file_name
end
# e.g. diff_file_name is "--- a/chapters/10-pagination.md"
def extract_file_name(diff_file_name)
file_name = diff_file_name.to_s.split("/")
# Remove the first part of "--- a"
file_name.shift
# Re-join the remaining parts
file_name = file_name.join('/')
return file_name
end
def set_file_name
file_name_old_version = (@difffile.patch).match(/^[-]{3}.*/)
file_name_new_version = (@difffile.patch).match(/^[+]{3}.*/)
file_name_old_version = extract_file_name(file_name_old_version)
file_name_new_version = extract_file_name(file_name_new_version)
# The file names would be /dev/null when the file is added/ deleted
file_name_old_version = file_name_new_version if file_name_old_version == "dev/null"
file_name_new_version = file_name_old_version if file_name_new_version == "dev/null"
# The file names would be nil if the file permissions are changed
file_name_old_version = file_name_new_version if file_name_old_version.nil?
file_name_new_version = file_name_old_version if file_name_new_version.nil?
file_name_old_version = file_name_new_version if file_name_old_version == ""
file_name_new_version = file_name_old_version if file_name_new_version == ""
raise "Both file names must be same" if file_name_old_version != file_name_new_version
@file_name = file_name_new_version
end
def generate_patch()
patch_changes = @difffile.patch
@patch = Patch.new(patch_changes)
end
end