Last Updated: February 25, 2016
·
3.01K
· dmedvinsky

Pretty output from remote git

Doing deploy with git push is getting popular these days. After all, “Git is new FTP” as they say.

Many of us do some kind of cool stuff in the git hooks on the receiving side. Of course often we want some logs of what's happening. But when simply echoing stuff from the hook, we get a lot of nasty remote: on each line.

Today we'll learn how to get rid of those lines. Here's what we'll see:

Before:

Counting objects: 5, done.
Writing objects: 100% (3/3), 241 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
remote: Updated branch 'master' of 'repo'. Deploying to dev.
remote: Doing a lot of awesome stuff.
remote: Done a lot of awesome stuff.
remote: All done.
To git@server:repo.git
   7188851..ee7ba34  master -> master

After:

Counting objects: 5, done.
Writing objects: 100% (3/3), 242 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
------> Updated branch 'master' of 'repo'. Deploying to dev.
        Doing a lot of awesome stuff.
        Done a lot of awesome stuff.
------> All done.
To git@server:repo.git
   a0830ed..7188851  master -> master

Here's the trick

Before echoing the text, issue a control code:

\e[1G

It tells the terminal emulator to go to the first position horizontally, i.e. to the first column in current line. After that, any following output in this line will overwrite the text already present there (“remote:” in our case).

You don't even have to indent your lines if you know you will have enough symbols to hide the “remote:” text.

Anyway, here's how to echo control code in bash:

echo $'\e[1G'

The post-update hook template:

#!/usr/bin/env bash
refs=($@)
repopath=$(pwd)
reponame=$(basename ${repopath%.git})

for ref in $refs; do
    case $ref in
        refs/heads/master)
            echo $'\e[1G'"------>" "Updated branch 'master' of '${reponame}'. Deploying to dev."
            echo $'\e[1G'"       " "Doing a lot of awesome stuff."
            # deploy
            echo $'\e[1G'"       " "Done a lot of awesome stuff."
            echo $'\e[1G'"------>" "All done."
        ;;
        refs/heads/test)
            echo $'\e[1G'"------>" "Updated branch 'test' of '${reponame}'. Deploying to test."
            echo $'\e[1G'"       " "Doing a lot of awesome stuff."
            # deploy
            echo $'\e[1G'"       " "Done a lot of awesome stuff."
            echo $'\e[1G'"------>" "All done."
        ;;
        refs/heads/prod)
            echo $'\e[1G'"------>" "Updated branch 'prod' of '${reponame}'. Deploying to prod."
            echo $'\e[1G'"       " "Doing a lot of awesome stuff."
            # deploy
            echo $'\e[1G'"       " "Done a lot of awesome stuff."
            echo $'\e[1G'"------>" "All done."
        ;;
    esac
done

2 Responses
Add your response

Nice one! Great example of how the best solution is also usually the simplest :)

Will be adding this to ZaneA/Gitolite-Hooks on GitHub (with credit of course!)

over 1 year ago ·

Not sure how old this is, but there's another control code you can use to improve your output: \e[K This one clears any output after the cursor on the current line, and it does so without moving the cursor. That means you just have to change $'\e[1G' to $'\e[1G\e[K' and you can always skip indentation.

Hope that helps someone else!

over 1 year ago ·