def Restore(repoPath, branchList, transaction):
    print('Restoring state for transaction: {tr}'.format(tr=transaction))
    print('branch list:')
    for br in branchList:
        print('  - Branch {br} at {hash}.{current}'.format(br=br['name'], hash=br['commit'], current=(' Current.' if br['is_current'] else '')))
    state = {'transaction': transaction, 'branch_list': branchList}
    repo = git.open(repoPath)
    if (repo is None):
        print("Failed to open git repository '{r}'".format(r=repoPath))
        return 1
    stateFilePath = None
    with tempfile.NamedTemporaryFile(mode='w+', prefix='ac2git_state_', delete=False) as stateFile:
        stateFilePath = stateFile.name
        stateFile.write(json.dumps(state))
    hashObj = repo.raw_cmd(['git', 'hash-object', '-w', stateFilePath])
    if (hashObj is None):
        raise Exception('Failed to restore state! git hash-object -w {f}, returned {r}.'.format(f=stateFilePath, r=hashObj))
    else:
        os.remove(stateFilePath)
    refResult = repo.raw_cmd(['git', 'update-ref', 'refs/ac2git/state', hashObj])
    if (refResult is None):
        raise Exception('Failed to restore state! git update-ref refs/ac2git/state {h}, returned {r}.'.format(h=hashObj, r=refResult))
    return 0
def Restore(repoPath, branchList, transaction):
    print('Restoring state for transaction: {tr}'.format(tr=transaction))
    print('branch list:')
    for br in branchList:
        print('  - Branch {br} at {hash}.{current}'.format(br=br['name'], hash=br['commit'], current=(' Current.' if br['is_current'] else )))
    state = {'transaction': transaction, 'branch_list': branchList}
    repo = git.open(repoPath)
    if (repo is None):
        print("Failed to open git repository '{r}'".format(r=repoPath))
        return 1
    stateFilePath = None
    with tempfile.NamedTemporaryFile(mode='w+', prefix='ac2git_state_', delete=False) as stateFile:
        stateFilePath = stateFile.name
        stateFile.write(json.dumps(state))
    hashObj = repo.raw_cmd(['git', 'hash-object', '-w', stateFilePath])
    if (hashObj is None):
        raise Exception('Failed to restore state! git hash-object -w {f}, returned {r}.'.format(f=stateFilePath, r=hashObj))
    else:
        os.remove(stateFilePath)
    refResult = repo.raw_cmd(['git', 'update-ref', 'refs/ac2git/state', hashObj])
    if (refResult is None):
        raise Exception('Failed to restore state! git update-ref refs/ac2git/state {h}, returned {r}.'.format(h=hashObj, r=refResult))
    return 0