Avoid trailing white spaces before pushing a review request

I often forget to check my patches for trailing white spaces before pushing a review request to Gerrit. To avoid trailing white spaces before pushing a review request I added the following pre commit hook into the .git/hooks/pre-commit file.

#!/bin/bash -xe

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

git diff-index --check --cached HEAD --

if [[ $? -ne 0 ]]; then
  echo "Commit aborted. Remove trailing whitespaces first."
  exit 1
fi

Vagrantfile to test the OpenStack Installation Guide

A Vagrantfile to build a multi-machine environment. It can be used to work with the OpenStack Installation Guide and openSUSE.

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

VAGRANTFILE_API_VERSION = "2" if not defined? VAGRANTFILE_API_VERSION

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "berendt/opensuse-13.1-x86_64"
  config.vm.synced_folder '.', '/vagrant', disabled: true
  config.vm.provision "shell", inline: "zypper addrepo -f obs://Cloud:OpenStack:Juno/openSUSE_13.1 Juno"
  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--memory", "4096"]
    vb.customize ["modifyvm", :id, "--cpus", "4"]
  end
  config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'"
  if Vagrant.has_plugin?("vagrant-hostmanager")
    config.hostmanager.enabled = true
    config.hostmanager.manage_host = true
    config.hostmanager.ignore_private_ip = false
    config.hostmanager.include_offline = true
  end
  if Vagrant.has_plugin?("vagrant-cachier")
    config.cache.scope = :box
  end
  config.vm.define "controller", primary: true do |controller|
    controller.vm.hostname= "controller"
    controller.vm.network :private_network, ip: '10.0.0.11'
    controller.vm.network "forwarded_port", guest: 80, host: 8080
  end
  config.vm.define "network" do |network|
    network.vm.hostname= "network"
    network.vm.network :private_network, ip: '10.0.0.21'
    network.vm.network :private_network, virtualbox__intnet: "tunnels", ip: '10.0.1.21'
    network.vm.network :private_network, virtualbox__intnet: "external"
  end
  config.vm.define "compute1" do |compute1|
    compute1.vm.hostname= "compute1"
    compute1.vm.network :private_network, ip: '10.0.0.31'
    compute1.vm.network :private_network, virtualbox__intnet: "tunnels", ip: '10.0.1.31'
  end
  config.vm.define "storage1" do |storage1|
    storage1.vm.hostname= "storage1"
    storage1.vm.network :private_network, ip: '10.0.0.41'
    storage1.vm.provider "virtualbox" do |vb|
    vb.customize ['createhd', '--filename', '/tmp/c3a12ef0.vdi', '--size', 16384]
    vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', '/tmp/c3a12ef0.vdi']
    end
  end
end

Synchronisieren eines Forks auf Github

Nachfolgend ist die Synchronisierung eines Forks auf Github am Beispiel des Forks berendt/graphing-openstack beschrieben. Der Fork wurde vom Repository jogo/graphing-openstack erstellt.

Diese Beschreibung ist eine Zusammenfassung aus mehreren Artikeln aus der Hilfe von Github.

Ursprüngliches Repository des Forks als Quelle (upstream) hinzufügen:
https://help.github.com/articles/configuring-a-remote-for-a-fork/

% git remote -v
origin  git@github.com:berendt/graphing-openstack.git (fetch)
origin  git@github.com:berendt/graphing-openstack.git (push)

% git remote add upstream https://github.com/jogo/graphing-openstack.git

% git remote -v
origin  git@github.com:berendt/graphing-openstack.git (fetch)
origin  git@github.com:berendt/graphing-openstack.git (push)
upstream        https://github.com/jogo/graphing-openstack.git (fetch)
upstream        https://github.com/jogo/graphing-openstack.git (push)

Synchronisierung durchführen:
https://help.github.com/articles/syncing-a-fork/

% git fetch upstream
remote: Counting objects: 12, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 12 (delta 6), reused 10 (delta 4)
Unpacking objects: 100% (12/12), done.
From https://github.com/jogo/graphing-openstack
 * [new branch]      master     -> upstream/master

% git checkout master
Already on 'master'

% git merge upstream/master
Updating bf65467..4673eff
Fast-forward
 .gitignore     | 3 ++-
 graph.py       | 4 +++-
 openstack.yaml | 5 +++++
 3 files changed, 10 insertions(+), 2 deletions(-)

Änderungen auf Github bereitstellen:
https://help.github.com/articles/pushing-to-a-remote/

% git push origin master
Counting objects: 32, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (23/23), done.
Writing objects: 100% (24/24), 2.67 KiB | 0 bytes/s, done.
Total 24 (delta 15), reused 0 (delta 0)
To git@github.com:berendt/graphing-openstack.git
   bf65467..4673eff  master -> master

Hinzufügen von \n an das Ende von Dateien

Nachfolgendes Skript kann genutzt werden um Textdateien ein fehlendes \n am Ende der Datei anzuhängen. Warum man das machen möchte wird hier motiviert.

#!/usr/bin/python

# Copyright 2014 Christian Berendt <mail@cberendt.net>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import print_function
import glob2
from mimetypes import MimeTypes
import os
import sys

mime = MimeTypes()

if len(sys.argv) != 2:
    print("usage: %s PATH" % sys.argv[0])
    sys.exit(1)

for filename in glob2.glob("%s/**/*" % sys.argv[1]):
    if os.path.isdir(filename):
        continue
    with open(filename, 'r') as rfp:
        lines = rfp.readlines()
    if lines and not lines[-1].endswith('\n'):
        mimetype, _ = mime.guess_type(filename)
        if not mimetype:
            print("unknown: %s" % filename)
        elif(mimetype.startswith('text') or
             mimetype.endswith('json') or
             mimetype.endswith('javascript')):
            print("modifying: %s" % filename)
        with open(filename, 'a') as wfp:
            print('\n', end='', file=wfp)
        else:
            print("skipping: %s - %s" % (filename, mimetype))