Skip to content

Commit

Permalink
Update cloned git repo remote if url changed
Browse files Browse the repository at this point in the history
If the URL of a `GitRepository` changes we can't know that our cloned
sparse repo is still valid so we should update the remote and refresh
  • Loading branch information
agrare committed Apr 3, 2024
1 parent 6330e2c commit af6ee0a
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/models/git_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class GitRepository < ApplicationRecord

has_many :git_branches, :dependent => :destroy
has_many :git_tags, :dependent => :destroy
after_update :broadcast_update_repo_remote, :if => proc { |object| object.previous_changes.include?('url') }
after_destroy :broadcast_repo_dir_delete

INFO_KEYS = %w[commit_sha commit_message commit_time name].freeze
Expand All @@ -25,6 +26,18 @@ def self.delete_repo_dir(id, directory_name)
FileUtils.rm_rf(directory_name)
end

def update_repo_remote
git_transaction do
if Dir.exist?(directory_name)
with_worktree do |worktree|
# TODO: add this to GitWorktree
worktree.instance_variable_get(:@repo).remotes.set_url("origin", url)
end
refresh
end
end
end

# Ping the git repository endpoint to verify that the network connection is still up.
# We use this approach for now over Rugged#check_connection because of a segfault:
#
Expand Down Expand Up @@ -279,6 +292,14 @@ def proxy_url
URI::Generic.build(uri_opts).to_s
end

def broadcast_update_repo_remote
MiqQueue.broadcast(
:class_name => self.class.name,
:instance_id => id,
:method_name => "update_repo_remote"
)
end

def broadcast_repo_dir_delete
MiqQueue.broadcast(
:class_name => self.class.name,
Expand Down

0 comments on commit af6ee0a

Please sign in to comment.