summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 1e61b76)
raw | patch | inline | side by side (parent: 1e61b76)
author | Carlos Rica <jasampler@gmail.com> | |
Wed, 5 Sep 2007 01:38:24 +0000 (03:38 +0200) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Wed, 5 Sep 2007 18:29:33 +0000 (11:29 -0700) |
A function intended to be called from builtins updating refs
by locking them before write, specially those that came from
scripts using "git update-ref".
[jc: with minor fixups]
Signed-off-by: Carlos Rica <jasampler@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
by locking them before write, specially those that came from
scripts using "git update-ref".
[jc: with minor fixups]
Signed-off-by: Carlos Rica <jasampler@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin-fetch--tool.c | patch | blob | history | |
builtin-update-ref.c | patch | blob | history | |
refs.c | patch | blob | history | |
refs.h | patch | blob | history | |
send-pack.c | patch | blob | history |
diff --git a/builtin-fetch--tool.c b/builtin-fetch--tool.c
index e2f8ede9ae4507ed1e431f9d14fc649f6475627d..24c7e6f7dbcee4d02daad7182bb7587c4c9c3418 100644 (file)
--- a/builtin-fetch--tool.c
+++ b/builtin-fetch--tool.c
find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
}
-static int update_ref(const char *action,
+static int update_ref_env(const char *action,
const char *refname,
unsigned char *sha1,
unsigned char *oldval)
{
char msg[1024];
char *rla = getenv("GIT_REFLOG_ACTION");
- static struct ref_lock *lock;
if (!rla)
rla = "(reflog update)";
- snprintf(msg, sizeof(msg), "%s: %s", rla, action);
- lock = lock_any_ref_for_update(refname, oldval, 0);
- if (!lock)
- return 1;
- if (write_ref_sha1(lock, sha1, msg) < 0)
- return 1;
- return 0;
+ if (snprintf(msg, sizeof(msg), "%s: %s", rla, action) >= sizeof(msg))
+ warning("reflog message too long: %.*s...", 50, msg);
+ return update_ref(msg, refname, sha1, oldval, 0, QUIET_ON_ERR);
}
static int update_local_ref(const char *name,
fprintf(stderr, "* %s: storing %s\n",
name, note);
show_new(type, sha1_new);
- return update_ref(msg, name, sha1_new, NULL);
+ return update_ref_env(msg, name, sha1_new, NULL);
}
if (!hashcmp(sha1_old, sha1_new)) {
if (!strncmp(name, "refs/tags/", 10)) {
fprintf(stderr, "* %s: updating with %s\n", name, note);
show_new(type, sha1_new);
- return update_ref("updating tag", name, sha1_new, NULL);
+ return update_ref_env("updating tag", name, sha1_new, NULL);
}
current = lookup_commit_reference(sha1_old);
fprintf(stderr, "* %s: fast forward to %s\n",
name, note);
fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
- return update_ref("fast forward", name, sha1_new, sha1_old);
+ return update_ref_env("fast forward", name, sha1_new, sha1_old);
}
if (!force) {
fprintf(stderr,
"* %s: forcing update to non-fast forward %s\n",
name, note);
fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
- return update_ref("forced-update", name, sha1_new, sha1_old);
+ return update_ref_env("forced-update", name, sha1_new, sha1_old);
}
static int append_fetch_head(FILE *fp,
diff --git a/builtin-update-ref.c b/builtin-update-ref.c
index 8339cf19e2b68a19e88c1a014c52672bc6f0c7b5..fe1f74c9f3fa4a44cfd56508171e65a40e27342c 100644 (file)
--- a/builtin-update-ref.c
+++ b/builtin-update-ref.c
int cmd_update_ref(int argc, const char **argv, const char *prefix)
{
const char *refname=NULL, *value=NULL, *oldval=NULL, *msg=NULL;
- struct ref_lock *lock;
unsigned char sha1[20], oldsha1[20];
int i, delete, ref_flags;
if (oldval && *oldval && get_sha1(oldval, oldsha1))
die("%s: not a valid old SHA1", oldval);
- lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL, ref_flags);
- if (!lock)
- die("%s: cannot lock the ref", refname);
- if (write_ref_sha1(lock, sha1, msg) < 0)
- die("%s: cannot update the ref", refname);
- return 0;
+ return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
+ ref_flags, DIE_ON_ERR);
}
index 09a2c87fc23e4298bb3bcddc5c2cc649c3206a04..7fb3350789a407e36dc74418b79508e9bf916594 100644 (file)
--- a/refs.c
+++ b/refs.c
{
return do_for_each_reflog("", fn, cb_data);
}
+
+int update_ref(const char *action, const char *refname,
+ const unsigned char *sha1, const unsigned char *oldval,
+ int flags, enum action_on_err onerr)
+{
+ static struct ref_lock *lock;
+ lock = lock_any_ref_for_update(refname, oldval, flags);
+ if (!lock) {
+ const char *str = "Cannot lock the ref '%s'.";
+ switch (onerr) {
+ case MSG_ON_ERR: error(str, refname); break;
+ case DIE_ON_ERR: die(str, refname); break;
+ case QUIET_ON_ERR: break;
+ }
+ return 1;
+ }
+ if (write_ref_sha1(lock, sha1, action) < 0) {
+ const char *str = "Cannot update the ref '%s'.";
+ switch (onerr) {
+ case MSG_ON_ERR: error(str, refname); break;
+ case DIE_ON_ERR: die(str, refname); break;
+ case QUIET_ON_ERR: break;
+ }
+ return 1;
+ }
+ return 0;
+}
index f234eb76ba5d6aba03f484fe58d11ba81a90ff5a..6eb98a4caf150776c26fb04699133b1a976d0122 100644 (file)
--- a/refs.h
+++ b/refs.h
/** resolve ref in nested "gitlink" repository */
extern int resolve_gitlink_ref(const char *name, const char *refname, unsigned char *result);
+/** lock a ref and then write its file */
+enum action_on_err { MSG_ON_ERR, DIE_ON_ERR, QUIET_ON_ERR };
+int update_ref(const char *action, const char *refname,
+ const unsigned char *sha1, const unsigned char *oldval,
+ int flags, enum action_on_err onerr);
+
#endif /* REFS_H */
diff --git a/send-pack.c b/send-pack.c
index 9fc8a812f4c526d61e3acb8183b8b8da0e36895c..f74e66a8babd427ecd715c901d3d1680f1c5f13a 100644 (file)
--- a/send-pack.c
+++ b/send-pack.c
@@ -307,20 +307,14 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
rs.src = ref->name;
rs.dst = NULL;
if (!remote_find_tracking(remote, &rs)) {
- struct ref_lock *lock;
fprintf(stderr, " Also local %s\n", rs.dst);
if (will_delete_ref) {
if (delete_ref(rs.dst, NULL)) {
error("Failed to delete");
}
- } else {
- lock = lock_any_ref_for_update(rs.dst, NULL, 0);
- if (!lock)
- error("Failed to lock");
- else
- write_ref_sha1(lock, ref->new_sha1,
- "update by push");
- }
+ } else
+ update_ref("update by push", rs.dst,
+ ref->new_sha1, NULL, 0, 0);
free(rs.dst);
}
}