* Commit a change to a loose reference that has already been written * to the loose reference lockfile. Also update the reflogs if * necessary, using the specified lockmsg (which can be NULL). */
| 2029 | * necessary, using the specified lockmsg (which can be NULL). |
| 2030 | */ |
| 2031 | static int commit_ref_update(struct files_ref_store *refs, |
| 2032 | struct ref_lock *lock, |
| 2033 | const struct object_id *oid, const char *logmsg, |
| 2034 | int flags, |
| 2035 | struct strbuf *err) |
| 2036 | { |
| 2037 | files_assert_main_repository(refs, "commit_ref_update"); |
| 2038 | |
| 2039 | clear_loose_ref_cache(refs); |
| 2040 | if (files_log_ref_write(refs, lock->ref_name, &lock->old_oid, oid, NULL, |
| 2041 | logmsg, flags, err)) { |
| 2042 | char *old_msg = strbuf_detach(err, NULL); |
| 2043 | strbuf_addf(err, "cannot update the ref '%s': %s", |
| 2044 | lock->ref_name, old_msg); |
| 2045 | free(old_msg); |
| 2046 | unlock_ref(lock); |
| 2047 | return -1; |
| 2048 | } |
| 2049 | |
| 2050 | if (strcmp(lock->ref_name, "HEAD") != 0) { |
| 2051 | /* |
| 2052 | * Special hack: If a branch is updated directly and HEAD |
| 2053 | * points to it (may happen on the remote side of a push |
| 2054 | * for example) then logically the HEAD reflog should be |
| 2055 | * updated too. |
| 2056 | * A generic solution implies reverse symref information, |
| 2057 | * but finding all symrefs pointing to the given branch |
| 2058 | * would be rather costly for this rare event (the direct |
| 2059 | * update of a branch) to be worth it. So let's cheat and |
| 2060 | * check with HEAD only which should cover 99% of all usage |
| 2061 | * scenarios (even 100% of the default ones). |
| 2062 | */ |
| 2063 | int head_flag; |
| 2064 | const char *head_ref; |
| 2065 | |
| 2066 | head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD", |
| 2067 | RESOLVE_REF_READING, |
| 2068 | NULL, &head_flag); |
| 2069 | if (head_ref && (head_flag & REF_ISSYMREF) && |
| 2070 | !strcmp(head_ref, lock->ref_name)) { |
| 2071 | struct strbuf log_err = STRBUF_INIT; |
| 2072 | if (files_log_ref_write(refs, "HEAD", &lock->old_oid, |
| 2073 | oid, NULL, logmsg, flags, |
| 2074 | &log_err)) { |
| 2075 | error("%s", log_err.buf); |
| 2076 | strbuf_release(&log_err); |
| 2077 | } |
| 2078 | } |
| 2079 | } |
| 2080 | |
| 2081 | if (commit_ref(lock)) { |
| 2082 | strbuf_addf(err, "couldn't set '%s'", lock->ref_name); |
| 2083 | unlock_ref(lock); |
| 2084 | return -1; |
| 2085 | } |
| 2086 | |
| 2087 | unlock_ref(lock); |
| 2088 | return 0; |
no test coverage detected