Overwriting a branch in Git with remote results (and bypassing merge conflicts)

If you’ve ever tried to do a git pull to get a branch up to date and have run into conflicts that you just want to bypass and go with the contents of the remote branch, do the following:

# Fetch from the default remote of origin.
git fetch origin
# Check out the branch you want to override the conflicts upon.
git checkout master
# Reset your current branch of master to origin's master.
git reset --hard origin/master

Reference: Stack Overflow: Git pull from remote.. Can I force it to overwrite rather than report conflicts?

Get a list of files added, removed and modified in Git between two branches

It’s sometimes useful to know exactly what files you’ve made changes to in your feature branch as you’re getting ready to merge back into your trunk code. Git’s diff option allows for this to be done quick easily:

git diff --name-status master..newfeature

The output looks like the following:

M       .gitmodules
A       plugins/backplane.php
A       plugins/multiple-post-thumbnails
M       themes/sportsnet/css/scss/_inc-controls.scss
M       themes/sportsnet/functions.php
M       themes/sportsnet/header.php
M       themes/sportsnet/single-sn-article.php
M       themes/sportsnet/single-sn-blog-entry.php
M       themes/sportsnet/single-sn-signing.php
M       themes/sportsnet/single-sn-trade.php
A       themes/sportsnet/zones/articles-comment-form.php
A       themes/sportsnet/zones/global/user-account-links.php

Reference: Stack Overflow – Showing which files have changed between git branches

Using Mozilla Thunderbird to access your Outlook Web Access (OWA) e-mail account

Like many others, my employer makes heavy use of Microsoft’s Exchange system to run its corporate e-mail infrastructure. And for the most part I think highly of that collection of software and services: Microsoft Outlook and the Exchange system that runs behind it are full-featured and generally terrific. Unfortunately, its Web-based offering is pretty lame in comparison, and is generally set up on hardware that strains to keep up with the amount of use that it faces.

I’ve always liked Mozilla’s free Thunderbird e-mail client, and have tried in various ways to get it to pull down e-mail from Outlook Web Access (OWA) without success. Having finally sat down and considered the problem carefully, I’ve hit on success. The key is an amazing little piece of free software called DavMail Gateway, which installs on your local system and acts as a translator between your e-mail client and the remote OWA server. For posterity, I thought I’d capture how my setup works.

  1. Download and install DavMail Gateway to your system. On my Linux (Ubuntu 13) system, I followed the Debian package instructions, and actually did not apply any extra patches to get it running successfully.
  2. Next, I booted up DavMail Gateway from my Applications menu, which presented me with a setup screen. Here, I copied the settings I found in another post on the web and visible in the image shown above, but I’ll break them down again in specifics…
  3. Change the OWA URL value to http://webmail.rci.rogers.com/exchange/ .
  4. Leave all other port settings as-is (specifically IMAP as 1143 and SMTP as 1025) and press the Save button.
  5. Start up Mozilla Thunderbird and create a new account.
  6. Enter your Full Name, E-mail Address and the Password that you would use to access your e-mail regularly via OWA. Click the Test button; when it fails, you’ll be presented with a new set of fields.
  7. Your Incoming settings should be as follows: A server type of IMAP; a Server Hostname of “localhost” (no quotes), a Port of 1143, a SSL setting of “None”, and Authentication set to “Normal Password”.
  8. Your Outgoing settings should be as follows: A server type of SMTP; a Server Hostname of “localhost” (no quotes), a Port of 1025, a SSL setting of “None”, and Authentication set to “Normal Password”.
  9. Just one last field to set; have your Username value be your username without any domain: In my case, that would simply be “sully.syed” (no quotes).
  10. Click the Re-test button; this time, Thunderbird should let you proceed, and will begin the process of synchronizing your e-mail client with your mail. Welcome to webmail via Thunderbird!


Checking Akamai cache expiry times on your website’s pages

This involves sending some custom headers along with your HTTP GET request, so utilize either the wget command line tool:

wget -S -O /dev/null --header="Pragma: akamai-x-cache-on, akamai-x-cache-remote-on, akamai-x-check-cacheable, akamai-x-get-cache-key, akamai-x-get-extracted-values, akamai-x-get-nonces, akamai-x-get-ssl-client-session-id, akamai-x-get-true-cache-key, akamai-x-serial-no" http://www.sportsnet.ca/

Or the curl command line tool:

curl -H "Pragma: akamai-x-cache-on, akamai-x-cache-remote-on, akamai-x-check-cacheable, akamai-x-get-cache-key, akamai-x-get-extracted-values, akamai-x-get-nonces, akamai-x-get-ssl-client-session-id, akamai-x-get-true-cache-key, akamai-x-serial-no" -IXGET http://www.sportsnet.ca/

The X-Cache-Key setting will contain the amount of time the URL is cached for; in this example, the time is 1 minute (“1m”):

X-Cache-Key: /L/370/77322/1m/www.sportsnet.ca/

Running Windows? No problem – grab a compiled version of wget for Windows.

Source: Stack Overflow – What’s the best way to troubleshoot Akamai headers these days?

Selectively taking commits out of a GitHub pull request

Once in a while it’s necessary to get picky and only accept one or more commits from a bundle sent over in a GitHub pull request – or maybe someone’s been working on a fork of a repository of yours and you want to pull a change or two in. The git command cherry-pick is there for this purpose.

To start, you’ll need to run git fetch on the forked repository:

git fetch git@github.com:hakimel/Fokus.git

Next, take the hash of the commit you want to retrieve files for and run the cherry-pick command on it:

git cherry-pick 00988a520bda3e365c74033a44a96941d77be86f

That should do it! Check your git log to see the commit pulled into your repository.

Source: StackOverflow – Pull in changes from a Github fork

Profiling a MySQL query to optimize performance

The Query Profiler in MySQL isn’t something I’ve spent much time in recently – with more sites making use of popular CMSes like WordPress, query performance isn’t something that’s top of mind anymore (Automatic seems to do a good job in this area). But it’s still a useful tool when you’ve already picked the low hanging fruit off of the optimization tree.

Profiling is enabled on an individual basis for each MySQL session; when the session ends, all profiling information is lost.

To check to see if profiling is currently enabled for your session, do:

mysql> SELECT @@profiling;
| @@profiling |
|           0 |
1 row in set (0.00 sec)

Next, enable profiling for all queries:

mysql> SET profiling = 1;
Query OK, 0 rows affected (0.00 sec)

And run the query you’d like to see a breakdown for:

mysql> SELECT COUNT(*) FROM wp_posts;
| count(*) |
|   238121 |
1 row in set (18.80 sec)

Get the numeric ID of the profile we want to see:

| Query_ID | Duration | Query                                 |
|        1 | 18.80000 | SELECT COUNT(*) FROM wp_posts;        |
1 row in set (0.00 sec)

Finally, actually see the query profiled:

| Status                         | Duration  |
| starting                       |  0.000027 |
| checking query cache for query |  0.000041 |
| checking permissions           |  0.000017 |
| Opening tables                 |  0.000018 |
| System lock                    |  0.000008 |
| Table lock                     |  0.000037 |
| init                           |  0.000014 |
| optimizing                     |  0.000008 |
| statistics                     |  0.000016 |
| preparing                      |  0.000013 |
| executing                      |  0.000008 |
| Sending data                   | 18.802902 |
| end                            |  0.000015 |
| end                            |  0.000008 |
| query end                      |  0.000006 |
| storing result in query cache  |  0.000453 |
| freeing items                  |  0.000009 |
| closing tables                 |  0.000007 |
| logging slow query             |  0.000007 |
| logging slow query             |  0.000031 |
| cleaning up                    |  0.000005 |
21 rows in set (0.00 sec)

To get even more in depth, check out the optional type values for the SHOW PROFILE command, or use SHOW PROFILE ALL FOR QUERY 1; and view everything MySQL’s got at once.

Very basic JSONP

JSONP allows you to make HTTP requests outside of your own domain due to the SCRIPT tag not having the same-domain limitation XMLHttpRequest does. The basic form of this workaround is as follows:

var scr = document.createElement('script');
scr.src = 'http://openexchangerates.org/latest.json?callback=formatCurrency';

function formatCurrency(data) {
	// Do stuff with the data that's been returned.

Lines 1 – 3 of the above create a SCRIPT tag in the DOM, defines the URL to retrieve data from and in line 3 makes the request. To continue the execution cycle, the script at openexchangerates.org must wrap its data in a function:

formatCurrency({ "hello" : "Hi, I'm JSON. Who are you?"})

This calls the locally defined function formatCurrency(), which does whatever it needs to do with the returned data. A quick hack to make your API (assuming you control it) work with JSONP would be as follows:

// assume $json holds the JSON response
if ($GET['callback'] != '') $json = $GET['callback']."( $json )";
return $json;

I have yet to test this, but I imagine you could also be much more daring and avoid having to get the following code uploaded to your API of choice. To do this, execute the first bit of JavaScript on this page, and then execute an eval() statement on the data in the manner of the below:

eval('formatCurrency(' + returned_data + ')');

How to center text within a DIV element

This is one of those things I’m blogging about strictly so I can search it later and find the answer. Below, we use a DIV contained within a DIV to center text/images/anything on a page, which we’ve arbitrarily decided is 1024px in width.

<div style="width: 1024px;">
	<div style="width: 50%; margin: 0 auto;">
		Everything within this DIV element will be centered within the 1024px outer DIV element!

Reference: StackOverflow – How to center DIV in DIV?

Referring to a PHP object property using a variable

This is one of those really simple things that I use infrequently enough that I tend to forget: If you’ve tried using $object->$field_name to address a property of a PHP object, you’ll know that PHP isn’t happy with that syntax. Instead, use:

return $object->{$field_name};

Outputting MySQL results to a CSV file without OUTFILE access

The MySQL client’s -e (or –execute=) flag allows you to specify a query to be run at the command line, which can then be output back to a local file:

$ mysql -h server.com -u username -p -D database -e "SELECT user_id FROM users ORDER BY user_id" > output.tsv

Note that by default this will output a tab-separated value file (TSV), not a comma-separated value (CSV) file.