Mercurial tips

by iampeterbanjo on January 21, 2012

I have been using Mercurial as my version control system for a while now. Here are some pointers I picked up along the way.

Mercurial won’t forget files! ‘hg forget <filename>’ returns ‘No such file or directory’

Although Mercurial can perform commits, adds etc. from any location in the folder structure it seems like forget needs to be started from the right spot. Meaning when you type ‘hg status’ you see something like -

<path>/<filename>

so naturally I tried

hg forget <path>/<filename>

But that path did not resolve to the file I was trying to target from my current location in the hierarchy. This works -

hg forget <relative-path>/<filename>


How to restore a file from Mercurial?

First, use hg grep to find the deleted file you wish to recover. The output of this command will show you the last revision for which the file was present, and the path to the deleted file.

Second, run hg revert -r <revision number> <path to deleted file> The deleted file will now be in your working copy, ready to be committed back into head.
http://stackoverflow.com/questions/2175427/how-can-i-recover-a-removed-file-in-mercurial-if-at-all

Mercurial push returns HTTP Error: 500 (Permission denied)

Run chown -R www-data /path/to/repos on the server. It looks like this has to be done for every repository. Is it possible to run apache with higher privileges?

Share

How to deploy an iOS application using an ad hoc distribution

by iampeterbanjo on January 21, 2012

  • Go into www.developer.apple.com > Login > iOS dev centre > Provisioning portal
  • In Provision > Distribution generate a new profile using "ad hoc" distribution
  • Download the provisioning profile. Add it to iTunes using drag and drop or File > Add to Library
  • In Xcode create a new archive by right click on the project > Edit scheme > Archive > Ok
  • In the Xcode > Window > Organizer window select the archive and click share
  • To use iTunes to sync the app select iOS App Store Package (.ipa)
  • Find the generated file in Finder and add to iTunes
  • Attach the device to iTunes and perform a sync to add the application

See:
http://developer.apple.com/library/ios/#documentation/Xcode/Conceptual/ios_development_workflow/145-Distributing_Applications/distributing_applications.html
https://developer.apple.com/library/ios/#recipes/xcode_help-scheme_editor/Articles/SchemeArchive.html#//apple_ref/doc/uid/TP40010402-CH6
https://developer.apple.com/library/ios/#featuredarticles/FA_Wireless_Enterprise_App_Distribution/Introduction/Introduction.html

Share

Test Driving TDD

by iampeterbanjo on July 27, 2011

I have become inspired and convinced by the idea of test driven development. Kent Beck said it best when he said -

We write code because that is our work and we write tests so that we know when we are done.

- Beck, K.  Extreme Programming Explained: Embrace Change

Or something like that.

So I have started kicking the tires on the metaphorical test driven development car. I started reading this book on Test Driven JavaScript Development (Developer’s Library). You can read the first few chapters for free on Safari books online. I think I’m going to get a subscription to Safari books for a while since it might be cheaper for me given my reading habits. I digress.

So I was sitting at my desk and I had to do some AJAX thingy with a form and an email address as the input for a client’s website. It looked pretty straight forward. But how do I give some email validation feedback to the user? I am thinking a regex expression would be nice. But I am not a regex ninja so I might need a few goes at this and how do I know if it works? The last thing I want is to prevent the user from entering valid email addresses – false positives are better than false negatives. Aha! How about I write some tests?

It took me a while and I had a lot of help from this guy’s blog but I came up with a regular expression that passed enough test cases for me to feel satisfied with it. Simply run it in your browser’s developer’s console to see it go.

 

Share

Forbidden words in JavaScript

by iampeterbanjo on May 5, 2011

JavaScript has 3 forbidden words -

  • With
  • In
  • Eval

Eval is generally understood to perform poorly and to be unsafe. But what’s so bad about ‘with’ and ‘in’?

With

It is sort of a quirk with JavaScript where the scope of an operation changes when you use “with”. As with a few things in programming it is not that they can’t be used but they are often used badly. In JavaScript we are accustomed to the scope of “global” and functions. So a variable used in one function can’t be accessed by another function, right? Well when you use “with” you’re in ANOTHER scope, the scope of the thing you’re using. Let me explain.

Imagine you create a wipe function. It takes a cloth object and wipes a table. After you wipe a dirty table the cloth sadly becomes dirty. In JavaScript you could say –

You can run this code in a browser console to see the results. If you look at this simple program at the end the cloth is clean but it is not blue, what happened? We were supposed to be in the scope of the “cloth” object which is how we set its property to clean but something weird happened and we couldn’t set the cloth’s colour. Uh oh! We ran into problems because the colour property was defined but JavaScript usually lets us define properties on the fly.

As you can see, there are some cases where you might want to use “with” but it can also lead to bugs which are unexpected and so difficult to debug. There’s a thread on Stackoverflow about it. My point of view is along the lines of what Alan Storm says.

In

My opinion is as a result of coming across a bug a long while ago. I remember trying to use “in” to iterate over some DOM nodes because I thought it was cool but strange things kept happening and I was lost as to why so I used another method. Much later I read about how it walks up the prototype chain (with respect to DOM nodes it’s a bit more complicated than that). That’s why it’s evil. Again, not because it is “wrong” but it causes more problems than it solves so it is better to avoid it and do what I know is going to work the way I want it to work.

Share

Internet World – Earls Court, London

by iampeterbanjo on May 5, 2011

It has a cheesy name but there is a trade show event in London next week called – Internet World. The site doesn’t use comic sans anywhere and the speakers include Google, eBay, Microsoft and other trend setters.

I won’t be attending but I’ll be checking the site and signing-up to the mailing list. Linkage below.

Newsletter archive

http://www.internetworld.co.uk/page.cfm/ID=74

eCommerce and mCommerce theatre programme –

http://www.internetworld.co.uk/page.cfm/link=50

Videos from last years keynote

http://seminarstreams.com/conference-seminars/internet-world/2010/seminars.aspx?key=keynotes

Share

Synchronize vim using Dropbox

by iampeterbanjo on March 22, 2011

If you don’t know what Dropbox is, where have you been? Here’s how I use Dropbox to synchronize my vim installation on Windows XP at work and Windows 7 at home. If you don’t know what vim is, don’t worry about it. Maybe this tip will help you install something cool and keep your settings across your operating system(s).

  • Get a compatible version for the operating systems you will be running it on gvim73_46.exe is working on Windows XP and 7
  • Install into root Dropbox directory using the default settings. I did this first on Windows XP but you don’t have to. Vim will create a directory tree like \Vim\<vim version>\
  • Migrate your vim settings into new Vim directory in Dropbox into respective locations e.g. copy over _vimrc from “Vim” directory of old vim installation and
    from “\Vim\<old version>\” copy over plugins and colors folders
  • Install Vim on other operating system – in my case Windows 7 but don’t copy over the Vim files. I just wanted vim registered on Windows. I unchecked anything that said “create” but left features that would register Vim on this OS.

 

    installing gvim - my settings

    installing gvim to Dropbox root

    vim running my settings from Windows XP on Windows 7

    Done!

    If you have problems with your settings not being in sync make sure that you are executing the vim executable out of your Dropbox folder.

    Share

    Wrangling with SQL regular expressions

    by iampeterbanjo on March 2, 2011

    I have been doing some data wrangling recently. One problem I worked through was how to find all the instances of a new line character that had made it’s way into our database.

    After some researching the query looked like this

    Then I asked myself if I could strip out this character. Hmm.. So I came up with this –

    I would have liked to use this in an update statement but I couldn’t crack that last piece of the puzzle. Can you?

    Share

    404 Errors on your WordPress blog posts

    by iampeterbanjo on February 27, 2011

    If you start getting 404 errors when you click your posts in your WordPress blog try this

    • Log into the dashboard
    • Go to settings
    • Change the permalink structure. Try those links. They should be working now.
    • Change the permalink structure back to what it was. They should still be working.

      I am not entirely sure why this worked but it did for me, I hope it does for you.

      May the force be with you

      Share

      AJAX cross-domain request using iframes

      by iampeterbanjo on February 27, 2011

      I had a situation where I had to send a form to a 3rd party email subscription management thingy. But every time I posted the form using jQuery the browser was killing it because of cross-domain restrictions. I asked around the web dev team and came up with this solution. Simply create a URL fragment from the form inputs, and use it as the source of an iframe. Of course, as long as you don’t care what happens to the data this is fine – it’s basically best-effort.

      Caveats are that if the domain the client is on is HTTPS the URL fragment also has to be HTTPS but if the destination server doesn’t have a valid SSL certificate the submission fails. It fails silently in Chrome, Firefox and IE7. Sometimes it triggers a message in IE8 but it definitely triggers a security pop-up in IE6 (of course).

      I found that Google chrome is the best browser for developing this because it actually tells you what is going on in the console and in the iframe – the rest just act like nothing happened.

      Share

      Google Chrome Extensions–short development primer

      by iampeterbanjo on November 13, 2010

      I think a browser extension is the best place to get started because it’s easier to see immediately how an extension ties together.

      To create a “hello world” browser extension we’ll need -

      manifest.json

      The properties of our extension are described in a JavaScript object (JSON). This tells Chrome what kind of extension we are creating (which affects where the icon gets placed) and other useful details that the user sees. The format is something like this -

      Required properties – if we get any of these wrong an error will be shown

      • Dictionary keys must use double quotes
      • Version must be specified

      Recommended properties -

      • name
      • type
      • description
      • icons -

      popup.html

      This is the page that will be displayed when a user clicks on our extension icon. Imagine it is a proper HTML page with head, body and everything. We even don’t have to insert html, head or body tags although most extensions do.

      icons

      In order to display an icon on the extension management page we have to specify a 128×128 px version of our icon through the “icons” property So we’ll need two icons – one 128px square and another 19px square. An easy way is to create a 128 x 128 px icon in a png format and also save it as a 19 x 19 px version. You can name them “icon_128.png” and “icon.png” respectively.

      That’s all we need for now.

      Installation

      Download this file and unzip it - Google Chrome Extension – hello world

      In Chrome click on the “Settings” icon (the spanner/ratchet) and select “Extension”. This will take you to the extension management page.

      In the top right corner click on “Developer mode”. This will allow us to load an extension on our computer.

      Click on “Load unpacked extension” and navigate to the location of the unzipped extension.

      Once loaded an icon will appear in Chrome outside of the address bar – to the right.

      If you click on this you will the contents of popup.html appear

      That’s it for now.

      Make a comment or post any questions that you have. :-)

      Share