Current Git Branch in Bash Prompt

Good trick; saves typing git status or git branch so often. Note: it’s not very clear from the article, but the code for the prompt goes in ~/.bashrc.


I wasn’t interested in colors, and I’m fairly happy with Terminal’s standard prompt (\h:\w \u\$), so I ended up with something pretty close to the default:

parse_git_branch() {
  git name-rev HEAD 2> /dev/null | sed 's#HEAD\ \(.*\)#[\1]#'
}
export PS1="\h:\w \u\$(parse_git_branch)\$ "

Which yields a prompt like Pangu:~/programming/git/ratafiacurrant stilist[master]$.

The downsides are that it can get rather long (especially since I keep my Terminal window width to 80 characters plus scrollbar) and its placement can seem a bit odd.

written 21 January, 02010 Comments

How to easily back up your delicious bookmarks

Jason Scott:

Don’t trust the Cloud to safekeep this stuff. Hell yeah, use the Cloud, blow whatever you want into the Cloud. The Internet’s a big copy machine, as they say. Blow copies into the Cloud. But please:

  • Don’t blow anything into the Cloud that you don’t have a personal copy of.
  • Insult, berate and make fun of any company that offers you something like a “sharing” site that makes you push stuff in that you can’t make copies out of or which you can’t export stuff out of. They will burble about technology issues. They are fucking lying. They might go off further about business models. They are fucking stupid. Make fun of these people, and their shitty little Cloud Cities running on low-grade cooking fat and dreams. They will die and they will take your stuff into the hole. Don’t let them.
  • Recognize a Cloud when you see it. Are you paying for these services? No? You are a sucker. You are giving people stuff for free. I pay for Vimeo and I pay for Flickr and a couple other things. This makes me a customer. Neither of these places get my only copy of anything.

In that spirit, I’m going to demonstrate two ways to export your delicious bookmarks into an XML file for safety. As noted previously, this is really a pretty simple thing to do —  you just need to call the URL api.del.icio.us/v1/posts/all and pass in basic auth. For more information on this and everything else in the delicious API, please refer to the documentation.

cURL

cURL is definitely the easiest way to do this. Many web APIs will readily interact with cURL, and delicious’ is especially simple due to the use of basic auth (meaning there’s no need to do a login dance) and the usage of URLs rather than arguments.

curl --user 'username':'password' -o ~/Desktop/delicious.xml 'https://api.del.icio.us/v1/posts/all'

We can turn this into a simple little Bash script:

#!/bin/bash
user='username'
password='password'

curl --user $user:$password -o ~/Desktop/delicious.xml 'https://api.del.icio.us/v1/posts/all'

Ruby

The Ruby form is a little more verbose, but still quite simple. Please note that the following code has largely been borrowed from the nice example here, which unfortunately does not specify a license.

#!/usr/bin/ruby

require 'net/http'
require 'net/https'

user = 'user'
password = 'password'

http = Net::HTTP.new('api.del.icio.us', 443)
http.use_ssl = true
xml = http.start { |http|
  req = Net::HTTP::Get.new('/v1/posts/all', {'User-Agent' => 'Delicious backup'})
  req.basic_auth(user, password)
  http.request(req).body
}

dumpfile = File.new('delicious.xml', 'w')
dumpfile.puts xml
dumpfile.close

Automated backups

Now that you’ve got a script to make an XML backup, why not have it automatically run so you don’t have to remember?

Here’s my crontab, which automatically runs the backup every day at noon:

00 12 * * * /Users/stilist/deliciousbackup.sh

Where deliciousbackup.sh is the Bash script noted above.

Ideas for further development

  1. These examples download the entire bookmark collection regardless of whether anything has changed. Checking /posts/update could save on bandwidth.
  2. It’s sort of assumed that the service will always be around, and the API call will always succeed. Error handling would be useful.
  3. Only a single day’s archive is kept. This means that if something goes wrong with the online copy — accidentally deleting tags, account vandalism, or anything like that — you’ll have a maximum of twenty-four hours to fix it before the backup is overwritten.

Finally

The code here is provided under the MIT license. For a copy, drop by the git repository.

And if you code up any of the improvements noted in the previous section, I’d love to know!

written 21 January, 02009 Comments

Searching your delicious bookmarks for a lacking tag

Several months ago I began to more thoroughly tag things as I bookmarked them, adding in things like the publication year, authors, and source websites. Unfortunately, this meant that I had several years of bookmarks without these tags. This information is particularly useful with my free music bookmarks, allowing me to quickly find all albums from a given year or artist.

The specific problem that inspired this post was a wish to find everything I’d bookmarked from the Internet Archive that didn’t have the associated src:archive.org tag. Although delicious has a little-known system:has:x search, it doesn’t have a negation operator.

But! Because delicious has a nice API, we can get around this limitation by stringing together a few command line tools.

How it works

(Note: the following assumes a command prompt and a Mac or Linux system, and is written from the perspective of a Mac. I don’t know how this would be accomplished with Windows, and don’t particularly care.)

Because we can’t do this via the web, we’ll download a copy of our bookmarks and work locally:

curl --user username:password -o ~/Desktop/delicious.xml -O 'https://api.del.icio.us/v1/posts/all'

Inserting your own delicious username and password in the appropriate spots. Try not to do this too often, because a large number of bookmarks means a lot of work for the servers.

Now to do the actual processing:

cat delicious.xml |grep archive.org |grep -v src:archive.org > del.txt

What this line does:

  • opens the file that holds our downloaded bookmarks
  • hands it over to grep, which
  • looks for bookmarks that include the text archive.org (i.e. they’re from the site)
  • but not (-v) src:archive.org (they’re missing the tag)
  • then spits the results into a new text file

Finishing up

From there, we can just open the text file and add in the tags via the web interface.

Not an especially elegant solution (particularly as adding the tags is a slow process), but still much simpler than checking every link manually through the website.

written 15 December, 02008 Comments