From 0143464915c73271dff4561a00a4587dcefd5ac9 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 30 Jul 2014 12:46:07 -0700 Subject: [PATCH] Add support for git tags as labels --- .../server/JGitEnvironmentRepository.java | 28 +++++++++++++++---- .../JGitEnvironmentRepositoryTests.java | 9 ++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/JGitEnvironmentRepository.java b/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/JGitEnvironmentRepository.java index 12954992..29a1785d 100644 --- a/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/JGitEnvironmentRepository.java +++ b/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/JGitEnvironmentRepository.java @@ -26,6 +26,8 @@ import org.apache.commons.logging.LogFactory; import org.eclipse.jgit.api.CheckoutCommand; import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.ListBranchCommand; +import org.eclipse.jgit.api.ListBranchCommand.ListMode; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.util.FileUtils; @@ -118,14 +120,16 @@ public class JGitEnvironmentRepository implements EnvironmentRepository { git.fetch().call(); git.getRepository().getConfig().setString("branch", label, "merge", label); CheckoutCommand checkout = git.checkout(); - if (!containsBranch(git, label)) { + if (isBranch(git, label) && !isLocalBranch(git, label)) { trackBranch(git, checkout, label); } else { + // works for tags and local branches checkout.setName(label); } - checkout.call(); - if (git.status().call().isClean()) { + Ref ref = checkout.call(); + if (git.status().call().isClean() && ref!=null) { + // Assumes we are on a tracking branch (should be safe) git.pull().call(); } String search = git.getRepository().getDirectory().getParent(); @@ -145,10 +149,22 @@ public class JGitEnvironmentRepository implements EnvironmentRepository { .setStartPoint("origin/" + label); } - private boolean containsBranch(Git git, String label) throws GitAPIException { - List branches = git.branchList().call(); + private boolean isBranch(Git git, String label) throws GitAPIException { + return containsBranch(git, label, ListMode.ALL); + } + + private boolean isLocalBranch(Git git, String label) throws GitAPIException { + return containsBranch(git, label, null); + } + + private boolean containsBranch(Git git, String label, ListMode listMode) throws GitAPIException { + ListBranchCommand command = git.branchList(); + if (listMode!=null) { + command.setListMode(listMode); + } + List branches = command.call(); for (Ref ref : branches) { - if (ref.getName().equals("refs/heads/" + label)) { + if (ref.getName().endsWith("/" + label)) { return true; } } diff --git a/spring-platform-config-server/src/test/java/org/springframework/platform/config/server/JGitEnvironmentRepositoryTests.java b/spring-platform-config-server/src/test/java/org/springframework/platform/config/server/JGitEnvironmentRepositoryTests.java index 1fe55798..6fa5fb57 100644 --- a/spring-platform-config-server/src/test/java/org/springframework/platform/config/server/JGitEnvironmentRepositoryTests.java +++ b/spring-platform-config-server/src/test/java/org/springframework/platform/config/server/JGitEnvironmentRepositoryTests.java @@ -76,6 +76,15 @@ public class JGitEnvironmentRepositoryTests { environment.getPropertySources().get(0).getName()); } + @Test + public void tag() { + repository.setBasedir(basedir); + Environment environment = repository.findOne("bar", "staging", "foo"); + assertEquals(2, environment.getPropertySources().size()); + assertEquals(repository.getUri() + "/bar.properties", + environment.getPropertySources().get(0).getName()); + } + @Test public void basedir() { repository.setBasedir(basedir);