Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

version-control-system #45

Closed
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
8545230
Create vcs.md
iamanandkr Jul 16, 2017
31bc7b1
Create vcs.rb
iamanandkr Aug 16, 2017
30df542
Create vcs_internals.rb
iamanandkr Aug 16, 2017
4c05fbf
Create blob.rb
iamanandkr Aug 16, 2017
d7d87d0
Create tree.rb
iamanandkr Aug 16, 2017
5332694
Create commit.rb
iamanandkr Aug 16, 2017
d1f1658
Create diff-utility.rb
iamanandkr Aug 16, 2017
338179b
Update vcs.rb
iamanandkr Aug 16, 2017
4bfc151
Update vcs.rb
iamanandkr Aug 29, 2017
d95ac6b
Update vcs_internals.rb
iamanandkr Aug 29, 2017
0bfea34
Create file_storage.rb
iamanandkr Aug 29, 2017
d4bb34e
Delete blob.rb
iamanandkr Aug 29, 2017
4c00cc7
Delete commit.rb
iamanandkr Aug 29, 2017
c42013e
Delete tree.rb
iamanandkr Aug 29, 2017
f062559
Update vcs_internals.rb
iamanandkr Aug 29, 2017
4a923c2
Update vcs_internals.rb
iamanandkr Aug 29, 2017
64d6b09
Update diff-utility.rb
iamanandkr Aug 29, 2017
1fe0d6f
Create vcs_test.rb
iamanandkr Aug 31, 2017
4fd8216
Update vcs.rb
iamanandkr Oct 3, 2017
b9b2cf3
Update vcs_internals.rb
iamanandkr Oct 3, 2017
fe852d3
Update file_storage.rb
iamanandkr Oct 3, 2017
147ee00
Update diff-utility.rb
iamanandkr Oct 3, 2017
c7ab2ad
Create lcs.rb
iamanandkr Oct 25, 2017
7829c50
Create file_diff.rb
iamanandkr Oct 25, 2017
73c03a2
Delete diff-utility.rb
iamanandkr Oct 29, 2017
bc025fe
Update file_diff.rb
iamanandkr Oct 29, 2017
4dc4f10
Rename file_diff.rb to diff_utility.rb
iamanandkr Oct 29, 2017
293ebfa
Update and rename file_storage.rb to vcs_objects.rb
iamanandkr Oct 29, 2017
d53985d
Update vcs.rb
iamanandkr Oct 29, 2017
256db12
Delete vcs_internals.rb
iamanandkr Oct 29, 2017
31bdf8e
Create vcs_commands.rb
iamanandkr Oct 29, 2017
a71f996
Create exceptions.rb
iamanandkr Oct 29, 2017
d0325b2
Create file_utilities.rb
iamanandkr Oct 29, 2017
b4aca5f
Update vcs_commands.rb
iamanandkr Oct 29, 2017
312f476
Changes to be committed:
iamanandkr Oct 31, 2017
a89399d
Update vcs.rb
iamanandkr Oct 31, 2017
398bed5
Update diff_utility.rb
iamanandkr Oct 31, 2017
cce4e35
Update file_utilities.rb
iamanandkr Oct 31, 2017
4f2a6f3
Update vcs_internals.rb
iamanandkr Oct 31, 2017
5710482
Update vcs.rb
iamanandkr Oct 31, 2017
34900b6
Update vcs_objects.rb
iamanandkr Oct 31, 2017
0c4c349
Update vcs.rb
iamanandkr Oct 31, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 202 additions & 0 deletions solutions/anand-kumar/src/diff-utility.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
class Diff
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh, I wasn't able to follow along with all of your logic here, so I'm not convinced that it is correct yet.
Can you write a unit test for this specific file? Show me the edge cases.


def initialize(old_file_path, new_file_path, file_name=nil)
@old_file_path = old_file_path
@new_file_path = new_file_path
@file_name = file_name
@hunks_with_context = []
if (@file_name.nil?)
@file_name = @old_file_path
end
find_hunk_ranges()
end

def get_lines(file_path)
file_lines = []
open(file_path, "r") do |file|
while true
data = file.gets()
if data.nil?
break
end
file_lines.push(data.chomp())
end
end
return file_lines
end


def get_equivalent_lines()
old_lines = get_lines(@old_file_path)
new_lines = get_lines(@new_file_path)
old_line_equivalent_in_new = Array.new(old_lines.length() + 2, 0)
new_line_equivalent_in_old = Array.new(new_lines.length() + 2, 0)
old_line_equivalent_in_new[old_lines.length() + 1] = \
new_lines.length() + 1
new_line_equivalent_in_old[new_lines.length() + 1] = \
old_lines.length() + 1
lcs = Array.new(old_lines.length() + 1) \
{Array.new(new_lines.length() + 1, 0)}
preceding_position = Array.new(old_lines.length() + 1) \
{Array.new(new_lines.length() + 1, nil)}
for ii in (1..old_lines.length())
for jj in (1..new_lines.length())
if old_lines[ii - 1] == new_lines[jj - 1]
lcs[ii][jj] = lcs[ii - 1][jj - 1] + 1
preceding_position[ii][jj] = "up-left"
elsif lcs[ii - 1][jj] >= lcs[ii][jj - 1]
lcs[ii][jj] = lcs[ii - 1][jj]
preceding_position[ii][jj] = "up"
else
lcs[ii][jj] = lcs[ii][jj - 1]
preceding_position[ii][jj] = "left"
end
end
end
ii = old_lines.length()
jj = new_lines.length()
while not preceding_position[ii][jj].nil?
if preceding_position[ii][jj] == "up-left"
old_line_equivalent_in_new[ii] = jj
new_line_equivalent_in_old[jj] = ii
end
if preceding_position[ii][jj] == "left"
jj -= 1
elsif preceding_position[ii][jj] == "up"
ii -= 1
else
ii -= 1
jj -= 1
end
end
return old_line_equivalent_in_new, new_line_equivalent_in_old
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to add comments explaining these names, because it wasn't until I read the rest of the class that I truly understood what was going on.

end

def find_hunk_ranges()
old_line_equivalent_in_new, _new_line_equivalent_in_old = \
get_equivalent_lines()
range_start_old = 1
range_start_new = 1
old_line_number = 1
while old_line_number < old_line_equivalent_in_new.length()
if old_line_equivalent_in_new[old_line_number] != 0
add_context_around_hunk(
range_start_old,
old_line_number,
range_start_new,
old_line_equivalent_in_new[old_line_number],
)
range_start_old = old_line_number + 1
range_start_new = \
old_line_equivalent_in_new[old_line_number] + 1
end
old_line_number += 1
end
end

def add_context_around_hunk(
range_start_old,
range_end_old,
range_start_new,
range_end_new,
lines=3
)
total_hunks = @hunks_with_context.length()
if (
range_end_old - range_start_old == 0 and
range_end_new - range_start_new == 0
)
return
end
if total_hunks == 0
@hunks_with_context.push({
:range_start_old=>range_start_old,
:range_end_old=>range_end_old,
:range_start_new=>range_start_new,
:range_end_new=>range_end_new,
})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont you have to add/subtract lines in this case?

return
end
last_hunk = total_hunks - 1
if (
(
range_start_old - @hunks_with_context[last_hunk][:range_end_old]
) < 2 * lines or
(
range_start_new - @hunks_with_context[last_hunk][:range_end_new]
) < 2 * lines
)
@hunks_with_context[last_hunk][:range_start_old] -= lines
@hunks_with_context[last_hunk][:range_start_new] -= lines
@hunks_with_context[last_hunk][:range_start_old] = \
[1, @hunks_with_context[last_hunk][:range_start_old]].max
@hunks_with_context[last_hunk][:range_start_new] = \
[1, @hunks_with_context[last_hunk][:range_start_new]].max
@hunks_with_context[last_hunk][:range_end_old] = range_end_old
@hunks_with_context[last_hunk][:range_end_new] = range_end_new
else
@hunks_with_context[last_hunk][:range_end_old] += lines
@hunks_with_context[last_hunk][:range_end_new] += lines
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not do this in the if-clause too?

@hunks_with_context.push({
:range_start_old=>range_start_old - lines,
:range_end_old=>range_end_old,
:range_start_new=>range_start_new - lines,
:range_end_new=>range_end_new,
})
end
end

def show(
)
old_line_equivalent_in_new, new_line_equivalent_in_old = \
get_equivalent_lines()
if @hunks_with_context.length() == 0
return
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make a get_lines method :P

old_lines = get_lines(@old_file_path)
new_lines = get_lines(@new_file_path)
puts "diff a/#{@file_name} b/#{@file_name}"
puts "--- a/#{@file_name}"
puts "+++ b/#{@file_name}"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never print stuff from inside utility modules. Instead, return strings, and defer it as much as possible, so it is handled by your CLI logic.

for ii in (0..@hunks_with_context.length() - 1)
range_start_old = @hunks_with_context[ii][:range_start_old]
range_end_old = @hunks_with_context[ii][:range_end_old]
range_start_new = @hunks_with_context[ii][:range_start_new]
range_end_new = @hunks_with_context[ii][:range_end_new]
print "@@ -"
if range_end_old - range_start_old == 0
print "#{range_start_old + 1}"
elsif range_end_old - range_start_old == 1
print "#{}{range_start_old}"
else
print "#{range_start_old},#{range_end_old - range_start_old}"
end
print " +"
if range_end_new - range_start_new == 0
print "#{range_start_new + 1}"
elsif range_end_new - range_start_new == 1
print "#{range_start_new}"
else
print "#{range_start_new},#{range_end_new - range_start_new}"
end
puts " @@"
while (
range_start_old <= range_end_old or
range_start_new <= range_end_new
)
if old_line_equivalent_in_new[range_start_old] == 0
puts "-#{old_lines[range_start_old - 1]}"
range_start_old += 1
elsif new_line_equivalent_in_old[range_start_new] == 0
puts "+#{new_lines[range_start_new - 1]}"
range_start_new += 1
else
puts " #{old_lines[range_start_old - 1]}"
range_start_old += 1
range_start_new += 1
end
end
end
print "\n"
end
end
Loading