Skip to main content

git unhg

·2 mins

I should tidy up my $HOME/bin directory, some of actually useful scripts and configurations were scattered here and there. Anyway, I’ve been using this one script when I work with shreds:

#! /usr/bin/env bash

interim_branch=default

rollgit() {
    [[ $# -ne 1 ]] && {
        echo 'wrong number of arguments, USAGE:'
        printf "\tgit roll <branch>\n"
        exit 1
    }
    git rebase $1 && git checkout $1 && git rebase $interim_branch && git branch -D $interim_branch
}

unhg() {
    [[ $# -ne 2 ]] && {
        echo 'wrong number of arguments, USAGE:'
        printf "\tgit unhg <remote> <branch>\n"
        exit 1
    }
    git-hg fetch $1 && git checkout -b $interim_branch FETCH_HEAD && git roll $2
}

self=$(basename $0)

case $self in
    git-unhg)
        unhg $*
        ;;

    git-roll)
        rollgit $*
        ;;

    *)
        echo 'Invalid command specified'
        ;;
esac

Shreds source revisions is actually tracked on mercurial. But its integration testing and deployment hook is run under semaphoreci which only supports git. While exporting hg to git is easy, keeping the hash-chain consistent is apparently a bit tedious. I use git-hg which as its name implied is a reversed version of hg-git. It’s possible to re-import git commits back to mercurial. But it involves hg creating an entirely –disconnected– revision tree in the repo.

For example here is the commit we want to push back to mercurial, here is where the commit attached:

So we need to rebase from -r 1496 to -r 747.

Here’s after some rebase and strip:

I still wondering how should I wrote these steps into script like git-unhg above. Is it possible to detect detached tree in hg? Also have no idea how to find the correct changesets to rebase without tracing it manually ._.