summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2013-02-11 11:44:00 -0800
committerRussell Belfer <rb@github.com>2013-02-11 11:44:00 -0800
commit390a3c81410a219b13aa6f333949c803524c8bd2 (patch)
treeb6487640acf5adb87f5a0f8ee25dedd44df63ffb /src
parente026cfee003e103d79e56983d68a206ae907eada (diff)
parent3ad052218cd03fe58b254f878825e3f0fd4b3054 (diff)
downloadlibgit2-390a3c81410a219b13aa6f333949c803524c8bd2.tar.gz
Merge pull request #1190 from nulltoken/topic/reset-paths
reset: Allow the selective reset of pathspecs
Diffstat (limited to 'src')
-rw-r--r--src/branch.c2
-rw-r--r--src/diff_output.c4
-rw-r--r--src/diff_output.h2
-rw-r--r--src/index.c2
-rw-r--r--src/reset.c74
-rw-r--r--src/tree.c2
6 files changed, 80 insertions, 6 deletions
diff --git a/src/branch.c b/src/branch.c
index 936947a73..11ecbe9a1 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -429,7 +429,7 @@ int git_branch_tracking_name(
if (tracking_branch_name_out)
git_buf_copy_cstr(tracking_branch_name_out, buffer_size, &buf);
- error = buf.size + 1;
+ error = (int)buf.size + 1;
cleanup:
git_buf_free(&buf);
diff --git a/src/diff_output.c b/src/diff_output.c
index 4f1064b7f..c2c259161 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -1603,8 +1603,8 @@ int git_diff_patch_get_line_in_hunk(
if (line_origin) *line_origin = line->origin;
if (content) *content = line->ptr;
if (content_len) *content_len = line->len;
- if (old_lineno) *old_lineno = line->oldno;
- if (new_lineno) *new_lineno = line->newno;
+ if (old_lineno) *old_lineno = (int)line->oldno;
+ if (new_lineno) *new_lineno = (int)line->newno;
return 0;
diff --git a/src/diff_output.h b/src/diff_output.h
index ae1a491ec..083355676 100644
--- a/src/diff_output.h
+++ b/src/diff_output.h
@@ -42,7 +42,7 @@ typedef struct diff_patch_line diff_patch_line;
struct diff_patch_line {
const char *ptr;
size_t len;
- int lines, oldno, newno;
+ size_t lines, oldno, newno;
char origin;
};
diff --git a/src/index.c b/src/index.c
index f5bf954a8..25156d08f 100644
--- a/src/index.c
+++ b/src/index.c
@@ -929,7 +929,7 @@ int git_index_conflict_add(git_index *index,
goto on_error;
}
- return 0;
+ return 0;
on_error:
for (i = 0; i < 3; i++) {
diff --git a/src/reset.c b/src/reset.c
index b637e3730..700aac808 100644
--- a/src/reset.c
+++ b/src/reset.c
@@ -9,6 +9,7 @@
#include "commit.h"
#include "tag.h"
#include "merge.h"
+#include "diff.h"
#include "git2/reset.h"
#include "git2/checkout.h"
#include "git2/merge.h"
@@ -54,6 +55,79 @@ cleanup:
return error;
}
+int git_reset_default(
+ git_repository *repo,
+ git_object *target,
+ git_strarray* pathspecs)
+{
+ git_object *commit = NULL;
+ git_tree *tree = NULL;
+ git_diff_list *diff = NULL;
+ git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
+ size_t i;
+ git_diff_delta *delta;
+ git_index_entry entry;
+ int error;
+ git_index *index = NULL;
+
+ assert(pathspecs != NULL && pathspecs->count > 0);
+
+ memset(&entry, 0, sizeof(git_index_entry));
+
+ if ((error = git_repository_index(&index, repo)) < 0)
+ goto cleanup;
+
+ if (target) {
+ if (git_object_owner(target) != repo) {
+ giterr_set(GITERR_OBJECT,
+ "%s_default - The given target does not belong to this repository.", ERROR_MSG);
+ return -1;
+ }
+
+ if ((error = git_object_peel(&commit, target, GIT_OBJ_COMMIT)) < 0 ||
+ (error = git_commit_tree(&tree, (git_commit *)commit)) < 0)
+ goto cleanup;
+ }
+
+ opts.pathspec = *pathspecs;
+ opts.flags = GIT_DIFF_REVERSE;
+
+ if ((error = git_diff_tree_to_index(
+ &diff, repo, tree, index, &opts)) < 0)
+ goto cleanup;
+
+ git_vector_foreach(&diff->deltas, i, delta) {
+ if ((error = git_index_conflict_remove(index, delta->old_file.path)) < 0)
+ goto cleanup;
+
+ assert(delta->status == GIT_DELTA_ADDED ||
+ delta->status == GIT_DELTA_MODIFIED ||
+ delta->status == GIT_DELTA_DELETED);
+
+ if (delta->status == GIT_DELTA_DELETED) {
+ if ((error = git_index_remove(index, delta->old_file.path, 0)) < 0)
+ goto cleanup;
+ } else {
+ entry.mode = delta->new_file.mode;
+ git_oid_cpy(&entry.oid, &delta->new_file.oid);
+ entry.path = (char *)delta->new_file.path;
+
+ if ((error = git_index_add(index, &entry)) < 0)
+ goto cleanup;
+ }
+ }
+
+ error = git_index_write(index);
+
+cleanup:
+ git_object_free(commit);
+ git_tree_free(tree);
+ git_index_free(index);
+ git_diff_list_free(diff);
+
+ return error;
+}
+
int git_reset(
git_repository *repo,
git_object *target,
diff --git a/src/tree.c b/src/tree.c
index e05105a9d..59bd92ab1 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -355,7 +355,7 @@ size_t git_tree_entrycount(const git_tree *tree)
unsigned int git_treebuilder_entrycount(git_treebuilder *bld)
{
assert(bld);
- return bld->entries.length;
+ return (int)bld->entries.length;
}
static int tree_error(const char *str, const char *path)