Rewrote the config module into a class (GitConfig).

This class takes the initialization variable "global_only" which
will be set in the future whenever multiple repositories are
specified (See issue #24).

This will make sure that the git configuration is only read from the
global settings instead of per-repository.
This commit is contained in:
Adam Waldenberg 2015-10-30 01:22:15 +01:00
parent 5af89f798a
commit c01a59430c
2 changed files with 62 additions and 55 deletions

View File

@ -1,6 +1,6 @@
# coding: utf-8
#
# Copyright © 2013 Ejwa Software. All rights reserved.
# Copyright © 2013-2015 Ejwa Software. All rights reserved.
#
# This file is part of gitinspector.
#
@ -22,65 +22,71 @@ import os
import subprocess
from . import extensions, filtering, format, interval, optval
def __read_git_config__(repo, variable):
previous_directory = os.getcwd()
os.chdir(repo)
setting = subprocess.Popen(["git", "config", "inspector." + variable], bufsize=1, stdout=subprocess.PIPE).stdout
os.chdir(previous_directory)
class GitConfig(object):
def __init__(self, run, global_only=False):
self.run = run
self.global_only = global_only
try:
setting = setting.readlines()[0]
setting = setting.decode("utf-8", "replace").strip()
except IndexError:
setting = ""
def __read_git_config__(self, variable):
previous_directory = os.getcwd()
os.chdir(self.run.repo)
setting = subprocess.Popen(filter(None, ["git", "config", "--global" if self.global_only else "",
"inspector." + variable]), bufsize=1, stdout=subprocess.PIPE).stdout
os.chdir(previous_directory)
return setting
try:
setting = setting.readlines()[0]
setting = setting.decode("utf-8", "replace").strip()
except IndexError:
setting = ""
def __read_git_config_bool__(repo, variable):
try:
variable = __read_git_config__(repo, variable)
return optval.get_boolean_argument(False if variable == "" else variable)
except optval.InvalidOptionArgument:
return False
return setting
def __read_git_config_string__(repo, variable):
string = __read_git_config__(repo, variable)
return (True, string) if len(string) > 0 else (False, None)
def __read_git_config_bool__(self, variable):
try:
variable = self.__read_git_config__(variable)
return optval.get_boolean_argument(False if variable == "" else variable)
except optval.InvalidOptionArgument:
return False
def init(run):
var = __read_git_config_string__(run.repo, "file-types")
if var[0]:
extensions.define(var[1])
def __read_git_config_string__(self, variable):
string = self.__read_git_config__(variable)
return (True, string) if len(string) > 0 else (False, None)
var = __read_git_config_string__(run.repo, "exclude")
if var[0]:
filtering.add(var[1])
def read(self):
var = self.__read_git_config_string__("file-types")
if var[0]:
extensions.define(var[1])
var = __read_git_config_string__(run.repo, "format")
if var[0] and not format.select(var[1]):
raise format.InvalidFormatError(_("specified output format not supported."))
var = self.__read_git_config_string__("exclude")
if var[0]:
filtering.add(var[1])
run.hard = __read_git_config_bool__(run.repo, "hard")
run.list_file_types = __read_git_config_bool__(run.repo, "list-file-types")
run.localize_output = __read_git_config_bool__(run.repo, "localize-output")
run.metrics = __read_git_config_bool__(run.repo, "metrics")
run.responsibilities = __read_git_config_bool__(run.repo, "responsibilities")
run.useweeks = __read_git_config_bool__(run.repo, "weeks")
var = self.__read_git_config_string__("format")
if var[0] and not format.select(var[1]):
raise format.InvalidFormatError(_("specified output format not supported."))
var = __read_git_config_string__(run.repo, "since")
if var[0]:
interval.set_since(var[1])
self.run.hard = self.__read_git_config_bool__("hard")
self.run.list_file_types = self.__read_git_config_bool__("list-file-types")
self.run.localize_output = self.__read_git_config_bool__("localize-output")
self.run.metrics = self.__read_git_config_bool__("metrics")
self.run.responsibilities = self.__read_git_config_bool__("responsibilities")
self.run.useweeks = self.__read_git_config_bool__("weeks")
var = __read_git_config_string__(run.repo, "until")
if var[0]:
interval.set_until(var[1])
var = self.__read_git_config_string__("since")
if var[0]:
interval.set_since(var[1])
run.timeline = __read_git_config_bool__(run.repo, "timeline")
var = self.__read_git_config_string__("until")
if var[0]:
interval.set_until(var[1])
if __read_git_config_bool__(run.repo, "grading"):
run.hard = True
run.list_file_types = True
run.metrics = True
run.responsibilities = True
run.timeline = True
run.useweeks = True
self.run.timeline = self.__read_git_config_bool__("timeline")
if self.__read_git_config_bool__("grading"):
self.run.hard = True
self.run.list_file_types = True
self.run.metrics = True
self.run.responsibilities = True
self.run.timeline = True
self.run.useweeks = True

View File

@ -24,7 +24,8 @@ import getopt
import os
import sys
from .changes import Changes
from . import (basedir, clone, config, extensions, filtering, format, help, interval,
from .config import GitConfig
from . import (basedir, clone, extensions, filtering, format, help, interval,
localization, optval, terminal, version)
from .output import outputable
from .output.blameoutput import BlameOutput
@ -104,14 +105,14 @@ def main():
"localize-output:true", "metrics:true", "responsibilities:true",
"since=", "grading:true", "timeline:true", "until=", "version",
"weeks:true"])
for arg in __args__:
__run__.repo = arg
if len(__args__) > 0:
__run__.repo = __args__[-1]
#Try to clone the repo or return the same directory and bail out.
__run__.repo = clone.create(__run__.repo)
#We need the repo above to be set before we read the git config.
config.init(__run__)
GitConfig(__run__).read()
clear_x_on_next_pass = True
for o, a in __opts__: