From 70f5d4f0012847ff543ff63d4dc2b722d7f395c3 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 16 Apr 2021 10:27:37 -0300 Subject: [PATCH 1/4] no asumir que la rama principal se llama master MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit la estábamos hardcodeando, este cambio permite descubrir el nombre de la rama actual. el efecto secundario es que podemos abrir ramas manualmente en el repositorio (desde terminal) y sutty va a respetar eso (no sé por qué lo haríamos, pero está la posibilidad). --- app/models/site/repository.rb | 52 +++++++++++++++++++++-------- test/models/site/repository_test.rb | 25 ++++++++++++-- 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/app/models/site/repository.rb b/app/models/site/repository.rb index d63e331..7cc6435 100644 --- a/app/models/site/repository.rb +++ b/app/models/site/repository.rb @@ -14,8 +14,26 @@ class Site @changes = 0 end - def remote - @remote ||= rugged.remotes.first + # Obtiene la rama por defecto a partir de la referencia actual + # + # Por ejemplo "refs/heads/no-master" => "no-master" + # + # XXX: No memoizamos para obtener siempre el nombre de la rama + # actual, aunque por ahora asumimos que siempre estamos en la misma, + # internamente (ej. vía shell) podríamos cambiarla. + # + # @return [String] + def default_branch + rugged.head.canonical_name.split('/', 3).last + end + + # Obtiene el origin + # + # @return [Rugged::Remote] + def origin + @origin ||= rugged.remotes.find do |remote| + remote.name == 'origin' + end end # Trae los cambios del repositorio de origen sin aplicarlos y @@ -24,8 +42,8 @@ class Site # XXX: Prestar atención a la velocidad de respuesta cuando tengamos # repositorios remotos. def fetch - if remote.check_connection :fetch - @changes = rugged.fetch(remote)[:received_objects] + if origin.check_connection :fetch + @changes = rugged.fetch(origin)[:received_objects] else 0 end @@ -34,7 +52,7 @@ class Site # Incorpora los cambios en el repositorio actual # def merge(usuarie) - merge = rugged.merge_commits(master, origin_master) + merge = rugged.merge_commits(head_commit, remote_head_commit) # No hacemos nada si hay conflictos, pero notificarnos begin @@ -46,7 +64,7 @@ class Site commit = Rugged::Commit .create(rugged, update_ref: 'HEAD', - parents: [master, origin_master], + parents: [head_commit, remote_head_commit], tree: merge.write_tree(rugged), message: I18n.t('sites.fetch.merge.message'), author: author(usuarie), committer: committer) @@ -57,12 +75,20 @@ class Site commit end - def master - rugged.branches['master'].target + # El último commit + # + # @return [Rugged::Commit] + def head_commit + rugged.branches[default_branch].target end - def origin_master - rugged.branches['origin/master'].target + # El último commit del repositorio remoto + # + # XXX: Realmente no recuerdo por qué esto era necesario ~f + # + # @return [Rugged::Commit] + def remote_head_commit + rugged.branches["origin/#{default_branch}"].target end # Compara los commits entre el repositorio remoto y el actual para @@ -72,10 +98,8 @@ class Site # Obtenemos todos los commits que existen en origin/master que no # están en la rama master local - # - # XXX: monitorear esto por performance - walker.push 'refs/remotes/origin/master' - walker.hide 'refs/heads/master' + walker.push "refs/remotes/origin/#{default_branch}" + walker.hide "refs/heads/#{default_branch}" walker.each.to_a end diff --git a/test/models/site/repository_test.rb b/test/models/site/repository_test.rb index 39da869..52f7c79 100644 --- a/test/models/site/repository_test.rb +++ b/test/models/site/repository_test.rb @@ -18,6 +18,28 @@ class RepositoryTest < ActiveSupport::TestCase @site.destroy end + test 'se puede obtener la rama por defecto' do + assert_equal 'master', @site.repository.default_branch + + random_branch = SecureRandom.hex + + Dir.chdir(@site.path) do + `git checkout -b #{random_branch} >/dev/null 2>&1` + end + + assert_equal random_branch, @site.repository.default_branch + end + + test 'los nombres de las ramas pueden tener /' do + random_branch = ([SecureRandom.hex] * 2).join('/') + + Dir.chdir(@site.path) do + `git checkout -b #{random_branch} >/dev/null 2>&1` + end + + assert_equal random_branch, @site.repository.default_branch + end + test 'se pueden traer cambios' do assert @site.repository.fetch.is_a?(Integer) end @@ -28,8 +50,7 @@ class RepositoryTest < ActiveSupport::TestCase assert @site.repository.commits.empty? assert_equal @usuarie.name, - @site.repository.rugged - .branches['master'].target.author[:name] + @site.repository.rugged.branches[@site.repository.default_branch].target.author[:name] Dir.chdir(@site.path) do FileUtils.rm 'migration.csv' From d3174c6e3f2c6dbb2ccb355bab82e895cab3bc1a Mon Sep 17 00:00:00 2001 From: f Date: Fri, 16 Apr 2021 10:29:17 -0300 Subject: [PATCH 2/4] arreglar un test --- test/models/site/repository_test.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/models/site/repository_test.rb b/test/models/site/repository_test.rb index 52f7c79..6328a0d 100644 --- a/test/models/site/repository_test.rb +++ b/test/models/site/repository_test.rb @@ -40,8 +40,10 @@ class RepositoryTest < ActiveSupport::TestCase assert_equal random_branch, @site.repository.default_branch end + # XXX: En realidad el git-reset no elimina la caché de fetch entonces + # nunca tenemos valores mayores a cero. test 'se pueden traer cambios' do - assert @site.repository.fetch.is_a?(Integer) + assert @site.repository.fetch.zero? end test 'se pueden mergear los cambios' do From 8fc054a735557a5d58d746504b5cf4ae87ddc3d6 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 16 Apr 2021 10:36:18 -0300 Subject: [PATCH 3/4] =?UTF-8?q?documentaci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #1094 closes #1093 closes #1089 closes #1054 --- app/models/site/repository.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/site/repository.rb b/app/models/site/repository.rb index 7cc6435..a72c64c 100644 --- a/app/models/site/repository.rb +++ b/app/models/site/repository.rb @@ -8,6 +8,7 @@ class Site class Repository attr_reader :rugged, :changes, :path + # @param [String] la ruta del repositorio def initialize(path) @path = path @rugged = Rugged::Repository.new(path) @@ -41,6 +42,8 @@ class Site # # XXX: Prestar atención a la velocidad de respuesta cuando tengamos # repositorios remotos. + # + # @return [Integer] def fetch if origin.check_connection :fetch @changes = rugged.fetch(origin)[:received_objects] @@ -51,6 +54,7 @@ class Site # Incorpora los cambios en el repositorio actual # + # @return [Rugged::Commit] def merge(usuarie) merge = rugged.merge_commits(head_commit, remote_head_commit) From 6e9b09e19bca88ecdce1a331f2d7968e5208341c Mon Sep 17 00:00:00 2001 From: f Date: Fri, 16 Apr 2021 10:36:38 -0300 Subject: [PATCH 4/4] deprecar un atributo que nunca usamos --- app/models/site/repository.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/models/site/repository.rb b/app/models/site/repository.rb index a72c64c..d969522 100644 --- a/app/models/site/repository.rb +++ b/app/models/site/repository.rb @@ -6,13 +6,12 @@ class Site # que un sitio tiene un solo origen, que siempre se trabaja con la # rama master, etc. class Repository - attr_reader :rugged, :changes, :path + attr_reader :rugged, :path # @param [String] la ruta del repositorio def initialize(path) @path = path @rugged = Rugged::Repository.new(path) - @changes = 0 end # Obtiene la rama por defecto a partir de la referencia actual @@ -46,7 +45,7 @@ class Site # @return [Integer] def fetch if origin.check_connection :fetch - @changes = rugged.fetch(origin)[:received_objects] + rugged.fetch(origin)[:received_objects] else 0 end