Today I Learned

  • Viewing a file's history with git log

    I often want to look at all of the changes a file has gone through. Sometimes git blame is enough because I’m interested in the most recent change. But when the most recent change to a line isn’t relevant—a formatting change, for example—I need something more powerful. Enter git log.

    With git log --follow -p -- myfilename, I can view the entire commit history for a file with a diff of the changes introduced by each commit.

  • How to create a Vim session

    Julia Evans posted earlier about a neat Vim feature I had forgotten about—vim sessions.

    When closing Vim, you can save the view you have open by creating a session:

    :mks ~/.vim/sessions/my-project.vim

    When you need to come back to that work, just reopen the session:

    :source ~/.vim/sessions/my-project.vim
  • Use env to see your environment variables

    To see a list of your environment variables in a UNIX-like system, you can simply use env without any additional arguments. man env will show you other uses of the env utility.

  • Scrolling and threads on the Web

    I learned from Nolan Lawson’s article “Scrolling on the web: A primer” that all the major browser engines now support some degree of “off-main-thread” scrolling. Since scrolling is handled in a separate thread from the main UI thread, you can smoothly scroll through a page, even if that page has a lot going on (for example, JavaScript code blocking other actions on the page). The major browsers all handle scrolling a little differently, and there are differences between various scrolling methods (e.g. mouse wheel, keyboard, one finger on a touch screen).

    There are also some common gotchas to watch out for when using event listeners on scroll and touch events. Setting local or passive event listeners may help, but these implementations depend on what browsers you use.

  • Crypto 101

    Laurens Van Houten’s PyCon 2013 talk Crypto 101 is a great introduction to TLS, some of the basics of symmetric and asymmetric cryptography, and some of the most common hashing algorithms.

  • man -k to search man commands

    Let’s say you’re looking for more information about a command like ansible. You can use man ansible to print the man page for the ansible command. But maybe you want to see a list of man pages that mention the string “ansible”. You can simply write:

    > man -k ansible
    ansible(1)               - run a task on a target host(s)
    ansible-doc(1)           - show documentation on Ansible modules
    ansible-galaxy(1)        - manage roles using
    ansible-playbook(1)      - run an ansible playbook
    ansible-pull(1)          - pull playbooks from VCS server and run them using this machine as the target
    ansible-vault(1)         - manage encrypted ansible vars files (YAML)

    The number next to each man entry represents its section number. Thanks Justin Weissig for helping me learn this useful tip.

  • A bunch of console tricks

    I learned a handful of console tricks from this article.

    When logging to the console, you can use to name a group and collect console ouptput. Calling console.groupEnd() will end the named group. You can even create nested named groups.

    The following code will organize the console output under a group named “Starting up”. Nested under that group will be “First task” and “Second task” groups. These groups can also be auto-collapsed if you don’t want them cluttering up your console output.'Starting up');'First task');
    console.log('First task starting');
    console.log('First task complete');
    console.groupEnd('First task');'Second task');
    console.log('Second task starting');
    console.log('Second task complete');
    console.groupEnd('Second task');
    console.groupEnd('Starting up');


    The console.assert() function will print an error string to the console if the assertion used as the first parameter is false:

    console.assert(article.tagCount > 0, "There are no tags for this article.");

    Check out the full article for more tips.

  • Git Cherry to Compare Branches

    git cherry is a useful part of the git suite that allows you to find commits that have yet to be applied to an upstream branch. Let’s say you have an upstream branch origin/my-cool-feature.

    To see how your HEAD compares with the upstream branch, you can run git cherry origin/my-cool-feature. You will then see a list of commits yet to be applied to origin/my-cool-feature:

    - abcdefg123456...
    + jiw8134dadzfw...
    + zyxwvut123987...

    Those commits marked with a - can be dropped from HEAD when rebased on top of origin/my-cool-feature. The commits with a + will need to be kept so that they can be applied to the upstream branch.

    I found git cherry to be useful today when I wanted to compare a local branch to a branch we were testing for an upcoming release. I wanted to see whether some commits in master had been cherry-picked into our release, and git cherry -v our-release-candidate provided a nice list of the differences between the two branches (the useful -v flag outputs the commit subjects next to the SHA1s).

  • The Richardson Maturity Model of REST

    Developers often through around the term “RESTful API”, but we aren’t always on the same page about what “RESTful” means. I enjoyed this Martin Fowler article on the Richardson Maturity Model because of its helpful explanation of the steps toward REST.

    As a developer consuming APIs, I can get frustrated with APIs that are on lower rungs of this model. Their resources are not easily identified, and interacting with them can be a headache. When I work with APIs like this, I find myself referring to the API documentation way more than I would like to. Unfortunately, it’s hard to avoid doing so because the API is not necessarily consistent across resources.

    My favorite APIs to work with have clearly identified resources and use HTTP verbs as you might expect. Recently, I’ve had the pleasure of working with some APIs that implement hypermedia controls. Fetching a resource, requesting more information about its associated resources, and manipulating resources are easy because the API itself provides clues about where to go next.

    If you find yourself designing and writing APIs I sometimes do, please check out Fowler’s article.

  • View source code in Rails with Pry's show-source

    If you use binding.pry when debugging like I do, you may find Pry’s show-source method useful. Use it to see the source code of a method (complete with syntax highlighting). show-source has the useful alias $, so you can even write something like $ some_method to quickly see a method’s source code while debugging.

    For more on how show-source works, check out the Pry wiki.

  • How to encrypt a file with GPG symmetric encryption

    Sometimes you want to encrypt a file that contains sensitive information like API keys. One way to encrypt such a file is to use a symmetric cipher:

    > gpg --output secrets.yml.gpg --symmetric secrets.yml
    Enter passphrase:

    After entering and confirming a passphrase, you will now have an encrypted file that you can decrypt at any time. This technique might be useful if you want to check in the encrypted file into your version control system, for example.

    To learn more about encrypting and decrypting files, including how to use public-key cryptography to share encrypted files, check out The GNU Privacy Handbook.

  • How HAProxy works

    Mitchell Anicas at Digital Ocean wrote an excellent post about HAProxy which really helped make clear to me how load balancing with HAProxy works. The diagrams in the article are very clear and useful, and it’s worth checking out if you’re new to load balancing concepts.

subscribe via RSS