Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the class responsable for manipulating git repositories. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 68 additions & 60 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,60 +1,68 @@
<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>spgroup</groupId>
<artifactId>GremlinQuery</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.6.0-01</version>
</dependency>
</dependencies>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
</plugin>
<plugin>
<artifactId>groovy-eclipse-compiler</artifactId>
<groupId>org.codehaus.groovy</groupId>
<version>2.6.0-01</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<artifactId>groovy-all</artifactId>
<groupId>org.codehaus.groovy</groupId>
<version>2.0.5</version>
</dependency>

<dependency>
<groupId>com.tinkerpop.gremlin</groupId>
<artifactId>gremlin-groovy</artifactId>
<version>2.3.0</version>
</dependency>

<dependency>
<groupId>com.tinkerpop.blueprints</groupId>
<artifactId>blueprints-core</artifactId>
<version>2.3.0</version>
</dependency>

<dependency>
<groupId>com.tinkerpop.blueprints</groupId>
<artifactId>blueprints-neo4j-graph</artifactId>
<version>2.3.0</version>
</dependency>

</dependencies>
</project>
<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>spgroup</groupId>
<artifactId>GremlinQuery</artifactId>
<version>0.0.1-SNAPSHOT</version>
<repositories>
<repository>
<id>jgit-repository</id>
<url>https://repo.eclipse.org/content/groups/releases/</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.6.0-01</version>
</dependency>
</dependencies>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
</plugin>
<plugin>
<artifactId>groovy-eclipse-compiler</artifactId>
<groupId>org.codehaus.groovy</groupId>
<version>2.6.0-01</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<artifactId>groovy-all</artifactId>
<groupId>org.codehaus.groovy</groupId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>com.tinkerpop.gremlin</groupId>
<artifactId>gremlin-groovy</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.tinkerpop.blueprints</groupId>
<artifactId>blueprints-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.tinkerpop.blueprints</groupId>
<artifactId>blueprints-neo4j-graph</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>3.1.0.201310021548-r</version>
</dependency>
</dependencies>
</project>

151 changes: 151 additions & 0 deletions src/main/java/Extractor.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@


import org.eclipse.jgit.api.CheckoutCommand
import org.eclipse.jgit.api.CleanCommand
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.MergeCommand
import org.eclipse.jgit.api.MergeResult
import org.eclipse.jgit.api.ResetCommand
import org.eclipse.jgit.api.ResetCommand.ResetType
import org.eclipse.jgit.lib.Ref
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.storage.file.FileRepositoryBuilder

class Extractor {

// the url of the repository
def static final String REMOTE_URL = "https://github.com/prga/TGM.git"

// the directory to clone to
def static final String REPOSITORY_DIR = "C:/Users/Guilherme/Desktop/Itens Recentes/tgm"

// the commit to checkout
def static final String SHA1_1 = "448259185594ed4f0b9ea2c6be9197ca3f5573db"
def static final String SHA1_2 = "c0c6d0e7b0f175b800925472be4c550e5a39567d"


def cloneRepository(){
// prepare a new folder for the cloned repository
File gitWorkDir = new File(REPOSITORY_DIR)
gitWorkDir.mkdirs()

// then clone
println "Cloning from " + REMOTE_URL + " to " + gitWorkDir
Git.cloneRepository()
.setURI(REMOTE_URL)
.setDirectory(gitWorkDir)
.call();

// now open the created repository
FileRepositoryBuilder builder = new FileRepositoryBuilder()
Repository repository = builder.setGitDir(gitWorkDir)
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build();

println "Having repository: " + repository.getDirectory()
repository.close()

}

def Git openRepository() {
File gitWorkDir = new File(REPOSITORY_DIR);
Git git = Git.open(gitWorkDir);
Repository repository = git.getRepository()
return git
}

def listAllBranches() {
// opening the working directory
Git git = openRepository();

List<Ref> call = git.branchList().call();
for (Ref ref : call) {
println "Branch-Before: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName();
}
}

def checkoutMasterBranch() {
// opening the working directory
Git git = openRepository();

CheckoutCommand checkoutCommand = git.checkout()
checkoutCommand.setName("refs/heads/master")
Ref checkoutResult = checkoutCommand.call()
println "Checked out branch sucessfully: " + checkoutResult.getName()
}

def Ref checkoutAndCreateBranch(String branchName, String commit){
// opening the working directory
Git git = openRepository();

CheckoutCommand checkoutCommand = git.checkout()
checkoutCommand.setName(branchName)
checkoutCommand.setStartPoint(commit)
checkoutCommand.setCreateBranch(true)
Ref checkoutResult = checkoutCommand.call()
println "Checked out and created branch sucessfully: " + checkoutResult.getName()

return checkoutResult
}

def deleteBranch(String branchName) {
// opening the working directory
Git git = openRepository();

git.branchDelete()
.setBranchNames(branchName)
.call()
}

def reset() {
// opening the working directory
Git git = openRepository();

//git reset --hard SHA1_1
ResetCommand resetCommand = git.reset()
resetCommand.setMode(ResetType.HARD)
resetCommand.setRef(SHA1_1)
Ref resetResult = resetCommand.call()
println "Reseted sucessfully to: " + resetResult.getName()

// git clean -f
CleanCommand cleanCommandgit = git.clean()
cleanCommandgit.call()

// git checkout -b new SHA1_2
def refNew = checkoutAndCreateBranch("new", SHA1_2)

// git checkout master
checkoutMasterBranch()

MergeCommand mergeCommand = git.merge()
mergeCommand.include(refNew)
MergeResult res = mergeCommand.call()

if (res.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING)){
println "Revision Base: " + res.getBase().toString()
println "Conflitcts: " + res.getConflicts().toString()

println ""

Map allConflicts = res.getConflicts();
for (String path : allConflicts.keySet()) {
int[][] c = allConflicts.get(path);
println "Conflicts in file " + path
for (int i = 0; i < c.length; ++i) {
println " Conflict #" + i
for (int j = 0; j < (c[i].length) - 1; ++j) {
if (c[i][j] >= 0)
println" Chunk for " + res.getMergedCommits()[j] + " starts on line #" + c[i][j];
}
}
}
}
}

static void main(args) {
def extrc = new Extractor()
extrc.reset()
}
}