Skip to content

Commit 50b124c

Browse files
committed
Merge branch 'hn/reftable-prep-part-2' into seen
Further preliminary change to refs API. * hn/reftable-prep-part-2: Make HEAD a PSEUDOREF rather than PER_WORKTREE. Modify pseudo refs through ref backend storage t1400: use git rev-parse for testing PSEUDOREF existence
2 parents a51c706 + 55dd8b9 commit 50b124c

File tree

4 files changed

+36
-139
lines changed

4 files changed

+36
-139
lines changed

Documentation/git-update-ref.txt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,13 @@ still see a subset of the modifications.
148148

149149
LOGGING UPDATES
150150
---------------
151-
If config parameter "core.logAllRefUpdates" is true and the ref is one under
152-
"refs/heads/", "refs/remotes/", "refs/notes/", or the symbolic ref HEAD; or
153-
the file "$GIT_DIR/logs/<ref>" exists then `git update-ref` will append
154-
a line to the log file "$GIT_DIR/logs/<ref>" (dereferencing all
155-
symbolic refs before creating the log name) describing the change
156-
in ref value. Log lines are formatted as:
151+
If config parameter "core.logAllRefUpdates" is true and the ref is one
152+
under "refs/heads/", "refs/remotes/", "refs/notes/", or a pseudoref
153+
like HEAD or ORIG_HEAD; or the file "$GIT_DIR/logs/<ref>" exists then
154+
`git update-ref` will append a line to the log file
155+
"$GIT_DIR/logs/<ref>" (dereferencing all symbolic refs before creating
156+
the log name) describing the change in ref value. Log lines are
157+
formatted as:
157158

158159
oldsha1 SP newsha1 SP committer LF
159160

refs.c

Lines changed: 10 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,9 @@ int dwim_log(const char *str, int len, struct object_id *oid, char **log)
708708

709709
static int is_per_worktree_ref(const char *refname)
710710
{
711-
return !strcmp(refname, "HEAD") ||
712-
starts_with(refname, "refs/worktree/") ||
713-
starts_with(refname, "refs/bisect/") ||
714-
starts_with(refname, "refs/rewritten/");
711+
return starts_with(refname, "refs/worktree/") ||
712+
starts_with(refname, "refs/bisect/") ||
713+
starts_with(refname, "refs/rewritten/");
715714
}
716715

717716
static int is_pseudoref_syntax(const char *refname)
@@ -771,102 +770,6 @@ long get_files_ref_lock_timeout_ms(void)
771770
return timeout_ms;
772771
}
773772

774-
static int write_pseudoref(const char *pseudoref, const struct object_id *oid,
775-
const struct object_id *old_oid, struct strbuf *err)
776-
{
777-
const char *filename;
778-
int fd;
779-
struct lock_file lock = LOCK_INIT;
780-
struct strbuf buf = STRBUF_INIT;
781-
int ret = -1;
782-
783-
if (!oid)
784-
return 0;
785-
786-
strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
787-
788-
filename = git_path("%s", pseudoref);
789-
fd = hold_lock_file_for_update_timeout(&lock, filename, 0,
790-
get_files_ref_lock_timeout_ms());
791-
if (fd < 0) {
792-
strbuf_addf(err, _("could not open '%s' for writing: %s"),
793-
filename, strerror(errno));
794-
goto done;
795-
}
796-
797-
if (old_oid) {
798-
struct object_id actual_old_oid;
799-
800-
if (read_ref(pseudoref, &actual_old_oid)) {
801-
if (!is_null_oid(old_oid)) {
802-
strbuf_addf(err, _("could not read ref '%s'"),
803-
pseudoref);
804-
rollback_lock_file(&lock);
805-
goto done;
806-
}
807-
} else if (is_null_oid(old_oid)) {
808-
strbuf_addf(err, _("ref '%s' already exists"),
809-
pseudoref);
810-
rollback_lock_file(&lock);
811-
goto done;
812-
} else if (!oideq(&actual_old_oid, old_oid)) {
813-
strbuf_addf(err, _("unexpected object ID when writing '%s'"),
814-
pseudoref);
815-
rollback_lock_file(&lock);
816-
goto done;
817-
}
818-
}
819-
820-
if (write_in_full(fd, buf.buf, buf.len) < 0) {
821-
strbuf_addf(err, _("could not write to '%s'"), filename);
822-
rollback_lock_file(&lock);
823-
goto done;
824-
}
825-
826-
commit_lock_file(&lock);
827-
ret = 0;
828-
done:
829-
strbuf_release(&buf);
830-
return ret;
831-
}
832-
833-
static int delete_pseudoref(const char *pseudoref, const struct object_id *old_oid)
834-
{
835-
const char *filename;
836-
837-
filename = git_path("%s", pseudoref);
838-
839-
if (old_oid && !is_null_oid(old_oid)) {
840-
struct lock_file lock = LOCK_INIT;
841-
int fd;
842-
struct object_id actual_old_oid;
843-
844-
fd = hold_lock_file_for_update_timeout(
845-
&lock, filename, 0,
846-
get_files_ref_lock_timeout_ms());
847-
if (fd < 0) {
848-
error_errno(_("could not open '%s' for writing"),
849-
filename);
850-
return -1;
851-
}
852-
if (read_ref(pseudoref, &actual_old_oid))
853-
die(_("could not read ref '%s'"), pseudoref);
854-
if (!oideq(&actual_old_oid, old_oid)) {
855-
error(_("unexpected object ID when deleting '%s'"),
856-
pseudoref);
857-
rollback_lock_file(&lock);
858-
return -1;
859-
}
860-
861-
unlink(filename);
862-
rollback_lock_file(&lock);
863-
} else {
864-
unlink(filename);
865-
}
866-
867-
return 0;
868-
}
869-
870773
int refs_delete_ref(struct ref_store *refs, const char *msg,
871774
const char *refname,
872775
const struct object_id *old_oid,
@@ -875,11 +778,6 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,
875778
struct ref_transaction *transaction;
876779
struct strbuf err = STRBUF_INIT;
877780

878-
if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
879-
assert(refs == get_main_ref_store(the_repository));
880-
return delete_pseudoref(refname, old_oid);
881-
}
882-
883781
transaction = ref_store_transaction_begin(refs, &err);
884782
if (!transaction ||
885783
ref_transaction_delete(transaction, refname, old_oid,
@@ -1210,18 +1108,13 @@ int refs_update_ref(struct ref_store *refs, const char *msg,
12101108
struct strbuf err = STRBUF_INIT;
12111109
int ret = 0;
12121110

1213-
if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
1214-
assert(refs == get_main_ref_store(the_repository));
1215-
ret = write_pseudoref(refname, new_oid, old_oid, &err);
1216-
} else {
1217-
t = ref_store_transaction_begin(refs, &err);
1218-
if (!t ||
1219-
ref_transaction_update(t, refname, new_oid, old_oid,
1220-
flags, msg, &err) ||
1221-
ref_transaction_commit(t, &err)) {
1222-
ret = 1;
1223-
ref_transaction_free(t);
1224-
}
1111+
t = ref_store_transaction_begin(refs, &err);
1112+
if (!t ||
1113+
ref_transaction_update(t, refname, new_oid, old_oid, flags, msg,
1114+
&err) ||
1115+
ref_transaction_commit(t, &err)) {
1116+
ret = 1;
1117+
ref_transaction_free(t);
12251118
}
12261119
if (ret) {
12271120
const char *str = _("update_ref failed for ref '%s': %s");

t/t1400-update-ref.sh

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ test_expect_success 'core.logAllRefUpdates=always creates reflog by default' '
160160
git reflog exists $outside
161161
'
162162

163-
test_expect_success 'core.logAllRefUpdates=always creates no reflog for ORIG_HEAD' '
163+
test_expect_success 'core.logAllRefUpdates=always creates reflog for ORIG_HEAD' '
164164
test_config core.logAllRefUpdates always &&
165165
git update-ref ORIG_HEAD $A &&
166-
test_must_fail git reflog exists ORIG_HEAD
166+
git reflog exists ORIG_HEAD
167167
'
168168

169169
test_expect_success '--no-create-reflog overrides core.logAllRefUpdates=always' '
@@ -475,57 +475,57 @@ test_expect_success 'git cat-file blob master@{2005-05-26 23:42}:F (expect OTHER
475475

476476
test_expect_success 'given old value for missing pseudoref, do not create' '
477477
test_must_fail git update-ref PSEUDOREF $A $B 2>err &&
478-
test_path_is_missing .git/PSEUDOREF &&
479-
test_i18ngrep "could not read ref" err
478+
test_must_fail git rev-parse PSEUDOREF &&
479+
test_i18ngrep "unable to resolve reference" err
480480
'
481481

482482
test_expect_success 'create pseudoref' '
483483
git update-ref PSEUDOREF $A &&
484-
test $A = $(cat .git/PSEUDOREF)
484+
test $A = $(git rev-parse PSEUDOREF)
485485
'
486486

487487
test_expect_success 'overwrite pseudoref with no old value given' '
488488
git update-ref PSEUDOREF $B &&
489-
test $B = $(cat .git/PSEUDOREF)
489+
test $B = $(git rev-parse PSEUDOREF)
490490
'
491491

492492
test_expect_success 'overwrite pseudoref with correct old value' '
493493
git update-ref PSEUDOREF $C $B &&
494-
test $C = $(cat .git/PSEUDOREF)
494+
test $C = $(git rev-parse PSEUDOREF)
495495
'
496496

497497
test_expect_success 'do not overwrite pseudoref with wrong old value' '
498498
test_must_fail git update-ref PSEUDOREF $D $E 2>err &&
499-
test $C = $(cat .git/PSEUDOREF) &&
500-
test_i18ngrep "unexpected object ID" err
499+
test $C = $(git rev-parse PSEUDOREF) &&
500+
test_i18ngrep "cannot lock ref.*expected" err
501501
'
502502

503503
test_expect_success 'delete pseudoref' '
504504
git update-ref -d PSEUDOREF &&
505-
test_path_is_missing .git/PSEUDOREF
505+
test_must_fail git rev-parse PSEUDOREF
506506
'
507507

508508
test_expect_success 'do not delete pseudoref with wrong old value' '
509509
git update-ref PSEUDOREF $A &&
510510
test_must_fail git update-ref -d PSEUDOREF $B 2>err &&
511-
test $A = $(cat .git/PSEUDOREF) &&
512-
test_i18ngrep "unexpected object ID" err
511+
test $A = $(git rev-parse PSEUDOREF) &&
512+
test_i18ngrep "cannot lock ref.*expected" err
513513
'
514514

515515
test_expect_success 'delete pseudoref with correct old value' '
516516
git update-ref -d PSEUDOREF $A &&
517-
test_path_is_missing .git/PSEUDOREF
517+
test_must_fail git rev-parse PSEUDOREF
518518
'
519519

520520
test_expect_success 'create pseudoref with old OID zero' '
521521
git update-ref PSEUDOREF $A $Z &&
522-
test $A = $(cat .git/PSEUDOREF)
522+
test $A = $(git rev-parse PSEUDOREF)
523523
'
524524

525525
test_expect_success 'do not overwrite pseudoref with old OID zero' '
526526
test_when_finished git update-ref -d PSEUDOREF &&
527527
test_must_fail git update-ref PSEUDOREF $B $Z 2>err &&
528-
test $A = $(cat .git/PSEUDOREF) &&
528+
test $A = $(git rev-parse PSEUDOREF) &&
529529
test_i18ngrep "already exists" err
530530
'
531531

t/t1405-main-ref-store.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ test_expect_success 'create_symref(FOO, refs/heads/master)' '
3131
test_expect_success 'delete_refs(FOO, refs/tags/new-tag)' '
3232
git rev-parse FOO -- &&
3333
git rev-parse refs/tags/new-tag -- &&
34-
$RUN delete-refs 0 nothing FOO refs/tags/new-tag &&
34+
m=$(git rev-parse master) &&
35+
REF_NO_DEREF=1 &&
36+
$RUN delete-refs $REF_NO_DEREF nothing FOO refs/tags/new-tag &&
37+
test_must_fail git rev-parse --symbolic-full-name FOO &&
3538
test_must_fail git rev-parse FOO -- &&
3639
test_must_fail git rev-parse refs/tags/new-tag --
3740
'

0 commit comments

Comments
 (0)