From f00d6c6d45425dd2c98c7df5ce1cc2444bd8b20a Mon Sep 17 00:00:00 2001 From: f Date: Tue, 31 Oct 2023 13:54:19 -0300 Subject: [PATCH] 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