django-environments / bin / djenv.mercurial

#
# Mercurial utilities for django-environments
# 

hgbaseport=7100

# Hash function that should run just about anywhere
function _hash() {
    if which openssl > /dev/null; then
        openssl md5
    elif which md5 > /dev/null; then
        md5
    else
        cksum
    fi
}

# Start hg server in daemon mode and open first argument in browser
function hgserve() {
    _verify_project_root || return 1

    local tmpdir=$PROJECT_ROOT/tmp
    [ ! -d $tmpdir ] && mkdir $tmpdir
    hgpidfile=$tmpdir/hgserve.pid

    # Determine port number
    local numhash=`basename $PROJECT_ROOT | _hash | sed 's/[^0-9]//g'`
    hgport=`expr $hgbaseport + ${numhash:0:2}`

    # See if the server is alive - start it if not
    kill -0 `cat $hgpidfile 2>&1` > /dev/null 2>&1
    if [ ! $? -eq 0 ]; then
        cwd=`pwd`
        cdroot
        hg serve --port $hgport --daemon \
            --pid-file $PROJECT_ROOT/tmp/hgserve.pid
        sleep 0.5
        cd $cwd
    fi

    [ ! "$1" = "--no-open" ] && open_url http://localhost:$hgport/$1
}

# Browse files in current branch
function hgbrowse() {
    _verify_project_root || return 1

    hgserve --no-open

    path=file/`hg branch``echo \`pwd\` | sed "s#$PROJECT_ROOT##"`
    [ ! -z "$1" ] && path=$path/$1

    open_url http://localhost:$hgport/$path
}

# Shut down hg server
function hgkill() {
    kill -9 `cat "$hgpidfile" 2>&1` > /dev/null 2>&1
    if [ ! $? -eq 0 ]; then
        echo "Server not running or other error" 1>&2
    fi
}

# Runs an hg command on all hg repositories in the externals directory,
# e.g. 'hgexternals pull -u'
function hgexternals() {
    _verify_project_root || return 1

    for external in $PROJECT_ROOT/externals/*; do
        if [ -d $external/.hg ]; then
            echo `basename $external`:
            _IFS=$IFS
            IFS= # magic
            hg --repository $external $*
            IFS=$_IFS
            echo
        fi
    done
}

# Pull and update the project and all Mercurial externals
function hgfetchall() {
    _verify_project_root || return 1

    hg --repository $PROJECT_ROOT pull -u
    echo
    hgexternals pull -u
}

# List all .orig files
function hgfindorig() {
    _verify_project_root || return 1

    find -H $PROJECT_ROOT -name \*.orig
}

# Remove all .orig files
function hgremoveorig() {
    _verify_project_root || return 1

    find -H $PROJECT_ROOT -name \*.orig -print -delete
}

# Find out who added those XXX / TODO / FIXME statements
# Note: searches only through clean Mercurial-versioned files
function hgtodos() {
    _verify_project_root || return 1

    pattern="(TO.?DO|XXX|FIXME)\b"
    (
        IFS="
"
        cd $PROJECT_ROOT
        # Find clean files with comments and feed them to hg blame
        for file in `grep --extended-regexp $pattern --files-with-matches \
                            --binary-files=without-match \
                                \`hg status --clean --no-status\``; do
            hg blame --user --file --changeset --line-number $file | \
                sed 's/^ *//' | grep --extended-regexp $pattern
        done
    )
}

# Clean up
function _djenv_mercurial_cleanup () {
    # Stop server
    hgkill > /dev/null 2>&1
    # Clean environment
    unset hgport
}

_djenv_register_cleanup _djenv_mercurial_cleanup 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.