Skip to content

thiagolvlsantos/git-transactions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

git-transactions

CI with Maven CI with CodeQL CI with Sonar Quality Gate Status Coverage Maven Central Hex.pm

Transactions using Git repositories

Imagine you reading/writting locally to your file system attached to your Git repository as a transaction in your service class. An abstraction, using Aspects, to read and write files in your file system and automatically have them pulled/commited/pushed to your Git repository.

Welcome to git-transactions, see bellow how simple it is.

Usage

Include latest version Maven Central to your project.

		<dependency>
			<groupId>io.github.thiagolvlsantos</groupId>
			<artifactId>git-transactions</artifactId>
			<version>${latestVersion}</version>
		</dependency>

Add repositories configurations

To your application.properties add information about the repositories of interest. The example bellow we have projects and deployments repositories.

# Shared configuration, if nothing more specific to a repo is used.
gitt.repository.user=thiagolvlsantos
gitt.repository.password=thiagospassword

# this repo bellongs to thiagolvlsantos, so the user and password above are reused
gitt.repository.projects.read=data/read/projects
gitt.repository.projects.write=data/write/projects
gitt.repository.projects.remote=https://github.com/thiagolvlsantos/gitt-example-projects.git

# any repo can have its own user and password, multiple repos from different users can compose a file
gitt.repository.deployments.user=anotheruser
gitt.repository.deployments.password=anotherpassword
gitt.repository.deployments.read=data/read/deployments
gitt.repository.deployments.write=data/write/deployments
gitt.repository.deployments.remote=https://github.com/thiagolvlsantos/gitt-example-deployments.git

Add annotation @EnableGitTransactions

...
@EnableGitTransactions
public class Application {
	...main(String[] args) {...}
}

Add a reference to GitServices and annotate your methods with @GitRead or @GitWrite.

Reading

The following code shows how to read a file from Git which was automatically download by @GitRead(<repo_name>) annotation. Once the Git was downloaded the navigation through its structure is straightforward.

...
private @Autowired GitServices gitServices;

...

@GitRead("projects")
public String readProjectFile(String projectName) {
	File dir = gitServices.readDirectory("projects");
	return Files.readString(new File(dir,projectName+".json").toPath());
}

...

If you want to read contents of an specific revision, just pass the commit id in a @GitCommit annotated parameter as bellow:

...
private @Autowired GitServices gitServices;

...

@GitRead("projects")
public String readProjectFile(String projectName, @GitCommit("projects") String commit) {
	File dir = gitServices.readDirectory("projects");
	return Files.readString(new File(dir,projectName+".json").toPath());
}

...

Or programatically set it whenever you want, for example, to compare content versions:

...
private @Autowired GitServices gitServices;

...

@GitRead("projects")
public String readProjectFile(String projectName, String commit) {
	File dir = gitServices.readDirectory("projects");
	String current = Files.readString(new File(dir,projectName+".json").toPath());

	gitServices.setCommit(commit);
	dir = gitServices.readDirectory("projects");
	String old = Files.readString(new File(dir,projectName+".json").toPath());
	
	//compare old with current
}
...

The same behavior can be achieved by using an 'Long' timestamp attribute annotated with @GitCommit, or using services.setTimestamp(...).

Writing

If the user wants to send or update files or directories into a Git repository use @GitWrite(<repo_name>) and after method finalization the changes are automatically commited/pushed to the Git repository latest revision.

...
private @Autowired GitServices gitServices;

...

@GitWrite("projects")
public void writeProjectFile(String projectName) {
	File dir = gitServices.writeDirectory("projects");
	File newFile = new File(dir,projectName+".json");
	String newContent = "{\"name\":\""+projectName+"\"}"}
	Files.write(newFile.toPath(), newContent.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.WRITE,
				StandardOpenOption.TRUNCATE_EXISTING);
}

...

Information about commit author/committer is provided by an implemention of IGitAudit you provide.

Read/Write

Multiple combinations of read/write are allowed for different repositories. When mixing read and write, read repository downloads are performed first.

Examples

A larger set of examples can be found in test directory.

Build

Localy, from this root directory call Maven commands or bin/<script name> at your will.