Posted on 15-03-19 by Peter

How I use Git

So it's been awhile, my timing really wasn't good but just a couple of days after the previous post I actually went on vacation, took a nine days trip to NYC. But now I'm back and will write and hence work on this project more regularly.

Before writing about Git and my repository I thought a short explanation of my setup. All my jobs are done on a virtual server, that are either a speciel setup for a special client, to match their real server or in most cases a Centos 6 server with all necessary stuff like php etc. Problem with centos 6 is that i.e php version is to old, 5.3 so I strongly recommend to download the latest source and compile it yourself.

So i'm using Git, really no surprise there but I didn't read the symfony book about setting up a Git repository. My initial thought was to only have my own sourcecode in the repository, and in my world that meant only my bundle but I quickly realised that there will be source code in web folder to like css and images. That itself wouldn't really create a problem for versioning but then I wanted to add some packages (which I'll tell you more about later) and that raised some questions for me. Should I only add the composer file and/or packages that I added or not anything of that. Or should I add the whole structure? Well I guess the answer was there from the beginning and I knew it. Imagine if we didn't have the full structure in the repository, we would then

a) Everytime we do anything in the composer file we have to do that everywhere. But why not add the composer file to repository? well we need to add the lock file to, that would create a problem each time we run a composer update/install the lockfile is changed, we update the repository and then we have to run composer update/install on the next server and hence problem since the lockfile claims you have version y of a package but you really still have version x of it.
b) We would have to run composer update/install on each server and that is easier to forgett than if you have it in a repository working with git, if your branch is behind you get information about it.

Well I guess I could explain how I built this project with Git but to be honest this book at git-scm.com is the only thing you need to get going, it's good for anyone no matter your skills. If you as a reader still thinks that it would be interesting to read about how I built it just say so on twitter.

But I made some changes to this blogpost codewise and the first thing I did was to make a template for codesnippets (design that I stole directly from symfony.com ) so I easily could reuse it. The nonstatic code in that is the number of lines and the code, evrything else is static.
The two lines in the template file looks like this:

                       1 2 3
                    
{{ literalblock.lines }}

{{ literalblock.code|trim|raw }}
                    

I trim the code so no spacings and also just the raw code which later on isn't desired. So when writing my blogpost and want to add some codelines as above I simply create an array with keys lines, code and corresponding values as strings, when including the template I add the array as an argument. That looks like this:

                       1 2 3 4 5 6 7
                    
{% set literalblock = {
            'lines':'1',
            'code':'some code'
       }
%}
{{ include('LetitrockBlogBundle::literalblock.html.twig',
        { 'literalblock': literalblock }) }}
                    

After that I also moved out the blogpost into it's own file and in the index template I just do the same kind of inclusion as mentioned above but with no argument.