Repository: coala/coala

Issue:
git commit bear doesn't work if coala isn't run in the same directory
it should also work if I provide a coafile with `-c`, @Makman2 can you fix that soonish? This is kind of the reason why this breaks everywhere for my gitmate experiments.



Discussion:
@sils1297 you know how to retrieve the filename of the coafile being used? Then I can just passing this as a working directory to git.

@Makman2 so the `section` might have a `config` setting that points to the coafile. Actually @abhsag24 has a function in one of his PRs that retrieves that location for you, we just need to move it out later.


Resolving patch:
diff --git a/bears/vcs/git/GitCommitBear.py b/bears/vcs/git/GitCommitBear.py
--- a/bears/vcs/git/GitCommitBear.py
+++ b/bears/vcs/git/GitCommitBear.py
@@ -1,4 +1,5 @@
 import shutil
+import os
 
 from coalib.bears.GlobalBear import GlobalBear
 from coalib.misc.Shell import run_shell_command
@@ -38,6 +39,10 @@ def run(self,
         :param allow_empty_commit_message: Whether empty commit messages are
                                            allowed or not.
         """
+        config_dir = self.get_config_dir()
+        old_dir = os.getcwd()
+        if config_dir:
+            os.chdir(config_dir)
         stdout, stderr = run_shell_command(self._git_command)
 
         if stderr:
@@ -56,6 +61,8 @@ def run(self,
         yield from self.check_shortlog(shortlog_length, stdout[0])
         yield from self.check_body(body_line_length, force_body, stdout[1:])
 
+        os.chdir(old_dir)
+
     def check_shortlog(self, shortlog_length, shortlog):
         """
         Checks the given shortlog.
diff --git a/coalib/bears/Bear.py b/coalib/bears/Bear.py
--- a/coalib/bears/Bear.py
+++ b/coalib/bears/Bear.py
@@ -5,6 +5,7 @@
 from coalib.output.printers.LogPrinter import LogPrinter
 from coalib.settings.FunctionMetadata import FunctionMetadata
 from coalib.settings.Section import Section
+from coalib.settings.ConfigurationGathering import get_config_directory
 
 
 class Bear(Printer, LogPrinter):
@@ -180,3 +181,11 @@ def check_prerequisites(cls):
                  that serves a more detailed description of what's missing.
         """
         return True
+
+    def get_config_dir(self):
+        """
+        Gives the directory where the configuration file is
+
+        :return: Directory of the config file
+        """
+        return get_config_directory(self.section)


Test patch:
diff --git a/bears/tests/vcs/git/GitCommitBearTest.py b/bears/tests/vcs/git/GitCommitBearTest.py
--- a/bears/tests/vcs/git/GitCommitBearTest.py
+++ b/bears/tests/vcs/git/GitCommitBearTest.py
@@ -5,11 +5,14 @@
 import stat
 import shutil
 import unittest
+import re
 
 from coalib.misc.Shell import run_shell_command
 from coalib.settings.Section import Section
+from coalib.settings.Setting import Setting
 from bears.vcs.git.GitCommitBear import GitCommitBear
 from bears.tests.BearTestHelper import generate_skip_decorator
+from coalib.settings.ConfigurationGathering import get_config_directory
 
 
 @generate_skip_decorator(GitCommitBear)
@@ -42,7 +45,8 @@ def run_uut(self, *args, **kwargs):
 
     def setUp(self):
         self.msg_queue = Queue()
-        self.uut = GitCommitBear(None, Section(""), self.msg_queue)
+        self.section = Section("")
+        self.uut = GitCommitBear(None, self.section, self.msg_queue)
 
         self._old_cwd = os.getcwd()
         self.gitdir = mkdtemp()
@@ -146,3 +150,20 @@ def test_body_checks(self):
         self.assertEqual(self.run_uut(body_line_length=41),
                          ["Body of HEAD commit contains too long lines."])
         self.assertTrue(self.msg_queue.empty())
+
+    def test_different_path(self):
+        no_git_dir = mkdtemp()
+        self.git_commit("A shortlog that is too long is not good for history")
+        os.chdir(no_git_dir)
+        # When section doesn't have a config setting
+        self.assertEqual(self.run_uut(), [])
+        git_error = self.msg_queue.get().message
+        self.assertEqual(git_error[:4], "git:")
+        # when section does have a config setting
+        self.section.append(Setting("config", re.escape(self.gitdir)))
+        self.assertEqual(self.run_uut(),
+                         ["Shortlog of HEAD commit is too long."])
+        self.assertEqual(get_config_directory(self.section),
+                         self.gitdir)
+        os.chdir(self.gitdir)
+        os.rmdir(no_git_dir)
diff --git a/coalib/tests/bears/BearTest.py b/coalib/tests/bears/BearTest.py
--- a/coalib/tests/bears/BearTest.py
+++ b/coalib/tests/bears/BearTest.py
@@ -166,3 +166,9 @@ def test_check_prerequisites(self):
                            "The bear BearWithPrerequisites does not fulfill "
                            "all requirements. Just because I want to.")
         self.assertTrue(self.queue.empty())
+
+    def test_get_config_dir(self):
+        section = Section("default")
+        section.append(Setting("config", "/path/to/dir/config"))
+        uut = TestBear(section, None)
+        self.assertEqual(uut.get_config_dir(), "/path/to/dir")