PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/hgext/fetch.py

https://bitbucket.org/mirror/mercurial/
Python | 155 lines | 144 code | 4 blank | 7 comment | 1 complexity | 4bf7ea5d9e3ca1738d372c87138fb87e MD5 | raw file
Possible License(s): GPL-2.0
  1. # fetch.py - pull and merge remote changes
  2. #
  3. # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
  4. #
  5. # This software may be used and distributed according to the terms of the
  6. # GNU General Public License version 2 or any later version.
  7. '''pull, update and merge in one command (DEPRECATED)'''
  8. from mercurial.i18n import _
  9. from mercurial.node import nullid, short
  10. from mercurial import commands, cmdutil, hg, util, error
  11. from mercurial.lock import release
  12. cmdtable = {}
  13. command = cmdutil.command(cmdtable)
  14. testedwith = 'internal'
  15. @command('fetch',
  16. [('r', 'rev', [],
  17. _('a specific revision you would like to pull'), _('REV')),
  18. ('e', 'edit', None, _('edit commit message')),
  19. ('', 'force-editor', None, _('edit commit message (DEPRECATED)')),
  20. ('', 'switch-parent', None, _('switch parents when merging')),
  21. ] + commands.commitopts + commands.commitopts2 + commands.remoteopts,
  22. _('hg fetch [SOURCE]'))
  23. def fetch(ui, repo, source='default', **opts):
  24. '''pull changes from a remote repository, merge new changes if needed.
  25. This finds all changes from the repository at the specified path
  26. or URL and adds them to the local repository.
  27. If the pulled changes add a new branch head, the head is
  28. automatically merged, and the result of the merge is committed.
  29. Otherwise, the working directory is updated to include the new
  30. changes.
  31. When a merge is needed, the working directory is first updated to
  32. the newly pulled changes. Local changes are then merged into the
  33. pulled changes. To switch the merge order, use --switch-parent.
  34. See :hg:`help dates` for a list of formats valid for -d/--date.
  35. Returns 0 on success.
  36. '''
  37. date = opts.get('date')
  38. if date:
  39. opts['date'] = util.parsedate(date)
  40. parent, p2 = repo.dirstate.parents()
  41. branch = repo.dirstate.branch()
  42. try:
  43. branchnode = repo.branchtip(branch)
  44. except error.RepoLookupError:
  45. branchnode = None
  46. if parent != branchnode:
  47. raise util.Abort(_('working dir not at branch tip '
  48. '(use "hg update" to check out branch tip)'))
  49. if p2 != nullid:
  50. raise util.Abort(_('outstanding uncommitted merge'))
  51. wlock = lock = None
  52. try:
  53. wlock = repo.wlock()
  54. lock = repo.lock()
  55. mod, add, rem, del_ = repo.status()[:4]
  56. if mod or add or rem:
  57. raise util.Abort(_('outstanding uncommitted changes'))
  58. if del_:
  59. raise util.Abort(_('working directory is missing some files'))
  60. bheads = repo.branchheads(branch)
  61. bheads = [head for head in bheads if len(repo[head].children()) == 0]
  62. if len(bheads) > 1:
  63. raise util.Abort(_('multiple heads in this branch '
  64. '(use "hg heads ." and "hg merge" to merge)'))
  65. other = hg.peer(repo, opts, ui.expandpath(source))
  66. ui.status(_('pulling from %s\n') %
  67. util.hidepassword(ui.expandpath(source)))
  68. revs = None
  69. if opts['rev']:
  70. try:
  71. revs = [other.lookup(rev) for rev in opts['rev']]
  72. except error.CapabilityError:
  73. err = _("other repository doesn't support revision lookup, "
  74. "so a rev cannot be specified.")
  75. raise util.Abort(err)
  76. # Are there any changes at all?
  77. modheads = repo.pull(other, heads=revs)
  78. if modheads == 0:
  79. return 0
  80. # Is this a simple fast-forward along the current branch?
  81. newheads = repo.branchheads(branch)
  82. newchildren = repo.changelog.nodesbetween([parent], newheads)[2]
  83. if len(newheads) == 1 and len(newchildren):
  84. if newchildren[0] != parent:
  85. return hg.update(repo, newchildren[0])
  86. else:
  87. return 0
  88. # Are there more than one additional branch heads?
  89. newchildren = [n for n in newchildren if n != parent]
  90. newparent = parent
  91. if newchildren:
  92. newparent = newchildren[0]
  93. hg.clean(repo, newparent)
  94. newheads = [n for n in newheads if n != newparent]
  95. if len(newheads) > 1:
  96. ui.status(_('not merging with %d other new branch heads '
  97. '(use "hg heads ." and "hg merge" to merge them)\n') %
  98. (len(newheads) - 1))
  99. return 1
  100. if not newheads:
  101. return 0
  102. # Otherwise, let's merge.
  103. err = False
  104. if newheads:
  105. # By default, we consider the repository we're pulling
  106. # *from* as authoritative, so we merge our changes into
  107. # theirs.
  108. if opts['switch_parent']:
  109. firstparent, secondparent = newparent, newheads[0]
  110. else:
  111. firstparent, secondparent = newheads[0], newparent
  112. ui.status(_('updating to %d:%s\n') %
  113. (repo.changelog.rev(firstparent),
  114. short(firstparent)))
  115. hg.clean(repo, firstparent)
  116. ui.status(_('merging with %d:%s\n') %
  117. (repo.changelog.rev(secondparent), short(secondparent)))
  118. err = hg.merge(repo, secondparent, remind=False)
  119. if not err:
  120. # we don't translate commit messages
  121. message = (cmdutil.logmessage(ui, opts) or
  122. ('Automated merge with %s' %
  123. util.removeauth(other.url())))
  124. editopt = opts.get('edit') or opts.get('force_editor')
  125. n = repo.commit(message, opts['user'], opts['date'],
  126. editor=cmdutil.getcommiteditor(edit=editopt))
  127. ui.status(_('new changeset %d:%s merges remote changes '
  128. 'with local\n') % (repo.changelog.rev(n),
  129. short(n)))
  130. return err
  131. finally:
  132. release(lock, wlock)