hgext/fetch.py
author Bryan O'Sullivan <bos@serpentine.com>
Wed, 05 Mar 2008 09:20:57 -0800
changeset 6207 03b13d853dc6
parent 6206 0b6f12495276
child 6211 f89fd07fc51d
permissions -rw-r--r--
fetch: make test reproducible, tiny code cleanup
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2800
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     1
# fetch.py - pull and merge remote changes
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     2
#
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     3
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     4
#
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     7
3891
6b4127c7d52a Simplify i18n imports
Matt Mackall <mpm@selenic.com>
parents: 3877
diff changeset
     8
from mercurial.i18n import _
2800
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     9
from mercurial.node import *
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 3891
diff changeset
    10
from mercurial import commands, cmdutil, hg, node, util
2800
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    11
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    12
def fetch(ui, repo, source='default', **opts):
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    13
    '''Pull changes from a remote repository, merge new changes if needed.
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    14
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    15
    This finds all changes from the repository at the specified path
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    16
    or URL and adds them to the local repository.
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    17
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    18
    If the pulled changes add a new head, the head is automatically
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    19
    merged, and the result of the merge is committed.  Otherwise, the
6206
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    20
    working directory is updated to include the new changes.
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    21
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    22
    When a merge occurs, the newly pulled changes are assumed to be
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    23
    "authoritative".  The head of the new changes is used as the first
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    24
    parent, with local changes as the second.  To switch the merge
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    25
    order, use --switch-parent.
6163
1f733c2f0165 Document log date ranges and mention 'hg help dates' for all commands (issue998)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6139
diff changeset
    26
1f733c2f0165 Document log date ranges and mention 'hg help dates' for all commands (issue998)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6139
diff changeset
    27
    See 'hg help dates' for a list of formats valid for -d/--date.
1f733c2f0165 Document log date ranges and mention 'hg help dates' for all commands (issue998)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6139
diff changeset
    28
    '''
2800
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    29
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
    30
    def postincoming(other, modheads):
2800
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    31
        if modheads == 0:
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    32
            return 0
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    33
        if modheads == 1:
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
    34
            return hg.clean(repo, repo.changelog.tip())
2800
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    35
        newheads = repo.heads(parent)
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    36
        newchildren = [n for n in repo.heads(parent) if n != parent]
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    37
        newparent = parent
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    38
        if newchildren:
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    39
            newparent = newchildren[0]
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
    40
            hg.clean(repo, newparent)
2800
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    41
        newheads = [n for n in repo.heads() if n != newparent]
6206
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    42
        if len(newheads) > 1:
2800
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    43
            ui.status(_('not merging with %d other new heads '
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    44
                        '(use "hg heads" and "hg merge" to merge them)') %
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    45
                      (len(newheads) - 1))
6206
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    46
            return
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    47
        err = False
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    48
        if newheads:
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    49
            # By default, we consider the repository we're pulling
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    50
            # *from* as authoritative, so we merge our changes into
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    51
            # theirs.
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    52
            if opts['switch_parent']:
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    53
                firstparent, secondparent = newparent, newheads[0]
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    54
            else:
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    55
                firstparent, secondparent = newheads[0], newparent
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    56
                ui.status(_('updating to %d:%s\n') %
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    57
                          (repo.changelog.rev(firstparent),
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    58
                           short(firstparent)))
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    59
            hg.clean(repo, firstparent)
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    60
            ui.status(_('merging with %d:%s\n') %
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    61
                      (repo.changelog.rev(secondparent), short(secondparent)))
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    62
            err = hg.merge(repo, secondparent, remind=False)
2800
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    63
        if not err:
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
    64
            mod, add, rem = repo.status()[:3]
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 3891
diff changeset
    65
            message = (cmdutil.logmessage(opts) or
5798
86f5d8f608b7 fetch: hide authentication details
Bryan O'Sullivan <bos@serpentine.com>
parents: 5147
diff changeset
    66
                       (_('Automated merge with %s') %
86f5d8f608b7 fetch: hide authentication details
Bryan O'Sullivan <bos@serpentine.com>
parents: 5147
diff changeset
    67
                        util.removeauth(other.url())))
2800
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    68
            n = repo.commit(mod + add + rem, message,
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
    69
                            opts['user'], opts['date'],
2800
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    70
                            force_editor=opts.get('force_editor'))
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    71
            ui.status(_('new changeset %d:%s merges remote changes '
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    72
                        'with local\n') % (repo.changelog.rev(n),
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    73
                                           short(n)))
6206
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
    74
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
    75
    def pull():
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 3891
diff changeset
    76
        cmdutil.setremoteconfig(ui, opts)
2800
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    77
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    78
        other = hg.repository(ui, ui.expandpath(source))
5798
86f5d8f608b7 fetch: hide authentication details
Bryan O'Sullivan <bos@serpentine.com>
parents: 5147
diff changeset
    79
        ui.status(_('pulling from %s\n') %
86f5d8f608b7 fetch: hide authentication details
Bryan O'Sullivan <bos@serpentine.com>
parents: 5147
diff changeset
    80
                  util.hidepassword(ui.expandpath(source)))
2800
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    81
        revs = None
6207
03b13d853dc6 fetch: make test reproducible, tiny code cleanup
Bryan O'Sullivan <bos@serpentine.com>
parents: 6206
diff changeset
    82
        if opts['rev']:
03b13d853dc6 fetch: make test reproducible, tiny code cleanup
Bryan O'Sullivan <bos@serpentine.com>
parents: 6206
diff changeset
    83
            if not other.local():
03b13d853dc6 fetch: make test reproducible, tiny code cleanup
Bryan O'Sullivan <bos@serpentine.com>
parents: 6206
diff changeset
    84
                raise util.Abort(_("fetch -r doesn't work for remote "
03b13d853dc6 fetch: make test reproducible, tiny code cleanup
Bryan O'Sullivan <bos@serpentine.com>
parents: 6206
diff changeset
    85
                                   "repositories yet"))
03b13d853dc6 fetch: make test reproducible, tiny code cleanup
Bryan O'Sullivan <bos@serpentine.com>
parents: 6206
diff changeset
    86
            else:
03b13d853dc6 fetch: make test reproducible, tiny code cleanup
Bryan O'Sullivan <bos@serpentine.com>
parents: 6206
diff changeset
    87
                revs = [other.lookup(rev) for rev in opts['rev']]
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
    88
        modheads = repo.pull(other, heads=revs)
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
    89
        return postincoming(other, modheads)
3223
53e843840349 Whitespace/Tab cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2849
diff changeset
    90
6139
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5798
diff changeset
    91
    date = opts.get('date')
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5798
diff changeset
    92
    if date:
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5798
diff changeset
    93
        opts['date'] = util.parsedate(date)
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5798
diff changeset
    94
2800
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    95
    parent, p2 = repo.dirstate.parents()
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    96
    if parent != repo.changelog.tip():
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    97
        raise util.Abort(_('working dir not at tip '
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    98
                           '(use "hg update" to check out tip)'))
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    99
    if p2 != nullid:
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   100
        raise util.Abort(_('outstanding uncommitted merge'))
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4730
diff changeset
   101
    wlock = lock = None
2825
0496cfb05243 fetch: lock repo across pull and commit
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2824
diff changeset
   102
    try:
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4730
diff changeset
   103
        wlock = repo.wlock()
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4730
diff changeset
   104
        lock = repo.lock()
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
   105
        mod, add, rem = repo.status()[:3]
2827
2a0c599f7bb0 fetch: hold lock and wlock across all operations
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2825
diff changeset
   106
        if mod or add or rem:
2a0c599f7bb0 fetch: hold lock and wlock across all operations
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2825
diff changeset
   107
            raise util.Abort(_('outstanding uncommitted changes'))
2a0c599f7bb0 fetch: hold lock and wlock across all operations
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2825
diff changeset
   108
        if len(repo.heads()) > 1:
2a0c599f7bb0 fetch: hold lock and wlock across all operations
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2825
diff changeset
   109
            raise util.Abort(_('multiple heads in this repository '
2a0c599f7bb0 fetch: hold lock and wlock across all operations
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2825
diff changeset
   110
                               '(use "hg heads" and "hg merge" to merge)'))
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
   111
        return pull()
2825
0496cfb05243 fetch: lock repo across pull and commit
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2824
diff changeset
   112
    finally:
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4730
diff changeset
   113
        del lock, wlock
2800
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   114
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   115
cmdtable = {
135823f37304 new extension: fetch -> combine pull and merge/update
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   116
    'fetch':
4730
eadfaa9ec487 Updated command tables in commands.py and hgext extensions.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4549
diff changeset
   117
        (fetch,
5147
c80af96943aa refactor options from cmdtable
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 4917
diff changeset
   118
        [('r', 'rev', [], _('a specific revision you would like to pull')),
4730
eadfaa9ec487 Updated command tables in commands.py and hgext extensions.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4549
diff changeset
   119
         ('f', 'force-editor', None, _('edit commit message')),
6206
0b6f12495276 fetch: switch the default parent used for a merge
Bryan O'Sullivan <bos@serpentine.com>
parents: 6163
diff changeset
   120
         ('', 'switch-parent', None, _('switch parents when merging')),
5147
c80af96943aa refactor options from cmdtable
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 4917
diff changeset
   121
        ] + commands.commitopts + commands.commitopts2 + commands.remoteopts,
4730
eadfaa9ec487 Updated command tables in commands.py and hgext extensions.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4549
diff changeset
   122
        _('hg fetch [SOURCE]')),
eadfaa9ec487 Updated command tables in commands.py and hgext extensions.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4549
diff changeset
   123
}