Commit 54ca5f4b authored by Christian Wulf's avatar Christian Wulf

initial commit

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="lib/commons-lang3-3.1.jar" sourcepath="lib/commons-lang3-3.1-sources.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lib/commons-io-2.4.jar" sourcepath="lib/commons-io-2.4-sources.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>git-gui</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<repositoryBundle>
<repositories>
<repositoryDirName>C:\Users\chw/workspace-esn/cockpit</repositoryDirName>
<url>git@10.20.90.10:cockpit.git</url>
</repositories>
<repositories>
<repositoryDirName>C:\Users\chw/workspace-esn/cockpit-plugins/cockpit-common</repositoryDirName>
<url>git@10.20.90.10:cockpit-common.git</url>
</repositories>
<repositories>
<repositoryDirName>C:\Users\chw/workspace-esn/cockpit-plugins/cockpit-dashboard</repositoryDirName>
<url>git@10.20.90.10:cockpit-dashboard.git</url>
</repositories>
<repositories>
<repositoryDirName>C:\Users\chw/workspace-esn/cockpit-plugins/cockpit-gis</repositoryDirName>
<url>git@10.20.90.10:cockpit-gis.git</url>
</repositories>
<repositories>
<repositoryDirName>C:\Users\chw/workspace-esn/cockpit-plugins/cockpit-historization</repositoryDirName>
<url>git@10.20.90.10:cockpit-historization.git</url>
</repositories>
<repositories>
<repositoryDirName>C:\Users\chw/workspace-esn/cockpit-plugins/cockpit-digital-pen</repositoryDirName>
<url>git@10.20.90.10:cockpit-digital-pen.git</url>
</repositories>
<repositories>
<repositoryDirName>C:\Users\chw/workspace-esn/cockpit-plugins/cockpit-tbm</repositoryDirName>
<url>git@10.20.90.10:cockpit-tbm.git</url>
</repositories>
<repositories>
<repositoryDirName>C:\Users\chw/workspace-esn/cockpit-plugins/cockpit-housing-industry-datamodel</repositoryDirName>
<url>git@10.20.90.10:cockpit-housing-industry-datamodel.git</url>
</repositories>
<repositories>
<repositoryDirName>C:\Users\chw/workspace-esn/cockpit-plugins/cockpit-blue-folder</repositoryDirName>
<url>git@10.20.90.10:cockpit-blue-folder.git</url>
</repositories>
<repositories>
<repositoryDirName>C:\Users\chw/workspace-esn/cockpit-plugins/cockpit-all-jobs</repositoryDirName>
<url>git@10.20.90.10:cockpit-all-jobs.git</url>
</repositories>
</repositoryBundle>
package git.cmd;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class CommandLineGitProvider implements GitProvider {
private static final String GIT_CMD = "git";
private static final String PUSH_CMD = "push";
private static final String PULL_CMD = "pull";
private static final String COMMIT_CMD = "commit";
private static final String ADD_AND_MSG_CMD = "-am";
private static final String STATUS_CMD = "status";
private static final String CLONE_CMD = "clone";
private final GitLogger logger;
private String repositoryDirName;
public CommandLineGitProvider(final GitLogger logger) {
this.logger = logger;
}
private void execSynchronously(final String[] cmdArray) {
File workingDir = new File(repositoryDirName);
execSynchronously(cmdArray, workingDir);
}
private void execSynchronously(final String[] cmdArray, final File workingDir) {
logger.logWorkingDir(workingDir.getAbsolutePath());
logger.logNativeCommand(cmdArray);
try {
Process process = Runtime.getRuntime().exec(cmdArray, null, workingDir);
process.waitFor();
logger.error(process.getErrorStream());
logger.info(process.getInputStream());
} catch (IOException | InterruptedException e) {
logger.catching(e);
}
}
@Override
public void pull() {
logger.startPulling();
execSynchronously(new String[] { GIT_CMD, PULL_CMD });
}
@Override
public void push() {
logger.startPushing();
execSynchronously(new String[] { GIT_CMD, PUSH_CMD });
}
@Override
public void commit(final String message) {
logger.startCommitting();
execSynchronously(new String[] { GIT_CMD, COMMIT_CMD, ADD_AND_MSG_CMD, message });
}
@Override
public void status() {
logger.startStatus();
execSynchronously(new String[] { GIT_CMD, STATUS_CMD });
}
@Override
public void cloneRepository(final String gitUrl) {
Path workingDir = Paths.get(repositoryDirName).getParent();
logger.startCloning();
execSynchronously(new String[] { GIT_CMD, CLONE_CMD, gitUrl }, workingDir.toFile());
}
@Override
public void setRepositoryDirName(final String repositoryDirName) {
this.repositoryDirName = repositoryDirName;
logger.setRepositoryDirName(repositoryDirName);
}
}
package git.cmd;
import java.io.InputStream;
public class EmptyGitLogger implements GitLogger {
protected String repositoryDirName;
@Override
public void error(final InputStream errorStream) {
// do nothing
}
@Override
public void info(final InputStream inputStream) {
// do nothing
}
@Override
public void catching(final Exception e) {
// do nothing
}
@Override
public void startPulling() {
// do nothing
}
@Override
public void startPushing() {
// do nothing
}
@Override
public void startCommitting() {
// do nothing
}
@Override
public void startStatus() {
// do nothing
}
@Override
public void startCloning() {
// do nothing
}
@Override
public void setRepositoryDirName(final String repositoryDirName) {
this.repositoryDirName = repositoryDirName;
}
@Override
public void logNativeCommand(final String[] cmdArray) {
// do nothing
}
@Override
public void logWorkingDir(final String absolutePath) {
// do nothing
}
}
package git.cmd;
import java.io.InputStream;
public interface GitLogger {
void error(InputStream errorStream);
void info(InputStream inputStream);
void catching(Exception e);
void startPulling();
void startPushing();
void startCommitting();
void startStatus();
void startCloning();
void setRepositoryDirName(String repositoryDirName);
void logNativeCommand(String[] cmdArray);
void logWorkingDir(String absolutePath);
}
package git.cmd;
public interface GitProvider extends LimitedGitProvider {
void cloneRepository(String gitUrl);
void setRepositoryDirName(String repositoryDirName);
}
package git.cmd;
public interface LimitedGitProvider {
void pull();
void push();
void status();
void commit(String message);
}
package git.gui;
import git.cmd.GitLogger;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Paths;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
public class JTextPaneGitLogger implements GitLogger {
public enum LogLevel {
DEBUG(2), INFO(1), ERROR(0);
private final int value;
private LogLevel(final int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
private static final Color COLOR_GREEN = new Color(0, 200, 0);
private final JTextPane textPane;
private String repositoryDirName;
private LogLevel logLevel = LogLevel.INFO;
public JTextPaneGitLogger(final JTextPane textPane) {
this.textPane = textPane;
}
@Override
public void error(final InputStream errorStream) {
try (InputStream is = errorStream) {
String message = IOUtils.toString(is);
message = message.trim();
if (!message.isEmpty()) {
addErrorToLog(message);
}
} catch (IOException e) {
this.catching(e);
}
}
@Override
public void info(final InputStream inputStream) {
try (BufferedReader is = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = is.readLine()) != null) {
line = line.trim();
if (!line.isEmpty()) {
boolean displayLine = true;
displayLine = displayLine && !line.equals("#");
displayLine = displayLine && !line.startsWith("# On branch");
displayLine = displayLine && !line.startsWith("# Your branch");
displayLine = displayLine && !line.startsWith("# Changes not staged");
displayLine = displayLine && !line.startsWith("# (use");
// int index = line.indexOf("modified");
// int index = line.indexOf("new file");
// int index = line.indexOf("deleted");
// int index = line.indexOf("renamed");
// int index = line.indexOf("unmerged");
if (displayLine) {
addGoodToLog(line);
}
}
}
} catch (IOException e) {
this.catching(e);
}
}
@Override
public void logWorkingDir(final String absolutePath) {
addDebugToLog("cd " + absolutePath);
}
@Override
public void logNativeCommand(final String[] nativeCommand) {
addDebugToLog("Native command: " + StringUtils.join(nativeCommand, " "));
}
@Override
public void catching(final Exception e) {
addErrorToLog(e.getLocalizedMessage());
}
@Override
public void startPulling() {
addInfoToLog("Pulling '" + Paths.get(repositoryDirName).getFileName() + "'...");
}
@Override
public void startPushing() {
addInfoToLog("Pushing '" + Paths.get(repositoryDirName).getFileName() + "'...");
}
@Override
public void startCommitting() {
addInfoToLog("Committing '" + Paths.get(repositoryDirName).getFileName() + "'...");
}
@Override
public void startStatus() {
addInfoToLog("Status '" + Paths.get(repositoryDirName).getFileName() + "'...");
}
@Override
public void startCloning() {
addInfoToLog("Cloning '" + Paths.get(repositoryDirName).getFileName() + "'...");
}
public void addInfoToLog(final String message) {
if (logLevel.getValue() >= LogLevel.INFO.getValue()) {
final StyledDocument doc = textPane.getStyledDocument();
final Style style = textPane.addStyle("Info", null);
StyleConstants.setForeground(style, Color.BLUE);
insertToDocLog(message, doc, style);
}
}
public void addGoodToLog(final String message) {
final StyledDocument doc = textPane.getStyledDocument();
final Style style = textPane.addStyle("Good", null);
StyleConstants.setForeground(style, COLOR_GREEN);
insertToDocLog(message, doc, style);
}
public void addDebugToLog(final String message) {
if (logLevel.getValue() >= LogLevel.DEBUG.getValue()) {
final StyledDocument doc = textPane.getStyledDocument();
final Style style = textPane.addStyle("Debug", null);
StyleConstants.setForeground(style, Color.DARK_GRAY);
insertToDocLog(message, doc, style);
}
}
public void addErrorToLog(final String message) {
if (logLevel.getValue() >= LogLevel.ERROR.getValue()) {
final StyledDocument doc = textPane.getStyledDocument();
final Style style = textPane.addStyle("Error", null);
StyleConstants.setForeground(style, Color.RED);
insertToDocLog(message, doc, style);
}
}
private void insertToDocLog(final String message, final StyledDocument doc, final Style style) {
try {
doc.insertString(doc.getLength(), message + "\n", style);
} catch (final BadLocationException e) {
}
textPane.setCaretPosition(textPane.getDocument().getLength());
}
@Override
public void setRepositoryDirName(final String repositoryDirName) {
this.repositoryDirName = repositoryDirName;
}
public LogLevel getLogLevel() {
return logLevel;
}
public void setLogLevel(final LogLevel logLevel) {
this.logLevel = logLevel;
}
}
package git.repository;
public class Repository {
private String url;
private String repositoryDirName;
public String getUrl() {
return url;
}
public void setUrl(final String url) {
this.url = url;
}
public String getRepositoryDirName() {
return repositoryDirName;
}
public void setRepositoryDirName(final String repositoryDirName) {
this.repositoryDirName = repositoryDirName;
}
}
package git.repository.bundle;
import git.cmd.GitProvider;
import git.cmd.LimitedGitProvider;
import git.repository.Repository;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import javax.xml.bind.JAXB;
public class RepositoriesBundle implements LimitedGitProvider {
private final String repositoriesBundleDirName;
private String repositoriesBundleName;
private GitProvider gitProvider;
public RepositoriesBundle(final String repositoriesBundleDirName) {
this.repositoriesBundleDirName = repositoriesBundleDirName;
}
public void setGitProvider(final GitProvider gitProvider) {
this.gitProvider = gitProvider;
}
@Override
public void pull() {
for (Repository repo : findRepositoryNames()) {
gitProvider.setRepositoryDirName(repo.getRepositoryDirName());
if (!new File(repo.getRepositoryDirName()).exists()) {
gitProvider.cloneRepository(repo.getUrl());
} else {
gitProvider.pull();
}
}
}
@Override
public void push() {
for (Repository repo : findRepositoryNames()) {
gitProvider.setRepositoryDirName(repo.getRepositoryDirName());
gitProvider.push();
}
}
@Override
public void status() {
for (Repository repo : findRepositoryNames()) {
gitProvider.setRepositoryDirName(repo.getRepositoryDirName());
gitProvider.status();
}
}
@Override
public void commit(final String message) {
for (Repository repo : findRepositoryNames()) {
gitProvider.setRepositoryDirName(repo.getRepositoryDirName());
gitProvider.commit(message);
}
}
protected List<Repository> findRepositoryNames() {
Path path = Paths.get(repositoriesBundleDirName, repositoriesBundleName + ".xml");
RepositoryBundle bundle = JAXB.unmarshal(path.toString(), RepositoryBundle.class);
return bundle.getRepositories();
}
public String getRepositoriesBundleName() {
return repositoriesBundleName;
}
public void setRepositoriesBundleName(final String repositoriesBundleName) {
this.repositoriesBundleName = repositoriesBundleName;
}
}
package git.repository.bundle;
import git.repository.Repository;
import java.util.LinkedList;
import java.util.List;
public class RepositoryBundle {
private List<Repository> repositories = new LinkedList<>();
public List<Repository> getRepositories() {
return repositories;
}
public void setRepositories(final List<Repository> repositories) {
this.repositories = repositories;
}
}
package git.repository;
import git.repository.bundle.RepositoryBundle;
import java.io.File;
import javax.xml.bind.JAXB;
import org.junit.Test;
public class RepositoryTest {
@Test
public void test() {
RepositoryBundle bundle = new RepositoryBundle();
String repositoryName;
Repository repository;
repositoryName = "cockpit";
repository = new Repository();
repository.setRepositoryDirName(System.getProperty("user.home") + "/workspace-esn/" + repositoryName);
repository.setUrl("git@10.20.90.10:" + repositoryName + ".git");
bundle.getRepositories().add(repository);
repositoryName = "cockpit-common";
repository = new Repository();
repository.setRepositoryDirName(System.getProperty("user.home") + "/workspace-esn/cockpit-plugins/"
+ repositoryName);
repository.setUrl("git@10.20.90.10:" + repositoryName + ".git");
bundle.getRepositories().add(repository);
repositoryName = "cockpit-dashboard";
repository = new Repository();
repository.setRepositoryDirName(System.getProperty("user.home") + "/workspace-esn/cockpit-plugins/"
+ repositoryName);
repository.setUrl("git@10.20.90.10:" + repositoryName + ".git");
bundle.getRepositories().add(repository);
repositoryName = "cockpit-gis";
repository = new Repository();
repository.setRepositoryDirName(System.getProperty("user.home") + "/workspace-esn/cockpit-plugins/"
+ repositoryName);
repository.setUrl("git@10.20.90.10:" + repositoryName + ".git");
bundle.getRepositories().add(repository);
repositoryName = "cockpit-historization";
repository = new Repository();
repository.setRepositoryDirName(System.getProperty("user.home") + "/workspace-esn/cockpit-plugins/"
+ repositoryName);
repository.setUrl("git@10.20.90.10:" + repositoryName + ".git");
bundle.getRepositories().add(repository);
repositoryName = "cockpit-digital-pen";
repository = new Repository();
repository.setRepositoryDirName(System.getProperty("user.home") + "/workspace-esn/cockpit-plugins/"
+ repositoryName);
repository.setUrl("git@10.20.90.10:" + repositoryName + ".git");
bundle.getRepositories().add(repository);
repositoryName = "cockpit-tbm";
repository = new Repository();
repository.setRepositoryDirName(System.getProperty("user.home") + "/workspace-esn/cockpit-plugins/"
+ repositoryName);
repository.setUrl("git@10.20.90.10:" + repositoryName + ".git");
bundle.getRepositories().add(repository);
repositoryName = "cockpit-housing-industry-datamodel";
repository = new Repository();
repository.setRepositoryDirName(System.getProperty("user.home") + "/workspace-esn/cockpit-plugins/"
+ repositoryName);
repository.setUrl("git@10.20.90.10:" + repositoryName + ".git");
bundle.getRepositories().add(repository);
repositoryName = "cockpit-blue-folder";
repository = new Repository();
repository.setRepositoryDirName(System.getProperty("user.home") + "/workspace-esn/cockpit-plugins/"
+ repositoryName);
repository.setUrl("git@10.20.90.10:" + repositoryName + ".git");
bundle.getRepositories().add(repository);
repositoryName = "cockpit-all-jobs";
repository = new Repository();
repository.setRepositoryDirName(System.getProperty("user.home") + "/workspace-esn/cockpit-plugins/"
+ repositoryName);
repository.setUrl("git@10.20.90.10:" + repositoryName + ".git");
bundle.getRepositories().add(repository);
JAXB.marshal(bundle, new File("resources/FM.xml"));
}
}
package git.repository.bundle;
import static org.junit.Assert.assertEquals;
import git.cmd.CommandLineGitProvider;
import git.cmd.EmptyGitLogger;
import git.repository.Repository;
import java.util.LinkedList;
import java.util.List;