Thursday, September 28, 2017

Cheat-sheet of implementation for Convention over Git

This is a cheat-sheet for the simplest implementation of the Convention over Git approach.
It is used for an automated synchronization between remote Git repositories.

If you understand the idea of the Convention over Git, then it is easier to look at this cheat-sheet. Otherwise look at the Convention over Git idea article.

Here we'll start.
Let's say some two development teams in companies "BCD-1" and "JHI-2" want to auto-synchronize a Git-repository named "repo_name" with each other.

We have some preconditions:
  • The teams want to synchronize only conventionally named branches and tags. Not the entire repository, which is also possible.
  • Logic below uses bare repositories hosted on some your synchronization-agent machine.
  • There is a huge amount of variations for what I'm writing here.
  • There are some unmentioned details why this is done in such a way.

In every synchronization cycle we shall do:

Creating a bare repositories of synchronization-agent, if they do not exist. One for each side (team).

Checking if there are changes in remote Git repositories. Interrupt if not.

git ls-remote --heads --tags
git ls-remote --heads --tags

Updating repositories of synchronization-agent.

git fetch --prune "+refs/heads/*:refs/heads/*" "+refs/tags/*:refs/tags/*"
git fetch --prune "+refs/heads/*:refs/heads/*" "+refs/tags/*:refs/tags/*"

Checking if the Deletion & Recovering is allowable. Use git show-ref and git show to do this.
Each repository at least must have a branch with the same sha-1 and some fixed commit. There should be a difference in ref-names to allow the Deletion & Recovering.
If allowable, add the "--prune" options to git pushes of the following step.

Deletion & Recovering of refs. And a fast-forward synchronization of refs.
(the "--prune" option below is commented out by a bash inline comment)

git push `#--prune` "refs/heads/bcd1/*:refs/heads/bcd1/*" "refs/tags/bcd1/*:refs/tags/bcd1/*"
git push `#--prune` "refs/heads/jhi2/*:refs/heads/jhi2/*" "refs/tags/jhi2/*:refs/tags/jhi2/*"

Updating repositories of synchronization-agent.

git fetch --prune "+refs/heads/*:refs/heads/*" "+refs/tags/*:refs/tags/*"
git fetch --prune "+refs/heads/*:refs/heads/*" "+refs/tags/*:refs/tags/*"

Resolving conflicts of conventionally named refs. Do non-fast-forward pushes.

git push "+refs/heads/bcd1/*:refs/heads/bcd1/*" "+refs/tags/bcd1/*:refs/tags/bcd1/*"
git push "+refs/heads/jhi2/*:refs/heads/jhi2/*" "+refs/tags/jhi2/*:refs/tags/jhi2/*"

All-in-one working implementation

You can test an all-in-one implementation that I published on GitHub.
It creates local and remote repositories in your folder, so you can easily emulate and play with Convention over Git on your machine.

No comments:

Post a Comment