Skip to content

The Unified and Autonomous Development Platform

Add symbolic ref name operation commands.

robin shine9e74756f5a12
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 b34bf5d0d8..dbc3a19277 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
@@ -38,7 +38,9 @@ import com.pmease.commons.git.command.ResetCommand;
import com.pmease.commons.git.command.ShowCommand;
import com.pmease.commons.git.command.ShowForConsumeCommand;
import com.pmease.commons.git.command.ShowRefCommand;
+import com.pmease.commons.git.command.ShowSymbolicRefCommand;
import com.pmease.commons.git.command.UpdateRefCommand;
+import com.pmease.commons.git.command.UpdateSymbolicRefCommand;
import com.pmease.commons.util.FileUtils;
import com.pmease.commons.util.GeneralException;
import com.pmease.commons.util.execution.StreamConsumer;
@@ -158,6 +160,34 @@ public class Git implements Serializable {
return this;
}
+ /**
+ * Update specified symbolic ref name with specified ref name.
+ *
+ * @param symbolicRefName
+ * symbolic ref name to be updated
+ * @param refName
+ * ref name to point to
+ * @param reason
+ * reason to update the symbolic ref
+ */
+ public Git updateSymbolicRef(String symbolicRefName, String refName, @Nullable String reason) {
+ new UpdateSymbolicRefCommand(repoDir).symbolicRefName(symbolicRefName).refName(refName)
+ .reason(reason).call();
+ return this;
+ }
+
+ /**
+ * Show ref name for specified symbolic ref name.
+ *
+ * @param symbolicRefName
+ * symbolic ref name to be shown
+ * @return
+ * ref name of specified symbolic ref name
+ */
+ public String showSymbolicRef(String symbolicRefName) {
+ return new ShowSymbolicRefCommand(repoDir).symbolicRefName(symbolicRefName).call();
+ }
+
/**
* Update specified ref with specified revision.
*
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/ShowSymbolicRefCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/ShowSymbolicRefCommand.java
new file mode 100644
index 0000000000..2f63d001d5
--- /dev/null
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/ShowSymbolicRefCommand.java
@@ -0,0 +1,47 @@
+package com.pmease.commons.git.command;
+
+import java.io.File;
+
+import org.apache.commons.lang3.StringUtils;
+
+import com.google.common.base.Preconditions;
+import com.pmease.commons.util.execution.Commandline;
+import com.pmease.commons.util.execution.LineConsumer;
+
+public class ShowSymbolicRefCommand extends GitCommand<String> {
+
+ private String symbolicRefName;
+
+ public ShowSymbolicRefCommand(File repoDir) {
+ super(repoDir);
+ }
+
+ public ShowSymbolicRefCommand symbolicRefName(String symbolicRefName) {
+ this.symbolicRefName = symbolicRefName;
+ return this;
+ }
+
+ @Override
+ public String call() {
+ Preconditions.checkNotNull(symbolicRefName, "symbolicRefName has to be specified.");
+
+ Commandline cmd = cmd().addArgs("symbolic-ref", symbolicRefName);
+
+ final String shortRefName[] = new String[]{null};
+
+ cmd.execute(new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ if (StringUtils.isNotBlank(line))
+ shortRefName[0] = line;
+ }
+
+ }, errorLogger).checkReturnCode();
+
+ Preconditions.checkNotNull(shortRefName[0]);
+
+ return shortRefName[0];
+ }
+
+}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/UpdateSymbolicRefCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/UpdateSymbolicRefCommand.java
new file mode 100644
index 0000000000..56910148ea
--- /dev/null
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/UpdateSymbolicRefCommand.java
@@ -0,0 +1,50 @@
+package com.pmease.commons.git.command;
+
+import java.io.File;
+
+import com.google.common.base.Preconditions;
+import com.pmease.commons.util.execution.Commandline;
+
+public class UpdateSymbolicRefCommand extends GitCommand<Void> {
+
+ private String symbolicRefName;
+
+ private String refName;
+
+ private String reason;
+
+ public UpdateSymbolicRefCommand(File repoDir) {
+ super(repoDir);
+ }
+
+ public UpdateSymbolicRefCommand symbolicRefName(String symbolicRefName) {
+ this.symbolicRefName = symbolicRefName;
+ return this;
+ }
+
+ public UpdateSymbolicRefCommand refName(String refName) {
+ this.refName = refName;
+ return this;
+ }
+
+ public UpdateSymbolicRefCommand reason(String reason) {
+ this.reason = reason;
+ return this;
+ }
+
+ @Override
+ public Void call() {
+ Preconditions.checkNotNull(symbolicRefName, "symbolicRefName has to be specified.");
+ Preconditions.checkNotNull(refName, "refName has to be specified.");
+
+ Commandline cmd = cmd().addArgs("symbolic-ref", symbolicRefName, refName);
+
+ if (reason != null)
+ cmd.addArgs("-m", reason);
+
+ cmd.execute(debugLogger, errorLogger).checkReturnCode();
+
+ return null;
+ }
+
+}