[why] When upstream-repos' url has changed
https://git.archlinux.org/linux ==> https://mirrors.tuna.tsinghua.edu.cn/git/linux.git
The bare git repo's remote url has not changed together.
/srv/git/l/linux/linux.git% git ls-remote --get-url origin https://git.archlinux.org/linux
Because the original logic only git fetch when git repo is existed. Now add check for git url. If git url has changed, rm the original git repo and clone again.
Signed-off-by: Lin Jiaxin ljx.joe@qq.com --- lib/git_mirror.rb | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/lib/git_mirror.rb b/lib/git_mirror.rb index 13cb7c0..84a908b 100644 --- a/lib/git_mirror.rb +++ b/lib/git_mirror.rb @@ -63,16 +63,29 @@ class GitMirror return fetch_info.include? '->' end
+ def git_url_check(url, mirror_dir) + url = get_url(Array(url)[0]) + git_url = %x(git -C #{mirror_dir} ls-remote --get-url origin).chomp + + return true if url == git_url + + return false + end + + def git_repo_download(url, mirror_dir) + return git_clone(url, mirror_dir) unless File.directory?(mirror_dir) + + return git_fetch(mirror_dir) if git_url_check(url, mirror_dir) + + FileUtils.rm_r(mirror_dir) + return git_clone(url, mirror_dir) + end + def mirror_sync fork_info = @queue.pop mirror_dir = "/srv/git/#{fork_info['git_repo']}" mirror_dir = "#{mirror_dir}.git" unless fork_info['is_submodule'] - possible_new_refs = false - if File.directory?(mirror_dir) - possible_new_refs = git_fetch(mirror_dir) - else - possible_new_refs = git_clone(fork_info['url'], mirror_dir) - end + possible_new_refs = git_repo_download(fork_info['url'], mirror_dir) feedback(fork_info['git_repo'], possible_new_refs) end