hgext/journal.py
author Yuya Nishihara <yuya@tcha.org>
Sun, 08 Apr 2018 11:14:47 +0900
changeset 37472 2f81926c7f89
parent 37318 9954d0e2ad00
child 39701 dea771e63c3c
permissions -rw-r--r--
wireproto: fix repr(frame) to not crash by unknown type id Follows up 5ef2da00e935.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
     1
# journal.py
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
     2
#
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
     3
# Copyright 2014-2016 Facebook, Inc.
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
     4
#
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
31600
809fa1d7eb99 journal: use lowercase for docstring title
Jun Wu <quark@fb.com>
parents: 31330
diff changeset
     7
"""track previous positions of bookmarks (EXPERIMENTAL)
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
     8
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
     9
This extension adds a new command: `hg journal`, which shows you where
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    10
bookmarks were previously located.
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    11
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    12
"""
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    13
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    14
from __future__ import absolute_import
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    15
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    16
import collections
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
    17
import errno
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    18
import os
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
    19
import weakref
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    20
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    21
from mercurial.i18n import _
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    22
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    23
from mercurial import (
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    24
    bookmarks,
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    25
    cmdutil,
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    26
    dispatch,
36666
d79d68bb9f7c py3: replace __str__ to __bytes__ in hgext/journal.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36653
diff changeset
    27
    encoding,
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    28
    error,
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    29
    extensions,
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
    30
    hg,
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
    31
    localrepo,
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
    32
    lock,
35888
c8e2d6ed1f9e cmdutil: drop aliases for logcmdutil functions (API)
Yuya Nishihara <yuya@tcha.org>
parents: 35462
diff changeset
    33
    logcmdutil,
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    34
    node,
35000
135edf120d76 py3: handle keyword arguments in hgext/journal.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33923
diff changeset
    35
    pycompat,
32337
46ba2cdda476 registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32291
diff changeset
    36
    registrar,
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    37
    util,
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    38
)
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36668
diff changeset
    39
from mercurial.utils import (
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36668
diff changeset
    40
    dateutil,
37120
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 37084
diff changeset
    41
    procutil,
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36668
diff changeset
    42
    stringutil,
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36668
diff changeset
    43
)
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    44
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    45
cmdtable = {}
32337
46ba2cdda476 registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32291
diff changeset
    46
command = registrar.command(cmdtable)
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    47
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 29773
diff changeset
    48
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    49
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    50
# be specifying the version(s) of Mercurial they are tested with, or
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    51
# leave the attribute unspecified.
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 29773
diff changeset
    52
testedwith = 'ships-with-hg-core'
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    53
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    54
# storage format version; increment when the format changes
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    55
storageversion = 0
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    56
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    57
# namespaces
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    58
bookmarktype = 'bookmark'
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
    59
wdirparenttype = 'wdirparent'
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
    60
# In a shared repository, what shared feature name is used
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
    61
# to indicate this namespace is shared with the source?
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
    62
sharednamespaces = {
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
    63
    bookmarktype: hg.sharedbookmarks,
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
    64
}
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    65
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    66
# Journal recording, register hooks and storage object
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    67
def extsetup(ui):
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    68
    extensions.wrapfunction(dispatch, 'runcommand', runcommand)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    69
    extensions.wrapfunction(bookmarks.bmstore, '_write', recordbookmarks)
33384
bc1f373cf3f6 journal: use wrapfilecache instead of wrapfunction on func of filecache
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 33383
diff changeset
    70
    extensions.wrapfilecache(
bc1f373cf3f6 journal: use wrapfilecache instead of wrapfunction on func of filecache
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 33383
diff changeset
    71
        localrepo.localrepository, 'dirstate', wrapdirstate)
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
    72
    extensions.wrapfunction(hg, 'postshare', wrappostshare)
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
    73
    extensions.wrapfunction(hg, 'copystore', unsharejournal)
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    74
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    75
def reposetup(ui, repo):
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    76
    if repo.local():
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    77
        repo.journal = journalstorage(repo)
33436
9bb4decd43b0 repovfs: add a ward to check if locks are properly taken
Boris Feld <boris.feld@octobus.net>
parents: 33384
diff changeset
    78
        repo._wlockfreeprefix.add('namejournal')
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    79
33383
774beab915fe journal: execute setup procedures for already instantiated dirstate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 32375
diff changeset
    80
        dirstate, cached = localrepo.isfilecached(repo, 'dirstate')
774beab915fe journal: execute setup procedures for already instantiated dirstate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 32375
diff changeset
    81
        if cached:
774beab915fe journal: execute setup procedures for already instantiated dirstate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 32375
diff changeset
    82
            # already instantiated dirstate isn't yet marked as
774beab915fe journal: execute setup procedures for already instantiated dirstate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 32375
diff changeset
    83
            # "journal"-ing, even though repo.dirstate() was already
774beab915fe journal: execute setup procedures for already instantiated dirstate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 32375
diff changeset
    84
            # wrapped by own wrapdirstate()
774beab915fe journal: execute setup procedures for already instantiated dirstate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 32375
diff changeset
    85
            _setupdirstate(repo, dirstate)
774beab915fe journal: execute setup procedures for already instantiated dirstate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 32375
diff changeset
    86
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    87
def runcommand(orig, lui, repo, cmd, fullargs, *args):
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    88
    """Track the command line options for recording in the journal"""
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    89
    journalstorage.recordcommand(*fullargs)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    90
    return orig(lui, repo, cmd, fullargs, *args)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    91
33383
774beab915fe journal: execute setup procedures for already instantiated dirstate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 32375
diff changeset
    92
def _setupdirstate(repo, dirstate):
774beab915fe journal: execute setup procedures for already instantiated dirstate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 32375
diff changeset
    93
    dirstate.journalstorage = repo.journal
774beab915fe journal: execute setup procedures for already instantiated dirstate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 32375
diff changeset
    94
    dirstate.addparentchangecallback('journal', recorddirstateparents)
774beab915fe journal: execute setup procedures for already instantiated dirstate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 32375
diff changeset
    95
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
    96
# hooks to record dirstate changes
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
    97
def wrapdirstate(orig, repo):
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
    98
    """Make journal storage available to the dirstate object"""
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
    99
    dirstate = orig(repo)
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   100
    if util.safehasattr(repo, 'journal'):
33383
774beab915fe journal: execute setup procedures for already instantiated dirstate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 32375
diff changeset
   101
        _setupdirstate(repo, dirstate)
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   102
    return dirstate
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   103
29773
f2241c13d5a1 journal: use the dirstate parentchange callbacks
Mateusz Kwapich <mitrandir@fb.com>
parents: 29756
diff changeset
   104
def recorddirstateparents(dirstate, old, new):
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   105
    """Records all dirstate parent changes in the journal."""
29773
f2241c13d5a1 journal: use the dirstate parentchange callbacks
Mateusz Kwapich <mitrandir@fb.com>
parents: 29756
diff changeset
   106
    old = list(old)
f2241c13d5a1 journal: use the dirstate parentchange callbacks
Mateusz Kwapich <mitrandir@fb.com>
parents: 29756
diff changeset
   107
    new = list(new)
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   108
    if util.safehasattr(dirstate, 'journalstorage'):
29773
f2241c13d5a1 journal: use the dirstate parentchange callbacks
Mateusz Kwapich <mitrandir@fb.com>
parents: 29756
diff changeset
   109
        # only record two hashes if there was a merge
f2241c13d5a1 journal: use the dirstate parentchange callbacks
Mateusz Kwapich <mitrandir@fb.com>
parents: 29756
diff changeset
   110
        oldhashes = old[:1] if old[1] == node.nullid else old
f2241c13d5a1 journal: use the dirstate parentchange callbacks
Mateusz Kwapich <mitrandir@fb.com>
parents: 29756
diff changeset
   111
        newhashes = new[:1] if new[1] == node.nullid else new
f2241c13d5a1 journal: use the dirstate parentchange callbacks
Mateusz Kwapich <mitrandir@fb.com>
parents: 29756
diff changeset
   112
        dirstate.journalstorage.record(
f2241c13d5a1 journal: use the dirstate parentchange callbacks
Mateusz Kwapich <mitrandir@fb.com>
parents: 29756
diff changeset
   113
            wdirparenttype, '.', oldhashes, newhashes)
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   114
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   115
# hooks to record bookmark changes (both local and remote)
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   116
def recordbookmarks(orig, store, fp):
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   117
    """Records all bookmark changes in the journal."""
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   118
    repo = store._repo
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   119
    if util.safehasattr(repo, 'journal'):
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   120
        oldmarks = bookmarks.bmstore(repo)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   121
        for mark, value in store.iteritems():
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   122
            oldvalue = oldmarks.get(mark, node.nullid)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   123
            if value != oldvalue:
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   124
                repo.journal.record(bookmarktype, mark, oldvalue, value)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   125
    return orig(store, fp)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   126
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   127
# shared repository support
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   128
def _readsharedfeatures(repo):
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   129
    """A set of shared features for this repository"""
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   130
    try:
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   131
        return set(repo.vfs.read('shared').splitlines())
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   132
    except IOError as inst:
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   133
        if inst.errno != errno.ENOENT:
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   134
            raise
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   135
        return set()
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   136
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   137
def _mergeentriesiter(*iterables, **kwargs):
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   138
    """Given a set of sorted iterables, yield the next entry in merged order
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   139
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   140
    Note that by default entries go from most recent to oldest.
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   141
    """
35000
135edf120d76 py3: handle keyword arguments in hgext/journal.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33923
diff changeset
   142
    order = kwargs.pop(r'order', max)
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   143
    iterables = [iter(it) for it in iterables]
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   144
    # this tracks still active iterables; iterables are deleted as they are
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   145
    # exhausted, which is why this is a dictionary and why each entry also
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   146
    # stores the key. Entries are mutable so we can store the next value each
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   147
    # time.
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   148
    iterable_map = {}
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   149
    for key, it in enumerate(iterables):
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   150
        try:
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   151
            iterable_map[key] = [next(it), key, it]
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   152
        except StopIteration:
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   153
            # empty entry, can be ignored
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   154
            pass
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   155
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   156
    while iterable_map:
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   157
        value, key, it = order(iterable_map.itervalues())
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   158
        yield value
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   159
        try:
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   160
            iterable_map[key][0] = next(it)
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   161
        except StopIteration:
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   162
            # this iterable is empty, remove it from consideration
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   163
            del iterable_map[key]
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   164
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   165
def wrappostshare(orig, sourcerepo, destrepo, **kwargs):
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   166
    """Mark this shared working copy as sharing journal information"""
29756
46d9a5bb2fb0 journal: take wlock for writting the 'shared' file
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29679
diff changeset
   167
    with destrepo.wlock():
46d9a5bb2fb0 journal: take wlock for writting the 'shared' file
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29679
diff changeset
   168
        orig(sourcerepo, destrepo, **kwargs)
46d9a5bb2fb0 journal: take wlock for writting the 'shared' file
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29679
diff changeset
   169
        with destrepo.vfs('shared', 'a') as fp:
46d9a5bb2fb0 journal: take wlock for writting the 'shared' file
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29679
diff changeset
   170
            fp.write('journal\n')
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   171
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   172
def unsharejournal(orig, ui, repo, repopath):
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   173
    """Copy shared journal entries into this repo when unsharing"""
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   174
    if (repo.path == repopath and repo.shared() and
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   175
            util.safehasattr(repo, 'journal')):
36159
0fe7e39dc683 hg: move share._getsrcrepo into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35954
diff changeset
   176
        sharedrepo = hg.sharedreposource(repo)
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   177
        sharedfeatures = _readsharedfeatures(repo)
32291
bd872f64a8ba cleanup: use set literals
Martin von Zweigbergk <martinvonz@google.com>
parents: 31600
diff changeset
   178
        if sharedrepo and sharedfeatures > {'journal'}:
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   179
            # there is a shared repository and there are shared journal entries
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   180
            # to copy. move shared date over from source to destination but
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   181
            # move the local file first
29870
6d11ae3c4c4b journal: rename on disk files to 'namejournal'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29841
diff changeset
   182
            if repo.vfs.exists('namejournal'):
31330
74ee10702bbf journal: directly use repo.vfs.join
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30131
diff changeset
   183
                journalpath = repo.vfs.join('namejournal')
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   184
                util.rename(journalpath, journalpath + '.bak')
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   185
            storage = repo.journal
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   186
            local = storage._open(
29870
6d11ae3c4c4b journal: rename on disk files to 'namejournal'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29841
diff changeset
   187
                repo.vfs, filename='namejournal.bak', _newestfirst=False)
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   188
            shared = (
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   189
                e for e in storage._open(sharedrepo.vfs, _newestfirst=False)
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   190
                if sharednamespaces.get(e.namespace) in sharedfeatures)
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   191
            for entry in _mergeentriesiter(local, shared, order=min):
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   192
                storage._write(repo.vfs, entry)
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   193
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   194
    return orig(ui, repo, repopath)
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   195
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   196
class journalentry(collections.namedtuple(
30131
85d5708eae4e py3: namedtuple takes unicode (journal ext)
Mateusz Kwapich <mitrandir@fb.com>
parents: 29928
diff changeset
   197
        u'journalentry',
85d5708eae4e py3: namedtuple takes unicode (journal ext)
Mateusz Kwapich <mitrandir@fb.com>
parents: 29928
diff changeset
   198
        u'timestamp user command namespace name oldhashes newhashes')):
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   199
    """Individual journal entry
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   200
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   201
    * timestamp: a mercurial (time, timezone) tuple
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   202
    * user: the username that ran the command
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   203
    * namespace: the entry namespace, an opaque string
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   204
    * name: the name of the changed item, opaque string with meaning in the
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   205
      namespace
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   206
    * command: the hg command that triggered this record
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   207
    * oldhashes: a tuple of one or more binary hashes for the old location
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   208
    * newhashes: a tuple of one or more binary hashes for the new location
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   209
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   210
    Handles serialisation from and to the storage format. Fields are
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   211
    separated by newlines, hashes are written out in hex separated by commas,
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   212
    timestamp and timezone are separated by a space.
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   213
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   214
    """
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   215
    @classmethod
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   216
    def fromstorage(cls, line):
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   217
        (time, user, command, namespace, name,
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   218
         oldhashes, newhashes) = line.split('\n')
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   219
        timestamp, tz = time.split()
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   220
        timestamp, tz = float(timestamp), int(tz)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   221
        oldhashes = tuple(node.bin(hash) for hash in oldhashes.split(','))
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   222
        newhashes = tuple(node.bin(hash) for hash in newhashes.split(','))
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   223
        return cls(
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   224
            (timestamp, tz), user, command, namespace, name,
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   225
            oldhashes, newhashes)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   226
36666
d79d68bb9f7c py3: replace __str__ to __bytes__ in hgext/journal.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36653
diff changeset
   227
    def __bytes__(self):
d79d68bb9f7c py3: replace __str__ to __bytes__ in hgext/journal.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36653
diff changeset
   228
        """bytes representation for storage"""
37318
9954d0e2ad00 py3: use pycompat.bytestr() intsead of str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37120
diff changeset
   229
        time = ' '.join(map(pycompat.bytestr, self.timestamp))
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   230
        oldhashes = ','.join([node.hex(hash) for hash in self.oldhashes])
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   231
        newhashes = ','.join([node.hex(hash) for hash in self.newhashes])
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   232
        return '\n'.join((
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   233
            time, self.user, self.command, self.namespace, self.name,
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   234
            oldhashes, newhashes))
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   235
36666
d79d68bb9f7c py3: replace __str__ to __bytes__ in hgext/journal.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36653
diff changeset
   236
    __str__ = encoding.strmethod(__bytes__)
d79d68bb9f7c py3: replace __str__ to __bytes__ in hgext/journal.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36653
diff changeset
   237
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   238
class journalstorage(object):
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   239
    """Storage for journal entries
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   240
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   241
    Entries are divided over two files; one with entries that pertain to the
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   242
    local working copy *only*, and one with entries that are shared across
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   243
    multiple working copies when shared using the share extension.
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   244
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   245
    Entries are stored with NUL bytes as separators. See the journalentry
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   246
    class for the per-entry structure.
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   247
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   248
    The file format starts with an integer version, delimited by a NUL.
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   249
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   250
    This storage uses a dedicated lock; this makes it easier to avoid issues
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   251
    with adding entries that added when the regular wlock is unlocked (e.g.
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   252
    the dirstate).
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   253
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   254
    """
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   255
    _currentcommand = ()
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   256
    _lockref = None
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   257
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   258
    def __init__(self, repo):
37120
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 37084
diff changeset
   259
        self.user = procutil.getuser()
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   260
        self.ui = repo.ui
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   261
        self.vfs = repo.vfs
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   262
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   263
        # is this working copy using a shared storage?
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   264
        self.sharedfeatures = self.sharedvfs = None
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   265
        if repo.shared():
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   266
            features = _readsharedfeatures(repo)
36159
0fe7e39dc683 hg: move share._getsrcrepo into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35954
diff changeset
   267
            sharedrepo = hg.sharedreposource(repo)
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   268
            if sharedrepo is not None and 'journal' in features:
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   269
                self.sharedvfs = sharedrepo.vfs
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   270
                self.sharedfeatures = features
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   271
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   272
    # track the current command for recording in journal entries
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   273
    @property
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   274
    def command(self):
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   275
        commandstr = ' '.join(
37120
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 37084
diff changeset
   276
            map(procutil.shellquote, journalstorage._currentcommand))
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   277
        if '\n' in commandstr:
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   278
            # truncate multi-line commands
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   279
            commandstr = commandstr.partition('\n')[0] + ' ...'
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   280
        return commandstr
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   281
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   282
    @classmethod
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   283
    def recordcommand(cls, *fullargs):
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   284
        """Set the current hg arguments, stored with recorded entries"""
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   285
        # Set the current command on the class because we may have started
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   286
        # with a non-local repo (cloning for example).
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   287
        cls._currentcommand = fullargs
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   288
29928
e5a97ec6ebb8 journal: properly check for held lock (issue5349)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29870
diff changeset
   289
    def _currentlock(self, lockref):
e5a97ec6ebb8 journal: properly check for held lock (issue5349)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29870
diff changeset
   290
        """Returns the lock if it's held, or None if it's not.
e5a97ec6ebb8 journal: properly check for held lock (issue5349)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29870
diff changeset
   291
e5a97ec6ebb8 journal: properly check for held lock (issue5349)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29870
diff changeset
   292
        (This is copied from the localrepo class)
e5a97ec6ebb8 journal: properly check for held lock (issue5349)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29870
diff changeset
   293
        """
e5a97ec6ebb8 journal: properly check for held lock (issue5349)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29870
diff changeset
   294
        if lockref is None:
e5a97ec6ebb8 journal: properly check for held lock (issue5349)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29870
diff changeset
   295
            return None
e5a97ec6ebb8 journal: properly check for held lock (issue5349)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29870
diff changeset
   296
        l = lockref()
e5a97ec6ebb8 journal: properly check for held lock (issue5349)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29870
diff changeset
   297
        if l is None or not l.held:
e5a97ec6ebb8 journal: properly check for held lock (issue5349)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29870
diff changeset
   298
            return None
e5a97ec6ebb8 journal: properly check for held lock (issue5349)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29870
diff changeset
   299
        return l
e5a97ec6ebb8 journal: properly check for held lock (issue5349)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29870
diff changeset
   300
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   301
    def jlock(self, vfs):
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   302
        """Create a lock for the journal file"""
29928
e5a97ec6ebb8 journal: properly check for held lock (issue5349)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29870
diff changeset
   303
        if self._currentlock(self._lockref) is not None:
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   304
            raise error.Abort(_('journal lock does not support nesting'))
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   305
        desc = _('journal of %s') % vfs.base
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   306
        try:
29870
6d11ae3c4c4b journal: rename on disk files to 'namejournal'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29841
diff changeset
   307
            l = lock.lock(vfs, 'namejournal.lock', 0, desc=desc)
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   308
        except error.LockHeld as inst:
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   309
            self.ui.warn(
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   310
                _("waiting for lock on %s held by %r\n") % (desc, inst.locker))
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   311
            # default to 600 seconds timeout
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   312
            l = lock.lock(
29870
6d11ae3c4c4b journal: rename on disk files to 'namejournal'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29841
diff changeset
   313
                vfs, 'namejournal.lock',
35207
d210723b73e5 lock: use configint for 'ui.timeout' config
Boris Feld <boris.feld@octobus.net>
parents: 35000
diff changeset
   314
                self.ui.configint("ui", "timeout"), desc=desc)
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   315
            self.ui.warn(_("got lock after %s seconds\n") % l.delay)
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   316
        self._lockref = weakref.ref(l)
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   317
        return l
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   318
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   319
    def record(self, namespace, name, oldhashes, newhashes):
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   320
        """Record a new journal entry
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   321
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   322
        * namespace: an opaque string; this can be used to filter on the type
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   323
          of recorded entries.
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   324
        * name: the name defining this entry; for bookmarks, this is the
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   325
          bookmark name. Can be filtered on when retrieving entries.
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   326
        * oldhashes and newhashes: each a single binary hash, or a list of
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   327
          binary hashes. These represent the old and new position of the named
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   328
          item.
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   329
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   330
        """
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   331
        if not isinstance(oldhashes, list):
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   332
            oldhashes = [oldhashes]
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   333
        if not isinstance(newhashes, list):
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   334
            newhashes = [newhashes]
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   335
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   336
        entry = journalentry(
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 36159
diff changeset
   337
            dateutil.makedate(), self.user, self.command, namespace, name,
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   338
            oldhashes, newhashes)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   339
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   340
        vfs = self.vfs
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   341
        if self.sharedvfs is not None:
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   342
            # write to the shared repository if this feature is being
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   343
            # shared between working copies.
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   344
            if sharednamespaces.get(namespace) in self.sharedfeatures:
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   345
                vfs = self.sharedvfs
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   346
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   347
        self._write(vfs, entry)
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   348
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   349
    def _write(self, vfs, entry):
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   350
        with self.jlock(vfs):
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   351
            version = None
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   352
            # open file in amend mode to ensure it is created if missing
33923
e6d421566906 journal: do not use atomictemp (issue5338)
Jun Wu <quark@fb.com>
parents: 33499
diff changeset
   353
            with vfs('namejournal', mode='a+b') as f:
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   354
                f.seek(0, os.SEEK_SET)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   355
                # Read just enough bytes to get a version number (up to 2
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   356
                # digits plus separator)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   357
                version = f.read(3).partition('\0')[0]
36668
e77cee5de1c7 py3: use b"%d" to covert integer to bytes instead of str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36667
diff changeset
   358
                if version and version != "%d" % storageversion:
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   359
                    # different version of the storage. Exit early (and not
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   360
                    # write anything) if this is not a version we can handle or
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   361
                    # the file is corrupt. In future, perhaps rotate the file
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   362
                    # instead?
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   363
                    self.ui.warn(
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   364
                        _("unsupported journal file version '%s'\n") % version)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   365
                    return
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   366
                if not version:
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   367
                    # empty file, write version first
36668
e77cee5de1c7 py3: use b"%d" to covert integer to bytes instead of str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36667
diff changeset
   368
                    f.write(("%d" % storageversion) + '\0')
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   369
                f.seek(0, os.SEEK_END)
36667
bcfc4e3b6548 py3: use bytes() instead of str()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36666
diff changeset
   370
                f.write(bytes(entry) + '\0')
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   371
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   372
    def filtered(self, namespace=None, name=None):
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   373
        """Yield all journal entries with the given namespace or name
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   374
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   375
        Both the namespace and the name are optional; if neither is given all
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   376
        entries in the journal are produced.
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   377
29504
7503d8874617 journal: add support for seaching by pattern
Martijn Pieters <mjpieters@fb.com>
parents: 29503
diff changeset
   378
        Matching supports regular expressions by using the `re:` prefix
7503d8874617 journal: add support for seaching by pattern
Martijn Pieters <mjpieters@fb.com>
parents: 29503
diff changeset
   379
        (use `literal:` to match names or namespaces that start with `re:`)
7503d8874617 journal: add support for seaching by pattern
Martijn Pieters <mjpieters@fb.com>
parents: 29503
diff changeset
   380
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   381
        """
29504
7503d8874617 journal: add support for seaching by pattern
Martijn Pieters <mjpieters@fb.com>
parents: 29503
diff changeset
   382
        if namespace is not None:
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36668
diff changeset
   383
            namespace = stringutil.stringmatcher(namespace)[-1]
29504
7503d8874617 journal: add support for seaching by pattern
Martijn Pieters <mjpieters@fb.com>
parents: 29503
diff changeset
   384
        if name is not None:
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36668
diff changeset
   385
            name = stringutil.stringmatcher(name)[-1]
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   386
        for entry in self:
29504
7503d8874617 journal: add support for seaching by pattern
Martijn Pieters <mjpieters@fb.com>
parents: 29503
diff changeset
   387
            if namespace is not None and not namespace(entry.namespace):
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   388
                continue
29504
7503d8874617 journal: add support for seaching by pattern
Martijn Pieters <mjpieters@fb.com>
parents: 29503
diff changeset
   389
            if name is not None and not name(entry.name):
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   390
                continue
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   391
            yield entry
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   392
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   393
    def __iter__(self):
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   394
        """Iterate over the storage
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   395
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   396
        Yields journalentry instances for each contained journal record.
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   397
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   398
        """
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   399
        local = self._open(self.vfs)
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   400
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   401
        if self.sharedvfs is None:
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   402
            return local
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   403
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   404
        # iterate over both local and shared entries, but only those
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   405
        # shared entries that are among the currently shared features
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   406
        shared = (
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   407
            e for e in self._open(self.sharedvfs)
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   408
            if sharednamespaces.get(e.namespace) in self.sharedfeatures)
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   409
        return _mergeentriesiter(local, shared)
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   410
29870
6d11ae3c4c4b journal: rename on disk files to 'namejournal'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29841
diff changeset
   411
    def _open(self, vfs, filename='namejournal', _newestfirst=True):
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   412
        if not vfs.exists(filename):
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   413
            return
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   414
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   415
        with vfs(filename) as f:
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   416
            raw = f.read()
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   417
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   418
        lines = raw.split('\0')
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   419
        version = lines and lines[0]
36668
e77cee5de1c7 py3: use b"%d" to covert integer to bytes instead of str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36667
diff changeset
   420
        if version != "%d" % storageversion:
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   421
            version = version or _('not available')
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   422
            raise error.Abort(_("unknown journal file version '%s'") % version)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   423
29503
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   424
        # Skip the first line, it's a version number. Normally we iterate over
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   425
        # these in reverse order to list newest first; only when copying across
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   426
        # a shared storage do we forgo reversing.
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   427
        lines = lines[1:]
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   428
        if _newestfirst:
0103b673d6ca journal: add share extension support
Martijn Pieters <mjpieters@fb.com>
parents: 29502
diff changeset
   429
            lines = reversed(lines)
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   430
        for line in lines:
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   431
            if not line:
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   432
                continue
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   433
            yield journalentry.fromstorage(line)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   434
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   435
# journal reading
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   436
# log options that don't make sense for journal
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   437
_ignoreopts = ('no-merges', 'graph')
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   438
@command(
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   439
    'journal', [
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   440
        ('', 'all', None, 'show history for all names'),
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   441
        ('c', 'commits', None, 'show commit metadata'),
32375
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32337
diff changeset
   442
    ] + [opt for opt in cmdutil.logopts if opt[1] not in _ignoreopts],
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   443
    '[OPTION]... [BOOKMARKNAME]')
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   444
def journal(ui, repo, *args, **opts):
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   445
    """show the previous position of bookmarks and the working copy
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   446
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   447
    The journal is used to see the previous commits that bookmarks and the
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   448
    working copy pointed to. By default the previous locations for the working
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   449
    copy.  Passing a bookmark name will show all the previous positions of
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   450
    that bookmark. Use the --all switch to show previous locations for all
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   451
    bookmarks and the working copy; each line will then include the bookmark
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   452
    name, or '.' for the working copy, as well.
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   453
29504
7503d8874617 journal: add support for seaching by pattern
Martijn Pieters <mjpieters@fb.com>
parents: 29503
diff changeset
   454
    If `name` starts with `re:`, the remainder of the name is treated as
7503d8874617 journal: add support for seaching by pattern
Martijn Pieters <mjpieters@fb.com>
parents: 29503
diff changeset
   455
    a regular expression. To match a name that actually starts with `re:`,
7503d8874617 journal: add support for seaching by pattern
Martijn Pieters <mjpieters@fb.com>
parents: 29503
diff changeset
   456
    use the prefix `literal:`.
7503d8874617 journal: add support for seaching by pattern
Martijn Pieters <mjpieters@fb.com>
parents: 29503
diff changeset
   457
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   458
    By default hg journal only shows the commit hash and the command that was
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   459
    running at that time. -v/--verbose will show the prior hash, the user, and
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   460
    the time at which it happened.
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   461
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   462
    Use -c/--commits to output log information on each commit hash; at this
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   463
    point you can use the usual `--patch`, `--git`, `--stat` and `--template`
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   464
    switches to alter the log output for these.
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   465
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   466
    `hg journal -T json` can be used to produce machine readable output.
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   467
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   468
    """
35000
135edf120d76 py3: handle keyword arguments in hgext/journal.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33923
diff changeset
   469
    opts = pycompat.byteskwargs(opts)
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   470
    name = '.'
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   471
    if opts.get('all'):
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   472
        if args:
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   473
            raise error.Abort(
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   474
                _("You can't combine --all and filtering on a name"))
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   475
        name = None
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   476
    if args:
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   477
        name = args[0]
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   478
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   479
    fm = ui.formatter('journal', opts)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   480
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   481
    if opts.get("template") != "json":
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   482
        if name is None:
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   483
            displayname = _('the working copy and bookmarks')
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   484
        else:
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   485
            displayname = "'%s'" % name
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   486
        ui.status(_("previous locations of %s:\n") % displayname)
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   487
35888
c8e2d6ed1f9e cmdutil: drop aliases for logcmdutil functions (API)
Yuya Nishihara <yuya@tcha.org>
parents: 35462
diff changeset
   488
    limit = logcmdutil.getlimit(opts)
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   489
    entry = None
35462
6f754b0fe54e journal: use pager
Jun Wu <quark@fb.com>
parents: 35207
diff changeset
   490
    ui.pager('journal')
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   491
    for count, entry in enumerate(repo.journal.filtered(name=name)):
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   492
        if count == limit:
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   493
            break
29677
69890b5dd32b journal: use fm.formatlist() to pass hashes in appropriate type (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 29675
diff changeset
   494
        newhashesstr = fm.formatlist(map(fm.hexfunc, entry.newhashes),
69890b5dd32b journal: use fm.formatlist() to pass hashes in appropriate type (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 29675
diff changeset
   495
                                     name='node', sep=',')
69890b5dd32b journal: use fm.formatlist() to pass hashes in appropriate type (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 29675
diff changeset
   496
        oldhashesstr = fm.formatlist(map(fm.hexfunc, entry.oldhashes),
69890b5dd32b journal: use fm.formatlist() to pass hashes in appropriate type (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 29675
diff changeset
   497
                                     name='node', sep=',')
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   498
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   499
        fm.startitem()
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   500
        fm.condwrite(ui.verbose, 'oldhashes', '%s -> ', oldhashesstr)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   501
        fm.write('newhashes', '%s', newhashesstr)
29502
8361131b4768 journal: add dirstate tracking
Martijn Pieters <mjpieters@fb.com>
parents: 29443
diff changeset
   502
        fm.condwrite(ui.verbose, 'user', ' %-8s', entry.user)
29504
7503d8874617 journal: add support for seaching by pattern
Martijn Pieters <mjpieters@fb.com>
parents: 29503
diff changeset
   503
        fm.condwrite(
7503d8874617 journal: add support for seaching by pattern
Martijn Pieters <mjpieters@fb.com>
parents: 29503
diff changeset
   504
            opts.get('all') or name.startswith('re:'),
7503d8874617 journal: add support for seaching by pattern
Martijn Pieters <mjpieters@fb.com>
parents: 29503
diff changeset
   505
            'name', '  %-8s', entry.name)
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   506
29679
e54bdd3aa23b journal: use fm.formatdate() to pass date tuple in appropriate type (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 29677
diff changeset
   507
        timestring = fm.formatdate(entry.timestamp, '%Y-%m-%d %H:%M %1%2')
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   508
        fm.condwrite(ui.verbose, 'date', ' %s', timestring)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   509
        fm.write('command', '  %s\n', entry.command)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   510
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   511
        if opts.get("commits"):
35954
386c1e45e671 logcmdutil: drop default arguments from changesetdisplayer/templater() calls
Yuya Nishihara <yuya@tcha.org>
parents: 35888
diff changeset
   512
            displayer = logcmdutil.changesetdisplayer(ui, repo, opts)
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   513
            for hash in entry.newhashes:
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   514
                try:
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   515
                    ctx = repo[hash]
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   516
                    displayer.show(ctx)
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   517
                except error.RepoLookupError as e:
36653
2f7a3c90c0d7 py3: use pycompat.bytestr() to convert error messages to bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36607
diff changeset
   518
                    fm.write('repolookuperror', "%s\n\n", pycompat.bytestr(e))
29443
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   519
            displayer.close()
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   520
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   521
    fm.end()
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   522
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   523
    if entry is None:
cf092a3d202a journal: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   524
        ui.status(_("no recorded locations\n"))