2018-03-14 19:03:01 +03:00
|
|
|
# Copyright 2018 Facebook, Inc.
|
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms of the
|
|
|
|
# GNU General Public License version 2 or any later version.
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
2018-04-05 00:42:14 +03:00
|
|
|
|
|
|
|
# Standard Library
|
2018-04-05 02:42:31 +03:00
|
|
|
import hashlib
|
2018-03-14 19:03:01 +03:00
|
|
|
import json
|
|
|
|
|
2018-04-05 02:42:31 +03:00
|
|
|
from mercurial.i18n import _
|
|
|
|
|
2018-05-29 21:13:08 +03:00
|
|
|
from . import commitcloudcommon, commitcloudutil
|
2018-03-14 19:03:01 +03:00
|
|
|
from .. import shareutil
|
2018-05-29 21:13:08 +03:00
|
|
|
|
2018-03-14 19:03:01 +03:00
|
|
|
|
|
|
|
class SyncState(object):
|
|
|
|
"""
|
|
|
|
Stores the local record of what state was stored in the cloud at the
|
|
|
|
last sync.
|
|
|
|
"""
|
2018-05-29 21:13:08 +03:00
|
|
|
|
2018-04-05 03:04:30 +03:00
|
|
|
@staticmethod
|
|
|
|
def _filename(workspace):
|
|
|
|
# make a unique valid filename
|
2018-05-29 21:13:08 +03:00
|
|
|
return (
|
|
|
|
"commitcloudstate."
|
|
|
|
+ "".join(x for x in workspace if x.isalnum())
|
|
|
|
+ ".%s" % (hashlib.sha256(workspace).hexdigest()[0:5])
|
|
|
|
)
|
2018-03-14 19:03:01 +03:00
|
|
|
|
2018-04-05 03:04:30 +03:00
|
|
|
@staticmethod
|
|
|
|
def erasestate(repo):
|
|
|
|
# get current workspace
|
2018-04-06 01:01:23 +03:00
|
|
|
workspace = commitcloudutil.getworkspacename(repo)
|
2018-04-05 03:04:30 +03:00
|
|
|
if not workspace:
|
2018-05-29 21:13:08 +03:00
|
|
|
raise commitcloudcommon.WorkspaceError(repo.ui, _("undefined workspace"))
|
2018-04-06 01:01:23 +03:00
|
|
|
|
2018-04-05 03:04:30 +03:00
|
|
|
filename = SyncState._filename(workspace)
|
|
|
|
# clean up the current state in force recover mode
|
|
|
|
if repo.svfs.exists(filename):
|
|
|
|
with repo.wlock(), repo.lock():
|
|
|
|
repo.svfs.unlink(filename)
|
2018-04-05 02:42:31 +03:00
|
|
|
|
2018-04-05 03:04:30 +03:00
|
|
|
def __init__(self, repo):
|
2018-04-05 02:42:31 +03:00
|
|
|
# get current workspace
|
2018-04-06 01:01:23 +03:00
|
|
|
workspace = commitcloudutil.getworkspacename(repo)
|
2018-04-05 02:42:31 +03:00
|
|
|
if not workspace:
|
2018-05-29 21:13:08 +03:00
|
|
|
raise commitcloudcommon.WorkspaceError(repo.ui, _("undefined workspace"))
|
2018-04-05 02:42:31 +03:00
|
|
|
|
2018-04-05 03:04:30 +03:00
|
|
|
self.filename = SyncState._filename(workspace)
|
|
|
|
repo = shareutil.getsrcrepo(repo)
|
|
|
|
self.repo = repo
|
|
|
|
if repo.svfs.exists(self.filename):
|
2018-05-29 21:13:08 +03:00
|
|
|
with repo.svfs.open(self.filename, "r") as f:
|
2018-04-05 03:04:30 +03:00
|
|
|
try:
|
|
|
|
data = json.load(f)
|
|
|
|
except Exception:
|
|
|
|
raise commitcloudcommon.InvalidWorkspaceDataError(
|
2018-05-29 21:13:08 +03:00
|
|
|
repo.ui, _("failed to parse %s") % self.filename
|
|
|
|
)
|
2018-04-05 02:42:31 +03:00
|
|
|
|
2018-05-29 21:13:08 +03:00
|
|
|
self.version = data["version"]
|
|
|
|
self.heads = [h.encode() for h in data["heads"]]
|
|
|
|
self.bookmarks = {
|
|
|
|
n.encode("utf-8"): v.encode() for n, v in data["bookmarks"].items()
|
|
|
|
}
|
2018-03-14 19:03:01 +03:00
|
|
|
else:
|
|
|
|
self.version = 0
|
|
|
|
self.heads = []
|
|
|
|
self.bookmarks = {}
|
|
|
|
|
|
|
|
def update(self, newversion, newheads, newbookmarks):
|
2018-05-29 21:13:08 +03:00
|
|
|
data = {"version": newversion, "heads": newheads, "bookmarks": newbookmarks}
|
2018-04-05 03:04:30 +03:00
|
|
|
with self.repo.wlock(), self.repo.lock():
|
2018-05-29 21:13:08 +03:00
|
|
|
with self.repo.svfs.open(self.filename, "w", atomictemp=True) as f:
|
2018-03-14 19:03:01 +03:00
|
|
|
json.dump(data, f)
|
|
|
|
self.version = newversion
|
|
|
|
self.heads = newheads
|
|
|
|
self.bookmarks = newbookmarks
|