sapling/hgext/commitcloud/state.py
Mark Thomas 6660438ea2 commitcloud: add basic cloudsync command
Summary:
Adds the initial skeleton of the `commitcloud` extension to Mercurial, which
will synchronise heads and bookmarks (and later, obsmarkers) with commit cloud.

Current limitations:
  - only supports a stub version of the service that uses local files
  - requires manual pushbackup to send commits to the server via infinitepush
  - doesn't do anything with obsmarkers

Differential Revision: D7167715

fbshipit-source-id: 53443f3fb8dff0fe52257d7b20fc601dbe10c883
2018-04-13 21:51:28 -07:00

45 lines
1.4 KiB
Python

# 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
import json
from .. import shareutil
class SyncState(object):
"""
Stores the local record of what state was stored in the cloud at the
last sync.
"""
filename = 'commitcloudstate'
def __init__(self, repo):
repo = shareutil.getsrcrepo(repo)
self.repo = repo
if repo.vfs.exists(self.filename):
with repo.vfs.open(self.filename, 'r') as f:
data = json.load(f)
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()}
else:
self.version = 0
self.heads = []
self.bookmarks = {}
def update(self, newversion, newheads, newbookmarks):
data = {
'version': newversion,
'heads': newheads,
'bookmarks': newbookmarks,
}
with self.repo.wlock():
with self.repo.vfs.open(self.filename, 'w', atomictemp=True) as f:
json.dump(data, f)
self.version = newversion
self.heads = newheads
self.bookmarks = newbookmarks