Skip to content

Commit

Permalink
Catch TreeErrors that are thrown in a different place than expected now
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacall committed Dec 18, 2024
1 parent 5ecee64 commit 432d401
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/controllers/git_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def render_path_not_found_error(ex)
end

def render_invalid_path_error(ex)
render_git_error("Invalid path: #{ex.path}", status: 422)
render_git_error("Invalid path#{ex.path ? ": #{ex.path}" : ""}", status: 422)
end

def render_invalid_url_error(ex)
Expand Down
36 changes: 22 additions & 14 deletions lib/git/operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,25 @@ def in_temp_dir

def add_file(path, io, message: nil)
message ||= "#{file_exists?(path) ? 'Updated' : 'Added'} #{path}"
perform_commit(message) do |index|
write_blob(index, path, io)
begin
perform_commit(message) do |index|
write_blob(index, path, io)
end
rescue Rugged::TreeError
raise Git::InvalidPathException.new(path: path)
end
end

def add_files(path_io_pairs, message: nil)
message ||= "Added/updated #{path_io_pairs.count} files"
perform_commit(message) do |index|
path_io_pairs.each do |path, io|
write_blob(index, path, io)
begin
perform_commit(message) do |index|
path_io_pairs.each do |path, io|
write_blob(index, path, io)
end
end
rescue Rugged::TreeError
raise Git::InvalidPathException.new
end
end

Expand All @@ -120,19 +128,19 @@ def remove_file(path, update_annotations: true)
def move_file(oldpath, newpath, update_annotations: true)
raise Git::PathNotFoundException.new(path: oldpath) unless file_exists?(oldpath)

c = perform_commit("Moved #{oldpath} -> #{newpath}") do |index|
existing = index[oldpath]
begin
begin
c = perform_commit("Moved #{oldpath} -> #{newpath}") do |index|
existing = index[oldpath]
index.add(path: newpath, oid: existing[:oid], mode: 0100644)
rescue Rugged::IndexError
raise Git::InvalidPathException.new(path: newpath)
index.remove(oldpath)
end
index.remove(oldpath)
end

git_annotations.where(path: oldpath).update_all(path: newpath) if respond_to?(:git_annotations) && update_annotations
git_annotations.where(path: oldpath).update_all(path: newpath) if respond_to?(:git_annotations) && update_annotations

c
c
rescue Rugged::TreeError
raise Git::InvalidPathException.new(path: newpath)
end
end

private
Expand Down

0 comments on commit 432d401

Please sign in to comment.