hg sync /

'''pull, update and merge in one command (DEPRECATED)'''

from mercurial.i18n import _
from mercurial.node import nullid, short
from mercurial import commands, cmdutil, hg, util
from mercurial.lock import release

testedwith = 'internal'

def sync(ui, repo, source='default', **opts):
    '''pull changes from a remote repository, merge new changes if needed.

    This finds all changes from the repository at the specified path
    or URL and adds them to the local repository.

    If the pulled changes add a new branch head, the head is
    automatically merged, and the result of the merge is committed.
    Otherwise, the working directory is updated to include the new

    When a merge is needed, the working directory is first updated to
    the newly pulled changes. Local changes are then merged into the
    pulled changes. To switch the merge order, use --switch-parent.

    See :hg:`help dates` for a list of formats valid for -d/--date.

    Returns 0 on success.

    date = opts.get('date')
    if date:
        opts['date'] = util.parsedate(date)

    parent, p2 = repo.dirstate.parents()
    branch = repo.dirstate.branch()

    if p2 != nullid:
        raise util.Abort(_('outstanding uncommitted merge'))

    wlock = lock = None
        wlock = repo.wlock()
        lock = repo.lock()

        remoterepository = hg.peer(repo, opts, ui.expandpath(source))

        ui.status(_('pulling from %s\n') % util.hidepassword(ui.expandpath(source)))
        modheads = repo.pull(remoterepository)
        ui.write('pull complete\n')
        mod, add, rem, del_ = repo.status()[:4]

        if mod or add or rem or del_:
            ui.warn('outstanding uncommitted changes, push skipped, not in sync\n')
            return 0

        bheads = repo.branchheads(branch)
        bheads = [head for head in bheads if len(repo[head].children()) == 0]
        if len(bheads) > 2:
            ui.status(_('not merging with %d remote repository new branch heads '
                        '(use "hg heads ." and "hg merge" to merge them)\n') %
                      (len(newheads) - 1))
            raise util.Abort('push skipped, not in sync\n')
            return 0

        err = False

        # Is this a simple fast-forward along the current branch?
        newheads = repo.branchheads(branch)
        newchildren = repo.changelog.nodesbetween([parent], newheads)[2]
        if len(newheads) == 1 and len(newchildren) and  newchildren[0] != parent:
            ui.status('updating local repository')
            updatefailed =  hg.update(repo, newchildren[0]) #Returns true if unresolved conflicts exist
            if updatefailed :
                ui.status('merge conflicts occurred, update skipped\n')
                raise util.Abort('push skipped, not in sync\n')
        elif len(newheads) > 1:
        # Otherwise, let's merge.
            if newheads:
                # By default, we consider the repository we're pulling
                # *from* as authoritative, so we merge our changes into
                # theirs.
                if opts['switch_parent']:
                    firstparent, secondparent = parent, newheads[0]
                    firstparent, secondparent = newheads[0], parent
                    ui.status(_('updating to %d:%s\n') %
                hg.clean(repo, firstparent)
                ui.status(_('merging with %d:%s\n') %
                          (repo.changelog.rev(secondparent), short(secondparent)))
                err = hg.merge(repo, secondparent, remind=False)

            if not err:
                # we don't translate commit messages
                message = (cmdutil.logmessage(ui, opts) or
                           ('Automated merge with %s' %
                editor = cmdutil.commiteditor
                if opts.get('edit'):
                    editor = cmdutil.commitforceeditor
                n = repo.commit(message, opts['user'], opts['date'], editor=editor)
                ui.status(_('new changeset %d:%s merges remote changes '
                            'with local\n') % (repo.changelog.rev(n),

        if not err:
            ui.status(_('pushing to %s\n') % util.hidepassword(ui.expandpath(source)))
            err = (repo.push(remoterepository) == 1)
            ui.status('push complete\n')
        return err

        release(lock, wlock)

cmdtable = {
             ('e', 'edit', None, _('edit commit message')),
             ('', 'switch-parent', None, _('switch parents when merging')),
         ] + commands.commitopts + commands.commitopts2 + commands.remoteopts,
         _('hg sync [SOURCE]')),