From 9abd35343e588586b170af8c290dab47cd584f01 Mon Sep 17 00:00:00 2001 From: Carson Gee Date: Fri, 5 Sep 2014 11:18:30 -0400 Subject: [PATCH 1/2] Fixes git export to support repositories with dots in their name --- .../contentstore/git_export_utils.py | 2 +- .../contentstore/tests/test_export_git.py | 46 +++++++++++++------ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/cms/djangoapps/contentstore/git_export_utils.py b/cms/djangoapps/contentstore/git_export_utils.py index db370112eb..e15f6e0264 100644 --- a/cms/djangoapps/contentstore/git_export_utils.py +++ b/cms/djangoapps/contentstore/git_export_utils.py @@ -126,7 +126,7 @@ def export_to_git(course_id, repo, user='', rdir=None): # export course as xml before commiting and pushing root_dir = os.path.dirname(rdirp) - course_dir = os.path.splitext(os.path.basename(rdirp))[0] + course_dir = os.path.basename(rdirp).rsplit('.git', 1)[0] try: export_to_xml(modulestore(), contentstore(), course_id, root_dir, course_dir) diff --git a/cms/djangoapps/contentstore/tests/test_export_git.py b/cms/djangoapps/contentstore/tests/test_export_git.py index 946cc7dae4..85a3d40236 100644 --- a/cms/djangoapps/contentstore/tests/test_export_git.py +++ b/cms/djangoapps/contentstore/tests/test_export_git.py @@ -34,6 +34,28 @@ class TestExportGit(CourseTestCase): self.course_module = modulestore().get_course(self.course.id) self.test_url = reverse_course_url('export_git', self.course.id) + def make_bare_repo_with_course(self, repo_name): + """ + Make a local bare repo suitable for exporting to in + tests + """ + # Build out local bare repo, and set course git url to it + repo_dir = os.path.abspath(git_export_utils.GIT_REPO_EXPORT_DIR) + os.mkdir(repo_dir) + self.addCleanup(shutil.rmtree, repo_dir) + + bare_repo_dir = '{0}/{1}.git'.format( + os.path.abspath(git_export_utils.GIT_REPO_EXPORT_DIR), + repo_name + ) + os.mkdir(bare_repo_dir) + self.addCleanup(shutil.rmtree, bare_repo_dir) + + subprocess.check_output(['git', '--bare', 'init', ], cwd=bare_repo_dir) + self.populate_course() + self.course_module.giturl = 'file://{}'.format(bare_repo_dir) + modulestore().update_item(self.course_module, self.user.id) + def test_giturl_missing(self): """ Test to make sure an appropriate error is displayed @@ -79,21 +101,15 @@ class TestExportGit(CourseTestCase): """ Test successful course export response. """ - # Build out local bare repo, and set course git url to it - repo_dir = os.path.abspath(git_export_utils.GIT_REPO_EXPORT_DIR) - os.mkdir(repo_dir) - self.addCleanup(shutil.rmtree, repo_dir) - - bare_repo_dir = '{0}/test_repo.git'.format( - os.path.abspath(git_export_utils.GIT_REPO_EXPORT_DIR)) - os.mkdir(bare_repo_dir) - self.addCleanup(shutil.rmtree, bare_repo_dir) - - subprocess.check_output(['git', '--bare', 'init', ], cwd=bare_repo_dir) - - self.populate_course() - self.course_module.giturl = 'file://{}'.format(bare_repo_dir) - modulestore().update_item(self.course_module, self.user.id) + self.make_bare_repo_with_course('test_repo') + response = self.client.get('{}?action=push'.format(self.test_url)) + self.assertIn('Export Succeeded', response.content) + + def test_repo_with_dots(self): + """ + Regression test for a bad directory pathing of repo's that have dots. + """ + self.make_bare_repo_with_course('test.repo') response = self.client.get('{}?action=push'.format(self.test_url)) self.assertIn('Export Succeeded', response.content) From 0891d9ad6e8070a51527a41a9d932d21571ee01f Mon Sep 17 00:00:00 2001 From: Carson Gee Date: Fri, 5 Sep 2014 15:46:44 -0400 Subject: [PATCH 2/2] Added git clean to git export feature This ensures that additional exports of the content are the same the first. --- .../contentstore/git_export_utils.py | 1 + .../contentstore/tests/test_export_git.py | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/cms/djangoapps/contentstore/git_export_utils.py b/cms/djangoapps/contentstore/git_export_utils.py index e15f6e0264..2ee7eeb7ff 100644 --- a/cms/djangoapps/contentstore/git_export_utils.py +++ b/cms/djangoapps/contentstore/git_export_utils.py @@ -111,6 +111,7 @@ def export_to_git(course_id, repo, user='', rdir=None): ['git', 'fetch', 'origin'], ['git', 'reset', '--hard', 'origin/{0}'.format(branch)], ['git', 'pull'], + ['git', 'clean', '-d', '-f'], ] else: cmds = [['git', 'clone', repo]] diff --git a/cms/djangoapps/contentstore/tests/test_export_git.py b/cms/djangoapps/contentstore/tests/test_export_git.py index 85a3d40236..7054b6b439 100644 --- a/cms/djangoapps/contentstore/tests/test_export_git.py +++ b/cms/djangoapps/contentstore/tests/test_export_git.py @@ -113,3 +113,30 @@ class TestExportGit(CourseTestCase): self.make_bare_repo_with_course('test.repo') response = self.client.get('{}?action=push'.format(self.test_url)) self.assertIn('Export Succeeded', response.content) + + def test_dirty_repo(self): + """ + Add additional items not in the repo and make sure they aren't + there after the export. This allows old content to removed + in the repo. + """ + repo_name = 'dirty_repo1' + self.make_bare_repo_with_course(repo_name) + git_export_utils.export_to_git(self.course.id, + self.course_module.giturl, self.user) + + # Make arbitrary change to course to make diff + self.course_module.matlab_api_key = 'something' + modulestore().update_item(self.course_module, self.user.id) + # Touch a file in the directory, export again, and make sure + # the test file is gone + repo_dir = os.path.join( + os.path.abspath(git_export_utils.GIT_REPO_EXPORT_DIR), + repo_name + ) + test_file = os.path.join(repo_dir, 'test.txt') + open(test_file, 'a').close() + self.assertTrue(os.path.isfile(test_file)) + git_export_utils.export_to_git(self.course.id, + self.course_module.giturl, self.user) + self.assertFalse(os.path.isfile(test_file))