From: Ronnie Sahlberg <sahlberg@google.com>
To: git@vger.kernel.org
Cc: Ronnie Sahlberg <sahlberg@google.com>
Subject: [PATCH 7/8] refs.c: add an err argument to create_reflog
Date: Tue, 21 Oct 2014 13:46:39 -0700 [thread overview]
Message-ID: <1413924400-15418-8-git-send-email-sahlberg@google.com> (raw)
In-Reply-To: <1413924400-15418-1-git-send-email-sahlberg@google.com>
Add err argument to create_reflog that can explain the reason for a
failure. This then eliminates the need to manage errno through this
function since we can just add strerror(errno) to the err string when
meaningful. No callers relied on errno from this function for anything
else than the error message.
log_ref_write is a private function that calls create_reflog. Update
this function to also take an err argument and pass it back to the caller.
This again eliminates the need to manage errno in this function.
Update the private function write_sha1_update_reflog to also take an
err argument.
Change-Id: I8f796a8c0c5f5d3f26e3e59fbc6421c894a4e814
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
builtin/checkout.c | 8 +++---
refs.c | 71 +++++++++++++++++++++++++++---------------------------
refs.h | 4 +--
3 files changed, 42 insertions(+), 41 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 60a68f7..d9cb9c3 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -590,10 +590,12 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
if (opts->new_orphan_branch) {
if (opts->new_branch_log && !log_all_ref_updates) {
char *ref_name = mkpath("refs/heads/%s", opts->new_orphan_branch);
+ struct strbuf err = STRBUF_INIT;
- if (create_reflog(ref_name)) {
- fprintf(stderr, _("Can not do reflog for '%s'\n"),
- opts->new_orphan_branch);
+ if (create_reflog(ref_name, &err)) {
+ fprintf(stderr, _("Can not do reflog for '%s'. %s\n"),
+ opts->new_orphan_branch, err.buf);
+ strbuf_release(&err);
return;
}
}
diff --git a/refs.c b/refs.c
index a5e1eff..4d30623 100644
--- a/refs.c
+++ b/refs.c
@@ -2889,8 +2889,7 @@ static int copy_msg(char *buf, const char *msg)
return cp - buf;
}
-/* This function must set a meaningful errno on failure */
-int create_reflog(const char *refname)
+int create_reflog(const char *refname, struct strbuf *err)
{
int logfd, oflags = O_APPEND | O_WRONLY;
char logfile[PATH_MAX];
@@ -2901,9 +2900,8 @@ int create_reflog(const char *refname)
starts_with(refname, "refs/notes/") ||
!strcmp(refname, "HEAD")) {
if (safe_create_leading_directories(logfile) < 0) {
- int save_errno = errno;
- error("unable to create directory for %s", logfile);
- errno = save_errno;
+ strbuf_addf(err, "unable to create directory for %s. "
+ "%s", logfile, strerror(errno));
return -1;
}
oflags |= O_CREAT;
@@ -2916,20 +2914,16 @@ int create_reflog(const char *refname)
if ((oflags & O_CREAT) && errno == EISDIR) {
if (remove_empty_directories(logfile)) {
- int save_errno = errno;
- error("There are still logs under '%s'",
- logfile);
- errno = save_errno;
+ strbuf_addf(err, "There are still logs under "
+ "'%s'", logfile);
return -1;
}
logfd = open(logfile, oflags, 0666);
}
if (logfd < 0) {
- int save_errno = errno;
- error("Unable to append to %s: %s", logfile,
- strerror(errno));
- errno = save_errno;
+ strbuf_addf(err, "Unable to append to %s: %s",
+ logfile, strerror(errno));
return -1;
}
}
@@ -2966,7 +2960,8 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
}
static int log_ref_write(const char *refname, const unsigned char *old_sha1,
- const unsigned char *new_sha1, const char *msg)
+ const unsigned char *new_sha1, const char *msg,
+ struct strbuf *err)
{
int logfd, result = 0, oflags = O_APPEND | O_WRONLY;
char log_file[PATH_MAX];
@@ -2975,7 +2970,7 @@ static int log_ref_write(const char *refname, const unsigned char *old_sha1,
log_all_ref_updates = !is_bare_repository();
if (log_all_ref_updates && !reflog_exists(refname))
- result = create_reflog(refname);
+ result = create_reflog(refname, err);
if (result)
return result;
@@ -2988,16 +2983,14 @@ static int log_ref_write(const char *refname, const unsigned char *old_sha1,
result = log_ref_write_fd(logfd, old_sha1, new_sha1,
git_committer_info(0), msg);
if (result) {
- int save_errno = errno;
close(logfd);
- error("Unable to append to %s", log_file);
- errno = save_errno;
+ strbuf_addf(err, "Unable to append to %s. %s", log_file,
+ strerror(errno));
return -1;
}
if (close(logfd)) {
- int save_errno = errno;
- error("Unable to append to %s", log_file);
- errno = save_errno;
+ strbuf_addf(err, "Unable to append to %s. %s", log_file,
+ strerror(errno));
return -1;
}
return 0;
@@ -3009,12 +3002,12 @@ int is_branch(const char *refname)
}
static int write_sha1_update_reflog(struct ref_lock *lock,
- const unsigned char *sha1, const char *logmsg)
+ const unsigned char *sha1,
+ const char *logmsg, struct strbuf *err)
{
- if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 ||
+ if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg, err) < 0 ||
(strcmp(lock->ref_name, lock->orig_ref_name) &&
- log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg) < 0)) {
- unlock_ref(lock);
+ log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg, err) < 0)) {
return -1;
}
if (strcmp(lock->orig_ref_name, "HEAD") != 0) {
@@ -3036,8 +3029,11 @@ static int write_sha1_update_reflog(struct ref_lock *lock,
head_ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
head_sha1, &head_flag);
if (head_ref && (head_flag & REF_ISSYMREF) &&
- !strcmp(head_ref, lock->ref_name))
- log_ref_write("HEAD", lock->old_sha1, sha1, logmsg);
+ !strcmp(head_ref, lock->ref_name) &&
+ log_ref_write("HEAD", lock->old_sha1, sha1, logmsg, err)) {
+ error("%s", err->buf);
+ strbuf_release(err);
+ }
}
return 0;
}
@@ -3051,6 +3047,7 @@ static int write_ref_sha1(struct ref_lock *lock,
{
static char term = '\n';
struct object *o;
+ struct strbuf err = STRBUF_INIT;
if (!lock) {
errno = EINVAL;
@@ -3085,8 +3082,10 @@ static int write_ref_sha1(struct ref_lock *lock,
return -1;
}
clear_loose_ref_cache(&ref_cache);
- if (write_sha1_update_reflog(lock, sha1, logmsg)) {
+ if (write_sha1_update_reflog(lock, sha1, logmsg, &err)) {
unlock_ref(lock);
+ error("%s", err.buf);
+ strbuf_release(&err);
return -1;
}
if (commit_ref(lock)) {
@@ -3106,6 +3105,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
int fd, len, written;
char *git_HEAD = git_pathdup("%s", ref_target);
unsigned char old_sha1[20], new_sha1[20];
+ struct strbuf err = STRBUF_INIT;
if (logmsg && read_ref(ref_target, old_sha1))
hashclr(old_sha1);
@@ -3154,9 +3154,11 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
#ifndef NO_SYMLINK_HEAD
done:
#endif
- if (logmsg && !read_ref(refs_heads_master, new_sha1))
- log_ref_write(ref_target, old_sha1, new_sha1, logmsg);
-
+ if (logmsg && !read_ref(refs_heads_master, new_sha1) &&
+ log_ref_write(ref_target, old_sha1, new_sha1, logmsg, &err))
+ error("%s", err.buf);
+
+ strbuf_release(&err);
free(git_HEAD);
return 0;
}
@@ -3904,10 +3906,7 @@ int transaction_commit(struct transaction *transaction,
goto cleanup;
}
if (write_sha1_update_reflog(update->lock, update->new_sha1,
- update->msg)) {
- if (err)
- strbuf_addf(err, "Failed to update log '%s'.",
- update->refname);
+ update->msg, err)) {
ret = -1;
goto cleanup;
}
@@ -3944,7 +3943,7 @@ int transaction_commit(struct transaction *transaction,
continue;
}
if (log_all_ref_updates && !reflog_exists(update->refname) &&
- create_reflog(update->refname)) {
+ create_reflog(update->refname, err)) {
ret = -1;
if (err)
strbuf_addf(err, "Failed to setup reflog for "
diff --git a/refs.h b/refs.h
index be16c08..1851fff 100644
--- a/refs.h
+++ b/refs.h
@@ -170,8 +170,8 @@ extern int read_ref_at(const char *refname, unsigned int flags,
/** Check if a particular reflog exists */
extern int reflog_exists(const char *refname);
-/** Create reflog. Set errno to something meaningful on failure. */
-extern int create_reflog(const char *refname);
+/** Create reflog. Fill in err on failure. */
+extern int create_reflog(const char *refname, struct strbuf *err);
/** Delete a reflog */
extern int delete_reflog(const char *refname);
--
2.1.0.rc2.206.gedb03e5
next prev parent reply other threads:[~2014-10-21 20:47 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-21 20:46 [PATCH 0/8] ref-transaction-send-pack Ronnie Sahlberg
2014-10-21 20:46 ` [PATCH 1/8] receive-pack.c: add protocol support to negotiate atomic-push Ronnie Sahlberg
2014-10-30 19:59 ` Junio C Hamano
2014-10-30 20:46 ` Ronnie Sahlberg
2014-10-21 20:46 ` [PATCH 2/8] send-pack.c: add an --atomic-push command line argument Ronnie Sahlberg
2014-10-30 20:04 ` Junio C Hamano
2014-10-30 21:21 ` Ronnie Sahlberg
2014-10-21 20:46 ` [PATCH 3/8] receive-pack.c: use a single transaction when atomic-push is negotiated Ronnie Sahlberg
2014-10-30 20:05 ` Junio C Hamano
2014-10-21 20:46 ` [PATCH 4/8] push.c: add an --atomic-push argument Ronnie Sahlberg
2014-10-21 20:46 ` [PATCH 5/8] t5543-atomic-push.sh: add basic tests for atomic pushes Ronnie Sahlberg
2014-10-21 20:46 ` [PATCH 6/8] receive-pack.c: add a receive.preferatomicpush configuration variable Ronnie Sahlberg
2014-10-30 20:11 ` Junio C Hamano
2014-10-30 21:36 ` Ronnie Sahlberg
2014-10-30 22:03 ` Junio C Hamano
2014-10-30 22:26 ` Ronnie Sahlberg
2014-10-21 20:46 ` Ronnie Sahlberg [this message]
2014-10-21 20:46 ` [PATCH 8/8] refs.c: add an err argument to create_symref Ronnie Sahlberg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1413924400-15418-8-git-send-email-sahlberg@google.com \
--to=sahlberg@google.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).