mirror of
				https://git.proxmox.com/git/libgit2
				synced 2025-11-04 14:08:17 +00:00 
			
		
		
		
	Add git_repository_is_detached, git_repository_is_orphan
Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
This commit is contained in:
		
							parent
							
								
									0b10c9ea6e
								
							
						
					
					
						commit
						35502d2ec4
					
				@ -218,6 +218,30 @@ GIT_EXTERN(void) git_repository_free(git_repository *repo);
 | 
			
		||||
 */
 | 
			
		||||
GIT_EXTERN(int) git_repository_init(git_repository **repo_out, const char *path, unsigned is_bare);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Check if a repository's HEAD is detached
 | 
			
		||||
 *
 | 
			
		||||
 * A repository's HEAD is detached when it points directly to a commit
 | 
			
		||||
 * instead of a branch.
 | 
			
		||||
 *
 | 
			
		||||
 * @param repo Repo to test
 | 
			
		||||
 * @return 1 if HEAD is detached, 0 if i'ts not; error code if there
 | 
			
		||||
 * was an error.
 | 
			
		||||
 */
 | 
			
		||||
int git_repository_is_detached(git_repository *repo);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Check if the current branch is an orphan
 | 
			
		||||
 *
 | 
			
		||||
 * An orphan branch is one named from HEAD but which doesn't exist in
 | 
			
		||||
 * the refs namespace, because it doesn't have any commit to point to.
 | 
			
		||||
 *
 | 
			
		||||
 * @param repo Repo to test
 | 
			
		||||
 * @return 1 if the current branch is an orphan, 0 if it's not; error
 | 
			
		||||
 * code if therewas an error
 | 
			
		||||
 */
 | 
			
		||||
int git_repository_is_orphan(git_repository *repo);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Check if a repository is empty
 | 
			
		||||
 *
 | 
			
		||||
 | 
			
		||||
@ -752,6 +752,47 @@ cleanup:
 | 
			
		||||
	return git__rethrow(error, "Failed to (re)init the repository `%s`", path);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int git_repository_is_detached(git_repository *repo)
 | 
			
		||||
{
 | 
			
		||||
	git_reference *ref;
 | 
			
		||||
	int error;
 | 
			
		||||
	size_t GIT_UNUSED(_size);
 | 
			
		||||
	git_otype type;
 | 
			
		||||
 | 
			
		||||
	error = git_reference_lookup(&ref, repo, GIT_HEAD_FILE);
 | 
			
		||||
	if (error < GIT_SUCCESS)
 | 
			
		||||
		return error;
 | 
			
		||||
 | 
			
		||||
	if (git_reference_type(ref) == GIT_REF_SYMBOLIC)
 | 
			
		||||
		return 0;
 | 
			
		||||
 | 
			
		||||
	error = git_odb_read_header(&_size, &type, repo->db, git_reference_oid(ref));
 | 
			
		||||
	if (error < GIT_SUCCESS)
 | 
			
		||||
		return error;
 | 
			
		||||
 | 
			
		||||
	if (type != GIT_OBJ_COMMIT)
 | 
			
		||||
		return git__throw(GIT_EOBJCORRUPTED, "HEAD is not a commit");
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int git_repository_is_orphan(git_repository *repo)
 | 
			
		||||
{
 | 
			
		||||
	git_reference *ref;
 | 
			
		||||
	int error;
 | 
			
		||||
 | 
			
		||||
	error = git_reference_lookup(&ref, repo, GIT_HEAD_FILE);
 | 
			
		||||
	if (error < GIT_SUCCESS)
 | 
			
		||||
		return error;
 | 
			
		||||
 | 
			
		||||
	if (git_reference_type(ref) == GIT_REF_OID)
 | 
			
		||||
		return 0;
 | 
			
		||||
 | 
			
		||||
	error = git_reference_resolve(&ref, ref);
 | 
			
		||||
 | 
			
		||||
	return error == GIT_ENOTFOUND ? 1 : error;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int git_repository_is_empty(git_repository *repo)
 | 
			
		||||
{
 | 
			
		||||
	git_reference *head, *branch;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user