obda.net

.gitignore per branch

Add a comment

Git does not support to ignore files based on the current branch. Patterns listed in any .gitignore file as well as in .git/info/exclude are always applied. A workaround is possible by creating per-branch ignore/exclude files, and automatically symlinking the correct one using Git’s post-checkout hook:

This hook is invoked when a git checkout is run after having updated the worktree. The hook is given three parameters: the ref of the previous HEAD, the ref of the new HEAD (which may or may not have changed), and a flag indicating whether the checkout was a branch checkout (changing branches, flag=1) or a file checkout (retrieving a file from the index, flag=0). This hook cannot affect the outcome of git checkout.

For example, the following script looks if there is a file named exclude.<branchname> in the .git/info directory, and uses this as link target for .git/info/exclude. If there is no branch-specific exclude file, it will symlink exclude.__default__ instead.

#!/bin/sh

old_ref=$1
new_ref=$2
branch_switched=$3

if [[ $branch_switched != '1' ]]
then
    exit 0
fi

current_branch=$(git rev-parse --abbrev-ref HEAD)
hook_dir=$(dirname $0)
info_dir=$(realpath "$hook_dir/../info")

exclude_target='__default__'
if [[ -f "$info_dir/exclude.$current_branch" ]]
then
    exclude_target=$current_branch
fi
cd $info_dir
rm exclude
ln -s exclude.$exclude_target exclude

Modify this script as needed, save it as .git/hooks/post-checkout, and make it executable—et voilà.

Ignore join/part/quit messages in WeeChat

2 comments

cheat-sheet articles are about code snippets that I need every once in a while, and which I constantly forget about.

From the WeeChat FAQ:

With smart filter (keep join/part/quit from users who spoke recently):

/set irc.look.smart_filter on
/filter add irc_smart * irc_smart_filter *

With a global filter (hide all join/part/quit):

/filter add joinquit * irc_join,irc_part,irc_quit *

Ignoring the messages only in specific channels can be achieved by providing the full channel names (in a comma-separated list) after the filter name:

/filter add myfilter irc.freenode.#mychannel irc_smart_filter *

Use /filter disable myfilter or /filter del myfilter to disable or delete the filter.

Simple Local Web Server with Python

Add a comment

cheat-sheet articles are about code snippets that I need every once in a while, and which I constantly forget about.

Python’s standard distribution includes a simple HTTP server, which can be used to serve a local directory on the fly. Just open a terminal, change to the directory you want to share, and run:

python -m SimpleHTTPServer

Once the server is running, you will see the message:

Serving HTTP on 0.0.0.0 port 8000 ...

In your browser, you can now access the server by surfing to http://localhost:8000. Moreover, you can also access the server from other machines in your network, not only from localhost.

If you want to change the server’s port, just provide an additional argument when launching it:

python -m SimpleHTTPServer 8888

Note: The above solution works only for Python 2, as Python 3 has reorganized some modules. If you are working with the latest Python version, you need to run:

python -m http.server

or, with a different port:

python -m http.server 8888