changing case

This commit is contained in:
Eugen 2019-07-28 09:35:46 +03:00
parent 38b37df873
commit 1db074d22d
12 changed files with 464 additions and 0 deletions

3
jgit/README.md Normal file
View File

@ -0,0 +1,3 @@
## Relevant articles:
- [A Guide to JGit](http://www.baeldung.com/jgit)

53
jgit/pom.xml Normal file
View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>JGit</artifactId>
<version>1.0-SNAPSHOT</version>
<name>JGit</name>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<repositories>
<repository>
<id>jgit-repository</id>
<url>https://repo.eclipse.org/content/groups/releases/</url>
</repository>
</repositories>
<!-- Core Library -->
<dependencies>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>${org.eclipse.jgit.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit.archive</artifactId>
<version>${org.eclipse.jgit.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
</dependencies>
<properties>
<org.eclipse.jgit.version>4.5.0.201609210915-r</org.eclipse.jgit.version>
</properties>
</project>

View File

@ -0,0 +1,30 @@
package com.baeldung.jgit;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
/**
* Simple snippet which shows how to create a new repository
*
*
*/
public class CreateNewRepository {
public static void main(String[] args) throws IOException, IllegalStateException, GitAPIException {
// prepare a new folder
File localPath = File.createTempFile("TestGitRepository", "");
if(!localPath.delete()) {
throw new IOException("Could not delete temporary file " + localPath);
}
// create the directory
try (Git git = Git.init().setDirectory(localPath).call()) {
System.out.println("Having repository: " + git.getRepository().getDirectory());
}
FileUtils.deleteDirectory(localPath);
}
}

View File

@ -0,0 +1,65 @@
package com.baeldung.jgit;
import com.baeldung.jgit.helper.Helper;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import java.io.File;
import java.io.IOException;
/**
* Simple snippet which shows how to open an existing repository
*
*
*/
public class OpenRepository {
public static void main(String[] args) throws IOException, GitAPIException {
// first create a test-repository, the return is including the .get directory here!
File repoDir = createSampleGitRepo();
// now open the resulting repository with a FileRepositoryBuilder
FileRepositoryBuilder builder = new FileRepositoryBuilder();
try (Repository repository = builder.setGitDir(repoDir)
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build()) {
System.out.println("Having repository: " + repository.getDirectory());
// the Ref holds an ObjectId for any type of object (tree, commit, blob, tree)
Ref head = repository.exactRef("refs/heads/master");
System.out.println("Ref of refs/heads/master: " + head);
}
}
private static File createSampleGitRepo() throws IOException, GitAPIException {
try (Repository repository = Helper.createNewRepository()) {
System.out.println("Temporary repository at " + repository.getDirectory());
// create the file
File myfile = new File(repository.getDirectory().getParent(), "testfile");
if(!myfile.createNewFile()) {
throw new IOException("Could not create file " + myfile);
}
// run the add-call
try (Git git = new Git(repository)) {
git.add()
.addFilepattern("testfile")
.call();
// and then commit the changes
git.commit()
.setMessage("Added testfile")
.call();
}
System.out.println("Added file " + myfile + " to repository at " + repository.getDirectory());
return repository.getDirectory();
}
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.jgit.helper;
import java.io.File;
import java.io.IOException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
public class Helper {
public static Repository openJGitRepository() throws IOException {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
return builder
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build();
}
public static Repository createNewRepository() throws IOException {
// prepare a new folder
File localPath = File.createTempFile("TestGitRepository", "");
if(!localPath.delete()) {
throw new IOException("Could not delete temporary file " + localPath);
}
// create the directory
Repository repository = FileRepositoryBuilder.create(new File(localPath, ".git"));
repository.create();
return repository;
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.jgit.porcelain;
import java.io.File;
import java.io.IOException;
import com.baeldung.jgit.helper.Helper;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
/**
* Simple snippet which shows how to add a file to the index
*
*
*/
public class AddFile {
public static void main(String[] args) throws IOException, GitAPIException {
// prepare a new test-repository
try (Repository repository = Helper.createNewRepository()) {
try (Git git = new Git(repository)) {
// create the file
File myfile = new File(repository.getDirectory().getParent(), "testfile");
if(!myfile.createNewFile()) {
throw new IOException("Could not create file " + myfile);
}
// run the add-call
git.add()
.addFilepattern("testfile")
.call();
System.out.println("Added file " + myfile + " to repository at " + repository.getDirectory());
}
}
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.jgit.porcelain;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import com.baeldung.jgit.helper.Helper;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
/**
* Simple snippet which shows how to commit all files
*
*
*/
public class CommitAll {
public static void main(String[] args) throws IOException, GitAPIException {
// prepare a new test-repository
try (Repository repository = Helper.createNewRepository()) {
try (Git git = new Git(repository)) {
// create the file
File myfile = new File(repository.getDirectory().getParent(), "testfile");
if(!myfile.createNewFile()) {
throw new IOException("Could not create file " + myfile);
}
// Stage all files in the repo including new files
git.add().addFilepattern(".").call();
// and then commit the changes.
git.commit()
.setMessage("Commit all changes including additions")
.call();
try(PrintWriter writer = new PrintWriter(myfile)) {
writer.append("Hello, world!");
}
// Stage all changed files, omitting new files, and commit with one command
git.commit()
.setAll(true)
.setMessage("Commit changes to all files")
.call();
System.out.println("Committed all changes to repository at " + repository.getDirectory());
}
}
}
}

View File

@ -0,0 +1,56 @@
package com.baeldung.jgit.porcelain;
import java.io.IOException;
import com.baeldung.jgit.helper.Helper;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
/**
* Simple snippet which shows how to create a tag
*
*
*/
public class CreateAndDeleteTag {
public static void main(String[] args) throws IOException, GitAPIException {
// prepare test-repository
try (Repository repository = Helper.openJGitRepository()) {
try (Git git = new Git(repository)) {
// remove the tag before creating it
git.tagDelete().setTags("tag_for_testing").call();
// set it on the current HEAD
Ref tag = git.tag().setName("tag_for_testing").call();
System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory());
// remove the tag again
git.tagDelete().setTags("tag_for_testing").call();
// read some other commit and set the tag on it
ObjectId id = repository.resolve("HEAD^");
try (RevWalk walk = new RevWalk(repository)) {
RevCommit commit = walk.parseCommit(id);
tag = git.tag().setObjectId(commit).setName("tag_for_testing").call();
System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory());
// remove the tag again
git.tagDelete().setTags("tag_for_testing").call();
// create an annotated tag
tag = git.tag().setName("tag_for_testing").setAnnotated(true).call();
System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory());
// remove the tag again
git.tagDelete().setTags("tag_for_testing").call();
walk.dispose();
}
}
}
}
}

View File

@ -0,0 +1,74 @@
package com.baeldung.jgit.porcelain;
import java.io.IOException;
import com.baeldung.jgit.helper.Helper;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
/**
* Simple snippet which shows how to get the commit-ids for a file to provide log information.
*
*
*/
public class Log {
@SuppressWarnings("unused")
public static void main(String[] args) throws IOException, GitAPIException {
try (Repository repository = Helper.openJGitRepository()) {
try (Git git = new Git(repository)) {
Iterable<RevCommit> logs = git.log()
.call();
int count = 0;
for (RevCommit rev : logs) {
//System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
count++;
}
System.out.println("Had " + count + " commits overall on current branch");
logs = git.log()
.add(repository.resolve(git.getRepository().getFullBranch()))
.call();
count = 0;
for (RevCommit rev : logs) {
System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
count++;
}
System.out.println("Had " + count + " commits overall on "+git.getRepository().getFullBranch());
logs = git.log()
.all()
.call();
count = 0;
for (RevCommit rev : logs) {
//System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
count++;
}
System.out.println("Had " + count + " commits overall in repository");
logs = git.log()
// for all log.all()
.addPath("README.md")
.call();
count = 0;
for (RevCommit rev : logs) {
//System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
count++;
}
System.out.println("Had " + count + " commits on README.md");
logs = git.log()
// for all log.all()
.addPath("pom.xml")
.call();
count = 0;
for (RevCommit rev : logs) {
//System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
count++;
}
System.out.println("Had " + count + " commits on pom.xml");
}
}
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,33 @@
package com.baeldung.jgit;
import com.baeldung.jgit.helper.Helper;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevWalk;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertNotNull;
/**
* Tests which show issues with JGit that we reported upstream.
*/
public class JGitBugIntegrationTest {
@Test
public void testRevWalkDisposeClosesReader() throws IOException {
try (Repository repo = Helper.openJGitRepository()) {
try (ObjectReader reader = repo.newObjectReader()) {
try (RevWalk walk = new RevWalk(reader)) {
walk.dispose();
Ref head = repo.exactRef("refs/heads/master");
System.out.println("Found head: " + head);
ObjectLoader loader = reader.open(head.getObjectId());
assertNotNull(loader);
}
}
}
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.jgit.porcelain;
import org.junit.Test;
public class PorcelainUnitTest {
@Test
public void runSamples() throws Exception {
// simply call all the samples to see any severe problems with the samples
AddFile.main(null);
CommitAll.main(null);
CreateAndDeleteTag.main(null);
Log.main(null);
}
}