Setting up git-clang-format

I am draconian when it comes to code formatting. Combined with an “I’ll know it when I see it” attitude, this can become irritating. In exasperation, a colleague suggested using clang-format to automatically format our Objective C codebase. Their proposal:

I’ll get the tool’s formatting specification as close to what you like as possible. In return, you agree that you’ll accept what the tool produces.

So, quite some time ago, we introduced a clang-formatting file to our repository.

Huge, horrible whitespace monsters

Originally we suggested using a plugin for Xcode to format code. This is a pretty great tool, but we had a problem: it was hard to format just the lines that had been changed, leading to frustration picking apart “real” changes from white space changes.

The other day I came across a script to solve this problem, from the LLVM team themselves, git-clang-format. It’s a python script that hooks into git to give you a git clang-format command, which runs clang-format over the changes staged for commit.

Surprisingly – to me at least – it’s really easy to install a git extension:

  1. Call the script something starting with git-.
  2. Put it in your path.
  3. Make it executable.

So for git-clang-format, the process runs something like this:

  1. Install the standalone clang-format tool: brew install clang-format.
  2. Download git-clang-format from here. Read it to check for nastiness.
  3. Move the script somewhere in your path, for me mv git-clang-format ~/bin/git-clang-format.
  4. Make the script executable: chmod +x ~/bin/git-clang-format.
  5. Check it’s been picked up by git: git clang-format -h.
  6. Try it out with: git clang-format --diff.

You can now run clang-format, which will modify your working copy, rather than your staged files. This allows for easy backing out of the changes it makes.

> git status
On branch 45845-reusable-fetcher
Your branch is up-to-date with 'origin/45845-reusable-fetcher'.

Changes to be committed:
  (use "git reset HEAD ..." to unstage)

    modified:   Classes/common/CDTFetchChanges.h

> git clang-format
changed files:
    Classes/common/CDTFetchChanges.h

Running on every commit

The final step is to make sure clang-format is run before each commit. Some may want to just run the tool on every commit, but I’m happier with a simple reminder.

Following some instructions from Atlassian, I wrote a simple git pre-commit hook which runs git clang-format --diff, aborting the commit if something other than no modified files to format or clang-format did not modify any files is returned. Rather fragile, but as long as I don’t update my git-clang-format script, it should continue working.

#!/usr/bin/env python

import subprocess
output = subprocess.check_output(["git", "clang-format", "--diff"])

if output not in ['no modified files to format\n', 'clang-format did not modify any files\n']:
    print "Run git clang-format, then commit.\n"
    exit(1)
else:
    exit(0)

Git stores commit hooks on a per-repository basis, inside the .git folder of the repository. So we need to add this script there, and make it executable:

  1. cp check_formatting.sh .git/hooks/pre-commit
  2. chmod -x .git/hooks/pre-commit

Once all this is in place, if you try to make a commit with which clang-format finds an issue the commit will be aborted. Of course, you could be more or less terse in your message:

> git commit -m "Documentation updates"
Run git clang-format, then commit.

Given how lax I am at keeping to my own standards, this is invaluable.

Facebook's presumably a more successful AOL

Back in the late 1990s, AOL tried to create an alternative internet. It was roundly derided, and everyone instead used its sign-up CDs as drink coasters.

As far as I can see, Facebook is now trying very hard to create an alternative internet for its users, one which threatens to be more successful.

Facebook has said publicly that it wants to make the experience of consuming content online more seamless. News articles on Facebook are currently linked to the publisher’s own website, and open in a web browser[…]

In addition to hosting content directly on Facebook, the company is talking with publishers about other technical ways to hasten delivery of their articles.

(nyt)

Facebook has a lot of users, and it’s worrying how many organisations are willing to give up their own identities — online and off — for a perceived share of those. Any company that believes Facebook has anyone’s interests but Facebook’s at heart is a fool. Otherwise its way of making “consuming content online more seamless” would probably not involve putting it into Facebook.

Instead I’m inclined to agree with Dave Pell:

Here’s what tech people are really good at:

Tech.

Because tech happens to be at the center of a financial and cultural revolution, the people who have been talented and fortunate enough to make to the apex of the industry are perceived as oracles of industry; every industry.

And therefore are no more qualified to tell other industries what to do than the people who tell technology companies what to do from the outside.

Though that coming from someone who’s ceded their writing to Medium is a little comic.

Using Instruments to track down memory leaks

I was prototyping using TMDb for film search in One to Watch to solve the problem of Freebase’s API going away. While doing this I noticed that the memory usage of the app was going up significantly every time I opened a film’s details view.

Using the Allocations tool in Instruments, I could see that the memory was increasing in a staircase like manner. Looking at the memory listing shown in Instruments, it was clear that this stepping was caused by decoded PNG images, used to display the cover art. Each time the details view was shown, another 3MB PNG image appeared in Instruments.

The way displaying a film’s detail works is incredibly simple and standard: the details view, SavedItemDetailsView is pushed onto a UINavigationContoller object’s stack. It’s popped off the stack when the view is dismissed. Why wasn’t the SavedItemDetailsView object getting freed when the details view was popped from the stack maintained by the UINavigationController?

At first I had a classic “blame the compiler” moment: “the UINavigationController must be retaining my view for too long!”. StackOverflow, bless its socks, had a bunch of people having similar issues and leaping to blame UINavigationController too. Of course, it turned out to be my fault. As I assume it did for most of those SO posts.

Thankfully a friend pointed me toward solving the issue by pointing out how to see retain/release calls during the running of the program:

  1. Start a new Allocations session in Instruments.
  2. Before starting your run:
    1. Select the Allocations tool in the top segment of the screen.
    2. Show the right hand side bar (View -> Inspectors -> Show Record Settings).
    3. Enable “Record reference counts”.

Now, start the application running using the “record” button in the top left. Do the thing which is causing memory to increase, in my case showing and hiding the details view. As described above, at first I just saw a lot of PNG memory usage. However, this PNG image just got allocated via ImageIO so didn’t give much away. So my first port of call was the parent view controller; it just seemed a good place to start. So I needed to see the retain/release process for the view controller:

  1. Put the name of the class (SavedItemDetailsView in my case) into the search box next to the inspector sidebar.
  2. Hit the tiny right-arrow that appears when you mouseover the class name for one of the instances.
  3. Hit the tiny right-arrow appearing when hovering over the memory address.

You should get a (potentially very long) list of retain/release calls for your class. In my listing was an obvious unbalanced retain call. Once I found this, I easily tracked down to a delegate that was strong rather than weak. Correcting the property declaration quickly fixed my problem.

One to Watch's film querying at risk

One to Watch, my app to help you remember films you want to watch, uses Freebase when it’s searching for films. Freebase is an amazing database, built on community data. Google bought Freebase a few years ago, and made Freebase into the basis of Knowledge Graph. Up until now, this hasn’t been a problem; Google kept the old Freebase API up and running, albeit at new URLs.

However, by chance when updating some URL handling code in One to Watch, I was checking the Freebase API and noticed:

Warning: The Freebase API will be retired on June 30, 2015.

Oh oh.

On the Freebase blog:

[Before the end of March 2015] We’ll announce a transition plan for the Freebase Search API & Suggest Widget to a Knowledge Graph-based solution

Fingers crossed for anything half so powerful as the Freebase API. Otherwise I’m not sure what One to Watch will use. In addition, Freebase becomes read-only on March 31st, which means there’ll be no new films added.

Slightly worried: I can’t easily tell my users why OtW might just stop working for them — even if I can update the app, users of older versions are going to be upset :(

Careful wording

Moonpig, an online greetings card company suffered a security vulnerability. While the vulnerability was serious, far worse was the company’s deliberately misleading statement in response to the disclosure:

Indeed this tiny subset of information was “safe”. What was leaked was all other personal information held by Moonpig about every customer. Much worse than a password or a credit card that can be easily changed. That the company issues a statement like this indicates how little they care about their customers.

And obviously the fact that the company knew about the problem for 17 months and did nothing.