pseudorefs
v4 cover pseudorefs
This version removes the old patch 2/6, which changed notes to use normal refs instead of pseudorefs. We don't actually want to forbid per-worktree notes merge; instead, we want to either ensure that they don't conflict, or use a completely different merge mechanism. So we omit this patch.
In addition, this version fixes a style issue reported by Eric Sunshine. It also fixes a lock leak and removes some redundant code; Eric also suggested both of these changes.
It also corrects the word "rebase" with "bisect" in a commit message; Junio noticed this one.
PATCH v4 1/5 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.
--- Documentation/glossary-content.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
Documentation/glossary-content.txt +21 -0
@@ -411,6 +411,27 @@ exclude;; core Git. Porcelains expose more of a <<def_SCM,SCM>> interface than the <<def_plumbing,plumbing>>. +[[def_per_worktree_ref]]per-worktree ref:: + Refs that are per-<<def_worktree,worktree>>, rather than + global. This is presently only <<def_HEAD,HEAD>>, 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. Psuedorefs both have names that are all-caps, + and always start with a line consisting of a + <<def_sha1,SHA-1>> 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 + <<def_per_worktree_ref,per-worktree refs>>, 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 <<def_branch,branch>> means to <<def_fetch,fetch>> it and <<def_merge,merge>> it. See also linkgit:git-pull[1].
Signature · 1 line
2.0.4.315.gad8727a-twtrsrc
PATCH v4 2/5 refs: add ref_type function
In reply to David TurnerAdd 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.
--- refs.c | 26 ++++++++++++++++++++++++++ refs.h | 8 ++++++++ 2 files changed, 34 insertions(+)
refs.c +26 -0
@@ -2848,6 +2848,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 *old_sha1,unsigned int flags) {
refs.h +8 -0
@@ -445,6 +445,14 @@ extern int parse_hide_refs_config(const char *var, const char *value, const charextern 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 ,
Signature · 1 line
2.0.4.315.gad8727a-twtrsrc
PATCH v4 3/5 pseudorefs: create and use pseudoref update and delete functions
In reply to David TurnerPseudorefs 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.
--- refs.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 93 insertions(+), 8 deletions(-)
refs.c +93 -8
@@ -2874,12 +2874,88 @@ 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 *old_sha1, unsigned int flags) { struct ref_transaction *transaction; struct strbuf err = STRBUF_INIT; + if (ref_type(refname) == REF_TYPE_PSEUDOREF) + return delete_pseudoref(refname, old_sha1); + transaction = ref_transaction_begin(&err); if (!transaction || ref_transaction_delete(transaction, refname, old_sha1,
@@ -3973,17 +4049,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);
@@ -3998,7 +4082,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 ; }
Signature · 1 line
2.0.4.315.gad8727a-twtrsrc
PATCH v4 4/5 bisect: use update_ref
In reply to David TurnerInstead 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.
--- bisect.c | 37 ++++++++----------------------------- 1 file changed, 8 insertions(+), 29 deletions(-)
bisect.c +8 -29
@@ -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 };static const char *term_bad;static const char *term_good;
@@ -675,34 +674,16 @@ static int is_expected_rev(const struct object_id *oid)return res; } -static void mark_expected_rev (char *bisect_rev_hex) -{ -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 ); - -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) +static int bisect_checkout (const unsigned char *bisect_rev,int no_checkout) { +char bisect_rev_hex[GIT_SHA1_HEXSZ +1 ]; -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 );
@@ -804,7 +785,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)); } }
@@ -948,7 +929,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 ];read_bisect_terms (&term_bad, &term_good);if (read_bisect_refs ())
@@ -986,11 +966,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 %s commit\n" , bisect_rev_hex, +printf ("%s is the first %s commit\n" ,sha1_to_hex (bisect_rev), term_bad);show_diff_tree (prefix, revs.commits->item);/* This means the bisection process succeeded. */
@@ -1003,7 +982,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)
Signature · 1 line
2.0.4.315.gad8727a-twtrsrc
PATCH v4 5/5 sequencer: replace write_cherry_pick_head with update_ref
In reply to David TurnerNow 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.
--- sequencer.c | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-)
sequencer.c +4 -19
@@ -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
Signature · 1 line
2.0.4.315.gad8727a-twtrsrc
Re: [PATCH v4 1/5] refs: Introduce pseudoref and per-worktree ref concepts
In reply to David TurnerQuoted 1 line · David Turner
+ specially by git. Psuedorefs both have names that are all-caps,
s/Psuedo/Pseudo/
...Johan
Signature · 2 lines
Johan Herland, <[email protected]> www.herland.net
Re: [PATCH v4 1/5] refs: Introduce pseudoref and per-worktree ref concepts
In reply to David TurnerQuoted 9 lines · David Turner
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 <[email protected]>---
Will do s/refs: I/refs: i/; on the subject, and squash the following in to make the doc-toolchain happy (they are reported as missing link targets), before pushing the results out on 'pu'.
Thanks.
Documentation/glossary-content.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/glossary-content.txt +2 -2
@@ -412,7 +412,7 @@ exclude;; interface than the <<def_plumbing,plumbing>>. [[def_per_worktree_ref]]per-worktree ref:: - Refs that are per-<<def_worktree,worktree>>, rather than + Refs that are per-<<def_working_tree,worktree>>, rather than global. This is presently only <<def_HEAD,HEAD>>, but might later include other unusual refs.
@@ -421,7 +421,7 @@ exclude;; like refs for the purposes of rev-parse, but which are treated specially by git. Psuedorefs both have names that are all-caps, and always start with a line consisting of a - <<def_sha1,SHA-1>> followed by whitespace. So, HEAD is not a + <<def_SHA1,SHA-1>> 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