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.
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 article.

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

We have some preconditions:
  • The teams want to synchronize only conventionally named branches. Not the entire repository, which is also possible.
  • GitLab of one of the teams automatically blocks tags deletion. So, they decided to exclude Git-tags from syncing.
  • Guys have no time for optimization and struggle with GitLab, they just want a working solution.
  • Logic below uses bare repositories (sync-repo) hosted on some your synchronization-agent machine.
  • They have a side effect "do not push to another side branch more than once a minute".

In every synchronization cycle we shall do:


Creating a bare repositories of a 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  https://git.a1.com/repo  a1/*  b2/*
git ls-remote --heads  https://b2.com/git/repo  a1/*  b2/*


Updating repositories of synchronization-agent. (the first)

# for a1 sync-repo
git fetch --prune https://git.a1.com/repo  +heads/a1/*:heads/a1/*  +heads/b2/*:heads/b2/*
# for b2 sync-repo
git fetch --prune https://b2.com/git/repo  +heads/a1/*:heads/a1/*  +heads/b2/*:heads/b2/*


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 also some fixed commit.
Scip Deletion & Recovering if there are no differences in current ref-names (without sha-1).


Deletion & Recovering of refs (also a partial fast-forward synchronization as a side effect).

# from a1 sync-repo
git push --prune https://b2.com/git/repo  heads/a1/*:heads/a1/*
# from b2 sync-repo
git push --prune https://git.a1.com/repo  heads/b2/*:heads/b2/*


Updating repositories of synchronization-agent. (repeat of the first)


Plain syncing of refs

# from a1 sync-repo
git push https://b2.com/git/repo  heads/a1/*:heads/a1/*  heads/b2/*:heads/b2/*
# from b2 sync-repo
git push https://git.a1.com/repo  heads/a1/*:heads/a1/*  heads/b2/*:heads/b2/*


Updating repositories of synchronization-agent. (repeat of the first)


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

# from a1 sync-repo
git push https://b2.com/git/repo  +heads/a1/*:heads/a1/*
# from b2 sync-repo
git push https://git.a1.com/repo  +heads/b2/*:heads/b2/*

All-in-one working implementation

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

No comments:

Post a Comment