Skip to content

Commit bc2def0

Browse files
committed
Merge branch 'master' into B1
# Conflicts: # build.gradle
2 parents a45f92d + 2b76591 commit bc2def0

File tree

6 files changed

+136
-89
lines changed

6 files changed

+136
-89
lines changed

.gitignore

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
1212
hs_err_pid*
13-
/.gradle/
14-
/.settings
15-
/.classpath
16-
/.project
17-
/build/
18-
/bin/
13+
/.*
14+
/build/
15+
/bin/
16+

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Features:
2525
- Named randomly (uuid is used)
2626

2727
# Using pk-vcs-git
28-
- Add github-hosted pk-vcs-git project as maven dependency using [jitpack.io](https://jitpack.io/). As an example, add following to gradle.build file:
28+
- Add github-hosted pk-vcs-git and pk-vcs-api projects as maven dependencies using [jitpack.io](https://jitpack.io/). As an example, add following to gradle.build file:
2929
```gradle
3030
allprojects {
3131
repositories {
@@ -34,11 +34,12 @@ Features:
3434
}
3535

3636
dependencies {
37-
// versioning: master-SNAPSHOT (lastest build, unstable), + (lastest release, stable) or certain version (e.g. 1.0)
37+
// versioning: master-SNAPSHOT (lastest build, unstable), + (lastest release, stable) or certain version (e.g. 1.1)
3838
compile 'com.github.ProjectKaiser:pk-vcs-git:+'
39+
compile 'com.github.ProjectKaiser:pk-vcs-api:+'
3940
}
4041
```
41-
Or download release jars from https://github.com/ProjectKaiser/pk-vcs-git/releases
42+
Or download release jars from https://github.com/ProjectKaiser/pk-vcs-git/releases, https://github.com/ProjectKaiser/pk-vcs-api/releases
4243
- Create Workspace Home instance providing path to any folder as Workspace Home folder path. This folder will contain repositories folders (if different vcs or repositories are used)
4344
```java
4445
public static final String WORKSPACE_DIR = System.getProperty("java.io.tmpdir") + "git-workspaces";
@@ -68,7 +69,7 @@ Features:
6869

6970
# Functional testing
7071
- New local file-based Test Repository is created before each test and deletes automatically after each test
71-
- To execute tests just run GitVCSTest class as JUnit test. Tests from VCSAbstractTest class will be executed. See [pk-vcs-test](https://github.com/ProjectKaiser/pk-vcs-test) for details
72+
- To execute tests just run GitVCSTest class as JUnit test. Tests from VCSAbstractTest class will be executed. See [pk-vcs-test](https://github.com/ProjectKaiser/pk-vcs-test) for details
7273
- Run `gradle test` to execute tests
7374

7475
# Limitations

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ repositories {
1616

1717
dependencies {
1818
compile 'com.github.ProjectKaiser:pk-vcs-api:1.1'
19-
compile 'commons-logging:commons-logging:1.2'
2019
compile 'org.eclipse.jgit:org.eclipse.jgit:4.3.0.201604071810-r'
2120

2221
testCompile 'org.mockito:mockito-core:2.0.62-beta'
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Fri Apr 22 10:26:49 GMT+03:00 2016
1+
#Tue Mar 28 23:38:07 MSK 2017
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-all.zip

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

Lines changed: 124 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.net.URI;
1313
import java.nio.charset.StandardCharsets;
1414
import java.util.ArrayList;
15-
import java.util.Arrays;
15+
import java.util.Collections;
1616
import java.util.HashSet;
1717
import java.util.List;
1818
import java.util.Set;
@@ -30,11 +30,15 @@
3030
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
3131
import org.eclipse.jgit.diff.DiffEntry.Side;
3232
import org.eclipse.jgit.diff.DiffFormatter;
33+
import org.eclipse.jgit.lib.AnyObjectId;
3334
import org.eclipse.jgit.lib.Constants;
35+
import org.eclipse.jgit.lib.ObjectId;
3436
import org.eclipse.jgit.lib.ObjectReader;
3537
import org.eclipse.jgit.lib.Ref;
3638
import org.eclipse.jgit.lib.Repository;
3739
import org.eclipse.jgit.revwalk.RevCommit;
40+
import org.eclipse.jgit.revwalk.RevObject;
41+
import org.eclipse.jgit.revwalk.RevSort;
3842
import org.eclipse.jgit.revwalk.RevWalk;
3943
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
4044
import org.eclipse.jgit.transport.CredentialsProvider;
@@ -44,6 +48,7 @@
4448

4549
import com.projectkaiser.scm.vcs.api.IVCS;
4650
import com.projectkaiser.scm.vcs.api.VCSChangeType;
51+
import com.projectkaiser.scm.vcs.api.VCSCommit;
4752
import com.projectkaiser.scm.vcs.api.VCSDiffEntry;
4853
import com.projectkaiser.scm.vcs.api.VCSMergeResult;
4954
import com.projectkaiser.scm.vcs.api.exceptions.EVCSBranchExists;
@@ -59,7 +64,7 @@ public class GitVCS implements IVCS {
5964
private static final String REFS_REMOTES_ORIGIN = Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/";
6065

6166
private CredentialsProvider credentials;
62-
private IVCSRepositoryWorkspace repo;
67+
private final IVCSRepositoryWorkspace repo;
6368

6469
public CredentialsProvider getCredentials() {
6570
return credentials;
@@ -72,6 +77,56 @@ public GitVCS(IVCSRepositoryWorkspace repo) {
7277
public void setCredentials(CredentialsProvider credentials) {
7378
this.credentials = credentials;
7479
}
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+
}
75130

76131
@Override
77132
public void createBranch(String srcBranchName, String newBranchName, String commitMessage) {
@@ -116,10 +171,6 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
116171
}
117172
}
118173

119-
private String getRealBranchName(String branchName) {
120-
return branchName == null ? MASTER_BRANCH_NAME : branchName;
121-
}
122-
123174
@Override
124175
public void deleteBranch(String branchName, String commitMessage) {
125176
try {
@@ -161,39 +212,6 @@ public void deleteBranch(String branchName, String commitMessage) {
161212
}
162213
}
163214

164-
public Git getLocalGit(IVCSLockedWorkingCopy wc) {
165-
Repository gitRepo;
166-
try {
167-
gitRepo = new FileRepositoryBuilder()
168-
.setGitDir(new File(wc.getFolder(), ".git"))
169-
.build();
170-
} catch (IOException e) {
171-
throw new RuntimeException(e);
172-
}
173-
Boolean repoInited = gitRepo
174-
.getObjectDatabase()
175-
.exists();
176-
Git git = new Git(gitRepo);
177-
if (!repoInited) {
178-
try {
179-
Git
180-
.cloneRepository()
181-
.setDirectory(wc.getFolder())
182-
.setURI(repo.getRepoUrl())
183-
.setCredentialsProvider(credentials)
184-
.setNoCheckout(true)
185-
.setBranch(Constants.R_HEADS + Constants.MASTER)
186-
.call()
187-
.close();
188-
return git;
189-
} catch (Exception e) {
190-
throw new EVCSException(e);
191-
192-
}
193-
}
194-
return git;
195-
}
196-
197215
@Override
198216
public VCSMergeResult merge(String srcBranchName, String dstBranchName, String commitMessage) {
199217
try {
@@ -269,10 +287,10 @@ public void setProxy(final String host, final int port, String proxyUser, String
269287
@Override
270288
public List<Proxy> select(URI uri) {
271289
if (uri.toString().toLowerCase().contains(repo.getRepoUrl().toLowerCase())) {
272-
return Arrays.asList(new Proxy(Type.HTTP, InetSocketAddress
273-
.createUnresolved(host, port)));
290+
return Collections.singletonList(new Proxy(Type.HTTP, InetSocketAddress
291+
.createUnresolved(host, port)));
274292
} else {
275-
return delegate == null ? Arrays.asList(Proxy.NO_PROXY)
293+
return delegate == null ? Collections.singletonList(Proxy.NO_PROXY)
276294
: delegate.select(uri);
277295
}
278296
}
@@ -327,7 +345,7 @@ public String getFileContent(String branchName, String fileRelativePath, String
327345
}
328346

329347
@Override
330-
public void setFileContent(String branchName, String filePath, String content, String commitMessage) {
348+
public String setFileContent(String branchName, String filePath, String content, String commitMessage) {
331349
try {
332350
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
333351
try (Git git = getLocalGit(wc)) {
@@ -359,7 +377,7 @@ public void setFileContent(String branchName, String filePath, String content, S
359377
fw.write(content);
360378
fw.close();
361379

362-
git
380+
RevCommit res = git
363381
.commit()
364382
.setOnly(filePath)
365383
.setMessage(commitMessage)
@@ -374,6 +392,8 @@ public void setFileContent(String branchName, String filePath, String content, S
374392
.setCredentialsProvider(credentials)
375393
.call();
376394
git.getRepository().close();
395+
396+
return res.getName();
377397
}
378398
}
379399
} catch (GitAPIException e) {
@@ -449,19 +469,6 @@ public List<VCSDiffEntry> getBranchesDiff(String srcBranchName, String dstBranch
449469
throw new RuntimeException(e);
450470
}
451471
}
452-
453-
private VCSChangeType gitChangeTypeToVCSChangeType(ChangeType changeType) {
454-
switch (changeType) {
455-
case ADD:
456-
return VCSChangeType.ADD;
457-
case DELETE:
458-
return VCSChangeType.DELETE;
459-
case MODIFY:
460-
return VCSChangeType.MODIFY;
461-
default:
462-
return VCSChangeType.UNKNOWN;
463-
}
464-
}
465472

466473
@Override
467474
public Set<String> getBranches() {
@@ -498,6 +505,7 @@ public List<String> getCommitMessages(String branchName, Integer limit) {
498505

499506
List<String> res = new ArrayList<>();
500507
for (RevCommit commit : logs) {
508+
commit.getId().getName();
501509
res.add(commit.getFullMessage());
502510
}
503511
git.getRepository().close();
@@ -516,7 +524,7 @@ public String getVCSTypeString() {
516524
}
517525

518526
@Override
519-
public void removeFile(String branchName, String filePath, String commitMessage) {
527+
public String removeFile(String branchName, String filePath, String commitMessage) {
520528
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
521529
try (Git git = getLocalGit(wc)) {
522530
String bn = getRealBranchName(branchName);
@@ -537,7 +545,7 @@ public void removeFile(String branchName, String filePath, String commitMessage)
537545
.setCached(false)
538546
.call();
539547

540-
git
548+
RevCommit res = git
541549
.commit()
542550
.setMessage(commitMessage)
543551
.setAll(true)
@@ -550,11 +558,68 @@ public void removeFile(String branchName, String filePath, String commitMessage)
550558
.call();
551559

552560
git.getRepository().close();
561+
return res.getName();
553562
}
554563
} catch (GitAPIException e) {
555564
throw new EVCSException(e);
556565
} catch (Exception e) {
557566
throw new RuntimeException(e);
558567
}
559568
}
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+
}
560625
}

0 commit comments

Comments
 (0)