739
return timeout_ms;
740
}
741
742
-static int write_pseudoref(const char *pseudoref, const struct object_id *oid,
743
- const struct object_id *old_oid, struct strbuf *err)
744
-{
745
- const char *filename;
746
- int fd;
747
- struct lock_file lock = LOCK_INIT;
748
- struct strbuf buf = STRBUF_INIT;
749
- int ret = -1;
750
-
751
- if (!oid)
752
- return 0;
753
-
754
- strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
755
-
756
- filename = git_path("%s", pseudoref);
757
- fd = hold_lock_file_for_update_timeout(&lock, filename, 0,
758
- get_files_ref_lock_timeout_ms());
759
- if (fd < 0) {
760
- strbuf_addf(err, _("could not open '%s' for writing: %s"),
761
- filename, strerror(errno));
762
- goto done;
763
- }
764
-
765
- if (old_oid) {
766
- struct object_id actual_old_oid;
767
-
768
- if (read_ref(pseudoref, &actual_old_oid)) {
769
- if (!is_null_oid(old_oid)) {
770
- strbuf_addf(err, _("could not read ref '%s'"),
771
- pseudoref);
772
- rollback_lock_file(&lock);
773
- goto done;
774
- }
775
- } else if (is_null_oid(old_oid)) {
776
- strbuf_addf(err, _("ref '%s' already exists"),
777
- pseudoref);
778
- rollback_lock_file(&lock);
779
- goto done;
780
- } else if (!oideq(&actual_old_oid, old_oid)) {
781
- strbuf_addf(err, _("unexpected object ID when writing '%s'"),
782
- pseudoref);
783
- rollback_lock_file(&lock);
784
- goto done;
785
- }
786
- }
787
-
788
- if (write_in_full(fd, buf.buf, buf.len) < 0) {
789
- strbuf_addf(err, _("could not write to '%s'"), filename);
790
- rollback_lock_file(&lock);
791
- goto done;
792
- }
793
-
794
- commit_lock_file(&lock);
795
- ret = 0;
796
-done:
797
- strbuf_release(&buf);
798
- return ret;
799
-}
800
-
801
-static int delete_pseudoref(const char *pseudoref, const struct object_id *old_oid)
802
-{
803
- const char *filename;
804
-
805
- filename = git_path("%s", pseudoref);
806
-
807
- if (old_oid && !is_null_oid(old_oid)) {
808
- struct lock_file lock = LOCK_INIT;
809
- int fd;
810
- struct object_id actual_old_oid;
811
-
812
- fd = hold_lock_file_for_update_timeout(
813
- &lock, filename, 0,
814
- get_files_ref_lock_timeout_ms());
815
- if (fd < 0) {
816
- error_errno(_("could not open '%s' for writing"),
817
- filename);
818
- return -1;
819
- }
820
- if (read_ref(pseudoref, &actual_old_oid))
821
- die(_("could not read ref '%s'"), pseudoref);
822
- if (!oideq(&actual_old_oid, old_oid)) {
823
- error(_("unexpected object ID when deleting '%s'"),
824
- pseudoref);
825
- rollback_lock_file(&lock);
826
- return -1;
827
- }
828
-
829
- unlink(filename);
830
- rollback_lock_file(&lock);
831
- } else {
832
- unlink(filename);
833
- }
834
-
835
- return 0;
836
-}
837
-
742
int refs_delete_ref(struct ref_store *refs, const char *msg,
743
const char *refname,
744
const struct object_id *old_oid,
747
struct ref_transaction *transaction;
748
struct strbuf err = STRBUF_INIT;
749
846
- if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
847
- assert(refs == get_main_ref_store(the_repository));
848
- return delete_pseudoref(refname, old_oid);
849
- }
850
-
750
transaction = ref_store_transaction_begin(refs, &err);
751
if (!transaction ||
752
ref_transaction_delete(transaction, refname, old_oid,
1078
struct strbuf err = STRBUF_INIT;
1079
int ret = 0;
1080
1182
- if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
1183
- assert(refs == get_main_ref_store(the_repository));
1184
- ret = write_pseudoref(refname, new_oid, old_oid, &err);
1185
- } else {
1186
- t = ref_store_transaction_begin(refs, &err);
1187
- if (!t ||
1188
- ref_transaction_update(t, refname, new_oid, old_oid,
1189
- flags, msg, &err) ||
1190
- ref_transaction_commit(t, &err)) {
1191
- ret = 1;
1192
- ref_transaction_free(t);
1193
- }
1081
+ t = ref_store_transaction_begin(refs, &err);
1082
+ if (!t ||
1083
+ ref_transaction_update(t, refname, new_oid, old_oid, flags, msg,
1084
+ &err) ||
1085
+ ref_transaction_commit(t, &err)) {
1086
+ ret = 1;
1087
+ ref_transaction_free(t);
1088
}
1089
if (ret) {
1090
const char *str = _("update_ref failed for ref '%s': %s");