-
Notifications
You must be signed in to change notification settings - Fork 0
/
JGitPushAfterGcDemoTest.java
136 lines (113 loc) · 5.15 KB
/
JGitPushAfterGcDemoTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package jgitdemo;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.TextProgressMonitor;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* This JUnit test assumes that a git repo exists on github, and that valid github credentials are given in a property file.
*/
public class JGitPushAfterGcDemoTest {
private static final Logger LOGGER = Logger.getLogger(JGitPushAfterGcDemoTest.class);
private static final TextProgressMonitor PROGRESS_MONITOR = new TextProgressMonitor(new LoggerPrintWriter(LOGGER, Level.DEBUG));
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Test
public void runGithub() throws Exception {
final Properties properties = readProperties("github");
final String gitUsername = properties.getProperty("user");
final String gitPassword = properties.getProperty("password");
final UsernamePasswordCredentialsProvider credProvider = new UsernamePasswordCredentialsProvider(gitUsername, gitPassword);
final String githubProject = "ruby";
final String remoteURL = "https://github.com/" + gitUsername + "/" + githubProject;
// choose between cleaned-up folder and fixed folder
// final File localFolder = folder.newFolder(githubProject);
final File localFolder = new File("/tmp/" + githubProject);
reproducePushSlowdownAfterGc(gitUsername, credProvider, localFolder, remoteURL);
}
private void reproducePushSlowdownAfterGc(
final String gitUsername,
final UsernamePasswordCredentialsProvider credProvider,
final File localFolder,
final String remoteURL) throws GitAPIException, IOException {
LOGGER.debug("Cloning from " + remoteURL + " as " + gitUsername + " to " + localFolder);
final Git git = cloneOrPullRepository(remoteURL, localFolder, credProvider);
/* *********** Call to reproduce later git push delay **********/
LOGGER.debug("Call GC");
git.gc().setProgressMonitor(PROGRESS_MONITOR).call();
LOGGER.debug("Modify, add and commit");
modifyAndCommitReadme(gitUsername, localFolder, git);
LOGGER.debug("Push");
final long start = System.currentTimeMillis();
// push
final Iterable<PushResult> pushResults = git.push()
.setProgressMonitor(PROGRESS_MONITOR)
.setCredentialsProvider(credProvider)
.call();
LOGGER.info("Push took " + (System.currentTimeMillis() - start));
LOGGER.info(pushResults);
}
private void modifyAndCommitReadme(final String githubUsername, final File localFolder, final Git git) throws IOException, GitAPIException {
final File readmeFile = new File(localFolder, "README.md");
// modify README
try (FileWriter fr = new FileWriter(readmeFile, true)) {
fr.write(System.currentTimeMillis() + "\n");
}
git.add()
.addFilepattern(readmeFile.getAbsolutePath())
.call();
git.commit()
.setCommitter(githubUsername, githubUsername)
.setAuthor(githubUsername, githubUsername)
.setMessage("commit " + System.currentTimeMillis())
.call();
}
private Properties readProperties(final String repo) {
// needs local copy of template with credential
try (final InputStream stream = getClass().getResourceAsStream(repo + ".properties")) {
final Properties properties = new Properties();
properties.load(stream);
return properties;
/* or properties.loadFromXML(...) */
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
private static Git pullRepository(
final File workingDir,
final UsernamePasswordCredentialsProvider credProvider
) throws GitAPIException, IOException {
final Git git = Git.open(workingDir);
git.pull().setProgressMonitor(PROGRESS_MONITOR)
.setRebase(true)
.setCredentialsProvider(credProvider)
.call();
return git;
}
private static Git cloneOrPullRepository(
final String gitUrl,
final File workingDir,
final UsernamePasswordCredentialsProvider credentialsProvider
) throws GitAPIException, IOException {
if (workingDir.exists()) {
LOGGER.warn("Skipping clone, pulling");
return pullRepository(workingDir, credentialsProvider);
}
return Git.cloneRepository()
.setURI(gitUrl)
.setDirectory(workingDir)
.setProgressMonitor(PROGRESS_MONITOR)
.setCredentialsProvider(credentialsProvider)
.call();
}
}