Git trick #628: automatically set commit author based on repo URL

Standard

If you have more than one email identity that you use to commit to different projects you have to remember to change it in .git/config every time you git clone a new repository. I suck at remembering things and it’s been annoying me for a long time that I kept pushing commits with wrong email addresses to wrong repositories.

I can’t believe I am the only one having this problem, but I could not find anything on the interwebs so I just fixed it myself and I’m posting it here so that maybe hopefuly someone else will find it useful too :).

The trick is very simple: we create a post-checkout hook that will check the value of user.email in .git/config and set it to whatever we want based on URL of the “origin” remote.  Why post-checkout? Because there’s no post-clone hook, but git automatically checkouts master after clone so the hook gets executed. It also gets executed every time you run git checkout by hand but the overhead is minimal and we have a guard against overwriting the identity in case it’s already set.

#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# (C) 2015 Daniel Vrátil <dvratil@kde.org>
# License: GPL
#
# Requires: Python 2 or 3 and compatible GitPython
#

# https://github.com/gitpython-developers/GitPython
import git
import ConfigParser
import os
import sys

repo = git.Repo(os.getcwd())

# Don't do anything if an identity is already configured in this
# repo's .git/config
config = repo.config_reader(config_level = 'repository')
try:
    # The value of user.email is non-empty, stop here
    if config.get_value('user', 'email'):
        sys.exit(0)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
    # Section or option does not exist, continue
    pass


origin = repo.remote('origin')
if not origin:
    print('** Failed to detect remote origin, identity not updated! **')
    sys.exit(0)

# This is where you adjust the code to fit your needs
if 'kde.org' in origin.url or origin.url.startswith('kde:'):
    email = 'dvratil@kde.org'
elif 'fedoraproject.org' in origin.url:
    email = 'dvratil@fedoraproject.org'
elif 'kdab.com' in origin.url:
    email = 'daniel.vratil@kdab.com'
else:
    print('** Failed to detect identity! **')
    sys.exit(0)

# Write the option to .git/config
config = repo.config_writer()
config.set_value('user', 'email', email)
config.release()
print('** User identity for this repository set to \'%s\' **' % email)

To install it, just copy the script above to ~/.git-templates/hooks/post-checkout, make it executable and run

git config --global init.templatedir ~/.git-templates

All hooks from templatedir are automatically copied into .git/hooks when a new repository is created (git init or git clone) – this way the hook will get automatically deployed to every new repo.

And here’s a proof that it works :-)

[dvratil@Odin ~/devel/KDE]
$ git clone kde:kasync
Cloning into 'kasync'...
remote: Counting objects: 450, done.
remote: Compressing objects: 100% (173/173), done.
remote: Total 450 (delta 285), reused 431 (delta 273)
Receiving objects: 100% (450/450), 116.44 KiB | 0 bytes/s, done.
Resolving deltas: 100% (285/285), done.
Checking connectivity... done.
** User identity for this repository set to 'dvratil@kde.org' **

[dvratil@Odin ~/packaging/fedpkg]
$ git clone ssh://dvratil@pkgs.fedoraproject.org/gammaray
Cloning into 'gammaray'...
remote: Counting objects: 287, done.
remote: Compressing objects: 100% (286/286), done.
remote: Total 287 (delta 113), reused 0 (delta 0)
Receiving objects: 100% (287/287), 57.24 KiB | 0 bytes/s, done.
Resolving deltas: 100% (113/113), done.
Checking connectivity... done.
** User identity for this repository set to 'dvratil@fedoraproject.org' **

Update 1: added utf-8 coding (thanks, Andrea)
Update 2: changed shebang to more common /usr/bin/python (/bin/python is rather Fedora-specific), added “Requires” comment to top of the script (thanks, Derek)

How free software makes us better people

Standard

I don’t write this kind of blogs very often (actually not at all), partially because I don’t have much to say, partially because I suck at it (as you’ll probably find out yourself if you decide to continue reading). However this evening I had a nice cup of tea with a friend of mine and I’m feeling oddly relaxed now. I remembered an earlier discussion we had and it made me think about my inner motivations. In this text I tried to follow all my thoughts and see if they lead anywhere.

This post is about helping. Helping others and helping myself and why I think it’s free software, and the community around it that brings up the good in me and in others. Those who were in Brno on last year Akademy might find this kinda similar to Cornelius’ keynote about how KDE makes us better people, and it might as well be, since I really liked the keynote and agreed with everything Cornelius said. But I want to tell my story here, so bare with me…:)

Maybe what I’m writing here is absolutely obvious to everyone, but maybe it will help someone to realize what I realized while writing this post. But most of all it helped me to sort out my own thoughts, and I think that after a long time I won’t have troubles falling asleep tonight. Maybe I should start writing this kind of stuff more often… :-)

 

 

 

People help other people. That’s how the world works. But why do we help others? Because it’s a good thing to do? Because it makes us feel better? I guess I first thought about my motivations to help a couple weeks ago when I attended an HTML and CSS workshop organized by Czechitas – a Czech non-profit organization where girls teach other girls IT and programming. You might have heard about Rail Girls, so Czechitas is something similar and they are organizing workshops on various topics – from web coding and WordPress to graphical design (using open source tools!). Each workshop is also attended by tutors (that’s where I come in) who have some experience in the subject and who guide a small group of girls during the workshop, answer their questions and help them with their project. One question I got during that workshop was “Why?” – why are we willing to spent our entire Saturday just by sitting there and watching bunch of people we don’t know to struggle with HTML. I also got asked the same question couple days later when I talked about this to my friend and it made me think again – why indeed?

 

 

 

I think I always wanted to help people somehow. It makes me feel useful, it makes me feel good and I learn a thing or two during that, which is also important to me. I guess the first moment I realized this was back in 2009, when I started packaging weekly development snapshots of KDE for ArchLinux. I’ve been using Arch for over 2 years back then, but I was really just a user, I did not contribute in any way. I did not help. I felt I’ve been given so much, but I never gave anything in return.

 

 

 

I live in a sort of a bubble. My own world, my own reality. My day job involves working on free software, so I spend most of my time surrounded by free software hackers and other like-minded people. Thinking about it, I probably talk to other devs on IRC more than I do to my flatmates. But I don’t mind. I take the community as my second family, I consider some people there to be my very close friends and I really cannot express in words what being part of this family means to me. And what influences us more than our own family?

 

 

 

I see programming as a form of art. It’s creating something from pure thoughts and imagination. It’s building castles out of LEGO bricks, walking those castles and describing them in a form of a code. I do what I do because I enjoy it, because I don’t completely suck at it (some of the time), because I get recognized for what I do, and because I can actually see how what I do helps other people.

 

 

 

Richard Stallman defines “free” in free software as ‘free as in free speech, not free beer”. We are free, because we can take others’ ideas and build on them and we let others to take ours and build on them too. We share. We build powerful and high quality software because we share it with everyone, and we allow anyone to step in and help push it forward. We don’t want to see their CV, we don’t care what they have achieved in their life. We care about what they can achieve in the future If we let them. For a while I thought we were doing it simply because we want to use their potential to help us with our cause, our product. But now I see how wrong I was. After my first Akademy, my first chance to meet the community in person I realized it. We do it because we want to help those people. We want to help them to get better at what they do. We want to help them become part of the family. And we do so, because that’s how we became part of it. Because someone there cared enough. Because someone helped us and because we know it’s the right thing to do. We do it because we want to share the happiness we get out of being part of the family.

 

 

 

I guess this is where my thoughts lead. We don’t help, because we must, or because we are expected to. We help because we believe in it and because we believe it is the right way to make the world a better place. Ultimately it does not matter if I spend 30 seconds answering someone’s question on IRC, or if I spend a few hours helping uncountable many people by fixing a bug or implementing a new feature, or if I spend all day helping three girls to learn HTML. It’s not possible to say which is more useful, as we cannot see the full potential of those people. But we can help them. We can guide them and let them discover what they can do. And who knows, maybe the next person we help will achieve great things, and maybe those things will directly affect us, and help us in return. And even if they don’t, we will still teach them to help others – and they might be the ones to help someone else to do great things.

How does free software fit into all this? We help by sharing – we share what we know and we let others share that knowledge further, because knowledge is really the foundation of everything else. And we don’t care how much time it costs us.

I cannot really say what would I be like if I never got involved in free software. What I can say for sure however is that free software made me a better person. It taught me to share what I know with others and it taught me to help others. And yes, by free software I don’t mean any program. I mean the people.

My Top10 Sci-Fi shows

Standard

As maybe not mentioned anywhere in this blog, I’m also a big fan of scifi series. I’ve been to Festival Fantazie, first in summer 2010, then again in autumn. By that time I’ve already seen some series, but it really encouraged me to see more. I was talking about publishing my top-scifi chart and to-see list, so finally here it comes. Continue reading

KDE 4.6 beta preview

Standard

Based on KDE 4.6 beta 1 release last week, I decidedI to test the latest KDE snapshot 4.5.82 and here are the news I’ve found in there :) I haven’t been using development snapshots I’m creating every week for ArchLinux regularily last few weeks, since I’m mostly on Fedora now, so the changes are now more obvious for me. Continue reading