summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVicent Martí <tanoku@gmail.com>2011-08-01 08:19:12 -0700
committerVicent Martí <tanoku@gmail.com>2011-08-01 08:19:12 -0700
commit80de9ae03c82b6c7f3d313edc1db72b81ad01916 (patch)
tree999da7635c2a3505b42f4d4869aa7089f2e9f740 /src
parent77bb37eb47647180c0ed30517f6c6e4c219d131a (diff)
parenteed2714ba58d42c5bcfee6f6bf08bd670ffe7e85 (diff)
downloadlibgit2-80de9ae03c82b6c7f3d313edc1db72b81ad01916.tar.gz
Merge pull request #342 from schu/reflog-check-hash
reflog: avoid users writing a wrong ancestor OID
Diffstat (limited to 'src')
-rw-r--r--src/reflog.c31
1 files changed, 16 insertions, 15 deletions
diff --git a/src/reflog.c b/src/reflog.c
index c90abb20f..7609d9a4d 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -53,25 +53,15 @@ static int reflog_init(git_reflog **reflog, git_reference *ref)
return GIT_SUCCESS;
}
-static int reflog_write(git_repository *repo, const char *ref_name,
- const char *oid_old, const char *oid_new,
- const git_signature *committer, const char *msg)
+static int reflog_write(const char *log_path, const char *oid_old,
+ const char *oid_new, const git_signature *committer,
+ const char *msg)
{
int error;
- char log_path[GIT_PATH_MAX];
git_buf log = GIT_BUF_INIT;
git_filebuf fbuf;
- assert(repo && ref_name && oid_old && oid_new && committer);
-
- git_path_join_n(log_path, 3, repo->path_repository, GIT_REFLOG_DIR, ref_name);
-
- if (git_futils_exists(log_path)) {
- if ((error = git_futils_mkpath2file(log_path)) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to write reflog. Cannot create reflog directory");
-
- } else if (git_futils_isfile(log_path))
- return git__throw(GIT_ERROR, "Failed to write reflog. `%s` is directory", log_path);
+ assert(log_path && oid_old && oid_new && committer);
git_buf_puts(&log, oid_old);
git_buf_putc(&log, ' ');
@@ -222,6 +212,7 @@ int git_reflog_write(git_reference *ref, const git_oid *oid_old,
int error;
char old[GIT_OID_HEXSZ+1];
char new[GIT_OID_HEXSZ+1];
+ char log_path[GIT_PATH_MAX];
git_reference *r;
const git_oid *oid;
@@ -234,12 +225,22 @@ int git_reflog_write(git_reference *ref, const git_oid *oid_old,
git_oid_to_string(new, GIT_OID_HEXSZ+1, oid);
+ git_path_join_n(log_path, 3, ref->owner->path_repository, GIT_REFLOG_DIR, ref->name);
+
+ if (git_futils_exists(log_path)) {
+ if ((error = git_futils_mkpath2file(log_path)) < GIT_SUCCESS)
+ return git__rethrow(error, "Failed to write reflog. Cannot create reflog directory");
+ } else if (git_futils_isfile(log_path)) {
+ return git__throw(GIT_ERROR, "Failed to write reflog. `%s` is directory", log_path);
+ } else if (oid_old == NULL)
+ return git__throw(GIT_ERROR, "Failed to write reflog. Old OID cannot be NULL for existing reference");
+
if (oid_old)
git_oid_to_string(old, GIT_OID_HEXSZ+1, oid_old);
else
snprintf(old, GIT_OID_HEXSZ+1, "%0*d", GIT_OID_HEXSZ, 0);
- return reflog_write(ref->owner, ref->name, old, new, committer, msg);
+ return reflog_write(log_path, old, new, committer, msg);
}
unsigned int git_reflog_entrycount(git_reflog *reflog)