Skip to content

Commit 0afe581

Browse files
committed
fixing diffs to work in the right direction
1 parent 3d6fe50 commit 0afe581

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

bin/files_in_my_diff

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require 'bundler/setup'
5+
6+
require 'setup_git'
7+
require 'files_in_my_diff'
8+
9+
require 'json'
10+
11+
folder = ARGV[0]
12+
revision = ARGV[1]
13+
14+
begin
15+
decorated = FilesInMyDiff.checkout(folder:, revision:)
16+
puts decorated.to_json
17+
rescue FilesInMyDiff::Error => e
18+
puts e.message
19+
exit 1
20+
end

lib/files_in_my_diff/git/diff.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ def sha
1717
end
1818

1919
def changes
20-
@object.diff_parent.map do |change|
20+
parent = @object.parents.first
21+
return [] if parent.nil?
22+
23+
parent.diff(@object).map do |change|
2124
{ path: change.path, type: change.type }
2225
end
2326
end

lib/files_in_my_diff/resolver.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module FilesInMyDiff
44
class Resolver
5-
def initialize(folder:, revision:, file_strategy: TmpDir::FileStrategy, git_strategy: Git::Adapter.new(folder:))
5+
def initialize(folder:, revision:, file_strategy: TmpDir::FileStrategy, git_strategy: nil)
66
@folder = folder
77
@revision = revision
88
@file_strategy = file_strategy
@@ -11,19 +11,23 @@ def initialize(folder:, revision:, file_strategy: TmpDir::FileStrategy, git_stra
1111

1212
def call
1313
validate_folder!
14-
diff = @git_strategy.diff(@revision)
14+
diff = git_strategy.diff(@revision)
1515
diff.validate!
1616
rd = @file_strategy.revision_dir(diff.sha)
1717
rd.create!
18-
@git_strategy.checkout_worktree(rd.dir, diff.sha)
18+
git_strategy.checkout_worktree(rd.dir, diff.sha)
1919
rd.decorate(diff.changes)
2020
end
2121

2222
private
2323

2424
def validate_folder!
2525
raise ValidationError, 'Folder is required' if @folder.nil?
26-
raise ValidationError, 'Folder does not exist' unless @file_strategy.dir_exists?(@folder)
26+
raise ValidationError, "Folder '#{@folder}' does not exist" unless @file_strategy.dir_exists?(@folder)
27+
end
28+
29+
def git_strategy
30+
@git_strategy ||= Git::Adapter.new(folder: @folder)
2731
end
2832
end
2933
end

test/files_in_my_diff/git/diff_test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ def initialize(diff_success)
4242

4343
def sha = 'x'
4444

45-
def diff_parent
45+
def diff(_other)
4646
@diff_success ? [Change.new('file_1', :added)] : raise(Git::DiffError)
4747
end
48+
49+
def parents
50+
[self]
51+
end
4852
end
4953

5054
class Change

0 commit comments

Comments
 (0)