From 9ec0dc87a9426a3a101416e5044b40d8e274f08e Mon Sep 17 00:00:00 2001 From: f Date: Tue, 31 Oct 2023 13:53:45 -0300 Subject: [PATCH 1/4] fix: hacer un merge cuando hay cambios #14533 --- app/jobs/git_pull_job.rb | 6 +++++- app/models/site/repository.rb | 9 +++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/jobs/git_pull_job.rb b/app/jobs/git_pull_job.rb index dc4a285c..c7063650 100644 --- a/app/jobs/git_pull_job.rb +++ b/app/jobs/git_pull_job.rb @@ -8,6 +8,10 @@ class GitPullJob < ApplicationJob # @param :message [String] # @return [nil] def perform(site, usuarie, message) - site.repository.merge(usuarie, message) if site.repository.fetch&.positive? + site.repository.fetch + + return if site.repository.up_to_date? + + site.repository.merge(usuarie, message) end end \ No newline at end of file diff --git a/app/models/site/repository.rb b/app/models/site/repository.rb index acbf6553..17ee0792 100644 --- a/app/models/site/repository.rb +++ b/app/models/site/repository.rb @@ -111,10 +111,11 @@ class Site walker.each.to_a end - # Hay commits sin aplicar? - def needs_pull? - fetch - !commits.empty? + # Detecta si hay que hacer un pull o no + # + # @return [Boolean] + def up_to_date? + rugged.merge_analysis(remote_head_commit).include?(:up_to_date) end # Guarda los cambios en git From f00d6c6d45425dd2c98c7df5ce1cc2444bd8b20a Mon Sep 17 00:00:00 2001 From: f Date: Tue, 31 Oct 2023 13:54:19 -0300 Subject: [PATCH 2/4] feat: hacer fast forward si no hay cambios locales #14533 --- app/jobs/git_pull_job.rb | 27 +++++++++++++++++---------- app/models/site/repository.rb | 32 ++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/app/jobs/git_pull_job.rb b/app/jobs/git_pull_job.rb index c7063650..77f75ba9 100644 --- a/app/jobs/git_pull_job.rb +++ b/app/jobs/git_pull_job.rb @@ -1,17 +1,24 @@ # frozen_string_literal: true -# Permite traer los cambios desde webhooks - +# Permite traer los cambios desde el repositorio remoto class GitPullJob < ApplicationJob - # @param :site [Site] - # @param :usuarie [Usuarie] - # @param :message [String] - # @return [nil] - def perform(site, usuarie, message) - site.repository.fetch + # @param :site [Site] + # @param :usuarie [Usuarie] + # @param :message [String] + # @return [nil] + def perform(site, usuarie, message) + site.repository.fetch - return if site.repository.up_to_date? + return if site.repository.up_to_date? + if site.repository.fast_forward? + site.repository.fast_forward! + else site.repository.merge(usuarie, message) end -end \ No newline at end of file + + git_lfs_checkout + + nil + end +end diff --git a/app/models/site/repository.rb b/app/models/site/repository.rb index 17ee0792..e77aded9 100644 --- a/app/models/site/repository.rb +++ b/app/models/site/repository.rb @@ -75,13 +75,18 @@ class Site # Forzamos el checkout para mover el HEAD al Ășltimo commit y # escribir los cambios rugged.checkout 'HEAD', strategy: :force - - git_sh("git", "lfs", "fetch", "origin", default_branch) - # reemplaza los pointers por los archivos correspondientes - git_sh("git", "lfs", "checkout") + commit end + # Trae todos los archivos desde LFS + # + # @return [Boolean] + def git_lfs_checkout + git_sh('git', 'lfs', 'fetch', 'origin', default_branch) + git_sh('git', 'lfs', 'checkout') + end + # El Ășltimo commit # # @return [Rugged::Commit] @@ -118,6 +123,25 @@ class Site rugged.merge_analysis(remote_head_commit).include?(:up_to_date) end + # Detecta si es posible adelantar la historia local a la remota o + # necesitamos un merge + # + # @return [Boolean] + def fast_forward? + rugged.merge_analysis(remote_head_commit).include?(:fastforward) + end + + # Mueve la historia local a la remota + # + # @see {https://stackoverflow.com/a/27077322} + # @return [nil] + def fast_forward! + rugged.checkout_tree(remote_head_commit) + rugged.references.update(rugged.head.resolve, remote_head_commit.oid) + + nil + end + # Guarda los cambios en git # # @param :add [Array] Archivos a agregar From 6ef83624a174ebf0bdc3548b7116932b34da9e69 Mon Sep 17 00:00:00 2001 From: f Date: Tue, 31 Oct 2023 13:59:11 -0300 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20era=20un=20m=C3=A9todo=20del=20sitio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/git_pull_job.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/jobs/git_pull_job.rb b/app/jobs/git_pull_job.rb index 77f75ba9..49784f50 100644 --- a/app/jobs/git_pull_job.rb +++ b/app/jobs/git_pull_job.rb @@ -17,7 +17,7 @@ class GitPullJob < ApplicationJob site.repository.merge(usuarie, message) end - git_lfs_checkout + site.git_lfs_checkout nil end From 3193d72486bb37b95250c7180bf5ebfacd2f4ddf Mon Sep 17 00:00:00 2001 From: f Date: Tue, 31 Oct 2023 13:59:59 -0300 Subject: [PATCH 4/4] fix: era un metodo del repositorio! --- app/jobs/git_pull_job.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/jobs/git_pull_job.rb b/app/jobs/git_pull_job.rb index 49784f50..271ad91a 100644 --- a/app/jobs/git_pull_job.rb +++ b/app/jobs/git_pull_job.rb @@ -17,7 +17,7 @@ class GitPullJob < ApplicationJob site.repository.merge(usuarie, message) end - site.git_lfs_checkout + site.repository.git_lfs_checkout nil end