From 2036cb98d00b888262c3dbfad7e14ffd95165028 Mon Sep 17 00:00:00 2001 From: David Turner Date: Fri, 31 Jul 2015 02:06:17 -0400 Subject: [PATCH 1/6] refs: introduce pseudoref and per-worktree ref concepts Add glossary entries for both concepts. Pseudorefs and per-worktree refs do not yet have special handling, because the files refs backend already handles them correctly. Later, we will make the LMDB backend call out to the files backend to handle per-worktree refs. Signed-off-by: David Turner Signed-off-by: Junio C Hamano --- Documentation/glossary-content.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Documentation/glossary-content.txt b/Documentation/glossary-content.txt index ab18f4baca..8c6478b2f2 100644 --- a/Documentation/glossary-content.txt +++ b/Documentation/glossary-content.txt @@ -411,6 +411,27 @@ exclude;; core Git. Porcelains expose more of a <> interface than the <>. +[[def_per_worktree_ref]]per-worktree ref:: + Refs that are per-<>, rather than + global. This is presently only <>, but might + later include other unusual refs. + +[[def_pseudoref]]pseudoref:: + Pseudorefs are a class of files under `$GIT_DIR` which behave + like refs for the purposes of rev-parse, but which are treated + specially by git. Pseudorefs both have names that are all-caps, + and always start with a line consisting of a + <> followed by whitespace. So, HEAD is not a + pseudoref, because it is sometimes a symbolic ref. They might + optionally contain some additional data. `MERGE_HEAD` and + `CHERRY_PICK_HEAD` are examples. Unlike + <>, these files cannot + be symbolic refs, and never have reflogs. They also cannot be + updated through the normal ref update machinery. Instead, + they are updated by directly writing to the files. However, + they can be read as if they were refs, so `git rev-parse + MERGE_HEAD` will work. + [[def_pull]]pull:: Pulling a <> means to <> it and <> it. See also linkgit:git-pull[1]. From 266b18273a742a33970e634d1858f292befdf943 Mon Sep 17 00:00:00 2001 From: David Turner Date: Fri, 31 Jul 2015 02:06:18 -0400 Subject: [PATCH 2/6] refs: add ref_type function Add a function ref_type, which categorizes refs as per-worktree, pseudoref, or normal ref. Later, we will use this in refs.c to treat pseudorefs specially. Alternate ref backends may use it to treat both pseudorefs and per-worktree refs differently. Signed-off-by: David Turner Signed-off-by: Junio C Hamano --- refs.c | 26 ++++++++++++++++++++++++++ refs.h | 8 ++++++++ 2 files changed, 34 insertions(+) diff --git a/refs.c b/refs.c index 97a75f3c31..58be2595e5 100644 --- a/refs.c +++ b/refs.c @@ -2821,6 +2821,32 @@ 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"); +} + +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; +} + int delete_ref(const char *refname, const unsigned char *sha1, unsigned int flags) { struct ref_transaction *transaction; diff --git a/refs.h b/refs.h index 4f1b236568..1927bda993 100644 --- a/refs.h +++ b/refs.h @@ -380,6 +380,14 @@ int update_ref(const char *msg, const char *refname, extern int parse_hide_refs_config(const char *var, const char *value, const char *); extern int ref_is_hidden(const char *); +enum ref_type { + REF_TYPE_PER_WORKTREE, + REF_TYPE_PSEUDOREF, + REF_TYPE_NORMAL, +}; + +enum ref_type ref_type(const char *refname); + enum expire_reflog_flags { EXPIRE_REFLOGS_DRY_RUN = 1 << 0, EXPIRE_REFLOGS_UPDATE_REF = 1 << 1, From 74ec19d4be76c52b872e6f95b39ecbfbe5d9100c Mon Sep 17 00:00:00 2001 From: David Turner Date: Fri, 31 Jul 2015 02:06:19 -0400 Subject: [PATCH 3/6] pseudorefs: create and use pseudoref update and delete functions Pseudorefs should not be updated through the ref transaction API, because alternate ref backends still need to store pseudorefs in GIT_DIR (instead of wherever they store refs). Instead, change update_ref and delete_ref to call pseudoref-specific functions. Signed-off-by: David Turner Signed-off-by: Junio C Hamano --- refs.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 93 insertions(+), 8 deletions(-) diff --git a/refs.c b/refs.c index 58be2595e5..522b19b539 100644 --- a/refs.c +++ b/refs.c @@ -2847,11 +2847,87 @@ enum ref_type ref_type(const char *refname) 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]; + read_ref(pseudoref, actual_old_sha1); + 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; + const char *filename; + + filename = git_path("%s", pseudoref); + + if (old_sha1 && !is_null_sha1(old_sha1)) { + int fd; + unsigned char actual_old_sha1[20]; + + fd = hold_lock_file_for_update(&lock, filename, + LOCK_DIE_ON_ERROR); + if (fd < 0) + die_errno(_("Could not open '%s' for writing"), filename); + read_ref(pseudoref, actual_old_sha1); + if (hashcmp(actual_old_sha1, old_sha1)) { + warning("Unexpected sha1 when deleting %s", pseudoref); + rollback_lock_file(&lock); + return -1; + } + + unlink(filename); + rollback_lock_file(&lock); + } else { + unlink(filename); + } + + return 0; +} + int delete_ref(const char *refname, const unsigned char *sha1, unsigned int flags) { struct ref_transaction *transaction; struct strbuf err = STRBUF_INIT; + if (ref_type(refname) == REF_TYPE_PSEUDOREF) + return delete_pseudoref(refname, sha1); + transaction = ref_transaction_begin(&err); if (!transaction || ref_transaction_delete(transaction, refname, @@ -3908,17 +3984,25 @@ 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; + struct ref_transaction *t = NULL; struct strbuf err = STRBUF_INIT; + int ret = 0; - t = ref_transaction_begin(&err); - if (!t || - ref_transaction_update(t, refname, new_sha1, old_sha1, - flags, msg, &err) || - ref_transaction_commit(t, &err)) { + 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"; - ref_transaction_free(t); switch (onerr) { case UPDATE_REFS_MSG_ON_ERR: error(str, refname, err.buf); @@ -3933,7 +4017,8 @@ int update_ref(const char *msg, const char *refname, return 1; } strbuf_release(&err); - ref_transaction_free(t); + if (t) + ref_transaction_free(t); return 0; } From f3a977187edd82d6b20fd018e55e471f7cf13d6a Mon Sep 17 00:00:00 2001 From: David Turner Date: Fri, 31 Jul 2015 02:06:20 -0400 Subject: [PATCH 4/6] bisect: use update_ref Instead of manually writing a pseudoref (in one case) and shelling out to git update-ref (in another), use the update_ref function. This is much simpler. Signed-off-by: David Turner Signed-off-by: Junio C Hamano --- bisect.c | 37 ++++++++----------------------------- 1 file changed, 8 insertions(+), 29 deletions(-) diff --git a/bisect.c b/bisect.c index 03d5cd9454..7eaa37a929 100644 --- a/bisect.c +++ b/bisect.c @@ -19,7 +19,6 @@ static struct object_id *current_bad_oid; static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL}; static const char *argv_show_branch[] = {"show-branch", NULL, NULL}; -static const char *argv_update_ref[] = {"update-ref", "--no-deref", "BISECT_HEAD", NULL, NULL}; /* Remember to update object flag allocation in object.h */ #define COUNTED (1u<<16) @@ -666,34 +665,16 @@ static int is_expected_rev(const struct object_id *oid) return res; } -static void mark_expected_rev(char *bisect_rev_hex) +static int bisect_checkout(const unsigned char *bisect_rev, int no_checkout) { - int len = strlen(bisect_rev_hex); - const char *filename = git_path("BISECT_EXPECTED_REV"); - int fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600); + char bisect_rev_hex[GIT_SHA1_HEXSZ + 1]; - if (fd < 0) - die_errno("could not create file '%s'", filename); - - bisect_rev_hex[len] = '\n'; - write_or_die(fd, bisect_rev_hex, len + 1); - bisect_rev_hex[len] = '\0'; - - if (close(fd) < 0) - die("closing file %s: %s", filename, strerror(errno)); -} - -static int bisect_checkout(char *bisect_rev_hex, int no_checkout) -{ - - mark_expected_rev(bisect_rev_hex); + memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), GIT_SHA1_HEXSZ + 1); + update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR); argv_checkout[2] = bisect_rev_hex; if (no_checkout) { - argv_update_ref[3] = bisect_rev_hex; - if (run_command_v_opt(argv_update_ref, RUN_GIT_CMD)) - die("update-ref --no-deref HEAD failed on %s", - bisect_rev_hex); + update_ref(NULL, "BISECT_HEAD", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR); } else { int res; res = run_command_v_opt(argv_checkout, RUN_GIT_CMD); @@ -789,7 +770,7 @@ static void check_merge_bases(int no_checkout) handle_skipped_merge_base(mb); } else { printf("Bisecting: a merge base must be tested\n"); - exit(bisect_checkout(sha1_to_hex(mb), no_checkout)); + exit(bisect_checkout(mb, no_checkout)); } } @@ -903,7 +884,6 @@ int bisect_next_all(const char *prefix, int no_checkout) struct commit_list *tried; int reaches = 0, all = 0, nr, steps; const unsigned char *bisect_rev; - char bisect_rev_hex[GIT_SHA1_HEXSZ + 1]; if (read_bisect_refs()) die("reading bisect refs failed"); @@ -938,11 +918,10 @@ int bisect_next_all(const char *prefix, int no_checkout) } bisect_rev = revs.commits->item->object.sha1; - memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), GIT_SHA1_HEXSZ + 1); if (!hashcmp(bisect_rev, current_bad_oid->hash)) { exit_if_skipped_commits(tried, current_bad_oid); - printf("%s is the first bad commit\n", bisect_rev_hex); + printf("%s is the first bad commit\n", sha1_to_hex(bisect_rev)); show_diff_tree(prefix, revs.commits->item); /* This means the bisection process succeeded. */ exit(10); @@ -954,7 +933,7 @@ int bisect_next_all(const char *prefix, int no_checkout) "(roughly %d step%s)\n", nr, (nr == 1 ? "" : "s"), steps, (steps == 1 ? "" : "s")); - return bisect_checkout(bisect_rev_hex, no_checkout); + return bisect_checkout(bisect_rev, no_checkout); } static inline int log2i(int n) From d96a53996b6c02b96a9a2b4eed9eac4e9d661a38 Mon Sep 17 00:00:00 2001 From: David Turner Date: Fri, 31 Jul 2015 02:06:21 -0400 Subject: [PATCH 5/6] sequencer: replace write_cherry_pick_head with update_ref Now update_ref (via write_pseudoref) does almost exactly what write_cherry_pick_head did, so we can remove write_cherry_pick_head and just use update_ref. Signed-off-by: David Turner Signed-off-by: Junio C Hamano --- sequencer.c | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/sequencer.c b/sequencer.c index c4f4b7d571..554a704e23 100644 --- a/sequencer.c +++ b/sequencer.c @@ -158,23 +158,6 @@ static void free_message(struct commit *commit, struct commit_message *msg) unuse_commit_buffer(commit, msg->message); } -static void write_cherry_pick_head(struct commit *commit, const char *pseudoref) -{ - const char *filename; - int fd; - struct strbuf buf = STRBUF_INIT; - - strbuf_addf(&buf, "%s\n", sha1_to_hex(commit->object.sha1)); - - filename = git_path("%s", pseudoref); - fd = open(filename, O_WRONLY | O_CREAT, 0666); - if (fd < 0) - die_errno(_("Could not open '%s' for writing"), filename); - if (write_in_full(fd, buf.buf, buf.len) != buf.len || close(fd)) - die_errno(_("Could not write to '%s'"), filename); - strbuf_release(&buf); -} - static void print_advice(int show_hint, struct replay_opts *opts) { char *msg = getenv("GIT_CHERRY_PICK_HELP"); @@ -607,9 +590,11 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts) * write it at all. */ if (opts->action == REPLAY_PICK && !opts->no_commit && (res == 0 || res == 1)) - write_cherry_pick_head(commit, "CHERRY_PICK_HEAD"); + update_ref(NULL, "CHERRY_PICK_HEAD", commit->object.sha1, NULL, + REF_NODEREF, UPDATE_REFS_DIE_ON_ERR); if (opts->action == REPLAY_REVERT && ((opts->no_commit && res == 0) || res == 1)) - write_cherry_pick_head(commit, "REVERT_HEAD"); + update_ref(NULL, "REVERT_HEAD", commit->object.sha1, NULL, + REF_NODEREF, UPDATE_REFS_DIE_ON_ERR); if (res) { error(opts->action == REPLAY_REVERT From 2c3aed1381f22494bc06fd66dec8292a296db10f Mon Sep 17 00:00:00 2001 From: David Turner Date: Wed, 15 Jul 2015 18:05:28 -0400 Subject: [PATCH 6/6] pseudoref: check return values from read_ref() These codepaths attempt to compare the "expected" current value with the actual current value, but did not check if we successfully read the current value before comparison. Signed-off-by: David Turner Signed-off-by: Junio C Hamano --- refs.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/refs.c b/refs.c index 522b19b539..1db3654b8e 100644 --- a/refs.c +++ b/refs.c @@ -2868,7 +2868,9 @@ static int write_pseudoref(const char *pseudoref, const unsigned char *sha1, if (old_sha1) { unsigned char actual_old_sha1[20]; - read_ref(pseudoref, actual_old_sha1); + + 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); @@ -2904,7 +2906,8 @@ static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1 LOCK_DIE_ON_ERROR); if (fd < 0) die_errno(_("Could not open '%s' for writing"), filename); - read_ref(pseudoref, actual_old_sha1); + if (read_ref(pseudoref, actual_old_sha1)) + die("could not read ref '%s'", pseudoref); if (hashcmp(actual_old_sha1, old_sha1)) { warning("Unexpected sha1 when deleting %s", pseudoref); rollback_lock_file(&lock);