Commit 6117895f by Vicent Martí

Merge pull request #558 from schu/notes-api

Notes API
parents 0c3bae62 bf477ed4
......@@ -40,4 +40,6 @@
#include "git2/status.h"
#include "git2/indexer.h"
#include "git2/notes.h"
#endif
......@@ -191,7 +191,8 @@ GIT_EXTERN(const git_oid *) git_commit_parent_oid(git_commit *commit, unsigned i
* will be updated to point to this commit. If the reference
* is not direct, it will be resolved to a direct reference.
* Use "HEAD" to update the HEAD of the current branch and
* make it point to this commit
* make it point to this commit. If the reference doesn't
* exist yet, it will be created.
*
* @param author Signature representing the author and the authory
* time of this commit
......
/*
* Copyright (C) 2009-2012 the libgit2 contributors
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_git_note_h__
#define INCLUDE_git_note_h__
#include "oid.h"
/**
* @file git2/notes.h
* @brief Git notes management routines
* @defgroup git_note Git notes management routines
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL
/**
* Read the note for an object
*
* The note must be freed manually by the user.
*
* @param note the note; NULL in case of error
* @param repo the Git repository
* @param notes_ref OID reference to use (optional); defaults to "refs/notes/commits"
* @param oid OID of the object
*
* @return GIT_SUCCESS or an error code
*/
GIT_EXTERN(int) git_note_read(git_note **note, git_repository *repo,
const char *notes_ref, const git_oid *oid);
/**
* Get the note message
*
* @param note
* @return the note message
*/
GIT_EXTERN(const char *) git_note_message(git_note *note);
/**
* Get the note object OID
*
* @param note
* @return the note object OID
*/
GIT_EXTERN(const git_oid *) git_note_oid(git_note *note);
/**
* Add a note for an object
*
* @param oid pointer to store the OID (optional); NULL in case of error
* @param repo the Git repository
* @param author signature of the notes commit author
* @param committer signature of the notes commit committer
* @param notes_ref OID reference to update (optional); defaults to "refs/notes/commits"
* @param oid The OID of the object
* @param oid The note to add for object oid
*
* @return GIT_SUCCESS or an error code
*/
GIT_EXTERN(int) git_note_create(git_oid *out, git_repository *repo,
git_signature *author, git_signature *committer,
const char *notes_ref, const git_oid *oid,
const char *note);
/**
* Remove the note for an object
*
* @param repo the Git repository
* @param notes_ref OID reference to use (optional); defaults to "refs/notes/commits"
* @param author signature of the notes commit author
* @param committer signature of the notes commit committer
* @param oid the oid which note's to be removed
*
* @return GIT_SUCCESS or an error code
*/
GIT_EXTERN(int) git_note_remove(git_repository *repo, const char *notes_ref,
git_signature *author, git_signature *committer,
const git_oid *oid);
/**
* Free a git_note object
*
* @param note git_note object
*/
GIT_EXTERN(void) git_note_free(git_note *note);
/** @} */
GIT_END_DECL
#endif
......@@ -131,6 +131,9 @@ typedef struct git_reflog_entry git_reflog_entry;
/** Representation of a reference log */
typedef struct git_reflog git_reflog;
/** Representation of a git note */
typedef struct git_note git_note;
/** Time in a signature */
typedef struct git_time {
git_time_t time; /** time in seconds from epoch */
......
......@@ -146,10 +146,14 @@ int git_commit_create(
git_reference *target;
error = git_reference_lookup(&head, repo, update_ref);
if (error < GIT_SUCCESS)
if (error < GIT_SUCCESS && error != GIT_ENOTFOUND)
return git__rethrow(error, "Failed to create commit");
error = git_reference_resolve(&target, head);
if (error != GIT_ENOTFOUND) {
update_ref = git_reference_target(head);
error = git_reference_resolve(&target, head);
}
if (error < GIT_SUCCESS) {
if (error != GIT_ENOTFOUND) {
git_reference_free(head);
......@@ -162,7 +166,7 @@ int git_commit_create(
* point to) or after an orphan checkout, so if the target
* branch doesn't exist yet, create it and return.
*/
error = git_reference_create_oid(&target, repo, git_reference_target(head), oid, 1);
error = git_reference_create_oid(&target, repo, update_ref, oid, 1);
git_reference_free(head);
if (error == GIT_SUCCESS)
......
This diff is collapsed. Click to expand it.
/*
* Copyright (C) 2009-2012 the libgit2 contributors
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_note_h__
#define INCLUDE_note_h__
#include "common.h"
#include "git2/oid.h"
#define GIT_NOTES_DEFAULT_REF "refs/notes/commits"
#define GIT_NOTES_DEFAULT_MSG_ADD \
"Notes added by 'git_note_create' from libgit2"
#define GIT_NOTES_DEFAULT_MSG_RM \
"Notes removed by 'git_note_remove' from libgit2"
struct git_note {
git_oid oid;
char *message;
};
#endif /* INCLUDE_notes_h__ */
......@@ -169,4 +169,13 @@ GIT_INLINE(int) git__fromhex(char h)
return from_hex[(unsigned char) h];
}
GIT_INLINE(int) git__ishex(const char *str)
{
unsigned i;
for (i=0; i<strlen(str); i++)
if (git__fromhex(str[i]) < 0)
return 0;
return 1;
}
#endif /* INCLUDE_util_h__ */
#include "clar_libgit2.h"
static git_repository *_repo;
void test_commit_commit__initialize(void)
{
cl_fixture_sandbox("testrepo.git");
cl_git_pass(git_repository_open(&_repo, "testrepo.git"));
}
void test_commit_commit__cleanup(void)
{
git_repository_free(_repo);
cl_fixture_cleanup("testrepo.git");
}
void test_commit_commit__create_unexisting_update_ref(void)
{
git_oid oid;
git_tree *tree;
git_commit *commit;
git_signature *s;
git_reference *ref;
git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
cl_git_pass(git_commit_lookup(&commit, _repo, &oid));
git_oid_fromstr(&oid, "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
cl_git_pass(git_tree_lookup(&tree, _repo, &oid));
cl_git_pass(git_signature_now(&s, "alice", "alice@example.com"));
cl_git_fail(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
cl_git_pass(git_commit_create(&oid, _repo, "refs/heads/foo/bar", s, s,
NULL, "some msg", tree, 1, (const git_commit **) &commit));
cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
cl_assert(!git_oid_cmp(&oid, git_reference_oid(ref)));
git_tree_free(tree);
git_commit_free(commit);
git_signature_free(s);
git_reference_free(ref);
}
#include "clar_libgit2.h"
static git_repository *_repo;
static git_note *_note;
static git_blob *_blob;
static git_signature *_sig;
void test_notes_notes__initialize(void)
{
cl_fixture_sandbox("testrepo.git");
cl_git_pass(git_repository_open(&_repo, "testrepo.git"));
}
void test_notes_notes__cleanup(void)
{
git_note_free(_note);
git_blob_free(_blob);
git_signature_free(_sig);
git_repository_free(_repo);
cl_fixture_cleanup("testrepo.git");
}
void test_notes_notes__1(void)
{
git_oid oid, note_oid;
cl_git_pass(git_signature_now(&_sig, "alice", "alice@example.com"));
cl_git_pass(git_note_create(&note_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &oid, "hello world\n"));
cl_git_pass(git_note_create(&note_oid, _repo, _sig, _sig, NULL, &oid, "hello world\n"));
cl_git_pass(git_note_read(&_note, _repo, NULL, &oid));
cl_assert(!strcmp(git_note_message(_note), "hello world\n"));
cl_assert(!git_oid_cmp(git_note_oid(_note), &note_oid));
cl_git_pass(git_blob_lookup(&_blob, _repo, &note_oid));
cl_assert(!strcmp(git_note_message(_note), git_blob_rawcontent(_blob)));
cl_git_fail(git_note_create(&note_oid, _repo, _sig, _sig, NULL, &oid, "hello world\n"));
cl_git_fail(git_note_create(&note_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &oid, "hello world\n"));
cl_git_pass(git_note_remove(_repo, NULL, _sig, _sig, &oid));
cl_git_pass(git_note_remove(_repo, "refs/notes/some/namespace", _sig, _sig, &oid));
cl_git_fail(git_note_remove(_repo, NULL, _sig, _sig, &note_oid));
cl_git_fail(git_note_remove(_repo, "refs/notes/some/namespace", _sig, _sig, &oid));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment