Skip to content

Commit

Permalink
Merge pull request #669 from nulltoken/topic/reset
Browse files Browse the repository at this point in the history
Add git_reset()
  • Loading branch information
Vicent Martí committed Jun 7, 2012
2 parents b9ebcc5 + edebcef commit 6c08e69
Show file tree
Hide file tree
Showing 11 changed files with 380 additions and 61 deletions.
1 change: 1 addition & 0 deletions include/git2.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@
#include "git2/indexer.h"
#include "git2/submodule.h"
#include "git2/notes.h"
#include "git2/reset.h"

#endif
44 changes: 44 additions & 0 deletions include/git2/reset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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_reset_h__
#define INCLUDE_git_reset_h__

/**
* @file git2/reset.h
* @brief Git reset management routines
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL

/**
* Sets the current head to the specified commit oid and optionally
* resets the index and working tree to match.
*
* When specifying a Soft kind of reset, the head will be moved to the commit.
*
* Specifying a Mixed kind of reset will trigger a Soft reset and the index will
* be replaced with the content of the commit tree.
*
* TODO: Implement remaining kinds of resets.
*
* @param repo Repository where to perform the reset operation.
*
* @param target Object to which the Head should be moved to. This object
* must belong to the given `repo` and can either be a git_commit or a
* git_tag. When a git_tag is being passed, it should be dereferencable
* to a git_commit which oid will be used as the target of the branch.
*
* @param reset_type Kind of reset operation to perform.
*
* @return GIT_SUCCESS or an error code
*/
GIT_EXTERN(int) git_reset(git_repository *repo, const git_object *target, git_reset_type reset_type);

/** @} */
GIT_END_DECL
#endif
6 changes: 6 additions & 0 deletions include/git2/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ typedef enum {
GIT_BRANCH_REMOTE = 2,
} git_branch_t;

/** Kinds of reset operation. */
typedef enum {
GIT_RESET_SOFT = 1,
GIT_RESET_MIXED = 2,
} git_reset_type;

typedef struct git_refspec git_refspec;
typedef struct git_remote git_remote;

Expand Down
62 changes: 1 addition & 61 deletions src/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,66 +81,6 @@ int git_commit_create_v(
return res;
}

/* Update the reference named `ref_name` so it points to `oid` */
static int update_reference(git_repository *repo, git_oid *oid, const char *ref_name)
{
git_reference *ref;
int res;

res = git_reference_lookup(&ref, repo, ref_name);

/* If we haven't found the reference at all, we assume we need to create
* a new reference and that's it */
if (res == GIT_ENOTFOUND) {
giterr_clear();
return git_reference_create_oid(NULL, repo, ref_name, oid, 1);
}

if (res < 0)
return -1;

/* If we have found a reference, but it's symbolic, we need to update
* the direct reference it points to */
if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
git_reference *aux;
const char *sym_target;

/* The target pointed at by this reference */
sym_target = git_reference_target(ref);

/* resolve the reference to the target it points to */
res = git_reference_resolve(&aux, ref);

/*
* if the symbolic reference pointed to an inexisting ref,
* this is means we're creating a new branch, for example.
* We need to create a new direct reference with that name
*/
if (res == GIT_ENOTFOUND) {
giterr_clear();
res = git_reference_create_oid(NULL, repo, sym_target, oid, 1);
git_reference_free(ref);
return res;
}

/* free the original symbolic reference now; not before because
* we're using the `sym_target` pointer */
git_reference_free(ref);

if (res < 0)
return -1;

/* store the newly found direct reference in its place */
ref = aux;
}

/* ref is made to point to `oid`: ref is either the original reference,
* or the target of the symbolic reference we've looked up */
res = git_reference_set_oid(ref, oid);
git_reference_free(ref);
return res;
}

int git_commit_create(
git_oid *oid,
git_repository *repo,
Expand Down Expand Up @@ -192,7 +132,7 @@ int git_commit_create(
git_buf_free(&commit);

if (update_ref != NULL)
return update_reference(repo, oid, update_ref);
return git_reference__update(repo, oid, update_ref);

return 0;

Expand Down
59 changes: 59 additions & 0 deletions src/refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1705,3 +1705,62 @@ int git_reference_cmp(git_reference *ref1, git_reference *ref2)
return git_oid_cmp(&ref1->target.oid, &ref2->target.oid);
}

/* Update the reference named `ref_name` so it points to `oid` */
int git_reference__update(git_repository *repo, const git_oid *oid, const char *ref_name)
{
git_reference *ref;
int res;

res = git_reference_lookup(&ref, repo, ref_name);

/* If we haven't found the reference at all, we assume we need to create
* a new reference and that's it */
if (res == GIT_ENOTFOUND) {
giterr_clear();
return git_reference_create_oid(NULL, repo, ref_name, oid, 1);
}

if (res < 0)
return -1;

/* If we have found a reference, but it's symbolic, we need to update
* the direct reference it points to */
if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
git_reference *aux;
const char *sym_target;

/* The target pointed at by this reference */
sym_target = git_reference_target(ref);

/* resolve the reference to the target it points to */
res = git_reference_resolve(&aux, ref);

/*
* if the symbolic reference pointed to an inexisting ref,
* this is means we're creating a new branch, for example.
* We need to create a new direct reference with that name
*/
if (res == GIT_ENOTFOUND) {
giterr_clear();
res = git_reference_create_oid(NULL, repo, sym_target, oid, 1);
git_reference_free(ref);
return res;
}

/* free the original symbolic reference now; not before because
* we're using the `sym_target` pointer */
git_reference_free(ref);

if (res < 0)
return -1;

/* store the newly found direct reference in its place */
ref = aux;
}

/* ref is made to point to `oid`: ref is either the original reference,
* or the target of the symbolic reference we've looked up */
res = git_reference_set_oid(ref, oid);
git_reference_free(ref);
return res;
}
1 change: 1 addition & 0 deletions src/refs.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void git_repository__refcache_free(git_refcache *refs);

int git_reference__normalize_name(char *buffer_out, size_t out_size, const char *name);
int git_reference__normalize_name_oid(char *buffer_out, size_t out_size, const char *name);
int git_reference__update(git_repository *repo, const git_oid *oid, const char *ref_name);

/**
* Lookup a reference by name and try to resolve to an OID.
Expand Down
103 changes: 103 additions & 0 deletions src/reset.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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.
*/

#include "common.h"
#include "commit.h"
#include "tag.h"
#include "git2/reset.h"

#define ERROR_MSG "Cannot perform reset"

static int reset_error_invalid(const char *msg)
{
giterr_set(GITERR_INVALID, "%s - %s", ERROR_MSG, msg);
return -1;
}

int git_reset(
git_repository *repo,
const git_object *target,
git_reset_type reset_type)
{
git_otype target_type = GIT_OBJ_BAD;
git_object *commit = NULL;
git_index *index = NULL;
git_tree *tree = NULL;
int error = -1;

assert(repo && target);
assert(reset_type == GIT_RESET_SOFT || reset_type == GIT_RESET_MIXED);

if (git_object_owner(target) != repo)
return reset_error_invalid("The given target does not belong to this repository.");

if (reset_type == GIT_RESET_MIXED && git_repository_is_bare(repo))
return reset_error_invalid("Mixed reset is not allowed in a bare repository.");

target_type = git_object_type(target);

switch (target_type)
{
case GIT_OBJ_TAG:
if (git_tag_peel(&commit, (git_tag *)target) < 0)
goto cleanup;

if (git_object_type(commit) != GIT_OBJ_COMMIT) {
reset_error_invalid("The given target does not resolve to a commit.");
goto cleanup;
}
break;

case GIT_OBJ_COMMIT:
commit = (git_object *)target;
break;

default:
return reset_error_invalid("Only git_tag and git_commit objects are valid targets.");
}

//TODO: Check for unmerged entries

if (git_reference__update(repo, git_object_id(commit), GIT_HEAD_FILE) < 0)
goto cleanup;

if (reset_type == GIT_RESET_SOFT) {
error = 0;
goto cleanup;
}

if (git_commit_tree(&tree, (git_commit *)commit) < 0) {
giterr_set(GITERR_OBJECT, "%s - Failed to retrieve the commit tree.", ERROR_MSG);
goto cleanup;
}

if (git_repository_index(&index, repo) < 0) {
giterr_set(GITERR_OBJECT, "%s - Failed to retrieve the index.", ERROR_MSG);
goto cleanup;
}

if (git_index_read_tree(index, tree) < 0) {
giterr_set(GITERR_INDEX, "%s - Failed to update the index.", ERROR_MSG);
goto cleanup;
}

if (git_index_write(index) < 0) {
giterr_set(GITERR_INDEX, "%s - Failed to write the index.", ERROR_MSG);
goto cleanup;
}

error = 0;

cleanup:
if (target_type == GIT_OBJ_TAG)
git_object_free(commit);

git_index_free(index);
git_tree_free(tree);

return error;
}
47 changes: 47 additions & 0 deletions tests-clar/reset/mixed.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "clar_libgit2.h"
#include "posix.h"
#include "reset_helpers.h"
#include "path.h"

static git_repository *repo;
static git_object *target;

void test_reset_mixed__initialize(void)
{
repo = cl_git_sandbox_init("attr");
target = NULL;
}

void test_reset_mixed__cleanup(void)
{
git_object_free(target);
cl_git_sandbox_cleanup();
}

void test_reset_mixed__cannot_reset_in_a_bare_repository(void)
{
git_repository *bare;

cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git")));
cl_assert(git_repository_is_bare(bare) == true);

retrieve_target_from_oid(&target, bare, KNOWN_COMMIT_IN_BARE_REPO);

cl_git_fail(git_reset(bare, target, GIT_RESET_MIXED));

git_repository_free(bare);
}

void test_reset_mixed__resetting_refreshes_the_index_to_the_commit_tree(void)
{
unsigned int status;

cl_git_pass(git_status_file(&status, repo, "macro_bad"));
cl_assert(status == GIT_STATUS_CURRENT);
retrieve_target_from_oid(&target, repo, "605812ab7fe421fdd325a935d35cb06a9234a7d7");

cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED));

cl_git_pass(git_status_file(&status, repo, "macro_bad"));
cl_assert(status == GIT_STATUS_WT_NEW);
}
10 changes: 10 additions & 0 deletions tests-clar/reset/reset_helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "clar_libgit2.h"
#include "reset_helpers.h"

void retrieve_target_from_oid(git_object **object_out, git_repository *repo, const char *sha)
{
git_oid oid;

cl_git_pass(git_oid_fromstr(&oid, sha));
cl_git_pass(git_object_lookup(object_out, repo, &oid, GIT_OBJ_ANY));
}
6 changes: 6 additions & 0 deletions tests-clar/reset/reset_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "common.h"

#define KNOWN_COMMIT_IN_BARE_REPO "e90810b8df3e80c413d903f631643c716887138d"
#define KNOWN_COMMIT_IN_ATTR_REPO "217878ab49e1314388ea2e32dc6fdb58a1b969e0"

extern void retrieve_target_from_oid(git_object **object_out, git_repository *repo, const char *sha);
Loading

0 comments on commit 6c08e69

Please sign in to comment.