Run Unit Test Before Every Commit with pre-commit Hook

Why wait for CI/CD services to run your test? And suppose if you’ve submitted a pull request, you’ll have to push another commit to fix the cause of failed unit test. You can prevent that by running unit tests locally before every commit.

Introduction

Why wait for CI/CD services to run your test? And in case you’ve already submitted a pull request, you’ll have to push another commit to fix the cause of failed test held by CI provider. You can prevent that by running unit tests locally before every commit.

I’m building my website using the golang. On every git push, Travis CI runs test and create a docker image and pushes it to DockerHub. My plan is to pull that image every time it is pushed and deploy it to some production server.

Failed Travis build.
Failed Travis build.

One thing I’ve learned about this deployment system is, once I push my repo to GitHub and wait for the image to be pushed to DockerHub. I’m not sure if the test will pass and the image will be pushed. I have to open TravisCI every time I’ve to make sure.

Now I’ll show what workflow I use to to get this done.

Continue reading “Run Unit Test Before Every Commit with pre-commit Hook”

Install Local GitLab Server with Docker

Confirm that an active installation of Docker is installed.

Install inside Docker

docker pull gitlab/gitlab-ce

Start Docker service

sudo systemctl enable --now docker

Add current user to docker user group so that he does not have to use sudo every now and then.

sudo gpasswd -a $USER docker

The following command will start GitLab. Note that –restart always will restart the container at system startup.

sudo docker run --detach \                             
   --hostname gitlab.example.com \
   --publish 443:443 --publish 80:80 --publish 22:22 \
   --name gitlab \
   --restart always \
   --volume /srv/gitlab/config:/etc/gitlab \
   --volume /srv/gitlab/logs:/var/log/gitlab \
   --volume /srv/gitlab/data:/var/opt/gitlab \
   gitlab/gitlab-ce:latest
  • Go to 0.0.0.0

In a future post, I’ll show how to connect it to the LDAP server.

How would you exclude all extension-less executable from your repo?

In last post, I expressed how we can export a single file from a git repo the its own repo, preserving their commit history. In this post, I will tell how I dealt with extension-less file to ignore them in my repo.

I have recently started learning C++ from Udemy, it’s a free course. I encourage you to take it if you are willing to learn C++. I was following the instructions on Linux. And special thing about working with C or C++ in Linux is that the executable you get has no extensions in it, unlike Windows, which has .exe extension.

Continue reading “How would you exclude all extension-less executable from your repo?”

Extract a File from Git Repo to its own, Preserving the History

Today I will walk through how you can take out a single file from a git repository and create its own repository with all the file commit history preserved.

When I started working at my workplace I initialized Maya script directory to git. At that time one repo was looking enough for entire folder. But when I started working with a tool, I realized an entire repo would be good for that tool. But it was too late to create another repo because I had already made 12-15 commits on that tool and I don’t wanted to lose the changes I made.

Continue reading “Extract a File from Git Repo to its own, Preserving the History”

Merge more than one Commits before Pushing

When I first came to know about this, I was like:

What is Squashing?

The process of merging commits together is called squashing. There are many commands to do the same thing, but I will discuss the one I learned. There’s a read more section below if you want to know more about this topic.

Don’t go far Deep

One thing to keep in mind, before we start is, we should avoid merging our pushed commits. It will create a problem to other developers if you’re working in a team, and things will kinda mess up.

Codes and Explanations

Ariejan has already explained this in deep, so I will not do the same here but will do it for my reference. For merging last 3 commits, we can pass:

git rebase -i HEAD~3

This will open up your default editor configured with git with the following content:

pick hash Commit 1
pick hash Commit 2
pick hash Commit 3

The commits appear from older to newer. We’re probably gonna merge commit 2 & 3 into commit 1. In our code editor what we’ll do is replace pick from commit 2 & 3 lines to squash.

Sorry for the syntax highlighting. wordpress.com is preventing me from adding most external services like gist and all which has javascript embed (Twitter is working though).

What are your thoughts on this post? How do you squash your changes? I’m eager to know. Please leave comments.

Further Reading: