Skip to content

The Unified and Autonomous Development Platform

Git tag and branch operation.

robin shine942c55808d3e
diff --git a/commons.git/src/main/java/com/pmease/commons/git/Git.java b/commons.git/src/main/java/com/pmease/commons/git/Git.java
index 52e029022d..28eec46029 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/Git.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/Git.java
@@ -57,10 +57,14 @@ public class Git {
return new InitCommand(this);
}
- public ListBranchCommand listBranch() {
- return new ListBranchCommand(this);
+ public ListBranchesCommand listBranches() {
+ return new ListBranchesCommand(this);
}
+ public ListTagsCommand listTags() {
+ return new ListTagsCommand(this);
+ }
+
public GetCommitCommand getCommit() {
return new GetCommitCommand(this);
}
@@ -77,12 +81,12 @@ public class Git {
return new CommitCommand(this);
}
- public FindChangedFilesCommand findChangedFiles() {
- return new FindChangedFilesCommand(this);
+ public ListChangedFilesCommand listChangedFiles() {
+ return new ListChangedFilesCommand(this);
}
- public FindFilesCommand findFiles() {
- return new FindFilesCommand(this);
+ public ListFilesCommand listFiles() {
+ return new ListFilesCommand(this);
}
public CheckAncestorCommand checkAncestor() {
diff --git a/commons.git/src/main/java/com/pmease/commons/git/ListBranchCommand.java b/commons.git/src/main/java/com/pmease/commons/git/ListBranchesCommand.java
similarity index 86%
rename from commons.git/src/main/java/com/pmease/commons/git/ListBranchCommand.java
rename to commons.git/src/main/java/com/pmease/commons/git/ListBranchesCommand.java
index 3ff0694e35..7ac6655dd5 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/ListBranchCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/ListBranchesCommand.java
@@ -8,9 +8,9 @@ import org.apache.commons.lang3.StringUtils;
import com.pmease.commons.util.execution.Commandline;
import com.pmease.commons.util.execution.LineConsumer;
-public class ListBranchCommand extends GitCommand<Collection<String>> {
+public class ListBranchesCommand extends GitCommand<Collection<String>> {
- public ListBranchCommand(Git git) {
+ public ListBranchesCommand(Git git) {
super(git);
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/FindChangedFilesCommand.java b/commons.git/src/main/java/com/pmease/commons/git/ListChangedFilesCommand.java
similarity index 81%
rename from commons.git/src/main/java/com/pmease/commons/git/FindChangedFilesCommand.java
rename to commons.git/src/main/java/com/pmease/commons/git/ListChangedFilesCommand.java
index 8ffa045a5a..a1e3294da5 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/FindChangedFilesCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/ListChangedFilesCommand.java
@@ -8,22 +8,22 @@ import com.google.common.base.Preconditions;
import com.pmease.commons.util.execution.Commandline;
import com.pmease.commons.util.execution.LineConsumer;
-public class FindChangedFilesCommand extends GitCommand<Collection<String>> {
+public class ListChangedFilesCommand extends GitCommand<Collection<String>> {
private String fromRev;
private String toRev;
- public FindChangedFilesCommand(final Git git) {
+ public ListChangedFilesCommand(final Git git) {
super(git);
}
- public FindChangedFilesCommand fromRev(final String fromRev) {
+ public ListChangedFilesCommand fromRev(final String fromRev) {
this.fromRev = fromRev;
return this;
}
- public FindChangedFilesCommand toRev(final String toRev) {
+ public ListChangedFilesCommand toRev(final String toRev) {
this.toRev = toRev;
return this;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/FindFilesCommand.java b/commons.git/src/main/java/com/pmease/commons/git/ListFilesCommand.java
similarity index 84%
rename from commons.git/src/main/java/com/pmease/commons/git/FindFilesCommand.java
rename to commons.git/src/main/java/com/pmease/commons/git/ListFilesCommand.java
index e164bff85d..908887008d 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/FindFilesCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/ListFilesCommand.java
@@ -8,15 +8,15 @@ import com.google.common.base.Preconditions;
import com.pmease.commons.util.execution.Commandline;
import com.pmease.commons.util.execution.LineConsumer;
-public class FindFilesCommand extends GitCommand<Collection<String>> {
+public class ListFilesCommand extends GitCommand<Collection<String>> {
private String revision;
- public FindFilesCommand(final Git git) {
+ public ListFilesCommand(final Git git) {
super(git);
}
- public FindFilesCommand revision(final String revision) {
+ public ListFilesCommand revision(final String revision) {
this.revision = revision;
return this;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/ListTagsCommand.java b/commons.git/src/main/java/com/pmease/commons/git/ListTagsCommand.java
new file mode 100644
index 0000000000..f31077499a
--- /dev/null
+++ b/commons.git/src/main/java/com/pmease/commons/git/ListTagsCommand.java
@@ -0,0 +1,36 @@
+package com.pmease.commons.git;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.apache.commons.lang3.StringUtils;
+
+import com.pmease.commons.util.execution.Commandline;
+import com.pmease.commons.util.execution.LineConsumer;
+
+public class ListTagsCommand extends GitCommand<Collection<String>> {
+
+ public ListTagsCommand(Git git) {
+ super(git);
+ }
+
+ @Override
+ public Collection<String> call() {
+ Commandline cmd = git().cmd();
+ cmd.addArgs("tag");
+
+ final Collection<String> branches = new ArrayList<String>();
+
+ cmd.execute(new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ branches.add(StringUtils.stripStart(line, "*").trim());
+ }
+
+ }, errorLogger());
+
+ return branches;
+ }
+
+}
diff --git a/commons.git/src/test/java/com/pmease/commons/git/ListChangedFilesCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/ListChangedFilesCommandTest.java
index 0d3f1998bf..0de1c109ff 100644
--- a/commons.git/src/test/java/com/pmease/commons/git/ListChangedFilesCommandTest.java
+++ b/commons.git/src/test/java/com/pmease/commons/git/ListChangedFilesCommandTest.java
@@ -37,7 +37,7 @@ public class ListChangedFilesCommandTest {
git.add().addPath("a").call();
git.commit().message("commit").call();
- Collection<String> changedFiles = git.findChangedFiles().fromRev("HEAD~4").toRev("HEAD").call();
+ Collection<String> changedFiles = git.listChangedFiles().fromRev("HEAD~4").toRev("HEAD").call();
Assert.assertTrue(changedFiles.containsAll(Sets.newHashSet("a", "b", "c", "d")));
} finally {
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/ApprovedByAuthorizedUsers.java b/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/ApprovedByAuthorizedUsers.java
index 2bf9a74910..fa93466b9b 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/ApprovedByAuthorizedUsers.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/ApprovedByAuthorizedUsers.java
@@ -30,7 +30,7 @@ public class ApprovedByAuthorizedUsers extends AbstractGateKeeper {
@Override
public CheckResult check(MergeRequest request) {
- Collection<User> authorizedUsers = request.getTarget().getProject().findAuthorizedUsers(GeneralOperation.WRITE);
+ Collection<User> authorizedUsers = request.getTarget().getProject().listAuthorizedUsers(GeneralOperation.WRITE);
int approvals = 0;
int pendings = 0;
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/TouchSpecifiedFiles.java b/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/TouchSpecifiedFiles.java
index 9090dfa28e..a515f6d050 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/TouchSpecifiedFiles.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/TouchSpecifiedFiles.java
@@ -6,8 +6,8 @@ import java.util.Collection;
import org.hibernate.validator.constraints.NotEmpty;
import com.pmease.commons.editable.annotation.Editable;
-import com.pmease.commons.git.FindChangedFilesCommand;
-import com.pmease.commons.git.FindFilesCommand;
+import com.pmease.commons.git.ListChangedFilesCommand;
+import com.pmease.commons.git.ListFilesCommand;
import com.pmease.commons.git.Git;
import com.pmease.commons.loader.AppLoader;
import com.pmease.commons.util.pattern.WildcardUtils;
@@ -42,17 +42,17 @@ public class TouchSpecifiedFiles extends AbstractGateKeeper {
Collection<String> touchedFiles;
if (i == request.getEffectiveUpdates().size()-1) {
if (request.getMergeBase() == null) {
- FindFilesCommand command = new Git(repoDir).findFiles();
+ ListFilesCommand command = new Git(repoDir).listFiles();
command.revision(update.getRefName());
touchedFiles = command.call();
} else {
- FindChangedFilesCommand command = new Git(repoDir).findChangedFiles();
+ ListChangedFilesCommand command = new Git(repoDir).listChangedFiles();
command.fromRev(request.getMergeBase());
command.toRev(update.getRefName());
touchedFiles = command.call();
}
} else {
- FindChangedFilesCommand command = new Git(repoDir).findChangedFiles();
+ ListChangedFilesCommand command = new Git(repoDir).listChangedFiles();
command.fromRev(request.getEffectiveUpdates().get(i+1).getRefName());
command.toRev(update.getRefName());
touchedFiles = command.call();
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/PostReceiveServlet.java b/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/PostReceiveServlet.java
index 472707973c..b3fbb48863 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/PostReceiveServlet.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/PostReceiveServlet.java
@@ -51,7 +51,7 @@ public class PostReceiveServlet extends CallbackServlet {
logger.info("Executing post-receive hook against branch {}...", branchName);
- Branch branch = branchManager.find(project, branchName, true);
+ Branch branch = branchManager.find(project, branchName);
User user = User.getCurrent();
Preconditions.checkNotNull(user, "User pushing commits is unknown.");
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/BranchManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/BranchManager.java
index 5f85571c02..1fc9cca82c 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/BranchManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/BranchManager.java
@@ -1,7 +1,5 @@
package com.pmease.gitop.core.manager;
-import java.util.Collection;
-
import com.google.inject.ImplementedBy;
import com.pmease.commons.hibernate.dao.GenericDao;
import com.pmease.commons.util.namedentity.EntityLoader;
@@ -16,17 +14,6 @@ public interface BranchManager extends GenericDao<Branch> {
public Branch find(Project project, String branchName, boolean createIfNotExist);
- /**
- * Find all branches of specified project from repository. This method will add/remove
- * branches in database to make sure they match with branches stored in repository.
- *
- * @param project
- * project to find branches of
- * @return
- * collection of found branches
- */
- public Collection<Branch> findBranches(Project project);
-
public EntityLoader asEntityLoader(Project project);
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultBranchManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultBranchManager.java
index 959245f3e4..0abe817a68 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultBranchManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultBranchManager.java
@@ -1,20 +1,11 @@
package com.pmease.gitop.core.manager.impl;
-import java.io.File;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
import javax.inject.Inject;
import javax.inject.Singleton;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
-import com.pmease.commons.git.Git;
-import com.pmease.commons.git.ListBranchCommand;
import com.pmease.commons.hibernate.Sessional;
import com.pmease.commons.hibernate.Transactional;
import com.pmease.commons.hibernate.dao.AbstractGenericDao;
@@ -22,19 +13,15 @@ import com.pmease.commons.hibernate.dao.GeneralDao;
import com.pmease.commons.util.namedentity.EntityLoader;
import com.pmease.commons.util.namedentity.NamedEntity;
import com.pmease.gitop.core.manager.BranchManager;
-import com.pmease.gitop.core.manager.StorageManager;
import com.pmease.gitop.core.model.Branch;
import com.pmease.gitop.core.model.Project;
@Singleton
public class DefaultBranchManager extends AbstractGenericDao<Branch> implements BranchManager {
- private final StorageManager storageManager;
-
@Inject
- public DefaultBranchManager(GeneralDao generalDao, StorageManager storageManager) {
+ public DefaultBranchManager(GeneralDao generalDao) {
super(generalDao);
- this.storageManager = storageManager;
}
@Sessional
@@ -56,7 +43,7 @@ public class DefaultBranchManager extends AbstractGenericDao<Branch> implements
return branch;
}
- @Override
+ @Override
public EntityLoader asEntityLoader(final Project project) {
return new EntityLoader() {
@@ -107,42 +94,4 @@ public class DefaultBranchManager extends AbstractGenericDao<Branch> implements
};
}
- private void addBranch(Project project, String branchName) {
- Branch branch = new Branch();
- branch.setName(branchName);
- branch.setProject(project);
- project.getBranches().add(branch);
- save(branch);
- }
-
- @Transactional
- @Override
- public Collection<Branch> findBranches(Project project) {
- File codeDir = storageManager.getStorage(project).ofCode();
- ListBranchCommand cmd = new ListBranchCommand(new Git(codeDir));
-
- Collection<String> branchNamesInRepo = cmd.call();
-
- Set<String> branchNamesInDB = new HashSet<String>();
-
- List<Branch> deletedBranches = new ArrayList<Branch>();
- for (Branch branch: project.getBranches()) {
- branchNamesInDB.add(branch.getName());
-
- if (!branchNamesInRepo.contains(branch.getName())) {
- delete(branch);
- deletedBranches.add(branch);
- }
- }
-
- project.getBranches().removeAll(deletedBranches);
-
- for (String branchName: branchNamesInRepo) {
- if (!branchNamesInDB.contains(branchName)) {
- addBranch(project, branchName);
- }
- }
-
- return project.getBranches();
- }
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/model/MergeRequest.java b/gitop.core/src/main/java/com/pmease/gitop/core/model/MergeRequest.java
index eaa767499d..198fbc4eea 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/model/MergeRequest.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/model/MergeRequest.java
@@ -22,7 +22,7 @@ import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.pmease.commons.git.CalcMergeBaseCommand;
import com.pmease.commons.git.CheckAncestorCommand;
-import com.pmease.commons.git.FindChangedFilesCommand;
+import com.pmease.commons.git.ListChangedFilesCommand;
import com.pmease.commons.git.Git;
import com.pmease.commons.hibernate.AbstractEntity;
import com.pmease.gitop.core.Gitop;
@@ -290,7 +290,7 @@ public class MergeRequest extends AbstractEntity {
File repoDir = storageManager.getStorage(getTarget().getProject()).ofCode();
MergeRequestUpdate update = getLatestUpdate();
if (update != null) {
- FindChangedFilesCommand command = new Git(repoDir).findChangedFiles();
+ ListChangedFilesCommand command = new Git(repoDir).listChangedFiles();
command.fromRev(getTarget().getName());
command.toRev(update.getRefName());
return command.call();
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/model/Project.java b/gitop.core/src/main/java/com/pmease/gitop/core/model/Project.java
index 8eb8558e6c..f761e719b5 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/model/Project.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/model/Project.java
@@ -1,5 +1,6 @@
package com.pmease.gitop.core.model;
+import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
@@ -19,11 +20,16 @@ import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import com.pmease.commons.editable.annotation.Editable;
+import com.pmease.commons.git.Git;
+import com.pmease.commons.git.ListBranchesCommand;
+import com.pmease.commons.git.ListTagsCommand;
import com.pmease.commons.hibernate.AbstractEntity;
+import com.pmease.commons.util.GeneralException;
import com.pmease.gitop.core.Gitop;
import com.pmease.gitop.core.gatekeeper.AlwaysAccept;
import com.pmease.gitop.core.gatekeeper.GateKeeper;
import com.pmease.gitop.core.manager.BranchManager;
+import com.pmease.gitop.core.manager.StorageManager;
import com.pmease.gitop.core.manager.UserManager;
import com.pmease.gitop.core.permission.ObjectPermission;
import com.pmease.gitop.core.permission.object.ProtectedObject;
@@ -197,7 +203,7 @@ public class Project extends AbstractEntity implements UserBelonging {
/**
* Get branches for this project from database. The result might be
* different from actual branches in repository. To get actual
- * branches in repository, call {@link BranchManager#findBranches(Project)}
+ * branches in repository, call {@link BranchManager#listBranches(Project)}
* instead.
*
* @return
@@ -221,7 +227,7 @@ public class Project extends AbstractEntity implements UserBelonging {
}
}
- public Collection<User> findAuthorizedUsers(GeneralOperation operation) {
+ public Collection<User> listAuthorizedUsers(GeneralOperation operation) {
Set<User> authorizedUsers = new HashSet<User>();
for (User user: Gitop.getInstance(UserManager.class).query()) {
if (user.asSubject().isPermitted(new ObjectPermission(this, operation)))
@@ -230,8 +236,53 @@ public class Project extends AbstractEntity implements UserBelonging {
return authorizedUsers;
}
+ public Collection<String> listBranches() {
+ File codeDir = Gitop.getInstance(StorageManager.class).getStorage(this).ofCode();
+ ListBranchesCommand cmd = new ListBranchesCommand(new Git(codeDir));
+
+ return cmd.call();
+ }
+
+ public Collection<String> listTags() {
+ File codeDir = Gitop.getInstance(StorageManager.class).getStorage(this).ofCode();
+ ListTagsCommand cmd = new ListTagsCommand(new Git(codeDir));
+
+ return cmd.call();
+ }
+
+ public void createBranch(String branchName, String commitHash) {
+ File codeDir = Gitop.getInstance(StorageManager.class).getStorage(this).ofCode();
+ Git git = new Git(codeDir);
+ if (git.listBranches().call().contains(branchName))
+ throw new GeneralException("Branch %s already exists.", branchName);
+
+ git.updateRef().refName("refs/heads/" + branchName).revision(commitHash).call();
+ }
+
+ public void deleteBranch(String branchName) {
+ File codeDir = Gitop.getInstance(StorageManager.class).getStorage(this).ofCode();
+ Git git = new Git(codeDir);
+ git.deleteRef().refName("refs/heads/" + branchName).call();
+ }
+
+ public void createTag(String tagName, String commitHash) {
+ File codeDir = Gitop.getInstance(StorageManager.class).getStorage(this).ofCode();
+ Git git = new Git(codeDir);
+ if (git.listTags().call().contains(tagName))
+ throw new GeneralException("Tag %s already exists.", tagName);
+
+ git.updateRef().refName("refs/tags/" + tagName).revision(commitHash).call();
+ }
+
+ public void deleteTag(String tagName) {
+ File codeDir = Gitop.getInstance(StorageManager.class).getStorage(this).ofCode();
+ Git git = new Git(codeDir);
+ git.deleteRef().refName("refs/tags/" + tagName).call();
+ }
+
@Override
public String toString() {
return getOwner() + "/" + getName();
}
+
}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/test/MergeRequestsPage.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/test/MergeRequestsPage.java
index 581e026709..419d63679e 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/test/MergeRequestsPage.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/test/MergeRequestsPage.java
@@ -41,7 +41,8 @@ public class MergeRequestsPage extends AbstractLayoutPage {
@Override
protected List<MergeRequest> load() {
List<MergeRequest> mergeRequests = new ArrayList<MergeRequest>();
- for (Branch branch: Gitop.getInstance(BranchManager.class).findBranches(getProject())) {
+ for (String branchName: getProject().listBranches()) {
+ Branch branch = Gitop.getInstance(BranchManager.class).find(getProject(), branchName, true);
for (MergeRequest request: branch.getIngoingRequests()) {
if (request.getStatus() != MergeRequest.Status.CLOSED && request.getLastCheckResult() != null)
mergeRequests.add(request);