From: David Turner <dturner@twopensource.com>
To: git@vger.kernel.org, mhagger@alum.mit.edu
Cc: Ronnie Sahlberg <sahlberg@google.com>,
David Turner <dturner@twopensource.com>,
Junio C Hamano <gitster@pobox.com>
Subject: [PATCH v5 05/26] refs.c: move update_ref to refs.c
Date: Tue, 27 Oct 2015 22:14:06 -0400 [thread overview]
Message-ID: <1445998467-11511-6-git-send-email-dturner@twopensource.com> (raw)
In-Reply-To: <1445998467-11511-1-git-send-email-dturner@twopensource.com>
From: Ronnie Sahlberg <sahlberg@google.com>
Move update_ref() to the refs.c file since this function does not
contain any backend specific code. Move the ref classifier functions
and write_pseudoref as well, since update_ref depends on them.
Based on Ronnie Sahlberg's patch
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
Signed-off-by: David Turner <dturner@twopensource.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
refs-be-files.c | 117 +-------------------------------------------------------
refs.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 117 insertions(+), 116 deletions(-)
diff --git a/refs-be-files.c b/refs-be-files.c
index fe71ea0..f03d68c 100644
--- a/refs-be-files.c
+++ b/refs-be-files.c
@@ -2675,8 +2675,6 @@ struct pack_refs_cb_data {
struct ref_to_prune *ref_to_prune;
};
-static int is_per_worktree_ref(const char *refname);
-
/*
* An each_ref_entry_fn that is run over loose references only. If
* the loose reference can be packed, add an entry in the packed ref
@@ -2691,7 +2689,7 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
int is_tag_ref = starts_with(entry->name, "refs/tags/");
/* Do not pack per-worktree refs: */
- if (is_per_worktree_ref(entry->name))
+ if (ref_type(entry->name) == REF_TYPE_PER_WORKTREE)
return 0;
/* ALWAYS pack tags */
@@ -2886,77 +2884,6 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
return 0;
}
-static int is_per_worktree_ref(const char *refname)
-{
- return !strcmp(refname, "HEAD") ||
- starts_with(refname, "refs/bisect/");
-}
-
-static int is_pseudoref_syntax(const char *refname)
-{
- const char *c;
-
- for (c = refname; *c; c++) {
- if (!isupper(*c) && *c != '-' && *c != '_')
- return 0;
- }
-
- return 1;
-}
-
-enum ref_type ref_type(const char *refname)
-{
- if (is_per_worktree_ref(refname))
- return REF_TYPE_PER_WORKTREE;
- if (is_pseudoref_syntax(refname))
- return REF_TYPE_PSEUDOREF;
- return REF_TYPE_NORMAL;
-}
-
-static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
- const unsigned char *old_sha1, struct strbuf *err)
-{
- const char *filename;
- int fd;
- static struct lock_file lock;
- struct strbuf buf = STRBUF_INIT;
- int ret = -1;
-
- strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1));
-
- filename = git_path("%s", pseudoref);
- fd = hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
- if (fd < 0) {
- strbuf_addf(err, "Could not open '%s' for writing: %s",
- filename, strerror(errno));
- return -1;
- }
-
- if (old_sha1) {
- unsigned char actual_old_sha1[20];
-
- if (read_ref(pseudoref, actual_old_sha1))
- die("could not read ref '%s'", pseudoref);
- if (hashcmp(actual_old_sha1, old_sha1)) {
- strbuf_addf(err, "Unexpected sha1 when writing %s", pseudoref);
- rollback_lock_file(&lock);
- goto done;
- }
- }
-
- if (write_in_full(fd, buf.buf, buf.len) != buf.len) {
- strbuf_addf(err, "Could not write to '%s'", filename);
- rollback_lock_file(&lock);
- goto done;
- }
-
- commit_lock_file(&lock);
- ret = 0;
-done:
- strbuf_release(&buf);
- return ret;
-}
-
static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1)
{
static struct lock_file lock;
@@ -4105,48 +4032,6 @@ int ref_transaction_verify(struct ref_transaction *transaction,
flags, NULL, err);
}
-int update_ref(const char *msg, const char *refname,
- const unsigned char *new_sha1, const unsigned char *old_sha1,
- unsigned int flags, enum action_on_err onerr)
-{
- struct ref_transaction *t = NULL;
- struct strbuf err = STRBUF_INIT;
- int ret = 0;
-
- if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
- ret = write_pseudoref(refname, new_sha1, old_sha1, &err);
- } else {
- t = ref_transaction_begin(&err);
- if (!t ||
- ref_transaction_update(t, refname, new_sha1, old_sha1,
- flags, msg, &err) ||
- ref_transaction_commit(t, &err)) {
- ret = 1;
- ref_transaction_free(t);
- }
- }
- if (ret) {
- const char *str = "update_ref failed for ref '%s': %s";
-
- switch (onerr) {
- case UPDATE_REFS_MSG_ON_ERR:
- error(str, refname, err.buf);
- break;
- case UPDATE_REFS_DIE_ON_ERR:
- die(str, refname, err.buf);
- break;
- case UPDATE_REFS_QUIET_ON_ERR:
- break;
- }
- strbuf_release(&err);
- return 1;
- }
- strbuf_release(&err);
- if (t)
- ref_transaction_free(t);
- return 0;
-}
-
static int ref_update_reject_duplicates(struct string_list *refnames,
struct strbuf *err)
{
diff --git a/refs.c b/refs.c
index 77492ff..610fab0 100644
--- a/refs.c
+++ b/refs.c
@@ -1,3 +1,119 @@
/*
* Common refs code for all backends.
*/
+#include "cache.h"
+#include "refs.h"
+#include "lockfile.h"
+
+static int is_per_worktree_ref(const char *refname)
+{
+ return !strcmp(refname, "HEAD") ||
+ starts_with(refname, "refs/bisect/");
+}
+
+static int is_pseudoref_syntax(const char *refname)
+{
+ const char *c;
+
+ for (c = refname; *c; c++) {
+ if (!isupper(*c) && *c != '-' && *c != '_')
+ return 0;
+ }
+
+ return 1;
+}
+
+enum ref_type ref_type(const char *refname)
+{
+ if (is_per_worktree_ref(refname))
+ return REF_TYPE_PER_WORKTREE;
+ if (is_pseudoref_syntax(refname))
+ return REF_TYPE_PSEUDOREF;
+ return REF_TYPE_NORMAL;
+}
+
+static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
+ const unsigned char *old_sha1, struct strbuf *err)
+{
+ const char *filename;
+ int fd;
+ static struct lock_file lock;
+ struct strbuf buf = STRBUF_INIT;
+ int ret = -1;
+
+ strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1));
+
+ filename = git_path("%s", pseudoref);
+ fd = hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
+ if (fd < 0) {
+ strbuf_addf(err, "Could not open '%s' for writing: %s",
+ filename, strerror(errno));
+ return -1;
+ }
+
+ if (old_sha1) {
+ unsigned char actual_old_sha1[20];
+
+ if (read_ref(pseudoref, actual_old_sha1))
+ die("could not read ref '%s'", pseudoref);
+ if (hashcmp(actual_old_sha1, old_sha1)) {
+ strbuf_addf(err, "Unexpected sha1 when writing %s", pseudoref);
+ rollback_lock_file(&lock);
+ goto done;
+ }
+ }
+
+ if (write_in_full(fd, buf.buf, buf.len) != buf.len) {
+ strbuf_addf(err, "Could not write to '%s'", filename);
+ rollback_lock_file(&lock);
+ goto done;
+ }
+
+ commit_lock_file(&lock);
+ ret = 0;
+done:
+ strbuf_release(&buf);
+ return ret;
+}
+
+int update_ref(const char *msg, const char *refname,
+ const unsigned char *new_sha1, const unsigned char *old_sha1,
+ unsigned int flags, enum action_on_err onerr)
+{
+ struct ref_transaction *t = NULL;
+ struct strbuf err = STRBUF_INIT;
+ int ret = 0;
+
+ if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
+ ret = write_pseudoref(refname, new_sha1, old_sha1, &err);
+ } else {
+ t = ref_transaction_begin(&err);
+ if (!t ||
+ ref_transaction_update(t, refname, new_sha1, old_sha1,
+ flags, msg, &err) ||
+ ref_transaction_commit(t, &err)) {
+ ret = 1;
+ ref_transaction_free(t);
+ }
+ }
+ if (ret) {
+ const char *str = "update_ref failed for ref '%s': %s";
+
+ switch (onerr) {
+ case UPDATE_REFS_MSG_ON_ERR:
+ error(str, refname, err.buf);
+ break;
+ case UPDATE_REFS_DIE_ON_ERR:
+ die(str, refname, err.buf);
+ break;
+ case UPDATE_REFS_QUIET_ON_ERR:
+ break;
+ }
+ strbuf_release(&err);
+ return 1;
+ }
+ strbuf_release(&err);
+ if (t)
+ ref_transaction_free(t);
+ return 0;
+}
--
2.4.2.658.g6d8523e-twtrsrc
next prev parent reply other threads:[~2015-10-28 2:17 UTC|newest]
Thread overview: 67+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-28 2:14 [PATCH v5 00/26] refs backend pre-vtable David Turner
2015-10-28 2:14 ` [PATCH v5 01/26] refs.c: create a public version of verify_refname_available David Turner
2015-10-28 2:14 ` [PATCH v5 02/26] refs: make is_branch public David Turner
2015-10-28 2:14 ` [PATCH v5 03/26] refs-be-files.c: rename refs to refs-be-files David Turner
2015-10-28 2:14 ` [PATCH v5 04/26] refs.c: add a new refs.c file to hold all common refs code David Turner
2015-10-28 2:14 ` David Turner [this message]
2015-10-28 2:14 ` [PATCH v5 06/26] refs.c: move delete_pseudoref and delete_ref to the common code David Turner
2015-10-28 2:14 ` [PATCH v5 07/26] refs.c: move read_ref_at to the common refs file David Turner
2015-10-28 2:14 ` [PATCH v5 08/26] refs.c: move the hidden refs functions to the common code David Turner
2015-10-28 2:14 ` [PATCH v5 09/26] refs.c: move dwim and friend functions to the common refs code David Turner
2015-10-28 2:14 ` [PATCH v5 10/26] refs.c: move warn_if_dangling_symref* to the common code David Turner
2015-10-28 2:14 ` [PATCH v5 11/26] refs.c: move read_ref, read_ref_full and ref_exists " David Turner
2015-10-28 2:14 ` [PATCH v5 12/26] refs.c: move resolve_refdup to common David Turner
2015-10-28 2:14 ` [PATCH v5 13/26] refs.c: move check_refname_format to the common code David Turner
2015-10-28 2:14 ` [PATCH v5 14/26] refs.c: move is_branch " David Turner
2015-10-28 2:14 ` [PATCH v5 15/26] refs.c: move prettify_refname " David Turner
2015-10-28 2:14 ` [PATCH v5 16/26] refs.c: move ref iterators " David Turner
2015-11-01 4:39 ` Michael Haggerty
2015-10-28 2:14 ` [PATCH v5 17/26] refs.c: move head_ref_namespaced " David Turner
2015-10-28 2:14 ` [PATCH v5 18/26] refs: move transaction functions into " David Turner
2015-11-01 8:17 ` Michael Haggerty
2015-11-02 22:19 ` David Turner
2015-10-28 2:14 ` [PATCH v5 19/26] refs.c: move refname_is_safe to the " David Turner
2015-10-28 2:14 ` [PATCH v5 20/26] refs.c: move copy_msg " David Turner
2015-10-28 2:14 ` [PATCH v5 21/26] refs.c: move peel_object " David Turner
2015-10-28 2:14 ` [PATCH v5 22/26] refs.c: move should_autocreate_reflog to " David Turner
2015-10-28 2:14 ` [PATCH v5 23/26] initdb: move safe_create_dir into " David Turner
2015-10-28 2:14 ` [PATCH v5 24/26] refs: make files_log_ref_write functions public David Turner
2015-10-28 2:14 ` [PATCH v5 25/26] refs: break out ref conflict checks David Turner
2015-11-02 16:52 ` Michael Haggerty
2015-10-28 2:14 ` [PATCH v5 26/26] introduce "extensions" form of core.repositoryformatversion David Turner
2015-11-03 7:36 ` [PATCH v5 00/26] refs backend pre-vtable Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 00/25] " Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 01/25] refs: make is_branch public Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 02/25] refs/files-backend.c: new file, renamed from refs.c Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 03/25] refs: add a new file, refs/refs.c, to hold common refs code Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 04/25] refs: move update_ref to refs/refs.c Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 05/25] refs: move delete_pseudoref and delete_ref to the common code Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 06/25] refs: move read_ref_at to the common refs file Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 07/25] refs: move the hidden refs functions to the common code Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 08/25] refs: move dwim and friend functions to the common refs code Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 09/25] refs: move warn_if_dangling_symref* to the common code Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 10/25] refs: move read_ref, read_ref_full and ref_exists " Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 11/25] refs: move resolve_refdup to common Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 12/25] refs: move check_refname_format to the common code Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 13/25] refs: move is_branch " Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 14/25] refs: move prettify_refname " Michael Haggerty
2015-11-03 7:39 ` [PATCH v6 15/25] refs: move ref iterators " Michael Haggerty
2015-11-03 7:40 ` [PATCH v6 16/25] refs: move head_ref_namespaced " Michael Haggerty
2015-11-03 7:40 ` [PATCH v6 17/25] refs: move transaction functions " Michael Haggerty
2015-11-03 7:40 ` [PATCH v6 18/25] refs: move refname_is_safe " Michael Haggerty
2015-11-03 7:40 ` [PATCH v6 19/25] refs: move copy_msg " Michael Haggerty
2015-11-03 7:40 ` [PATCH v6 20/25] refs: move peel_object " Michael Haggerty
2015-11-03 7:40 ` [PATCH v6 21/25] refs: move should_autocreate_reflog to " Michael Haggerty
2015-11-03 7:40 ` [PATCH v6 22/25] initdb: make safe_create_dir public Michael Haggerty
2015-11-03 7:40 ` [PATCH v6 23/25] files_log_ref_write: new function Michael Haggerty
2015-11-03 7:40 ` [PATCH v6 24/25] refs: create a shared version of verify_refname_available Michael Haggerty
2015-11-03 7:40 ` [PATCH v6 25/25] refs: break out ref conflict checks Michael Haggerty
2015-11-04 21:01 ` David Turner
2015-11-05 4:00 ` Michael Haggerty
2015-11-05 16:22 ` David Turner
2015-11-06 13:34 ` Michael Haggerty
2015-11-06 17:28 ` Junio C Hamano
2015-11-06 23:24 ` Junio C Hamano
2015-11-08 5:03 ` Michael Haggerty
2015-11-08 5:54 ` Michael Haggerty
2015-11-08 18:23 ` Junio C Hamano
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=1445998467-11511-6-git-send-email-dturner@twopensource.com \
--to=dturner@twopensource.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=mhagger@alum.mit.edu \
--cc=sahlberg@google.com \
/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).