Skip to content

Instantly share code, notes, and snippets.

View hugowetterberg's full-sized avatar

Hugo Wetterberg hugowetterberg

View GitHub Profile
@hugowetterberg
hugowetterberg / gist:796784
Created January 26, 2011 14:55
Reading the reference from HEAD in a git repo at path
-(NSString*)currentBranch {
const char *refAnfang = "ref: ";
NSString *branch;
const char *filename = [[path stringByAppendingPathComponent:@"HEAD"] cStringUsingEncoding:NSUTF8StringEncoding];
FILE *file = fopen (filename, "r");
if (file != NULL) {
char line [256];
if (fgets(line, sizeof line, file) != NULL) {
line[strlen(line)-1] = 0;
<?php
class ServicesContext implements ArrayAccess {
protected $handlers;
protected $namespaces;
private $values = array();
function __construct($handlers = array(), $namespaces = array()) {
$this->handlers = $handlers;
$this->namespaces = $namespaces;
@hugowetterberg
hugowetterberg / brew_mamp.markdown
Created June 21, 2010 07:16
How to get a working XAMP stack with brew
@hugowetterberg
hugowetterberg / trickle_test.js
Created May 26, 2010 07:15
Test to sketch out how the trickle router could work
var sys = require('sys'),
trickle = require('../lib/trickle'),
router = new trickle.Router(),
number = /^\d+$/,
tests, path, route;
router.addRoute('node/:node', 'view_node');
router.addRoute('node/:node/edit', 'edit_node');
router.addRoute('node/:node/view', {
'sample': 'The route object can be anything!',
@hugowetterberg
hugowetterberg / install.php
Created May 25, 2010 12:21
Quick Drupal snippet/memo for bulk adding content permissions for roles
<?php
/**
* This update enables the content_permissions module and gives all anonymous
* and authenticated users view access to all fields; and all 'administrator'-
* users edit access.
*/
function skaneutil_update_6003() {
require('includes/skaneutil_updateutils.inc');
# Given a table term_article_count(tid, node_count)
# the following could be executed to refresh a node count cache.
TRUNCATE term_article_count;
INSERT INTO term_article_count(tid, node_count)
SELECT tn.tid, COUNT(n.nid)
FROM term_node AS tn
INNER JOIN node AS n ON (tn.vid=n.vid AND n.type='location' AND n.status=1)
GROUP BY tn.tid;
<?php
$update = util_record_exists('table_name', $record);
drupal_write_record('table_name', $record, $update);
function util_record_exists($table, $record, $return_keys = TRUE) {
$schema = drupal_get_schema($table);
$sql = "SELECT COUNT({$schema['primary key'][0]}) FROM {{$table}} WHERE ";
$keys = array();
@hugowetterberg
hugowetterberg / envelop_point.php
Created March 1, 2010 12:42
Function to create a circle with a radius of X km, centered on a lat lon
<?php
/**
* Envelop a coordinate in a circle.
*
* @param string $lat
* Latitude.
* @param string $lon
* Longitude.
* @param float|int $radius
@hugowetterberg
hugowetterberg / dumpdatabases.py
Created February 24, 2010 10:49
Small script to dump all local mysql databases
#!/usr/bin/env python
import os
import re
import subprocess
user="onlyme"
password="xxxxxxx"
proc = subprocess.Popen(["mysql", "-u" + user, "-p" + password],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
@hugowetterberg
hugowetterberg / gitopendiff.py
Created February 17, 2010 19:56
Script that shows the diff for a file (using git) using Apple's graphical diff tool FileMerge
#!/usr/bin/env python
import os
import sys
import tempfile
import subprocess
file = sys.argv[1];
proc = subprocess.Popen(['git', 'ls-tree', 'HEAD'],
stdout=subprocess.PIPE, stderr = subprocess.PIPE)
output, errors = proc.communicate()