Skip to content. | Skip to navigation

Sections
Personal tools
What is this?
Hi, my name is Tom Lazar and I'm a Plone and Zope developer based in Berlin, Germany and this is my personal and professional (no big difference, really...) website.
 

Blog

"I'm a geek -- with a life!" he insisted stubbornly...

Apr 23, 2010

Push a local git branch to remote repository

Filed Under:

Push a local branch to the remote repository and automatically make it track the new remote branch - in one step

So you create a local working branch and eventually decide you want to push it to the remote branch (either to share it with colleagues or with another machine, or simply to have a convenient remote backup).

That, of course is easy. First create a new branch:

# git checkout -b new_branch
Switched to a new branch 'new_branch'

Then perform some work:

# echo "foo" >> foo.txt
# git add foo.txt
# git commit foo.txt -m "more foo"
[new_branch 5ba552f] more foo
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 foo.txt

and when you're ready:

# git push
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@github.com:tomster/git-svn-helpers.git
 * [new branch]      HEAD -> new_branch

So far, so good. However, if you then want to pull in remote changes you get the following error:

# git pull
You asked me to pull without telling me which branch you
want to merge with, and 'branch.new_branch.merge' in
your configuration file does not tell me either.      Please
specify which branch you want to merge on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.

If you often merge with the same branch, you may want to
configure the following variables in your configuration
file:

    branch.new_branch.remote = <nickname>
    branch.new_branch.merge = <remote-ref>
    remote.<nickname>.url = <url>
    remote.<nickname>.fetch = <refspec>

See git-config(1) for details.

You can either enter the git-config commands, edit .git/config manually or simply use the following shell script:

#!/usr/bin/env bash
function parse_git_branch {
  ref=$(git-symbolic-ref HEAD 2> /dev/null) || return
  echo ${ref#refs/heads/}
}
BRANCHNAME=`parse_git_branch`
git push
git config branch.$BRANCHNAME.remote origin
git config branch.$BRANCHNAME.merge refs/heads/$BRANCHNAME

Save it as git-publish anywhere on your $PATH (don't forget to make it executable) and then all you need to do, whenever you want to push a local git branch for the first time is simply this:

# git publish
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@github.com:tomster/git-svn-helpers.git
 * [new branch]      HEAD -> new_branch
# git pull
Already up-to-date.

Update: changed the shebang to #!/usr/bin/env bash since it really is a bash script, thanks to @HansHuebner

Update (2): As of git 1.7 you can use git push --set-upstream to achieve the same effect.

Sep 06, 2009

gitosis and virtualenv

Filed Under:

Notes on setting up gitosis (on FreeBSD) using virtualenv.

Basically, the following is a condensed and simpler version of the excellent tutorial over at scie.nti.st, except that we don't use the system-wide site-packages. The crucial simplifications are:

  • we don't muck about with $PYTHONPATH or /etc/environment because that's evil :)
  • instead we use virtualenv

First, install git:

cd /usr/ports/devel/git
sudo make install

Install python and friends:

cd /usr/ports/lang/python25
sudo make install
...
cd /usr/ports/devel/py-setuptools
sudo make install
...
sudo easy_install virtualenv

Add a git user:

sudo pw adduser -n git -m

Copy the public key:

cp ~/.ssh/authorized_keys /tmp/gitosis.pub

Become the git user and install gitosis using virtualenv:

sudo su - git
virtualenv . --no-site-packages
mkdir src
cd src/
git clone git://eagain.net/gitosis.git
cd gitosis
../../bin/python setup.py install

The only thing missing now is to use absolut paths in the hook script (to make it work with virtualenv). IOW, /home/git/repositories/gitosis-admin.git/hooks/post-update should look like this:

#!/bin/sh
set -e
/home/git/bin/gitosis-run-hook post-update
/usr/local/libexec/git-core/git-update-server-info

Make sure, the hook's executable bit is set:

chmod a+x /home/git/repositories/gitosis-admin.git/hooks/post-update

Voila! You can clone the gitosis-admin repository and start working with it.

Mar 20, 2009

Annotatable TestRequest

Filed Under:

How to test browser views without (the overhead of) a browser test in Zope and Plone

Browser tests aren't the only (and often not the best) way to test browser views. Much nicer to just instantiate the view directly, like so:

>>> view = FooView(context, request)

Then just call any method of the view directly:

>>> self.assertEqual(view.foo(), [])

But where to get the request? Enter TestRequest:

>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()
>>> view = FooView(context, request)
>>> self.assertEqual(view.foo(), [])

However, sometimes this can result in the following error:

TypeError: ('Could not adapt', <zope.publisher.browser.TestRequest instance URL=http://127.0.0.1>, 
    <InterfaceClass zope.annotation.interfaces.IAnnotations>)

In that case you additionally need to make the request object you've just created annotatable, like so:

>>> from zope.publisher.browser import TestRequest
>>> from zope.annotation.interfaces import IAttributeAnnotatable
>>> from zope.interface import alsoProvides
>>> request = TestRequest()
>>> alsoProvides(request, IAttributeAnnotatable)

Jan 12, 2009

Git command aliases

Filed Under:

Not everything in svn is worse than in git!

One of the nice features of subversion are its built-in command aliases, i.e. instead of having to type svn commit you can just use svn ci, instead of propertyedit just type pe.

git doesn't have those, which some find annoying. However, you can easily remedy this by adding an [alias] section to your ~/.gitconfig file. Upon request and because it's really a nice feature here's an example for that from my own configuration. I've copied it myself mostly from Andi, who is as much more prolific than I as he is lazier to blog ;-)

Anyway, here it goes:

[core]
    excludesfile = /Users/tomster/.gitignore
    pager = "/opt/local/bin/less -RciqMSj5"
    editor = mate -w

[color]
    diff = auto
    branch = auto
    status = auto

[diff]
    renames = true

[alias]
    st = status
    d = diff
    ci = commit -v
    cia = commit -v -a
    co = checkout
    cp = cherry-pick
    l = log
    ll = log -p
    lt = log trunk..
    llt = log -p trunk..
    lm = log master..
    llm = log -p master..
    b = branch

The .gitignore file referenced above looks like this (and should look rather familiar to svn users):

.svn
*.egg-info
*.pyc
.DS_Store
tmtags

Update: @philikon found the FM :-)

Dec 12, 2008

Bristol Plone Performance Sprint Day #1

Make Plone go faster

The sprint started with a brainstorming session where we collected all the ideas and areas the participants brought into three core areas. They have been tentatively named Cataloging and Indexing, Instrumentation and Benchmarking. We then proceeded to collect input on these three areas by having everybody write down whatever came to mind on post-its and we just stuck them together on the wall.

2008-12-12 11:32:03 +0000

After lunch we split up into three groups where all the input for each topic was processed. The group session was limited to just over an hour, as the goal was not to jump into (anf get lost in) an exhaustive discussion of all aspects and details but rather to come up with specific, actionable proposals which would then be put to the entire sprint in a short presentation, where we collectively decided on what (partially already how) we were going to work on for the remainder of the sprint.

This approach turned out to be a really good idea for multiple reasons. For one, we collectively decided on what we would concentrate on. Nobody had to feel left out. By limiting the first session to an hour and demanding a short presentation from each group to the entire sprint, we avoid rushing into dead ends. And lastly, everybody was on the same picture from the very start.

The three presentations then provided each group with further feedback and by the time they were over everybody was ready to get some actual work done.

We ended day one by rewarding ourselves with a big dinner of real authentic English food (i.e. Indian curry) in an old converted bank. Rewarding ourselves with dinner