<commit_before>"""
	Version Control:
	
	Schema:
	
	properties (key, value)
	uncommitted (fname, ftype, content, timestamp)
	files (fname, ftype, content, timestamp, version)
	log (fname, ftype, version)
	bundle_files (fname primary key)
	
	Discussion:
	
	There are 2 databases, versions.db and versions-local.db
	
	All changes are commited to versions-local.db, when the patches are complete, the developer
	must pull the latest .wnf db and merge
	
	versions-local.db is never commited in the global repository
"""

import unittest
import os

test_file = {'fname':'test.js', 'ftype':'js', 'content':'test_code', 'timestamp':'1100'}
root_path = os.curdir

def edit_file():
	# edit a file
	p = os.path.join(root_path, 'lib/js/core.js')

	# read
	f1 = open(p, 'r')
	content = f1.read()
	f1.close()
	
	# write
	f = open(p, 'w')
	f.write(content)
	f.close()
	return os.path.relpath(p, root_path)
		
verbose = False

class TestVC(unittest.TestCase):
	def setUp(self):
		self.vc = VersionControl(root_path, True)
		self.vc.repo.setup()
	
	def test_add(self):
		self.vc.add(**test_file)
		ret = self.vc.repo.sql('select * from uncommitted', as_dict=1)[0]		
		self.assertTrue(ret['content']==test_file['content'])
	
	def test_commit(self):
		last_number = self.vc.repo.get_value('last_version_number')
		self.vc.add(**test_file)
		self.vc.commit()

		# test version
		number = self.vc.repo.get_value('last_version_number')
		version = self.vc.repo.sql("select version from versions where number=?", (number,))[0][0]
		self.assertTrue(number != last_number)

		# test file
		self.assertTrue(self.vc.repo.get_file('test.js')['content'] == test_file['content'])
		
		# test uncommitted
		self.assertFalse(self.vc.repo.sql("select * from uncommitted"))
	
		# test log
		self.assertTrue(self.vc.repo.sql("select * from log where version=?", (version,)))

	def test_diff(self):
		self.vc.add(**test_file)
		self.vc.commit()
		self.assertTrue(self.vc.repo.diff(None), ['test.js'])

	def test_walk(self):
		# add
		self.vc.add_all()

		# check if added
		ret = self.vc.repo.sql("select * from uncommitted", as_dict=1)
		self.assertTrue(len(ret)>0)

		self.vc.commit()

		p = edit_file()
		# add
		self.vc.add_all()

		# check if added
		ret = self.vc.repo.sql("select * from uncommitted", as_dict=1)
		self.assertTrue(p in [r['fname'] for r in ret])

	def test_merge(self):
		self.vc.add_all()
		
		self.vc.commit()
		
		# write the file
		self.vc.repo.conn.commit()

		# make master (copy)
		self.vc.setup_master()

		p = edit_file()
		
		self.vc.add_all()
		self.vc.commit()
				
		self.vc.merge(self.vc.repo, self.vc.master)
		
		log = self.vc.master.diff(int(self.vc.master.get_value('last_version_number'))-1)
		self.assertTrue(p in log)
				
	def tearDown(self):
		self.vc.close()
		if os.path.exists(self.vc.local_db_name()):
			os.remove(self.vc.local_db_name())
		if os.path.exists(self.vc.master_db_name()):
			os.remove(self.vc.master_db_name())

class VersionControl:
	def __init__(self, root=None, testing=False):
		self.testing = testing

		self.set_root(root)

		self.repo = Repository(self, self.local_db_name())
		self.ignore_folders = ['.git', '.', '..']
		self.ignore_files = ['py', 'pyc', 'DS_Store', 'txt', 'db-journal', 'db']
	
	def local_db_name(self):
		"""return local db name"""
		return os.path.join(self.root_path, 'versions-local.db' + (self.testing and '.test' or ''))

	def master_db_name(self):
		"""return master db name"""
		return os.path.join(self.root_path, 'versions-master.db' + (self.testing and '.test' or ''))
	
	def setup_master(self):
		"""
			setup master db from local (if not present)
		"""
		import os
		if not os.path.exists(self.master_db_name()):
			os.system('cp %s %s' % (self.local_db_name(), self.master_db_name()))

		self.master = Repository(self, self.master_db_name())
	
	def set_root(self, path=None):
		"""
			set / reset root and connect
			(the root path is the path of the folder)
		"""
		import os
		if not path:
			path = os.path.abspath(os.path.curdir)
		
		self.root_path = path
	
	def relpath(self, fname):
		"""
			get relative path from root path
		"""
		import os
		return os.path.relpath(fname, self.root_path)
	
	def timestamp(self, path):
		"""
			returns timestamp
		"""
		import os
		if os.path.exists(path):
			return int(os.stat(path).st_mtime)
		else:
			return 0

	def add_all(self):
		"""
			walk the root folder Add all dirty files to the vcs
		"""
		import os
		for wt in os.walk(self.root_path, followlinks = True):
			
			# ignore folders
			for folder in self.ignore_folders:<commit_msg>login message in place of popup<commit_after>"""
	Version Control:
	
	Schema:
	
	properties (key, value)
	uncommitted (fname, ftype, content, timestamp)
	files (fname, ftype, content, timestamp, version)
	log (fname, ftype, version)
	bundle_files (fname primary key)
	
	Discussion:
	
	There are 2 databases, versions.db and versions-local.db
	
	All changes are commited to versions-local.db, when the patches are complete, the developer
	must pull the latest .wnf db and merge
	
	versions-local.db is never commited in the global repository
"""

import unittest
import os

test_file = {'fname':'test.js', 'ftype':'js', 'content':'test_code', 'timestamp':'1100'}
root_path = os.curdir

def edit_file():
	# edit a file
	p = os.path.join(root_path, 'lib/js/core.js')

	# read
	f1 = open(p, 'r')
	content = f1.read()
	f1.close()
	
	# write
	f = open(p, 'w')
	f.write(content)
	f.close()
	return os.path.relpath(p, root_path)
		
verbose = False

class TestVC(unittest.TestCase):
	def setUp(self):
		self.vc = VersionControl(root_path, True)
		self.vc.repo.setup()
	
	def test_add(self):
		self.vc.add(**test_file)
		ret = self.vc.repo.sql('select * from uncommitted', as_dict=1)[0]		
		self.assertTrue(ret['content']==test_file['content'])
	
	def test_commit(self):
		last_number = self.vc.repo.get_value('last_version_number')
		self.vc.add(**test_file)
		self.vc.commit()

		# test version
		number = self.vc.repo.get_value('last_version_number')
		version = self.vc.repo.sql("select version from versions where number=?", (number,))[0][0]
		self.assertTrue(number != last_number)

		# test file
		self.assertTrue(self.vc.repo.get_file('test.js')['content'] == test_file['content'])
		
		# test uncommitted
		self.assertFalse(self.vc.repo.sql("select * from uncommitted"))
	
		# test log
		self.assertTrue(self.vc.repo.sql("select * from log where version=?", (version,)))

	def test_diff(self):
		self.vc.add(**test_file)
		self.vc.commit()
		self.assertTrue(self.vc.repo.diff(None), ['test.js'])

	def test_walk(self):
		# add
		self.vc.add_all()

		# check if added
		ret = self.vc.repo.sql("select * from uncommitted", as_dict=1)
		self.assertTrue(len(ret)>0)

		self.vc.commit()

		p = edit_file()
		# add
		self.vc.add_all()

		# check if added
		ret = self.vc.repo.sql("select * from uncommitted", as_dict=1)
		self.assertTrue(p in [r['fname'] for r in ret])

	def test_merge(self):
		self.vc.add_all()
		
		self.vc.commit()
		
		# write the file
		self.vc.repo.conn.commit()

		# make master (copy)
		self.vc.setup_master()

		p = edit_file()
		
		self.vc.add_all()
		self.vc.commit()
				
		self.vc.merge(self.vc.repo, self.vc.master)
		
		log = self.vc.master.diff(int(self.vc.master.get_value('last_version_number'))-1)
		self.assertTrue(p in log)
				
	def tearDown(self):
		self.vc.close()
		if os.path.exists(self.vc.local_db_name()):
			os.remove(self.vc.local_db_name())
		if os.path.exists(self.vc.master_db_name()):
			os.remove(self.vc.master_db_name())

class VersionControl:
	def __init__(self, root=None, testing=False):
		self.testing = testing

		self.set_root(root)

		self.repo = Repository(self, self.local_db_name())
		self.ignore_folders = ['.git', '.', '..']
		self.ignore_files = ['py', 'pyc', 'DS_Store', 'txt', 'db-journal', 'db']
	
	def local_db_name(self):
		"""return local db name"""
		return os.path.join(self.root_path, 'versions-local.db' + (self.testing and '.test' or ''))

	def master_db_name(self):
		"""return master db name"""
		return os.path.join(self.root_path, 'versions-master.db' + (self.testing and '.test' or ''))
	
	def setup_master(self):
		"""
			setup master db from local (if not present)
		"""
		import os
		if not os.path.exists(self.master_db_name()):
			os.system('cp %s %s' % (self.local_db_name(), self.master_db_name()))

		self.master = Repository(self, self.master_db_name())
	
	def set_root(self, path=None):
		"""
			set / reset root and connect
			(the root path is the path of the folder)
		"""
		import os
		if not path:
			path = os.path.abspath(os.path.curdir)
		
		self.root_path = path
	
	def relpath(self, fname):
		"""
			get relative path from root path
		"""
		import os
		return os.path.relpath(fname, self.root_path)
	
	def timestamp(self, path):
		"""
			returns timestamp
		"""
		import os
		if os.path.exists(path):
			return int(os.stat(path).st_mtime)
		else:
			return 0

	def add_all(self):
		"""
			walk the root folder Add all dirty files to the vcs
		"""
		import os
		for wt in os.walk(self.root_path, followlinks = True):
			# ignore folders
			for folder in self.ignore_folders:
{'fraction_non_alphanumeric': 0.1546888694127958, 'fraction_numerical': 0.0037248028045574057, 'mean_word_length': 3.8301587301587303, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4184317', 'n_tokens_mistral': 3705, 'n_tokens_neox': 3314, 'n_words': 756}