Category Archives: Linux

Experimenting with different layout options flexibly using AngularJS

One of the great things about AngularJS is the way you can easily reuse and embed templates and controllers. Recently I was asked to create two versions of a control panel – one where there were several individual pages, and another where we had the pages as tabs. As I was already using the excellent AngularJS Bootstrap plugin I thought about doing something like:

<tabset>
  <tab ...>
    <div ng-include=... />
  </tab ...>
  ...
<tabset>

However the issue with this is that you load all the templates (and controllers) at page-load so if I update data in one tab it will still show the old data (from page-load time) when I change to another tab.

After scratching my head for a while I came up with the following simple method:

<div ng-controller="Tabs">

  <ul class="nav nav-tabs noprint">
    <li role="presentation"><a href="#/">
      <span class="glyphicon glyphicon-circle-arrow-left"></span>
        Dön
    </a></li>
    <li role="presentation" ng-class="{ active: type == 'recipe' }"><a href="#/edit/recipe/{{ rec_id }}">
      <span class="glyphicon glyphicon-edit"></span>
      Tarife
    </a></li>
    ...
  </ul>

  <div ng-include="'templates/' + template + '.html'"></div>

</div>

Then create a simple controller:

t.controller('Tabs', function($scope, $routeParams) {
  $scope.rec_id = $routeParams.rec_id;
  $scope.template = $routeParams.template;
});

And set up the routing (individual pages have their own routes&params already set up):

// For new combined tabs
path: '/edit/:template/:rec_id?'

Job done!

Running processing (and updating the commit) straight after a commit in git

In one project I have a set of templates that I want built into a single file for quicker download. Rather than having to run a command manually after a commit I’d rather this was done at commit-time and then added to the commit bundle. I spent a while figuring out how to do this but basically you need to create a file .git/hooks/post-commit (in every repository – it doesn’t get pushed/pulled) containing the following:

#!/bin/sh

# Build templates as you wish eg "perl bin/build_templates.pl"

git diff --quiet compiled_file_name  # Did we have any change?
if [ $? != "0" ]; then # Yes - redo previous commit
    git commit -C HEAD --amend --no-verify compiled_file_name
fi

Importing and prepending subversion history to a git repo

So, when I converted some repos from svn to git a few years ago I just threw away the history (I think the git-svn tool wasn’t working or I was in a hurry or something). Anyway, today I was reminded of this and thought I’d backup all my svn repos into git and where possible prepend the history to the repositories. Based on this stackoverflow post and some experimenting I did the following:

git svn clone --preserve-empty-dirs file://path/to/svn-repo/project/trunk/
 
INITIAL_SHA1=$(git rev-list --reverse master | head -1)
# the last commit of old history branch
oldhead=$(git rev-parse --verify old-history)
# the initial commit of current branch
newinit=$(git rev-list master | tail -n 1)
# create a fake commit based on $newinit, but with a parent
# (note: at this point, $oldhead must be a full commit ID)
newfake=$(git cat-file commit "$newinit" \
  | sed "/^tree [0-9a-f]\+\$/aparent $oldhead" \
  | git hash-object -t commit -w --stdin)

# replace the initial commit with the fake one
git replace -f "$newinit" "$newfake"

git push origin 'refs/replace/*'
git filter-branch --tag-name-filter cat -- --all
git replace -d $INITIAL_SHA1

git push

Automatically removing a torrent when it has finished downloading

So as not to saturate my (very limited) uplink when a torrent as finished downloading (using transmission-daemon on my Raspberry Pi) I wanted it to be removed automatically. There were a number of docs on the web about how to do this but here is a simple 2-line file that will do it:

#!/bin/bash
transmission-remote -t $TR_TORRENT_ID -r

Save this as /usr/bin/torrent-complete.sh, chmod +x the script. Stop transmission-deamon (you need to do this rather than a restart as it always dumps the current live config on stop/restart so any changes you make while it is running will be nuked) Then open up /etc/transmission-daemon/settings.json and add (or modify) the following lines:

    "script-torrent-done-enabled": true,
    "script-torrent-done-filename": "/usr/bin/torrent-complete.sh",

Start up transmission-deamon and there you go.

Creating a git history out of a collection of tarballs

So, recently I found some projects from 10+ years back that I had a load of tarballs/zip files for which were taking up quite a bit of backup space. I figured I could unzip each of the repositories in date order and put the changes into a git repository in order to save a bit of space. Here are the commands to do this:

# Put the list of files to import in a file and manually
# edit the order if required to make the history the
# correct order
ls ../tarballs/*.bz2 > ../file_list
git init .
for i in `cat ../file_list`; do
 rm -fr *; # Clear out for next tarball
 tar xjf $i;
 git add -A; # Add any new files
 git add -u; # Remove any files removed from tarball
 git commit -am $i; # Commit with the filename
done

I also did this with some backups from an old psion – converting the 50 or so tarballs to a git repo reduced 500mb of backups to 30mb of git…