Code snippets, tech tricks and other bits and bobs

git: 'pull' is not a git-command fix - CentOS 5

This had been bugging me for a while:

# git pull origin master
git: 'pull' is not a git command. See 'git --help'.

Did you mean this?
    shell

I'd done my googly thang several times, and tried the few solutions suggested, to no avail.

One I'd probably seen mentioned before is setting the *nix environment variable.
Now, I'm not a full on sys-admin, and some bits I've just not really picked up, but this time I decided to do a little research into setting environment variables and give it a whack:

UNIX: Set Environment Variable [new window]

Yeah, ok, that's actually piss-easy.

So, first find out your git exec path. This is the location of your git-core directory.

Directory find is a bit tedious, so I just did:

# locate git-core

Which gave a massive list of results, but in there I found my path - in my case /usr/local/libexec/git-core

So following the instructions for bash:

# export GIT_EXEC_PATH=/usr/local/libexec/git-core

I than ran set to check the change had taken, and tried to pull again.

Huzzah!

At last.

Okay kiddies, that's the end of another adorable bedtime story, off to sleep now. 

Filed under  //   *nix   CentOS 5   Environment Variables   GIT  

If only... Wishing on a macrocosm

Would that my 2012 blog stats reflected the rest of the world.

IE < 9%

What bliss that would be...

Rank

Browser contribution to total:
1.   Firefox 43.15%
2.   Chrome 37.39%
3.   Internet Explorer 8.66%
4.   Safari 7.70%
5.   Opera 2.11%
6.   Android Browser 0.43%
7.   Mozilla Compatible Agent 0.09%
8.   Opera Mini 0.09%
9.   PagePeeker.com 0.09%
10.   RockMelt 0.09%

Cleaning up PHP - Terminal conditions with continue;/return;

PHP (or any language where code blocks are indented) can get a bit messy sometimes

for($i = 0; $i <= $n; $i++){
    if ($some_conditon) {
        if ($some_conditon2) {
            if ($some_conditon3) {
                if ($some_conditon4) {
                    if ($some_conditon5) {
                        do_something();
                    }
                }
            }
        }
    }
}

OK, that’s not a literal example, but it does happen. If your condition is terminal, that is: if it is not met, no further action is taken; then there is no need to wrap a massive chunk of code in curly braces and indent it yet further. Break out of the cycle! If you are in a loop, you can just continue; – for example instead of:

for($i = 0; $i <= $n; $i++){
    if($terminal_condition != false){
        do_something();
    }
}

you can do this:

for($i = 0; $i <= $n; $i++){
    if($terminal_condition == true){
        continue;
    }
    do_something();
}

In this ridiculously abbreviated example, it does look longer, and yes it will always result in more code, but where you have several terminal conditions in a section of logic which are separated by a number of lines, this technique can save you one hell of a lot of indenting, making your code that much more readable. Our original example, while longer, would be a lot simpler to follow:

for($i = 0; $i <= $n; $i++){
    if (!$some_conditon) {
        continue;
    }
    if (!$some_conditon2) {
        continue;
    }
    if (!$some_conditon3) {
        continue;
    }
    if (!$some_conditon4) {
        continue;
    }
    if (!$some_conditon5) {
        continue;
    }
    do_something();
 }

In a similar way to loops, if you are in a function, you can simply return; (or return true/false; as appropriate) earlier rather than doing it in an else statement; Shocking? No. A revelation? Hardly, these are not new constructs. But common? Not from the code I see. Give it a go. I will…

Filed under  //   PHP  

jQuery .width() may not return pixels if element is hidden

jQuery 1.7.1

This got me for a while - while there is some discussion of .width() not behaving properly, only this stackOverflow [new window] hinted at the fact I wasn't mad - all the others being sparse on examples, or poorly worded, leaving me wondering.

But, hell, I'll give it a go, I thought, and it turned out that I was suffering exactly this:

The jQuery documentation clearly states:

The difference between .css(width) and .width() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example,400px). The .width() method is recommended when an element's width needs to be used in a mathematical calculation.

Ok, fair enough.

But I had {n} columns in a graph, containing a bar in each. The columns are set to (100 / {n})% width while the graph is being populated - and most importantly here, hidden.

I wanted to give my bars a little padding in pixels, so I needed to get the column size in pixels. In my example I had 4 columns, width 25% each, which worked out as 88px. Calling $('.column).width() -6 I expected to get 82, but got 19 instead.

One stackOverflow suggested that you should use an id instead - not ideal, as I don't always know what the ids will be and don't want to bother storing one just for the sake of the calculation. But I tried it with this case as I did know the ids - and it still didn't work.

So I moved the column .width() call to after my .show() call - and magically, it returned 82.

Call it an 'undocumented feature', call it a bug, but watch out for it!

I ought to report this, but as of writing the jQuery trac is down. MySQL has gone away apparently :'(

Filed under  //   Javascript   jQuery  

Sublime Text is... sub... oh I can't do it

But it is. It really is.

I CAN NOT STRESS THIS ENOUGH.

What exactly?

Sublime Text 2

I've been searching for an IDE for years to break away from DreamWeaver, which has hung on this long simply becuase I knew it so well, and didn't want to have my workflow disrupted while I re-learned all those little things that make you productive, and nothing felt right.

And then... yeah, I don't really need to explicate further.

I can't even begin to enumerate the reasons, but for starters, it is beautiful, fast, simple, comprehensive and INSANELY customisable - all at the same time.

Just one example:

[Ctrl/Cmd + P], [@] and you have a list of all the functions in your file - select one to jump there.

Wonderful...

 

Get it fired up (Win/OSX/Linux)

Read this: Sublime Text Tips & Tricks
Read this: Customise Sublime Text 2

I'm currently working with the following essential packages:

 - Package Control (the daddy!)
 - Alignment
 - Bracket Highlighter
 - SFTP
 - Sidebar Improvements
 - Sidebar GIT
 - SublimeCodeIntel

I also have the following installed
 - Additional PHP Snippets
 - CodeIgniter 2.0
 - copy-file-name
 - INI
 - jQuery Snippets Pack
 - JS Minifier
 - JSFormat
 - PHPDoc
 - TrailingSpaces

No links are provided, as none are needed. Once you install Package Control (See the linked articles above for instructions) it is childishly simple to install packages through the Sublime interface. It's easy enough to browse the entire library to pick up what you need for your work flow and syntaxes - err... synti? Sod it, languages.

But I wouldn't recommend just mass installing - not all are perfect, and winnowing out problem packages could be a royal pain.

I don't recomend BufferScroll - it is insanely annoying, and seems to have no preferences.

Filed under  //   IDE   Sublime Text   Text Editor  

Mass delete email (or any files) over SSH

I get a lot of automated mail that I do need for analysis, but it can be a pain to delete it all.

Admittedly, if I set filters better it would be easier, but even so, if you have thousands of emails in a folder and you need to delete them using webmail remotely, it is time consuming.

So let's do it with SSH.

WARNING. This shit is dangerous. If any of this doesn't make sense DON'T DO IT!

Login to your server via SSH

Change to your mail dir (this is the location on my CentOS 5 box)
Note the mail address format - @ is escaped with \, period are replaced with underscores:

$ cd /home/{domain username}/mail/.{user\@domin_tld}/{folder}/

If you are cleaning your inbox the folder is

cur

Test the message selection before you delete!
I'm deleting Cron messages from a server. The message source for these all contain 'Subject: Cron <user\@server\>' (I've anonymised the server address)
Note the search is regex, so special chars must be escaped

$ grep -l 'Subject\: Cron \<user\@server\>' * > grep.txt

Then I open the grep.txt file, copy some filenames and then open those files to double check I'm selecting the right messages.

Once I'm satisfied I'm not selecting anything I shouldn't:

$ grep -l 'Subject\: Cron' * | xargs rm -f

Shazzam. From >3000 emails to 150 in two minutes. I should probably check that account more often...

Filed under  //   *nix   CentOS 5   Regex   SSH  

Tool causes Apache to freeze

When I read the headline, I thought they were talking about me...

http://www.h-online.com/open/news/item/Tool-causes-Apache-web-server-to-freez...

A previously unknown flaw in the code for processing byte range headers allows version 2.2.x of the Apache Web Server to be crippled from a single PC. A suitable "Apache Killer" Perl script that impressively demonstrates the problem has already been published on the Full Disclosure mailing list.

The tool sends GET requests with multiple "byte ranges" that will claim large portions of the system's memory space. A "byte range" statement allows a browser to only load certain parts of a document, for example bytes 500 to 1000. This method is used by programs such as download clients to resume downloads that have been interrupted; it is designed to reduce bandwidth requirements. However, it appears that stating multiple unsorted components in the header can cause an Apache server to malfunction.

No official patch has been released, but a functional workaround is to use rewrite rules that only allow a single range request in GET and HEAD headers. This should not present a problem for most applications. To enable the rules, administrators must load the Apache Web Server's mod_rewrite module.

Another suggested workaround is to use the mod_header module with the RequestHeader unset Range configuration to completely delete any range requests that may be contained in a header. However, this approach is likely to cause more problems than restricting the number of ranges. Admins should use the tool to test the effectiveness of their measures before others do it for them.

Filed under  //   Apache   Security   mod_rewrite  

[OS X] Set up DiffMerge for Dreamweaver file comparison

I've just started in a new office and we are all Mac'ed up, which changes the development process a bit. For one thing, I have SO much pixelated real estate I have had to put ghosts of standard window sizes on my desktop background, as it's easy to lose touch with real users' screen resolution. 1024x768 just gets lost in the corner!

One of my first productivity priorities was getting a decent visual diff tool hooked up to Dreamweaver - a colleague recommended TextWrangler but it doesn't highlight differences in clearly, so after a little research I settled on DiffMerge, a utility available for Mac, PC and *nix.

It takes a tiny bit of setting up however, a little knowledge of OS X file system, and the instructions contained a slight discrepancy in my version, so here's how if you find yourself in the same situation:

First download the DiffMerge DMG, mount the DMG (simply by double clicking the downloaded DMG) and drag DiffMerge.app to your applications.

All standard stuff so far, and any Mac user should be able to do that.

Now, launch Terminal (search for it with Finder if you need it).

Log in with your system password.

change into the mounted DMG:

$ cd /Volumes/DiffMerge\ {version number}/

For n00bs, once you have got as far as typing 'Diff' you can press [TAB] to autocomplete.

sudo copy (copy as a superuser) the shell script to /usr/bin:

$ sudo cp Extras/diffmerge.sh /usr/bin/diffmerge
 
You will be prompted for your system password

Set the permissions on the script:

$ sudo chmod 755 /usr/bin/diffmerge

Copy the man (manual file) to your system. This is where the instructions were in error as they listed the file as diffmerge.1, and my copy was diffmerge.man1:

$ sudo cp Extras/diffmerge.man1 /usr/share/man/man1/diffmerge.man1
 

Set permissions for the man file:

$ sudo chmod 644 /usr/share/man/man1/diffmerge.man1

Open Dreamweaver, and go to preferences (⌘ + U), select the 'File Compare' category and enter '{your drive}:usr:bin:diffmerge' where {your drive} is the name of your... drive... Yeh. For example: 'Macintosh HD:usr:bin:diffmerge' and click [OK]

Now you can start diffing files - when uploading and DW asks if you want to compare, click the button and DiffMerge will open automatically.

Alternatively [Ctrl] + Click on any file in choose 'Compare with {Remote Server} / {Local Server}' from the context menu as appropriate.

Filed under  //   Diff   DiffMerge   DreamWeaver   OS X   utilities  

Oops, I did it again... Outlook 2010 attachment and account check

We've all done it - please see attached.... DOH!

And with multiple email accounts, and forwarding to your main address, it's easy to send from your default account when you meant to use another...

But there are solutions to hand!

FAD (Forgotten Attachment Detection) from MS Office Labs solves problem 1:

http://www.officelabs.com/projects/forgottenattachmentdetector/Pages/default....

It requires Microsoft Visual Studio Tools for Microsoft Office V3 which is only 2mb:

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=23656

I believe .NET is required too.

 

For account selection, a small registry modification will prompt you to choose the account each time - essentially removing the 'default account' functionality.

http://www.msoutlook.info/question/477

Filed under  //   MS Office   MS Windows   Outlook 2010   utilities  

'Deprecated' error warnings - PHP 5.3

Just reviewing some sites in my portfolio, and I notice that a CubeCart installation on a shared host is broken. Oh dear...

Deprecated: Function set_magic_quotes_runtime() is deprecated in /path/to/domain/shop/ini.inc.php on line 114
Warning: ini_set(): Cannot change zlib.output_compression - headers already sent in /path/to/domain/shop/ini.inc.php on line 118 
Warning: Cannot modify header information - headers already sent by (output started at /path/to/domain/shop/ini.inc.php:114) in /path/to/domain/shop/index_enc_ion.php on line 31 
Warning: Cannot modify header information - headers already sent by (output started at /path/to/domain/shop/ini.inc.php:114) in /path/to/domain/shop/index_enc_ion.php on line 32
Deprecated: Function eregi() is deprecated in /path/to/domain/shop/includes/functions.inc.php on line 408

Ah, great. Without warning, we've been upgraded to PHP 5.3

Thanks for that...

Anyway, it's easily solved.

With 5.3 a new error level has been introduced - E_DEPRECATED, so it's easy to suppress deprecated errors:

In your scripts:

error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);

In the case of CubeCart 4 this can be acheived in ini.inc.php line 101.

In php.ini:

error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED

in .htaccess:

php_value error_reporting 1

.htaccess requires the correct integer value equivalent of your chosen reporting level - these are a touch obscure, but setting it to 1 means report fatal run-time errors and unrecoverable errors, which will do the trick.

Filed under  //   .htaccess   CubeCart   PHP   PHP 5.3   php.ini