'Create a new working directory linked to a repository.
:param path: Path in which to create the working tree.
:param main_repo: Main repository to reference
:param identifier: Worktree identifier
:param mkdir: Whether to create the directory
:return: `Repo` instance'

@classmethod
def _init_new_working_directory(cls, path, main_repo, identifier=None, mkdir=False):

    if mkdir:
        os.mkdir(path)
    if (identifier is None):
        identifier = os.path.basename(path)
    main_worktreesdir = os.path.join(main_repo.controldir(), WORKTREES)
    worktree_controldir = os.path.join(main_worktreesdir, identifier)
    gitdirfile = os.path.join(path, CONTROLDIR)
    with open(gitdirfile, 'wb') as f:
        f.write((('gitdir:    ' + worktree_controldir.encode(sys.getfilesystemencoding())) + '\n'))
    try:
        os.mkdir(main_worktreesdir)
    except OSError as e:
        if (e.errno != errno.EEXIST):
            raise
    try:
        os.mkdir(worktree_controldir)
    except OSError as e:
        if (e.errno != errno.EEXIST):
            raise
    with open(os.path.join(worktree_controldir, GITDIR), 'wb') as f:
        f.write((gitdirfile.encode(sys.getfilesystemencoding()) + '\n'))
    with open(os.path.join(worktree_controldir, COMMONDIR), 'wb') as f:
        f.write('../..\n')
    with open(os.path.join(worktree_controldir, 'HEAD'), 'wb') as f:
        f.write((main_repo.head() + '\n'))
    r = cls(path)
    r.reset_index()
    return r
