Skip to content

Commit 51a95e7

Browse files
author
Denis
committed
progress, tests implemented
1 parent c6b1e71 commit 51a95e7

File tree

2 files changed

+82
-32
lines changed

2 files changed

+82
-32
lines changed

src/main/java/com/projectkaiser/scm/vcs/GitVCS.java

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public void deleteBranch(String branchName, String commitMessage) {
141141
}
142142

143143
public Git getLocalGit(VCSWorkspace workspace) {
144+
144145
Repository repo;
145146
try {
146147
repo = new FileRepositoryBuilder()
@@ -288,34 +289,23 @@ public String getFileContent(String branchName, String filePath, String encoding
288289
try {
289290
try (Git git = getLocalGit(workspace)) {
290291

291-
checkout(branchName, git);
292+
git
293+
.pull()
294+
.setCredentialsProvider(credentials)
295+
.call();
292296

293-
Repository repository = git.getRepository();
294-
// find the HEAD
295-
ObjectId lastCommitId = repository.resolve(Constants.HEAD);
296-
// now we have to get the commit
297-
RevCommit commit;
298-
try (RevWalk revWalk = new RevWalk(repository)) {
299-
commit = revWalk.parseCommit(lastCommitId);
300-
}
301-
// and using commit's tree find the path
302-
RevTree tree = commit.getTree();
303-
ObjectId objectId;
304-
try (TreeWalk treeWalk = new TreeWalk(repository)) {
305-
treeWalk.addTree(tree);
306-
treeWalk.setRecursive(true);
307-
treeWalk.setFilter(PathFilter.create(filePath));
308-
if (!treeWalk.next()) {
309-
return null;
310-
}
311-
objectId = treeWalk.getObjectId(0);
312-
}
313-
ObjectLoader loader = repository.open(objectId);
314-
315-
InputStream in = loader.openStream();
316-
String res = IOUtils.toString(in, encoding);
297+
git
298+
.checkout()
299+
.setCreateBranch(false)
300+
.setForce(true)
301+
.addPath(filePath)
302+
.setName(branchName)
303+
//.setStartPoint("origin/" + destBranchUrl)
304+
//.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM)
305+
.call();
306+
File file = new File(workspace.getFolder(), filePath);
317307

318-
return res;
308+
return IOUtils.toString(file.toURI(), encoding);
319309
}
320310

321311
} finally {
@@ -335,7 +325,20 @@ public void setFileContent(String branchName, String filePath, String content, S
335325
try {
336326
try (Git git = getLocalGit(workspace)) {
337327

338-
checkout(branchName, git);
328+
git
329+
.pull()
330+
.setCredentialsProvider(credentials)
331+
.call();
332+
333+
git
334+
.checkout()
335+
.setCreateBranch(false)
336+
.setForce(true)
337+
.addPath(filePath)
338+
.setName(branchName)
339+
//.setStartPoint("origin/" + destBranchUrl)
340+
//.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM)
341+
.call();
339342

340343
File file = new File(workspace.getFolder(), filePath);
341344
FileWriter fw = new FileWriter(file, false);
@@ -344,15 +347,14 @@ public void setFileContent(String branchName, String filePath, String content, S
344347

345348
git
346349
.commit()
350+
.setOnly(filePath)
347351
.setMessage(commitMessage)
348-
.setAll(true)
349352
.call();
350353

351-
RefSpec refSpec = new RefSpec( ":refs/heads/" + branchName);
354+
RefSpec refSpec = new RefSpec(branchName + ":" + branchName);
352355

353356
git
354357
.push()
355-
.setPushAll()
356358
.setForce(true)
357359
.setRefSpecs(refSpec)
358360
.setRemote("origin")
@@ -363,12 +365,12 @@ public void setFileContent(String branchName, String filePath, String content, S
363365
} finally {
364366
workspace.unlock();
365367
}
368+
366369
} catch (GitAPIException e) {
367370
throw new EVCSException(e);
368371
} catch (Exception e) {
369372
throw new RuntimeException(e);
370373
}
371-
372374
}
373375

374376
@Override

src/test/java/com/projectkaiser/scm/vcs/GitVCSTest.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.projectkaiser.scm.vcs;
22

33

4+
import static org.junit.Assert.assertEquals;
45
import static org.junit.Assert.assertTrue;
56
import static org.junit.Assert.fail;
67

78
import java.io.File;
89
import java.io.IOException;
10+
import java.io.PrintWriter;
911
import java.util.UUID;
1012
import java.util.function.Consumer;
1113

@@ -14,6 +16,7 @@
1416
import org.eclipse.jgit.api.errors.GitAPIException;
1517
import org.eclipse.jgit.api.errors.NoFilepatternException;
1618
import org.eclipse.jgit.revwalk.RevCommit;
19+
import org.eclipse.jgit.transport.RefSpec;
1720
import org.junit.After;
1821
import org.junit.Before;
1922
import org.junit.BeforeClass;
@@ -36,10 +39,13 @@ public class GitVCSTest {
3639
private static final String SRC_BRANCH = "master";
3740
private static final String INITIAL_COMMIT_MESSAGE = "Initial commit";
3841
private static final String CREATED_DST_BRANCH_COMMIT_MESSAGE = "created dst branch";
42+
private static final String CONTENT_CHANGED_COMMIT_MESSAGE = "changed file content";
3943
private static final String MERGE_COMMIT_MESSAGE = "merged.";
4044
private static final String DELETE_BRANCH_COMMIT_MESSAGE = "deleted";
4145
private static final String PROXY_HOST = "localhost";
4246
private static final Integer PROXY_PORT = 3128;
47+
private static final String LINE_1 = "line 1";
48+
private static final String LINE_2 = "line 2";
4349

4450
private IVCS vcs;
4551
private GitHub github;
@@ -98,6 +104,48 @@ public void testGitCreateAndDeleteBranch() throws InterruptedException, IOExcept
98104
assertTrue (repo.getBranches().size() == 1);
99105
}
100106

107+
@Test
108+
public void testGetSetFileContent() throws NoFilepatternException, GitAPIException, IOException {
109+
VCSWorkspace w = VCSWorkspace.getLockedWorkspace(gitVCS.getRepoFolder());
110+
try {
111+
try (Git git = gitVCS.getLocalGit(w)) {
112+
File file = new File(w.getFolder(), "folder/file1.txt");
113+
file.getParentFile().mkdirs();
114+
file.createNewFile();
115+
PrintWriter out = new PrintWriter(file);
116+
out.println(LINE_1);
117+
out.close();
118+
119+
git
120+
.add()
121+
.addFilepattern("folder/file1.txt")
122+
.call();
123+
124+
git
125+
.commit()
126+
.setMessage(FILE1_ADDED_COMMIT_MESSAGE)
127+
.call();
128+
129+
RefSpec spec = new RefSpec("master:master");
130+
git
131+
.push()
132+
.setRefSpecs(spec)
133+
.setForce(true)
134+
.setRemote("origin")
135+
.setCredentialsProvider(gitVCS.getCredentials())
136+
.call();
137+
138+
vcs.setFileContent("master", "folder/file1.txt", LINE_2, CONTENT_CHANGED_COMMIT_MESSAGE);
139+
140+
assertEquals(vcs.getFileContent("master", "folder/file1.txt"), LINE_2);
141+
assertEquals(vcs.getFileContent("master", "folder/file1.txt", "UTF-8"), LINE_2);
142+
143+
}
144+
} finally {
145+
w.unlock();
146+
}
147+
}
148+
101149
@Test
102150
public void testGitMerge() throws IOException, NoFilepatternException, GitAPIException, InterruptedException {
103151
vcs.createBranch(SRC_BRANCH, NEW_BRANCH, CREATED_DST_BRANCH_COMMIT_MESSAGE);
@@ -141,7 +189,7 @@ public void testGitMerge() throws IOException, NoFilepatternException, GitAPIExc
141189
git
142190
.push()
143191
.setPushAll()
144-
.setForce(true)
192+
//.setForce(true)
145193
.setRemote("origin")
146194
.setCredentialsProvider(gitVCS.getCredentials())
147195
.call();

0 commit comments

Comments
 (0)