Skip to content

Commit 2b76591

Browse files
committed
getCommitsRange() method is implemented
minor refactor
1 parent 1cd92fd commit 2b76591

File tree

3 files changed

+126
-60
lines changed

3 files changed

+126
-60
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ repositories {
1515
}
1616

1717
dependencies {
18-
compile 'com.github.ProjectKaiser:master-SNAPSHOT'
18+
compile 'com.github.ProjectKaiser:pk-vcs-api:master-SNAPSHOT'
1919
compile 'org.eclipse.jgit:org.eclipse.jgit:4.3.0.201604071810-r'
2020

2121
testCompile 'org.mockito:mockito-core:2.0.62-beta'
2222
testCompile 'junit:junit:4.12'
23-
testCompile 'com.github.ProjectKaiser:pk-vcs-test:matser-SNAPSHOT'
23+
testCompile 'com.github.ProjectKaiser:pk-vcs-test:master-SNAPSHOT'
2424
}
2525

2626
task sourcesJar(type: Jar, dependsOn: classes) {

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

Lines changed: 124 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import java.net.SocketAddress;
1212
import java.net.URI;
1313
import java.nio.charset.StandardCharsets;
14-
import java.util.*;
14+
import java.util.ArrayList;
15+
import java.util.Collections;
16+
import java.util.HashSet;
17+
import java.util.List;
18+
import java.util.Set;
1519

1620
import org.apache.commons.io.FileUtils;
1721
import org.apache.commons.io.IOUtils;
@@ -26,11 +30,15 @@
2630
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
2731
import org.eclipse.jgit.diff.DiffEntry.Side;
2832
import org.eclipse.jgit.diff.DiffFormatter;
33+
import org.eclipse.jgit.lib.AnyObjectId;
2934
import org.eclipse.jgit.lib.Constants;
35+
import org.eclipse.jgit.lib.ObjectId;
3036
import org.eclipse.jgit.lib.ObjectReader;
3137
import org.eclipse.jgit.lib.Ref;
3238
import org.eclipse.jgit.lib.Repository;
3339
import org.eclipse.jgit.revwalk.RevCommit;
40+
import org.eclipse.jgit.revwalk.RevObject;
41+
import org.eclipse.jgit.revwalk.RevSort;
3442
import org.eclipse.jgit.revwalk.RevWalk;
3543
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
3644
import org.eclipse.jgit.transport.CredentialsProvider;
@@ -40,6 +48,7 @@
4048

4149
import com.projectkaiser.scm.vcs.api.IVCS;
4250
import com.projectkaiser.scm.vcs.api.VCSChangeType;
51+
import com.projectkaiser.scm.vcs.api.VCSCommit;
4352
import com.projectkaiser.scm.vcs.api.VCSDiffEntry;
4453
import com.projectkaiser.scm.vcs.api.VCSMergeResult;
4554
import com.projectkaiser.scm.vcs.api.exceptions.EVCSBranchExists;
@@ -68,6 +77,56 @@ public GitVCS(IVCSRepositoryWorkspace repo) {
6877
public void setCredentials(CredentialsProvider credentials) {
6978
this.credentials = credentials;
7079
}
80+
81+
private String getRealBranchName(String branchName) {
82+
return branchName == null ? MASTER_BRANCH_NAME : branchName;
83+
}
84+
85+
public Git getLocalGit(IVCSLockedWorkingCopy wc) {
86+
Repository gitRepo;
87+
try {
88+
gitRepo = new FileRepositoryBuilder()
89+
.setGitDir(new File(wc.getFolder(), ".git"))
90+
.build();
91+
} catch (IOException e) {
92+
throw new RuntimeException(e);
93+
}
94+
Boolean repoInited = gitRepo
95+
.getObjectDatabase()
96+
.exists();
97+
Git git = new Git(gitRepo);
98+
if (!repoInited) {
99+
try {
100+
Git
101+
.cloneRepository()
102+
.setDirectory(wc.getFolder())
103+
.setURI(repo.getRepoUrl())
104+
.setCredentialsProvider(credentials)
105+
.setNoCheckout(true)
106+
.setBranch(Constants.R_HEADS + Constants.MASTER)
107+
.call()
108+
.close();
109+
return git;
110+
} catch (Exception e) {
111+
throw new EVCSException(e);
112+
113+
}
114+
}
115+
return git;
116+
}
117+
118+
private VCSChangeType gitChangeTypeToVCSChangeType(ChangeType changeType) {
119+
switch (changeType) {
120+
case ADD:
121+
return VCSChangeType.ADD;
122+
case DELETE:
123+
return VCSChangeType.DELETE;
124+
case MODIFY:
125+
return VCSChangeType.MODIFY;
126+
default:
127+
return VCSChangeType.UNKNOWN;
128+
}
129+
}
71130

72131
@Override
73132
public void createBranch(String srcBranchName, String newBranchName, String commitMessage) {
@@ -112,10 +171,6 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
112171
}
113172
}
114173

115-
private String getRealBranchName(String branchName) {
116-
return branchName == null ? MASTER_BRANCH_NAME : branchName;
117-
}
118-
119174
@Override
120175
public void deleteBranch(String branchName, String commitMessage) {
121176
try {
@@ -157,39 +212,6 @@ public void deleteBranch(String branchName, String commitMessage) {
157212
}
158213
}
159214

160-
public Git getLocalGit(IVCSLockedWorkingCopy wc) {
161-
Repository gitRepo;
162-
try {
163-
gitRepo = new FileRepositoryBuilder()
164-
.setGitDir(new File(wc.getFolder(), ".git"))
165-
.build();
166-
} catch (IOException e) {
167-
throw new RuntimeException(e);
168-
}
169-
Boolean repoInited = gitRepo
170-
.getObjectDatabase()
171-
.exists();
172-
Git git = new Git(gitRepo);
173-
if (!repoInited) {
174-
try {
175-
Git
176-
.cloneRepository()
177-
.setDirectory(wc.getFolder())
178-
.setURI(repo.getRepoUrl())
179-
.setCredentialsProvider(credentials)
180-
.setNoCheckout(true)
181-
.setBranch(Constants.R_HEADS + Constants.MASTER)
182-
.call()
183-
.close();
184-
return git;
185-
} catch (Exception e) {
186-
throw new EVCSException(e);
187-
188-
}
189-
}
190-
return git;
191-
}
192-
193215
@Override
194216
public VCSMergeResult merge(String srcBranchName, String dstBranchName, String commitMessage) {
195217
try {
@@ -323,7 +345,7 @@ public String getFileContent(String branchName, String fileRelativePath, String
323345
}
324346

325347
@Override
326-
public void setFileContent(String branchName, String filePath, String content, String commitMessage) {
348+
public String setFileContent(String branchName, String filePath, String content, String commitMessage) {
327349
try {
328350
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
329351
try (Git git = getLocalGit(wc)) {
@@ -355,7 +377,7 @@ public void setFileContent(String branchName, String filePath, String content, S
355377
fw.write(content);
356378
fw.close();
357379

358-
git
380+
RevCommit res = git
359381
.commit()
360382
.setOnly(filePath)
361383
.setMessage(commitMessage)
@@ -370,6 +392,8 @@ public void setFileContent(String branchName, String filePath, String content, S
370392
.setCredentialsProvider(credentials)
371393
.call();
372394
git.getRepository().close();
395+
396+
return res.getName();
373397
}
374398
}
375399
} catch (GitAPIException e) {
@@ -445,19 +469,6 @@ public List<VCSDiffEntry> getBranchesDiff(String srcBranchName, String dstBranch
445469
throw new RuntimeException(e);
446470
}
447471
}
448-
449-
private VCSChangeType gitChangeTypeToVCSChangeType(ChangeType changeType) {
450-
switch (changeType) {
451-
case ADD:
452-
return VCSChangeType.ADD;
453-
case DELETE:
454-
return VCSChangeType.DELETE;
455-
case MODIFY:
456-
return VCSChangeType.MODIFY;
457-
default:
458-
return VCSChangeType.UNKNOWN;
459-
}
460-
}
461472

462473
@Override
463474
public Set<String> getBranches() {
@@ -494,6 +505,7 @@ public List<String> getCommitMessages(String branchName, Integer limit) {
494505

495506
List<String> res = new ArrayList<>();
496507
for (RevCommit commit : logs) {
508+
commit.getId().getName();
497509
res.add(commit.getFullMessage());
498510
}
499511
git.getRepository().close();
@@ -512,7 +524,7 @@ public String getVCSTypeString() {
512524
}
513525

514526
@Override
515-
public void removeFile(String branchName, String filePath, String commitMessage) {
527+
public String removeFile(String branchName, String filePath, String commitMessage) {
516528
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
517529
try (Git git = getLocalGit(wc)) {
518530
String bn = getRealBranchName(branchName);
@@ -533,7 +545,7 @@ public void removeFile(String branchName, String filePath, String commitMessage)
533545
.setCached(false)
534546
.call();
535547

536-
git
548+
RevCommit res = git
537549
.commit()
538550
.setMessage(commitMessage)
539551
.setAll(true)
@@ -546,11 +558,68 @@ public void removeFile(String branchName, String filePath, String commitMessage)
546558
.call();
547559

548560
git.getRepository().close();
561+
return res.getName();
549562
}
550563
} catch (GitAPIException e) {
551564
throw new EVCSException(e);
552565
} catch (Exception e) {
553566
throw new RuntimeException(e);
554567
}
555568
}
569+
570+
571+
572+
public List<VCSCommit> getCommitsRange(String branchName, String afterCommitId, String untilCommitId) {
573+
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
574+
try (Git git = getLocalGit(wc)) {
575+
String bn = getRealBranchName(branchName);
576+
git
577+
.checkout()
578+
.setCreateBranch(git.getRepository().exactRef("refs/heads/" + bn) == null)
579+
.setName(bn)
580+
.call();
581+
582+
ObjectId sinceCommit = afterCommitId == null ?
583+
getInitialCommit(git).getId() :
584+
ObjectId.fromString(afterCommitId);
585+
586+
ObjectId untilCommit = untilCommitId == null ?
587+
git.getRepository().exactRef("refs/heads/" + bn).getObjectId() :
588+
ObjectId.fromString(untilCommitId);
589+
590+
Iterable<RevCommit> commits;
591+
commits = git
592+
.log()
593+
.addRange(sinceCommit, untilCommit)
594+
.call();
595+
596+
List<VCSCommit> res = new ArrayList<>();
597+
for (RevCommit commit : commits) {
598+
VCSCommit vcsCommit = new VCSCommit(commit.getName(), commit.getFullMessage(),
599+
commit.getAuthorIdent().getName());
600+
res.add(vcsCommit);
601+
}
602+
603+
Collections.reverse(res);
604+
git.getRepository().close();
605+
return res;
606+
}
607+
} catch (GitAPIException e) {
608+
throw new EVCSException(e);
609+
} catch (Exception e) {
610+
throw new RuntimeException(e);
611+
}
612+
}
613+
614+
private RevObject getInitialCommit(Git git) throws Exception {
615+
try (RevWalk rw = new RevWalk(git.getRepository())) {
616+
AnyObjectId headId;
617+
headId = git.getRepository().resolve(Constants.HEAD);
618+
RevCommit root = rw.parseCommit(headId);
619+
rw.sort(RevSort.REVERSE);
620+
rw.markStart(root);
621+
RevCommit res = rw.next();
622+
return res;
623+
}
624+
}
556625
}

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

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

33

4-
import static org.junit.Assert.assertTrue;
5-
64
import java.io.File;
75
import java.io.IOException;
86

97
import org.apache.commons.io.FileUtils;
108
import org.eclipse.jgit.api.Git;
119
import org.eclipse.jgit.lib.Repository;
1210
import org.junit.After;
13-
import org.junit.BeforeClass;
1411
import org.mockito.Mockito;
1512

1613
import com.projectkaiser.scm.vcs.api.IVCS;

0 commit comments

Comments
 (0)