mirror of
				https://git.proxmox.com/git/libgit2
				synced 2025-11-04 01:31:47 +00:00 
			
		
		
		
	This changes the get_entry() method to return a refcounted version of the config entry, which you have to free when you're done. This allows us to avoid freeing the memory in which the entry is stored on a refresh, which may happen at any time for a live config. For this reason, get_string() has been forbidden on live configs and a new function get_string_buf() has been added, which stores the string in a git_buf which the user then owns. The functions which parse the string value takea advantage of the borrowing to parse safely and then release the entry.
		
			
				
	
	
		
			69 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			69 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#include "clar_libgit2.h"
 | 
						|
#include "config_helpers.h"
 | 
						|
#include "repository.h"
 | 
						|
#include "buffer.h"
 | 
						|
 | 
						|
void assert_config_entry_existence(
 | 
						|
	git_repository *repo,
 | 
						|
	const char *name,
 | 
						|
	bool is_supposed_to_exist)
 | 
						|
{
 | 
						|
	git_config *config;
 | 
						|
	git_config_entry *entry = NULL;
 | 
						|
	int result;
 | 
						|
 | 
						|
	cl_git_pass(git_repository_config__weakptr(&config, repo));
 | 
						|
	
 | 
						|
	result = git_config_get_entry(&entry, config, name);
 | 
						|
	git_config_entry_free(entry);
 | 
						|
 | 
						|
	if (is_supposed_to_exist)
 | 
						|
		cl_git_pass(result);
 | 
						|
	else
 | 
						|
		cl_assert_equal_i(GIT_ENOTFOUND, result);
 | 
						|
}
 | 
						|
 | 
						|
void assert_config_entry_value(
 | 
						|
	git_repository *repo,
 | 
						|
	const char *name,
 | 
						|
	const char *expected_value)
 | 
						|
{
 | 
						|
	git_config *config;
 | 
						|
	git_buf buf = GIT_BUF_INIT;
 | 
						|
 | 
						|
	cl_git_pass(git_repository_config__weakptr(&config, repo));
 | 
						|
 | 
						|
	cl_git_pass(git_config_get_string_buf(&buf, config, name));
 | 
						|
 | 
						|
	cl_assert_equal_s(expected_value, git_buf_cstr(&buf));
 | 
						|
	git_buf_free(&buf);
 | 
						|
}
 | 
						|
 | 
						|
static int count_config_entries_cb(
 | 
						|
	const git_config_entry *entry,
 | 
						|
	void *payload)
 | 
						|
{
 | 
						|
	int *how_many = (int *)payload;
 | 
						|
 | 
						|
	GIT_UNUSED(entry);
 | 
						|
 | 
						|
	(*how_many)++;
 | 
						|
 | 
						|
	return 0;
 | 
						|
}
 | 
						|
 | 
						|
int count_config_entries_match(git_repository *repo, const char *pattern)
 | 
						|
{
 | 
						|
	git_config *config;
 | 
						|
	int how_many = 0;
 | 
						|
 | 
						|
	cl_git_pass(git_repository_config(&config, repo));
 | 
						|
 | 
						|
	cl_assert_equal_i(0, git_config_foreach_match(
 | 
						|
		config,	pattern, count_config_entries_cb, &how_many));
 | 
						|
 | 
						|
	git_config_free(config);
 | 
						|
 | 
						|
	return how_many;
 | 
						|
}
 |