def predict_local_user(repo): config = GitConfigParser( [os.path.normpath(os.path.expanduser("~/.gitconfig"))], read_only=True ) writer = repo.config_writer() sections = config.sections() i = 0 is_set = False while i < len(sections) and not is_set: sec = sections[i] if 'multi-user' in sec: # name = re.findall(r'"([^"]*)"', sec)[1] user = dict(config.items(sec)) logging.debug("multi-user: %s", str(user)) if 'url' in user and 'name' in user and 'email' in user: for remote in repo.remotes: logging.debug("remote-url: %s", remote.url) prog = re.compile(user['url']) if prog.match(remote.url): logging.info( "%s found in remote url %s", user['url'], remote.url ) logging.info( "Setting local user.name to %s", user["name"] ) writer.set_value('user', 'name', user['name']) logging.info( "Setting local user.email to %s", user["email"] ) writer.set_value('user', 'email', user['email']) writer.release() is_set = True break elif 'url' not in user: logging.warning('url not set for %s', sec) elif 'name' not in user: logging.warning('name not set for %s', sec) elif 'email' not in user: logging.warning('email not set for %s', sec) i = i + 1 return is_set
def test_rename(self): file_obj = self._to_memcache(fixture_path('git_config')) cw = GitConfigParser(file_obj, read_only=False, merge_includes=False) self.failUnlessRaises(ValueError, cw.rename_section, "doesntexist", "foo") self.failUnlessRaises(ValueError, cw.rename_section, "core", "include") nn = "bee" assert cw.rename_section('core', nn) is cw assert not cw.has_section('core') assert len(cw.items(nn)) == 4 cw.release()
def test_add_to_multiple(self): file_obj = self._to_memcache(fixture_path('git_config_multiple')) with GitConfigParser(file_obj, read_only=False) as cw: cw.add_value('section1', 'option1', 'value1c') cw.write() file_obj.seek(0) cr = GitConfigParser(file_obj, read_only=True) self.assertEqual(cr.get_value('section1', 'option1'), 'value1c') self.assertEqual(cr.get_values('section1', 'option1'), ['value1a', 'value1b', 'value1c']) self.assertEqual(cr.items('section1'), [('option1', 'value1c'), ('other_option1', 'other_value1')]) self.assertEqual(cr.items_all('section1'), [('option1', ['value1a', 'value1b', 'value1c']), ('other_option1', ['other_value1'])])
def get_author() -> Actor: """ Get the name and email information in the user's .gitconfig. :return: the git author information as an :py:class:`~git.util.Actor` """ info = {} gitconfig = GitConfigParser(os.path.expanduser('~/.gitconfig')) sections = gitconfig.sections() assert 'user' in sections, 'No "user" field in .gitconfig' user_items = gitconfig.items('user') for tup in user_items: info[tup[0]] = tup[1] assert 'name' in info and 'email' in info, 'Incomplete information (name and/or email) in .gitconfig' return Actor(name=info['name'], email=info['email'])
def test_config_include(self, rw_dir): def write_test_value(cw, value): cw.set_value(value, 'value', value) # end def check_test_value(cr, value): assert cr.get_value(value, 'value') == value # end # PREPARE CONFIG FILE A fpa = os.path.join(rw_dir, 'a') cw = GitConfigParser(fpa, read_only=False) write_test_value(cw, 'a') fpb = os.path.join(rw_dir, 'b') fpc = os.path.join(rw_dir, 'c') cw.set_value('include', 'relative_path_b', 'b') cw.set_value('include', 'doesntexist', 'foobar') cw.set_value('include', 'relative_cycle_a_a', 'a') cw.set_value('include', 'absolute_cycle_a_a', fpa) cw.release() assert os.path.exists(fpa) # PREPARE CONFIG FILE B cw = GitConfigParser(fpb, read_only=False) write_test_value(cw, 'b') cw.set_value('include', 'relative_cycle_b_a', 'a') cw.set_value('include', 'absolute_cycle_b_a', fpa) cw.set_value('include', 'relative_path_c', 'c') cw.set_value('include', 'absolute_path_c', fpc) cw.release() # PREPARE CONFIG FILE C cw = GitConfigParser(fpc, read_only=False) write_test_value(cw, 'c') cw.release() cr = GitConfigParser(fpa, read_only=True) for tv in ('a', 'b', 'c'): check_test_value(cr, tv) # end for each test to verify assert len(cr.items( 'include')) == 8, "Expected all include sections to be merged" cr.release() # test writable config writers - assure write-back doesn't involve includes cw = GitConfigParser(fpa, read_only=False, merge_includes=True) tv = 'x' write_test_value(cw, tv) cw.release() cr = GitConfigParser(fpa, read_only=True) self.failUnlessRaises(cp.NoSectionError, check_test_value, cr, tv) cr.release() # But can make it skip includes alltogether, and thus allow write-backs cw = GitConfigParser(fpa, read_only=False, merge_includes=False) write_test_value(cw, tv) cw.release() cr = GitConfigParser(fpa, read_only=True) check_test_value(cr, tv) cr.release()
def test_config_include(self, rw_dir): def write_test_value(cw, value): cw.set_value(value, 'value', value) # end def check_test_value(cr, value): assert cr.get_value(value, 'value') == value # end # PREPARE CONFIG FILE A fpa = os.path.join(rw_dir, 'a') cw = GitConfigParser(fpa, read_only=False) write_test_value(cw, 'a') fpb = os.path.join(rw_dir, 'b') fpc = os.path.join(rw_dir, 'c') cw.set_value('include', 'relative_path_b', 'b') cw.set_value('include', 'doesntexist', 'foobar') cw.set_value('include', 'relative_cycle_a_a', 'a') cw.set_value('include', 'absolute_cycle_a_a', fpa) cw.release() assert os.path.exists(fpa) # PREPARE CONFIG FILE B cw = GitConfigParser(fpb, read_only=False) write_test_value(cw, 'b') cw.set_value('include', 'relative_cycle_b_a', 'a') cw.set_value('include', 'absolute_cycle_b_a', fpa) cw.set_value('include', 'relative_path_c', 'c') cw.set_value('include', 'absolute_path_c', fpc) cw.release() # PREPARE CONFIG FILE C cw = GitConfigParser(fpc, read_only=False) write_test_value(cw, 'c') cw.release() cr = GitConfigParser(fpa, read_only=True) for tv in ('a', 'b', 'c'): check_test_value(cr, tv) # end for each test to verify assert len(cr.items('include')) == 8, "Expected all include sections to be merged" cr.release() # test writable config writers - assure write-back doesn't involve includes cw = GitConfigParser(fpa, read_only=False, merge_includes=True) tv = 'x' write_test_value(cw, tv) cw.release() cr = GitConfigParser(fpa, read_only=True) self.failUnlessRaises(cp.NoSectionError, check_test_value, cr, tv) cr.release() # But can make it skip includes alltogether, and thus allow write-backs cw = GitConfigParser(fpa, read_only=False, merge_includes=False) write_test_value(cw, tv) cw.release() cr = GitConfigParser(fpa, read_only=True) check_test_value(cr, tv) cr.release()