diff --git a/lms/djangoapps/dashboard/git_import.py b/lms/djangoapps/dashboard/git_import.py index 67c2942ee1..336cee787e 100644 --- a/lms/djangoapps/dashboard/git_import.py +++ b/lms/djangoapps/dashboard/git_import.py @@ -97,23 +97,28 @@ def add_repo(repo, rdir_in): cwd = os.path.abspath(cwd) try: ret_git = cmd_log(cmd, cwd=cwd) - except subprocess.CalledProcessError: + except subprocess.CalledProcessError as ex: + log.exception('Error running git pull: %r', ex.output) raise GitImportError(GitImportError.CANNOT_PULL) # get commit id cmd = ['git', 'log', '-1', '--format=%H', ] try: commit_id = cmd_log(cmd, cwd=rdirp) - except subprocess.CalledProcessError: + except subprocess.CalledProcessError as ex: + log.exception('Unable to get git log: %r', ex.output) raise GitImportError(GitImportError.BAD_REPO) ret_git += '\nCommit ID: {0}'.format(commit_id) # get branch - cmd = ['git', 'rev-parse', '--abbrev-ref', 'HEAD', ] + cmd = ['git', 'symbolic-ref', '--short', 'HEAD', ] try: branch = cmd_log(cmd, cwd=rdirp) - except subprocess.CalledProcessError: + except subprocess.CalledProcessError as ex: + # I can't discover a way to excercise this, but git is complex + # so still logging and raising here in case. + log.exception('Unable to determine branch: %r', ex.output) raise GitImportError(GitImportError.BAD_REPO) ret_git += '{0}Branch: {1}'.format(' \n', branch) diff --git a/lms/djangoapps/dashboard/management/commands/tests/test_git_add_course.py b/lms/djangoapps/dashboard/management/commands/tests/test_git_add_course.py index ad4f05eebb..d231b0173d 100644 --- a/lms/djangoapps/dashboard/management/commands/tests/test_git_add_course.py +++ b/lms/djangoapps/dashboard/management/commands/tests/test_git_add_course.py @@ -102,3 +102,21 @@ class TestGitAddCourse(ModuleStoreTestCase): with self.assertRaisesRegexp(GitImportError, GitImportError.BAD_REPO): git_import.add_repo('file://{0}'.format(bare_repo), None) + + def test_detached_repo(self): + """ + Test repo that is in detached head state. + """ + repo_dir = getattr(settings, 'GIT_REPO_DIR') + # Test successful import from command + try: + os.mkdir(repo_dir) + except OSError: + pass + self.addCleanup(shutil.rmtree, repo_dir) + git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite') + subprocess.check_output(['git', 'checkout', 'HEAD~2', ], + stderr=subprocess.STDOUT, + cwd=repo_dir / 'edx4edx_lite') + with self.assertRaisesRegexp(GitImportError, GitImportError.CANNOT_PULL): + git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite')