Exclude a Directory when Grepping

As a developer-of-another-discipline who is now transitioning into a bunch of NodeJS projects, I grep a LOT. However I am usually only interested in the code in the project at hand, and not the dependencies – of course grep has a switch for that! To grep your project and not the dependencies in the node-modules folder, try this:

grep -R --exclude-dir node_modules [what to search for] * 

If you’re using a different tech stack you may want to exclude a different directory (for PHP, the directory would be called vendor), but this is a very handy tip and a bit nicer than the older approach I was using which did the whole search and then used a second grep to eliminate things by using the -v switch.

Using OS X From The Keyboard

I have a new job (Developer Advocate with IBM Cloud Data Services) and subsequently a new work laptop. It’s a mac and after almost 10 years as a linux-only user, that’s rather a shock! Due to some nasty RSI issues, I don’t use a mouse or any other pointing device*, so as well as learning a whole new operating system I also needed to learn its accessibility tools. I’m still at the swearing stage but mostly past the tears so I thought I would share what I’ve found – using a computer from the keyboard is fast and productive for everyone as well as less painful for me, so you may find some tools in here that you want to try yourself.

I collected all my tools, my own notes and scribbled cheatsheets and put them into this gist so that I could refer to them later; I also intend to keep updating this as a reference. Continue reading

Handling Composer “lock file out of date” Warning

Composer is dependency management for PHP, and it consists of two main files:

  • composer.json where you specify your dependencies
  • composer.lock where composer itself records exactly which precise version of every library and every dependency of every library it picked, so all installs will be identical

Crucially, the composer.lock also includes a hash of the current composer.json when it updates, so you can always tell if you’ve added a requirement to the composer.json file and forgotten to install it. Continue reading

Git Won’t Check Out A Path It Autocompleted

One of my git repositories has developed a tendency to refuse to checkout a feature branch locally that exists on the remote repo. My git bash completion works, but then strange things happen! It turned out to be that I had two remotes with the same refspec, so I thought I’d write down the behaviour I saw and hopefully help someone else to fix this problem faster if they see it. Continue reading

Git Pull Causes a Merge

If you type git pull and expect a fast-forward update, but get a merge instead, don’t panic! This usually happens when we’re collaborating on a branch with other people, and we’ve made changes on our local version of a branch, and someone else (or the other you, if you use git to sync between multiple dev platforms) has made changes to the remote version of a branch in the meantime. It also happens really frequently in teams where all commits are to the master branch … yet another reason to have a decent branching strategy.

All that’s happened is something like this:

$ git log --oneline --all --graph --decorate
* 054f163 (HEAD, branch1) Installation instructions for the application
| * 0ce808c (origin/branch1) Fixing template layout
|/  
* 927aad9 A random change of 731 to ideas2.txt

Since the last common commit, there are commits on your local branch, and the remote one. You could just let the merge go ahead but there are other options. You could also check out a new branch at this point, reset your tracking branch to the right place and then reapply your changes using cherry-pick or by rebasing and then fast-forward merging your branch. Continue reading

Debugging rst2pdf and pygments

I create my slide decks from ReStructuredText, which is a text markup format. Working this way makes it easy to add into source control, fast to work with, and also accessible since I don’t use a mouse or other pointing device so traditional slide deck creation programs are kind of difficult. Text-based wins every time for me. While working on a new slide template, I ran into some difficulties and had to figure out how to inspect what was going on. I seem to struggle with this every time so I am writing my troubleshooting guide here for when I need it next. Continue reading

Code Reviews: Before You Even Run The Code

I do a lot of code reviewing, both in my day job as principal developer and also as an open source maintainer. Sometimes it seems like I read more code than I write! Is that a problem? I’m tempted to say that it isn’t. To be a good writer, you must be well-read; I believe that to be a good developer, you need to be code-omnivorous and read as much of other people’s code as possible. Code reviews are like little chapters of someone else’s code to dip into.

Over time I’ve developed some particular processes that I find helpful when reviewing code. In particular, I often surprise people at how much review I do before I run the code. Sometimes I grab the branch so that I can use my local diff tools, but I don’t actually execute code until I’ve established some basic facts. This post is a little insight into what’s happening in this not-running-the-code-yet zone. Continue reading

Count Changed Lines in Git

I have a favourite set of switches to git log, but today I wanted to answer the question “You deleted how much code today?” so I thought I’d share how I did that

git log --numstat will show you how many lines were added (first column) and removed (next column) per file, kind of a more scientific version of the --stat switch. And if you’re thinking of scripting this to gather stats, try it with --oneline as well, it’s easier to parse.

Scaling and Sizing with PDFJam

I find myself needing to take a PDF, output it at a specific size, and have the result offset to the top right hand side of the screen. To achieve it, I needed a few new switches to my good friend PDFJam, so I thought I’d share my command!

pdfjam --suffix converted --papersize '{1920px,1080px}' --scale 0.4 --trim "-6cm -1cm 13cm 8cm" slides.pdf

The --suffix is instead of giving an output filename, whatever you feed in ends up with the suffix in its filename. This is very handy because I use this command in a script and only need to pass in one variable. The --papersize isn’t a switch I have used before either but you can set exact sizes for the final output which is nice. The --trim switch can also be used to set --clip=true to remove the trimmed space from the document if desired.

I find PDFJam a very handy tool but with not nearly enough blog posts and code snippets around, so I’m dropping my command for future reference (yours as well as mine!).

Vimdiff and Vim to Compare Files

At the moment I’m working on a tricky project where two similar projects diverged. Very similar things happened to them both, but not quite the same things – and now we’re merging the codebases to give us as much common code as possible. All this simply serves to set the scene of exactly what I was doing spending a whole day with large code diffs – I had to look up a few things so I thought I’d capture them while I can remember. Continue reading