From patchwork Sat Mar 30 22:46:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13611771 Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B53EA22092 for ; Sat, 30 Mar 2024 22:46:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838791; cv=none; b=D11qylLGNF9ir48TL6ulUiu1yZK8A2x6RjEuCAtWHCsDMsqXdXeMYFGesmCZgfsKErCyCiCIItJoWYBA5yO3GbizNQiH+AkuYW89gMmXz8TwFQLow08qtXABO4NvOQlpN/FM/3ZrOybSSJCTJ4sRIyO1vARniDCii+M7nwkKsx8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838791; c=relaxed/simple; bh=tc03nrFtENB78jrwmplH0WghzWwpiUozx0fmTrV66ws=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QuUxGrS1CZNRkh2c0tVMgYT5NSK6ZA6gxFgPQaZU2b251kyO+PLA0dglGoVTqWKdbvXhiD1jxyalkv/enkJG4tZZ7H7JEeU6Yh4H9uK4DRMFDVpT8C49/Hf5Jv9ZyTlpRl5qPB+7SPnQGHrEWToA+DRdzbqZ6B03FhxezWnCa20= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=GVowbYx5; arc=none smtp.client-ip=209.85.218.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GVowbYx5" Received: by mail-ej1-f43.google.com with SMTP id a640c23a62f3a-a4734ae95b3so381568566b.0 for ; Sat, 30 Mar 2024 15:46:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711838788; x=1712443588; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xbOEcYQGUM0QssVKyJxX5uLcLqMLVykNZR5bim81GBM=; b=GVowbYx5PPL+wE6zUJ73sAkLiD4HeC6l4p6/wYxE5MruuI3PRBR7Cn6GV1hU37n5vw X6KxsFiw2sJgC/bd5UG+98XBViHaUO7zNLJ8XwvaOuCDE5Sa+C6we9BzqyvIzFhJRwXo RzsF1G5n+1nKuy6U8m+sigNFXJX+OWVP+Kg0TaQBFeK6D087kkCU3XXuqRXjnuDVhHmQ TAM4rEjlTKmiVY6RPKBqHUabXOYqhI0zjjQb2W0ldiBtNlRy0IqqO2lJB2DwQgmolupP /m6ysZHAflF0NaWHMvi5/n3NslOt0o/mm3HZWxWqyRmBTe9YvzLkjzjYFR2TNjqmmvEZ JK0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711838788; x=1712443588; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xbOEcYQGUM0QssVKyJxX5uLcLqMLVykNZR5bim81GBM=; b=P3dD1N0V0RPG4pBF2tyTENa4BwRULv/Xjq1kUPRFjvcOWPoXaO1Q/TAAGlU6bsikHY XOg5CMLseUNYS9Rw3jTB6tCziOua2ibFZzgdOxBtHYhzsbYu59KgoE0UrJ7YYcrroO2b ArGzil5kJxBqY0dRUyitTlaQNTiwWykTvXb1bRMpBRdK999d+OkF/U3KWEWNz/Nu33eI dJk1iU2FnJxeFo+J1dpRunL49ftBwngc+tYQoFJISXW2gUKuvsNdtSeYB/Q9TcMI4AiI wVKu0QvXMW1Yv4MPd2BojQDPMo9vSGy4j22xVrAnjF/LT3DTYisqQUyxY0eToPgzDIeC 6cmg== X-Gm-Message-State: AOJu0Yzr4hi2pDLPJQ+HJp/NQdy9HT1xaN/ednCXCXBIQ8XadZfCaPg3 FvwGCe29Nm/bNjiQwGESXvpdxYez+qWZWPFQ/FPBS1lkXxYU6mtDYB8KqIbGcY4= X-Google-Smtp-Source: AGHT+IFkcQ5h3FDHZNzm4wIBvcfAnbRuI+nDRz4qGunUEl7SNRJP2kASGsdVbHX84ugWOwnUdWdI7A== X-Received: by 2002:a17:907:7da9:b0:a4e:5c8c:8be8 with SMTP id oz41-20020a1709077da900b00a4e5c8c8be8mr343136ejc.29.1711838787617; Sat, 30 Mar 2024 15:46:27 -0700 (PDT) Received: from laptop.fritz.box ([2a02:2455:826e:4900:9f01:e67d:4cf3:c3bc]) by smtp.gmail.com with ESMTPSA id o19-20020a17090637d300b00a46cc48ab07sm3533580ejc.221.2024.03.30.15.46.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Mar 2024 15:46:27 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: git@vger.kernel.org Cc: ps@pks.im, Karthik Nayak Subject: [PATCH 1/8] files-backend: extract out `create_symref_lock` Date: Sat, 30 Mar 2024 23:46:16 +0100 Message-ID: <20240330224623.579457-2-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240330224623.579457-1-knayak@gitlab.com> References: <20240330224623.579457-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Karthik Nayak The function `create_symref_locked` creates a symref by creating a '.lock' file and then committing the symref lock, which creates the final symref. Split this into two individual functions `create_and_commit_symref` and `create_symref_locked`. This way we can create the symref lock and commit it at different times. This will be used to provide symref support in `git-update-ref(1)`. Signed-off-by: Karthik Nayak --- refs/files-backend.c | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/refs/files-backend.c b/refs/files-backend.c index a098d14ea0..3f0f9521cb 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -1920,26 +1920,39 @@ static void update_symref_reflog(struct files_ref_store *refs, } } -static int create_symref_locked(struct files_ref_store *refs, - struct ref_lock *lock, const char *refname, - const char *target, const char *logmsg) +static int create_symref_lock(struct files_ref_store *refs, + struct ref_lock *lock, const char *refname, + const char *target) { + if (!fdopen_lock_file(&lock->lk, "w")) + return error("unable to fdopen %s: %s", + get_lock_file_path(&lock->lk), strerror(errno)); + + /* no error check; commit_ref will check ferror */ + fprintf(get_lock_file_fp(&lock->lk), "ref: %s\n", target); + return 0; +} + +static int create_and_commit_symref(struct files_ref_store *refs, + struct ref_lock *lock, const char *refname, + const char *target, const char *logmsg) +{ + int ret; + if (prefer_symlink_refs && !create_ref_symlink(lock, target)) { update_symref_reflog(refs, lock, refname, target, logmsg); return 0; } - if (!fdopen_lock_file(&lock->lk, "w")) - return error("unable to fdopen %s: %s", - get_lock_file_path(&lock->lk), strerror(errno)); + ret = create_symref_lock(refs, lock, refname, target); + if (!ret) { + update_symref_reflog(refs, lock, refname, target, logmsg); - update_symref_reflog(refs, lock, refname, target, logmsg); + if (commit_ref(lock) < 0) + return error("unable to write symref for %s: %s", refname, + strerror(errno)); + } - /* no error check; commit_ref will check ferror */ - fprintf(get_lock_file_fp(&lock->lk), "ref: %s\n", target); - if (commit_ref(lock) < 0) - return error("unable to write symref for %s: %s", refname, - strerror(errno)); return 0; } @@ -1960,7 +1973,8 @@ static int files_create_symref(struct ref_store *ref_store, return -1; } - ret = create_symref_locked(refs, lock, refname, target, logmsg); + ret = create_and_commit_symref(refs, lock, refname, target, logmsg); + unlock_ref(lock); return ret; } From patchwork Sat Mar 30 22:46:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13611772 Received: from mail-lf1-f41.google.com (mail-lf1-f41.google.com [209.85.167.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8F4AC45974 for ; Sat, 30 Mar 2024 22:46:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838793; cv=none; b=YkkNxDK/nZMexj90wPIx0K0znlzWmwP7uHKXoCF5fIhy4+NtMbpTGTxgdTBl2MkGaFaPjfQYbKNAfJWqq/yFc4EOFOVy0UPGHpyHwzcK15PgvQo7/m2syuptf6evmXAlCvFvCqKAMjpjHU2hSre08QhKfG7Wh2isjV3cQy3nnrA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838793; c=relaxed/simple; bh=h3OaLZRZFk9Fwrs4Xo4aFSzfuQmhttrjkaBtk+29bVY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AWE/1SNqJaB1stOTar6F0bwXUNqHiPPyptOYS03/Z3r8MQUk7JVtKpOkjU1eiYvH7ug+FLoz2fs8jqjMqoBtMJXWcPwN++lmD6TojG5Q3YkHjNPtErhhQR0I6th4NglziBn3V0Yv9SlM3wx+P+7b3aGAd3qjnTehnafZ+V7w3mw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SeTuROE1; arc=none smtp.client-ip=209.85.167.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SeTuROE1" Received: by mail-lf1-f41.google.com with SMTP id 2adb3069b0e04-515d55aaf5aso770645e87.3 for ; Sat, 30 Mar 2024 15:46:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711838789; x=1712443589; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+grWuy6CASfl5OTrcaKpId72tS98FfgYwyv5Wyx6I4o=; b=SeTuROE1EhPPp24vjn2OgaR5unk1JHsWwj9O6CDo9xdtbg4tYV7h6DXEWOyu7IrKqm ckVnfW5DOTg4QMth/IaD2EXU/PiES30DYtUQVRqSE98aqXWObimSGc31+/gdR+K3yAE6 4FXjqgudGDKgL9Zp+1YaACzpV+Z/Ytglnkqwa3nUc4VAAr+1kiqel06rcqhwWCtzh//s ZgRGzhuy+eS2Gppi8rlkXzp2w60FzKSJE1unkPcpKXBsJ5fgfOjw5ve4OJeXvirWpkPf fHIS5bYEq5zYzvFMVLmfreWuhCaLF+ZSDDme0ySQBlci0UoLWx3KlDWPSvNykiP/vKkk P3gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711838789; x=1712443589; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+grWuy6CASfl5OTrcaKpId72tS98FfgYwyv5Wyx6I4o=; b=uCytQFEKzXc7r/03QFjtR+CbfCiA1dok5vdMjZ/nd1VYYeCVGEXGW1zTOuq9cwirLr r/jo4YFUS1GL0jOS2GE3OcRLB6eN7hRc3WYP1GH9w/nbcLEng4QswlJrgcfvc9K5H9V0 ZdhdJ/2k35WBMGk7mSzUe8b0knJIooAShiilXsWy14zLZJBy4XwPVsqkpSGL1CIviQWf f79fXz9DSpu8hEdhNRJDRZtUsOAB6FA4o57XXqb2sT0OaXgx82pQN5wiYrJMbKPP/DAl gsu2oA6DTj8+u/mAqfNfMGovVf8/BteiyhgP852Qyc8o6Z2r0xKV5P/uh9i2kJBFB4IT 8ziQ== X-Gm-Message-State: AOJu0YwMFwQnKJYdOUrPVPCdfiLhvWEEHdMSehA7NQfyCDDxvFvbgImg ywqCTVtDcYIyGRe+6MfbwGpkUmZ55jbFZLrtrkaIdxIY4trw9BdsIGI6vjTgFFg= X-Google-Smtp-Source: AGHT+IHAO1sujdsJwBY6ZkIM89LQ+tnjiXA6ojSvjEZkD58FIcPZCkBgTAquv/FyMijq9sUkUPuHxg== X-Received: by 2002:ac2:5e62:0:b0:513:fad:3a79 with SMTP id a2-20020ac25e62000000b005130fad3a79mr3484805lfr.41.1711838788799; Sat, 30 Mar 2024 15:46:28 -0700 (PDT) Received: from laptop.fritz.box ([2a02:2455:826e:4900:9f01:e67d:4cf3:c3bc]) by smtp.gmail.com with ESMTPSA id o19-20020a17090637d300b00a46cc48ab07sm3533580ejc.221.2024.03.30.15.46.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Mar 2024 15:46:28 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: git@vger.kernel.org Cc: ps@pks.im, Karthik Nayak Subject: [PATCH 2/8] reftable-backend: extract out `write_symref_with_log` Date: Sat, 30 Mar 2024 23:46:17 +0100 Message-ID: <20240330224623.579457-3-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240330224623.579457-1-knayak@gitlab.com> References: <20240330224623.579457-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Karthik Nayak The function `write_create_symref_table`, creates a `reftable_ref_record` for a symref and adds it to the writer. Then it also creates a log entry for the symref. It does all of this while also obtaining and using a new update index. We extract out `write_symref_with_log` from this to provide the functionality of creating a symref without making changes to the update index. This will be used to add `update-symref` option to the `git-update-ref` command. Rename the `create` field to `arg` while we're here, as `create` is a bit misleading. Signed-off-by: Karthik Nayak --- refs/reftable-backend.c | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index e206d5a073..282a08e3cb 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -1222,23 +1222,22 @@ struct write_create_symref_arg { const char *logmsg; }; -static int write_create_symref_table(struct reftable_writer *writer, void *cb_data) +static int write_symref_with_log(struct reftable_writer *writer, + struct write_create_symref_arg *arg, + uint64_t update_index) { - struct write_create_symref_arg *create = cb_data; - uint64_t ts = reftable_stack_next_update_index(create->stack); struct reftable_ref_record ref = { - .refname = (char *)create->refname, + .refname = (char *)arg->refname, .value_type = REFTABLE_REF_SYMREF, - .value.symref = (char *)create->target, - .update_index = ts, + .value.symref = (char *)arg->target, + .update_index = update_index, }; + struct reftable_log_record log = {0}; struct object_id new_oid; struct object_id old_oid; int ret; - reftable_writer_set_limits(writer, ts, ts); - ret = reftable_writer_add_ref(writer, &ref); if (ret) return ret; @@ -1251,25 +1250,35 @@ static int write_create_symref_table(struct reftable_writer *writer, void *cb_da * not resolve for new repositories this ordering will ensure that this * never happens. */ - if (!create->logmsg || - !refs_resolve_ref_unsafe(&create->refs->base, create->target, + if (!arg->logmsg || + !refs_resolve_ref_unsafe(&arg->refs->base, arg->target, RESOLVE_REF_READING, &new_oid, NULL) || - !should_write_log(&create->refs->base, create->refname)) + !should_write_log(&arg->refs->base, arg->refname)) return 0; fill_reftable_log_record(&log); - log.refname = xstrdup(create->refname); - log.update_index = ts; - log.value.update.message = xstrndup(create->logmsg, - create->refs->write_options.block_size / 2); + log.refname = xstrdup(arg->refname); + log.update_index = update_index; + log.value.update.message = xstrndup(arg->logmsg, + arg->refs->write_options.block_size / 2); memcpy(log.value.update.new_hash, new_oid.hash, GIT_MAX_RAWSZ); - if (refs_resolve_ref_unsafe(&create->refs->base, create->refname, + if (refs_resolve_ref_unsafe(&arg->refs->base, arg->refname, RESOLVE_REF_READING, &old_oid, NULL)) memcpy(log.value.update.old_hash, old_oid.hash, GIT_MAX_RAWSZ); ret = reftable_writer_add_log(writer, &log); reftable_log_record_release(&log); return ret; + +} + +static int write_create_symref_table(struct reftable_writer *writer, void *cb_data) +{ + struct write_create_symref_arg *arg = cb_data; + uint64_t ts = reftable_stack_next_update_index(arg->stack); + reftable_writer_set_limits(writer, ts, ts); + + return write_symref_with_log(writer, arg, ts); } static int reftable_be_create_symref(struct ref_store *ref_store, From patchwork Sat Mar 30 22:46:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13611773 Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CDC444B5CD for ; Sat, 30 Mar 2024 22:46:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838793; cv=none; b=kQURp9dHvX+o9OBNh1/7MQNsezHCAyeBRoKFzN1EosPLuCtDCoIl6OVVRZr+o0MPfsF2pZDtzjLzMYbrBrFTHPQW4L8od/r2kwzeY2pCh5B2DiPuj6EFlI1oOrNaew2R2lEIYvNZmhnZrIS3n16XDyCBe6NL+BYNRofQO+wa8WM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838793; c=relaxed/simple; bh=SgRBdx85GJTZ8tVtuuv6d8V3jnqZDSTXU4n7fCBYEQk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OaI+CxAyetssdGz0I7Q9hWwxkkIRuFkXbkZ/MPl0OWqqxYaQLpPSt+s8QhKwZGYavHU8N3v9NrBdNKtR5EtV/zMGuZrFBhhPqJHGW4AFJV9XA9bptehYlLTKXQlDIZ+Us229AHGezjkQ8hD7WjfQ9VCzWundvk77A62LPD+paRQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SAm4iR5Z; arc=none smtp.client-ip=209.85.167.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SAm4iR5Z" Received: by mail-lf1-f42.google.com with SMTP id 2adb3069b0e04-513e6777af4so4962629e87.2 for ; Sat, 30 Mar 2024 15:46:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711838790; x=1712443590; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=scGVmOOzRt0YOi1oGfhnGq8ys0G08AR56BHOt+nkQPU=; b=SAm4iR5ZiRlVxwpw1VMR5ndcqycO59RHZcKuJ8x7QK7wpWYpxRU90ke3wNipL8ZM3t FYWJufuVWWXtNmRJ6VUctgOrRYGBw8u6Ir0PramjPFCBLnrlUkmKmed6G9M7aTGdTDnl 9nciwkqSWV8lmNEZt8qdwvK2i7NGD2ZvykP3Qa8c3KZ+H0X2cg4VXjY05WFpBOuMQQPz xK3iN6V+8/+6hZ00I8W83WKobROyBMnngUl4DdTWM4d9C7PGUEjdZqVGbMQmGioseWZF v3xF/7rBVtcB6cl8JohjyMYgD23yRXzadIYUA8obp4HjbWtqNxEBkDYKjYOqnBmXnP1N mukA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711838790; x=1712443590; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=scGVmOOzRt0YOi1oGfhnGq8ys0G08AR56BHOt+nkQPU=; b=ovjLT/kWIB7MvfQuGrIhMT/lYEX2WyCMsx63re6KqVwSB8dMCGW3BA0VFJAIglBphe 12Ei9qZDtag18BBYfelzCfQ58HeihP9XWlcE9198eFn3pwxXKF0DfPO/MbgmsyqEmKSz 8eU9OB3/A72QVIjcGZh4IErOcFIxZz0TvSuo7WrdGNxQej52/Sq99B6vnN/v00Vfb1Bh Ta/3UETYkAvTT6BjDlGg6wLr7H7T+o/c6AKMJBippJkEZvWNi8tG/aGYawgljlnZVRBb mFwKrwySd8+WPU20oFnPOZUtVkIvjq3/pEF2p+XgdPEFi4mPq8kijdZb7A3HlXQLb6d2 hsyg== X-Gm-Message-State: AOJu0YwzCRqYSGvI80zpIlhKjbXfNEvyiYJmXvz7PRixgK2FtCOgyq5C 56eMO9Scfiv40YRXfwbgXCEEYADNDeD4A6tSZCS327jEsyDxyeem13V3FGLdkGA= X-Google-Smtp-Source: AGHT+IEc+sAkFJfEhrtRL68TLxfpk6TprvykLzxREDG6zkHLAmaJSsaV2urLI+KxQklZvLZ9UsT/Zg== X-Received: by 2002:a19:2d54:0:b0:515:bfaa:fa12 with SMTP id t20-20020a192d54000000b00515bfaafa12mr4780247lft.22.1711838789719; Sat, 30 Mar 2024 15:46:29 -0700 (PDT) Received: from laptop.fritz.box ([2a02:2455:826e:4900:9f01:e67d:4cf3:c3bc]) by smtp.gmail.com with ESMTPSA id o19-20020a17090637d300b00a46cc48ab07sm3533580ejc.221.2024.03.30.15.46.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Mar 2024 15:46:29 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: git@vger.kernel.org Cc: ps@pks.im, Karthik Nayak Subject: [PATCH 3/8] reftable-backend: move `write_symref_with_log` up Date: Sat, 30 Mar 2024 23:46:18 +0100 Message-ID: <20240330224623.579457-4-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240330224623.579457-1-knayak@gitlab.com> References: <20240330224623.579457-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Karthik Nayak The `write_symref_with_log` code will be consequently used to provide symref creation functionality in transactions. To do this, we move the declaration up so it can be used accordingly. Signed-off-by: Karthik Nayak --- refs/reftable-backend.c | 116 ++++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index 282a08e3cb..9b53d42541 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -588,6 +588,64 @@ static const char *original_update_refname(struct ref_update *update) return update->refname; } +struct write_create_symref_arg { + struct reftable_ref_store *refs; + struct reftable_stack *stack; + const char *refname; + const char *target; + const char *logmsg; +}; + +static int write_symref_with_log(struct reftable_writer *writer, + struct write_create_symref_arg *arg, + uint64_t update_index) +{ + struct reftable_ref_record ref = { + .refname = (char *)arg->refname, + .value_type = REFTABLE_REF_SYMREF, + .value.symref = (char *)arg->target, + .update_index = update_index, + }; + + struct reftable_log_record log = {0}; + struct object_id new_oid; + struct object_id old_oid; + int ret; + + ret = reftable_writer_add_ref(writer, &ref); + if (ret) + return ret; + + /* + * Note that it is important to try and resolve the reference before we + * write the log entry. This is because `should_write_log()` will munge + * `core.logAllRefUpdates`, which is undesirable when we create a new + * repository because it would be written into the config. As HEAD will + * not resolve for new repositories this ordering will ensure that this + * never happens. + */ + if (!arg->logmsg || + !refs_resolve_ref_unsafe(&arg->refs->base, arg->target, + RESOLVE_REF_READING, &new_oid, NULL) || + !should_write_log(&arg->refs->base, arg->refname)) + return 0; + + fill_reftable_log_record(&log); + log.refname = xstrdup(arg->refname); + log.update_index = update_index; + log.value.update.message = xstrndup(arg->logmsg, + arg->refs->write_options.block_size / 2); + memcpy(log.value.update.new_hash, new_oid.hash, GIT_MAX_RAWSZ); + if (refs_resolve_ref_unsafe(&arg->refs->base, arg->refname, + RESOLVE_REF_READING, &old_oid, NULL)) + memcpy(log.value.update.old_hash, old_oid.hash, GIT_MAX_RAWSZ); + + ret = reftable_writer_add_log(writer, &log); + reftable_log_record_release(&log); + return ret; + +} + struct reftable_transaction_update { struct ref_update *update; struct object_id current_oid; @@ -1214,64 +1272,6 @@ static int reftable_be_pack_refs(struct ref_store *ref_store, return ret; } -struct write_create_symref_arg { - struct reftable_ref_store *refs; - struct reftable_stack *stack; - const char *refname; - const char *target; - const char *logmsg; -}; - -static int write_symref_with_log(struct reftable_writer *writer, - struct write_create_symref_arg *arg, - uint64_t update_index) -{ - struct reftable_ref_record ref = { - .refname = (char *)arg->refname, - .value_type = REFTABLE_REF_SYMREF, - .value.symref = (char *)arg->target, - .update_index = update_index, - }; - - struct reftable_log_record log = {0}; - struct object_id new_oid; - struct object_id old_oid; - int ret; - - ret = reftable_writer_add_ref(writer, &ref); - if (ret) - return ret; - - /* - * Note that it is important to try and resolve the reference before we - * write the log entry. This is because `should_write_log()` will munge - * `core.logAllRefUpdates`, which is undesirable when we create a new - * repository because it would be written into the config. As HEAD will - * not resolve for new repositories this ordering will ensure that this - * never happens. - */ - if (!arg->logmsg || - !refs_resolve_ref_unsafe(&arg->refs->base, arg->target, - RESOLVE_REF_READING, &new_oid, NULL) || - !should_write_log(&arg->refs->base, arg->refname)) - return 0; - - fill_reftable_log_record(&log); - log.refname = xstrdup(arg->refname); - log.update_index = update_index; - log.value.update.message = xstrndup(arg->logmsg, - arg->refs->write_options.block_size / 2); - memcpy(log.value.update.new_hash, new_oid.hash, GIT_MAX_RAWSZ); - if (refs_resolve_ref_unsafe(&arg->refs->base, arg->refname, - RESOLVE_REF_READING, &old_oid, NULL)) - memcpy(log.value.update.old_hash, old_oid.hash, GIT_MAX_RAWSZ); - - ret = reftable_writer_add_log(writer, &log); - reftable_log_record_release(&log); - return ret; - -} - static int write_create_symref_table(struct reftable_writer *writer, void *cb_data) { struct write_create_symref_arg *arg = cb_data; From patchwork Sat Mar 30 22:46:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13611774 Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C6A4E4E1D5 for ; Sat, 30 Mar 2024 22:46:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838795; cv=none; b=rWAAN1s8Y6v3f8j+ul2ZuyiBsUjMEAicwij1VV1M2pBA0ELPxG7j3luYnqAIPcqiLq4augjiJwA0/TC7AutCVxo/ZZYIItGXn/rXGXj8MCZ/XvKUGH6CWdDNkIp0oeubmTBG6H0ve5jWCkv2dKFt7NkHyvSXONNCBhdv9On1LrQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838795; c=relaxed/simple; bh=Tws7TOSnZBWgkZ/eo81LbUggM1WVsWpzpqgrqB/rEbE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OtpcDCIdzF0+mizv17rDNV9uvPK8JMChNSnabGmHxhu2+eCxZCvl6Fm7SqKcS4R+TWpj/b7LvmQTQet8+2XsSPl9/6WVDcv6RmnXV4zG4STqOMghgL+BWruU10uHTGPFNSrkMIJLzCYk6QdrFJL4M+2hZTA4UoQj1QYsrqeMHIY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=K0fYMZvT; arc=none smtp.client-ip=209.85.218.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="K0fYMZvT" Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-a4e38f941c6so188260466b.2 for ; Sat, 30 Mar 2024 15:46:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711838791; x=1712443591; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=enREyFM6NlyvigxTvwLNTRHQeKZUYTTd/s2po/Qtlr4=; b=K0fYMZvTREuDa/mQ+bi8JpiyzwMhoQkOzPuNwu8KuFqZL3e+TstpfcjzRK4RalrENC mjSBJA+bmjA0Ef+ikmhpdUxV63RLCWN+lG6kyjHGs0M6rH9zO2ICRm5tEm1p/v8oNekV eFbMWw9MdyK9SY0fsKBcMpQY36GzNKe9a78iHk9qIq5+stOO8fbuCQHpnc/HaHRcVDxD wYpIQUE+RqgSPdKoKC65yy6zQeEA0f3fPgW4Fc+hCfRIDJZoeEZNqjmunznJ0u2c/4i7 iu0wqfEsyiK6TR828lrEp5rfp/oF4oBRBa0huP6w/WUeaG0FvgTYubJrtlCImC6xZw3T 8X6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711838791; x=1712443591; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=enREyFM6NlyvigxTvwLNTRHQeKZUYTTd/s2po/Qtlr4=; b=Xr5f483sHLXDfla90BLd72JC+kIoAm36Q54GnTOQyEq5OtrMfYFem9egXjE7hZepsP vzKWjQeO1f1Wfl+9bNQ1T3t4mSyYv9d42rnNjfnQGptXy2Kirlm5qG1lti1CmhSZwpTL EaryaF8acov3c3bYFiMLLG4QjbW6qZktXDz2gjwmBZJRDBuGuBS9rxD9nIhcdNvUEpak tf7EX9qF5KLR1FyN2FQe8N7hy2BIlCCrLYkTfZjLwgp/dyC2y4r/mXYv++gd78l5AT4A c+2D5vtGZIA+rt9BDNKcz4fyPWzrJ5mWkISSrxMAiJbSTLIiU10C1U/J7L4IUokxA/rA dBbg== X-Gm-Message-State: AOJu0YzFuell8vF753AAe+tRd0+XPfx/+Qx0qGKJnAVVxvsrNWsZ04SF F3fU9FUNAwzhfgYvkLJ1v+vO2ONWY3IfytR7F2P0c4CBQ1a0A2kzCKXH0pkO2YE= X-Google-Smtp-Source: AGHT+IFNeZZZUY3UiImvZoH1QATci+Ndu/woxcmaBtXpSlY3TCH8g9KU9Bciq5KgRf6HoJW7yURl7A== X-Received: by 2002:a17:906:eec3:b0:a4e:1036:7da5 with SMTP id wu3-20020a170906eec300b00a4e10367da5mr3988704ejb.70.1711838790806; Sat, 30 Mar 2024 15:46:30 -0700 (PDT) Received: from laptop.fritz.box ([2a02:2455:826e:4900:9f01:e67d:4cf3:c3bc]) by smtp.gmail.com with ESMTPSA id o19-20020a17090637d300b00a46cc48ab07sm3533580ejc.221.2024.03.30.15.46.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Mar 2024 15:46:30 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: git@vger.kernel.org Cc: ps@pks.im, Karthik Nayak Subject: [PATCH 4/8] refs: accept symref in `ref_transaction_add_update` Date: Sat, 30 Mar 2024 23:46:19 +0100 Message-ID: <20240330224623.579457-5-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240330224623.579457-1-knayak@gitlab.com> References: <20240330224623.579457-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Karthik Nayak The `ref_transaction_add_update` function obtains ref information and flags to create a `ref_update` and add it to the transaction at hand. To extend symref support in transactions, we need to also accept the symref and process it. While we handle the processing in upcoming commits. In this commit, let's add the paramater to the function. Signed-off-by: Karthik Nayak --- branch.c | 2 +- builtin/fast-import.c | 6 +++--- builtin/fetch.c | 2 +- builtin/receive-pack.c | 2 +- builtin/replace.c | 2 +- builtin/tag.c | 2 +- builtin/update-ref.c | 2 +- refs.c | 16 ++++++++-------- refs.h | 9 +++++++-- refs/files-backend.c | 14 +++++++------- refs/refs-internal.h | 12 +++++++++--- refs/reftable-backend.c | 4 ++-- sequencer.c | 6 +++--- walker.c | 2 +- 14 files changed, 46 insertions(+), 35 deletions(-) diff --git a/branch.c b/branch.c index 621019fcf4..266dcb6a89 100644 --- a/branch.c +++ b/branch.c @@ -627,7 +627,7 @@ void create_branch(struct repository *r, if (!transaction || ref_transaction_update(transaction, ref.buf, &oid, forcing ? NULL : null_oid(), - 0, msg, &err) || + 0, msg, NULL, &err) || ref_transaction_commit(transaction, &err)) die("%s", err.buf); ref_transaction_free(transaction); diff --git a/builtin/fast-import.c b/builtin/fast-import.c index 71a195ca22..96c3fd5a30 100644 --- a/builtin/fast-import.c +++ b/builtin/fast-import.c @@ -1648,7 +1648,7 @@ static int update_branch(struct branch *b) transaction = ref_transaction_begin(&err); if (!transaction || ref_transaction_update(transaction, b->name, &b->oid, &old_oid, - 0, msg, &err) || + 0, msg, NULL, &err) || ref_transaction_commit(transaction, &err)) { ref_transaction_free(transaction); error("%s", err.buf); @@ -1688,8 +1688,8 @@ static void dump_tags(void) strbuf_reset(&ref_name); strbuf_addf(&ref_name, "refs/tags/%s", t->name); - if (ref_transaction_update(transaction, ref_name.buf, - &t->oid, NULL, 0, msg, &err)) { + if (ref_transaction_update(transaction, ref_name.buf, &t->oid, + NULL, 0, msg, NULL, &err)) { failure |= error("%s", err.buf); goto cleanup; } diff --git a/builtin/fetch.c b/builtin/fetch.c index 46a793411a..5a8a58b6fa 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -667,7 +667,7 @@ static int s_update_ref(const char *action, ret = ref_transaction_update(transaction, ref->name, &ref->new_oid, check_old ? &ref->old_oid : NULL, - 0, msg, &err); + 0, msg, NULL, &err); if (ret) { ret = STORE_REF_ERROR_OTHER; goto out; diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 56d8a77ed7..5318bc4b58 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -1596,7 +1596,7 @@ static const char *update(struct command *cmd, struct shallow_info *si) namespaced_name, new_oid, old_oid, 0, "push", - &err)) { + NULL, &err)) { rp_error("%s", err.buf); ret = "failed to update ref"; } else { diff --git a/builtin/replace.c b/builtin/replace.c index da59600ad2..b3281758d0 100644 --- a/builtin/replace.c +++ b/builtin/replace.c @@ -201,7 +201,7 @@ static int replace_object_oid(const char *object_ref, transaction = ref_transaction_begin(&err); if (!transaction || ref_transaction_update(transaction, ref.buf, repl, &prev, - 0, NULL, &err) || + 0, NULL, NULL, &err) || ref_transaction_commit(transaction, &err)) res = error("%s", err.buf); diff --git a/builtin/tag.c b/builtin/tag.c index 19a7e06bf4..8de32535de 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -624,7 +624,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix) if (!transaction || ref_transaction_update(transaction, ref.buf, &object, &prev, create_reflog ? REF_FORCE_CREATE_REFLOG : 0, - reflog_msg.buf, &err) || + reflog_msg.buf, NULL, &err) || ref_transaction_commit(transaction, &err)) { if (path) fprintf(stderr, diff --git a/builtin/update-ref.c b/builtin/update-ref.c index 61338a01ec..3807cf4106 100644 --- a/builtin/update-ref.c +++ b/builtin/update-ref.c @@ -205,7 +205,7 @@ static void parse_cmd_update(struct ref_transaction *transaction, if (ref_transaction_update(transaction, refname, &new_oid, have_old ? &old_oid : NULL, update_flags | create_reflog_flag, - msg, &err)) + msg, NULL, &err)) die("%s", err.buf); update_flags = default_flags; diff --git a/refs.c b/refs.c index 55d2e0b2cb..69b89a1aa2 100644 --- a/refs.c +++ b/refs.c @@ -1228,7 +1228,7 @@ struct ref_update *ref_transaction_add_update( const char *refname, unsigned int flags, const struct object_id *new_oid, const struct object_id *old_oid, - const char *msg) + const char *msg, const char *symref) { struct ref_update *update; @@ -1254,7 +1254,7 @@ int ref_transaction_update(struct ref_transaction *transaction, const struct object_id *new_oid, const struct object_id *old_oid, unsigned int flags, const char *msg, - struct strbuf *err) + const char *symref, struct strbuf *err) { assert(err); @@ -1280,7 +1280,7 @@ int ref_transaction_update(struct ref_transaction *transaction, flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0); ref_transaction_add_update(transaction, refname, flags, - new_oid, old_oid, msg); + new_oid, old_oid, msg, symref); return 0; } @@ -1295,7 +1295,7 @@ int ref_transaction_create(struct ref_transaction *transaction, return 1; } return ref_transaction_update(transaction, refname, new_oid, - null_oid(), flags, msg, err); + null_oid(), flags, msg, NULL, err); } int ref_transaction_delete(struct ref_transaction *transaction, @@ -1308,7 +1308,7 @@ int ref_transaction_delete(struct ref_transaction *transaction, BUG("delete called with old_oid set to zeros"); return ref_transaction_update(transaction, refname, null_oid(), old_oid, - flags, msg, err); + flags, msg, NULL, err); } int ref_transaction_verify(struct ref_transaction *transaction, @@ -1320,8 +1320,8 @@ int ref_transaction_verify(struct ref_transaction *transaction, if (!old_oid) BUG("verify called with old_oid set to NULL"); return ref_transaction_update(transaction, refname, - NULL, old_oid, - flags, NULL, err); + NULL, old_oid, flags, + NULL, NULL, err); } int refs_update_ref(struct ref_store *refs, const char *msg, @@ -1336,7 +1336,7 @@ int refs_update_ref(struct ref_store *refs, const char *msg, t = ref_store_transaction_begin(refs, &err); if (!t || ref_transaction_update(t, refname, new_oid, old_oid, flags, msg, - &err) || + NULL, &err) || ref_transaction_commit(t, &err)) { ret = 1; ref_transaction_free(t); diff --git a/refs.h b/refs.h index 298caf6c61..073653d1a4 100644 --- a/refs.h +++ b/refs.h @@ -694,13 +694,18 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err); */ #define REF_SKIP_REFNAME_VERIFICATION (1 << 11) +/* + * Used to denote a symbolic reference update. + */ +#define REF_UPDATE_SYMREF (1 << 12) + /* * Bitmask of all of the flags that are allowed to be passed in to * ref_transaction_update() and friends: */ #define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS \ (REF_NO_DEREF | REF_FORCE_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION | \ - REF_SKIP_REFNAME_VERIFICATION) + REF_SKIP_REFNAME_VERIFICATION | REF_UPDATE_SYMREF) /* * Add a reference update to transaction. `new_oid` is the value that @@ -721,7 +726,7 @@ int ref_transaction_update(struct ref_transaction *transaction, const struct object_id *new_oid, const struct object_id *old_oid, unsigned int flags, const char *msg, - struct strbuf *err); + const char *symref, struct strbuf *err); /* * Add a reference creation to transaction. new_oid is the value that diff --git a/refs/files-backend.c b/refs/files-backend.c index 3f0f9521cb..4dbe73c106 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -1198,7 +1198,7 @@ static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r) ref_transaction_add_update( transaction, r->name, REF_NO_DEREF | REF_HAVE_NEW | REF_HAVE_OLD | REF_IS_PRUNING, - null_oid(), &r->oid, NULL); + null_oid(), &r->oid, NULL, NULL); if (ref_transaction_commit(transaction, &err)) goto cleanup; @@ -1292,8 +1292,8 @@ static int files_pack_refs(struct ref_store *ref_store, * packed-refs transaction: */ if (ref_transaction_update(transaction, iter->refname, - iter->oid, NULL, - REF_NO_DEREF, NULL, &err)) + iter->oid, NULL, REF_NO_DEREF, + NULL, NULL, &err)) die("failure preparing to create packed reference %s: %s", iter->refname, err.buf); @@ -2323,7 +2323,7 @@ static int split_head_update(struct ref_update *update, transaction, "HEAD", update->flags | REF_LOG_ONLY | REF_NO_DEREF, &update->new_oid, &update->old_oid, - update->msg); + update->msg, NULL); /* * Add "HEAD". This insertion is O(N) in the transaction @@ -2386,7 +2386,7 @@ static int split_symref_update(struct ref_update *update, new_update = ref_transaction_add_update( transaction, referent, new_flags, &update->new_oid, &update->old_oid, - update->msg); + update->msg, NULL); new_update->parent_update = update; @@ -2777,7 +2777,7 @@ static int files_transaction_prepare(struct ref_store *ref_store, packed_transaction, update->refname, REF_HAVE_NEW | REF_NO_DEREF, &update->new_oid, NULL, - NULL); + NULL, NULL); } } @@ -3062,7 +3062,7 @@ static int files_initial_transaction_commit(struct ref_store *ref_store, ref_transaction_add_update(packed_transaction, update->refname, update->flags & ~REF_HAVE_OLD, &update->new_oid, &update->old_oid, - NULL); + NULL, NULL); } if (packed_refs_lock(refs->packed_ref_store, 0, err)) { diff --git a/refs/refs-internal.h b/refs/refs-internal.h index 56641aa57a..3fccf784d4 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -134,6 +134,12 @@ struct ref_update { unsigned int type; char *msg; + /* + * If (flags & REF_UPDATE_SYMREF), we update the reference to be a + * symbolic reference and the value is taken from this field. + */ + char *symref_target; + /* * If this ref_update was split off of a symref update via * split_symref_update(), then this member points at that @@ -164,8 +170,8 @@ int ref_update_reject_duplicates(struct string_list *refnames, /* * Add a ref_update with the specified properties to transaction, and * return a pointer to the new object. This function does not verify - * that refname is well-formed. new_oid and old_oid are only - * dereferenced if the REF_HAVE_NEW and REF_HAVE_OLD bits, + * that refname is well-formed. new_oid, old_oid, symref are only + * dereferenced if the REF_HAVE_NEW, REF_HAVE_OLD and REF_UPDATE_SYMREF bits, * respectively, are set in flags. */ struct ref_update *ref_transaction_add_update( @@ -173,7 +179,7 @@ struct ref_update *ref_transaction_add_update( const char *refname, unsigned int flags, const struct object_id *new_oid, const struct object_id *old_oid, - const char *msg); + const char *msg, const char *symref); /* * Transaction states. diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index 9b53d42541..92f2803e90 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -884,7 +884,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, new_update = ref_transaction_add_update( transaction, "HEAD", u->flags | REF_LOG_ONLY | REF_NO_DEREF, - &u->new_oid, &u->old_oid, u->msg); + &u->new_oid, &u->old_oid, u->msg, NULL); string_list_insert(&affected_refnames, new_update->refname); } @@ -963,7 +963,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, */ new_update = ref_transaction_add_update( transaction, referent.buf, new_flags, - &u->new_oid, &u->old_oid, u->msg); + &u->new_oid, &u->old_oid, u->msg, NULL); new_update->parent_update = u; /* diff --git a/sequencer.c b/sequencer.c index 4e14fa6541..668ea683ee 100644 --- a/sequencer.c +++ b/sequencer.c @@ -614,7 +614,7 @@ static int fast_forward_to(struct repository *r, ref_transaction_update(transaction, "HEAD", to, unborn && !is_rebase_i(opts) ? null_oid() : from, - 0, sb.buf, &err) || + 0, sb.buf, NULL, &err) || ref_transaction_commit(transaction, &err)) { ref_transaction_free(transaction); error("%s", err.buf); @@ -1232,7 +1232,7 @@ int update_head_with_reflog(const struct commit *old_head, if (!transaction || ref_transaction_update(transaction, "HEAD", new_head, old_head ? &old_head->object.oid : null_oid(), - 0, sb.buf, err) || + 0, sb.buf, NULL, err) || ref_transaction_commit(transaction, err)) { ret = -1; } @@ -3750,7 +3750,7 @@ static int do_label(struct repository *r, const char *name, int len) error(_("could not read HEAD")); ret = -1; } else if (ref_transaction_update(transaction, ref_name.buf, &head_oid, - NULL, 0, msg.buf, &err) < 0 || + NULL, 0, msg.buf, NULL, &err) < 0 || ref_transaction_commit(transaction, &err)) { error("%s", err.buf); ret = -1; diff --git a/walker.c b/walker.c index 65002a7220..33eef703ce 100644 --- a/walker.c +++ b/walker.c @@ -326,7 +326,7 @@ int walker_fetch(struct walker *walker, int targets, char **target, if (ref_transaction_update(transaction, refname.buf, oids + i, NULL, 0, msg ? msg : "fetch (unknown)", - &err)) { + NULL, &err)) { error("%s", err.buf); goto done; } From patchwork Sat Mar 30 22:46:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13611775 Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CA3124F20C for ; Sat, 30 Mar 2024 22:46:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838795; cv=none; b=TLFAmLTKiMnkRrv/t3QZBLPG27hHPkpv8Ubvt4+3GWZVexOhwgmsdlELSoV5iXc/PwbvpTBLJMltaYd6B3G1SF9fiU9eIBWZiuesABvv1L7QsermbxJgGYJ07PbXht04vMjeI4roHYV+jgqCe/8C8kZZa8Om2fCnjK/oqVs5g5I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838795; c=relaxed/simple; bh=wniBKXqMzZJhXjNSxfr9JWyMIKF9bX+jFkISRtWq5wo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UdDkZAcyglV9jkXt6hjUZqkdVxtWhRRKBlu/02ggVjJ5Ick7as6x4lHJuva5dnOgc78IJaG/oNoYJBVIGiw6IygMRM4TowgI1iYbd3S1OetAJBhDJIQxfc/jYP4u4PJYu1iOG2VQy9J2bIK9PaUnluHn2Hq/ogDOMrqLiHB0iLk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=HIWGnmO3; arc=none smtp.client-ip=209.85.218.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HIWGnmO3" Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-a46a7208eedso378650966b.0 for ; Sat, 30 Mar 2024 15:46:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711838792; x=1712443592; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=yO+h24QuUuCjFg8uUnlx4jBF2Q74WKPeJgQelstpoXM=; b=HIWGnmO3yPg+V0ueBplY0uegA891zTS0cWreLdZ9ayjOYTiJE0e9J1YsSrPbMZqHcY E6RWv7mefQcNjjizB8QI1yqDTGJ0AE0cUvGBjp4aRDymCt13VuLtz7yOwehGV9fja3NS LbbktbQEPqwX+bKWm4IosOpbw+c1BjVK1f0/ug750hr6l5RpYWgFZtSjCr6xDnAP8pdb UWqTrM7kmDy3fKUmuddyPrhUiHxZk9C4+nF0v9HwP4g1yTPZw2sCP/DBrpWWktKoRsHk x54qSGDiuZDi2Ix/9KkTtYJimdIeXkhmZFNfcHP/uYcsQ1SJ/9/rarb9f/Dbgt+V9Wrz uiDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711838792; x=1712443592; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yO+h24QuUuCjFg8uUnlx4jBF2Q74WKPeJgQelstpoXM=; b=IAYzlg1R9CyZGTSVw52AG1LZUL/GWA7kqBYugqg2BgF/nopJs0yycYc2Dmb2//cCKJ +7JC5Zg4dRxdfhICfvtGGbY5uUCEGzAbm97WwrWN4Bi4sieeAB0nb3DVLD5ZSk1e9GJh 5c3l+KDMLO1XyunYQNs/Tk6lYzo013/M7ycJDUaLQ5AZpIWJHZ7Z0qqhi4SN4LWoi5En a2x0oksZWyguJDC4nx9weVX/Uel8gWHAD7vgw7kUYkClgE+LpzNjfqBWvf92rmAwo9lk kToIb1SF5Tj0LAwYFQHVhRS2BQK8pzR11ZEG4PV1SYNjwKn4p96/mDJKDHi63KWJxUsK 0+8g== X-Gm-Message-State: AOJu0YxPrPEtvD3cudd4M3XdxlBKc9AeVuwbKdAD6eJ9Pi+Q1KxJo45D Gw0hQHebc1AlYVLjpkQKKKj0nGiFy/fSaADbcEOKonJebBeIJw3Igd8Z9aMIvrY= X-Google-Smtp-Source: AGHT+IEeG5UdzuG01ceMRHaRbfoB5ueETLngDbhDnQoDVLYHB7cnpa+6IAFBeklBzAQZ+8lkvO3clw== X-Received: by 2002:a17:906:6a0b:b0:a49:dfe1:d5e with SMTP id qw11-20020a1709066a0b00b00a49dfe10d5emr4163120ejc.57.1711838791805; Sat, 30 Mar 2024 15:46:31 -0700 (PDT) Received: from laptop.fritz.box ([2a02:2455:826e:4900:9f01:e67d:4cf3:c3bc]) by smtp.gmail.com with ESMTPSA id o19-20020a17090637d300b00a46cc48ab07sm3533580ejc.221.2024.03.30.15.46.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Mar 2024 15:46:31 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: git@vger.kernel.org Cc: ps@pks.im, Karthik Nayak Subject: [PATCH 5/8] refs/files-backend: add support for symref updates Date: Sat, 30 Mar 2024 23:46:20 +0100 Message-ID: <20240330224623.579457-6-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240330224623.579457-1-knayak@gitlab.com> References: <20240330224623.579457-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Karthik Nayak Add support for transactional symbolic reference updates in the files backend. This also adheres to the config of using symlinks for symbolic references. While this commit is setting up the files-backend to support symrefs in transaction's. It will only be used in a consequent commit, when we wire up the `update-symref` option for `git-update-ref`. Signed-off-by: Karthik Nayak --- refs/files-backend.c | 45 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/refs/files-backend.c b/refs/files-backend.c index 4dbe73c106..6b4cc80843 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -2323,7 +2323,7 @@ static int split_head_update(struct ref_update *update, transaction, "HEAD", update->flags | REF_LOG_ONLY | REF_NO_DEREF, &update->new_oid, &update->old_oid, - update->msg, NULL); + update->msg, update->symref_target); /* * Add "HEAD". This insertion is O(N) in the transaction @@ -2386,7 +2386,7 @@ static int split_symref_update(struct ref_update *update, new_update = ref_transaction_add_update( transaction, referent, new_flags, &update->new_oid, &update->old_oid, - update->msg, NULL); + update->msg, update->symref_target); new_update->parent_update = update; @@ -2396,7 +2396,7 @@ static int split_symref_update(struct ref_update *update, * done when new_update is processed. */ update->flags |= REF_LOG_ONLY | REF_NO_DEREF; - update->flags &= ~REF_HAVE_OLD; + update->flags &= ~(REF_HAVE_OLD|REF_UPDATE_SYMREF); /* * Add the referent. This insertion is O(N) in the transaction @@ -2567,6 +2567,27 @@ static int lock_ref_for_update(struct files_ref_store *refs, } } + if (update->flags & REF_UPDATE_SYMREF) { + if (create_symref_lock(refs, lock, update->refname, update->symref_target)) { + ret = TRANSACTION_GENERIC_ERROR; + goto out; + } + + if (close_ref_gently(lock)) { + strbuf_addf(err, "couldn't close '%s.lock'", + update->refname); + ret = TRANSACTION_GENERIC_ERROR; + goto out; + } + + /* + * Once we have created the symref lock, the commit + * phase of the transaction only needs to commit the lock. + */ + if (update->flags & REF_UPDATE_SYMREF) + update->flags |= REF_NEEDS_COMMIT; + } + if ((update->flags & REF_HAVE_NEW) && !(update->flags & REF_DELETING) && !(update->flags & REF_LOG_ONLY)) { @@ -2862,6 +2883,14 @@ static int files_transaction_finish(struct ref_store *ref_store, if (update->flags & REF_NEEDS_COMMIT || update->flags & REF_LOG_ONLY) { + if (update->flags & REF_UPDATE_SYMREF) { + if (!refs_resolve_ref_unsafe(&refs->base, update->symref_target, + RESOLVE_REF_READING, &update->new_oid, NULL)) { + strbuf_addf(err, "refname %s not found", update->symref_target); + goto cleanup; + } + } + if (files_log_ref_write(refs, lock->ref_name, &lock->old_oid, @@ -2879,6 +2908,16 @@ static int files_transaction_finish(struct ref_store *ref_store, goto cleanup; } } + + /* + * We try creating a symlink, if that succeeds we continue to the + * next updated. If not, we try and create a regular symref. + */ + if (update->flags & REF_UPDATE_SYMREF && prefer_symlink_refs) + if (!create_ref_symlink(lock, update->symref_target)) + continue; + + if (update->flags & REF_NEEDS_COMMIT) { clear_loose_ref_cache(refs); if (commit_ref(lock)) { From patchwork Sat Mar 30 22:46:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13611776 Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1102A45974 for ; Sat, 30 Mar 2024 22:46:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838796; cv=none; b=SCncWp4jQyfpdLMQFPwdhsYT11xC8KLPmR0Y7+texAsVgsQOIby2o+/9dpkQwrwZX0RfVZNhQzVjZ5+oNaCywVEt7Ehu9wFUOzbdxYMzS2fHR+YQh6CAkVul6qhLu5bMZ2N0/ceqqByB8kPt1cZG74qw4r5YfP50VzE7FwaH1kI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838796; c=relaxed/simple; bh=oz0nUYEbob5ei7Mc0yZWRkKwbqeYmbkG4MCjy8Ns1vA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GTNCPjXSZMWb+EOK728o77cxwA+MuclDOJkjte6exknVku7SY9224soFMc2M+UeRtZLcEDfuGPaI0LECgi9KKMjBXNsIN6JHBKHhh29E2wdPJIfoPLEM+//umt8Gg2STI322skXWVHuB328JnLQ2qKmNkX0odxdQixYWHjEnmXM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=k3Is2scr; arc=none smtp.client-ip=209.85.218.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="k3Is2scr" Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a4e0e3ef357so386959866b.2 for ; Sat, 30 Mar 2024 15:46:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711838793; x=1712443593; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=V0gDHTxpd/4lujMbr9UO7uvM+ApEAO4Xxdgj4x9P7BU=; b=k3Is2scrMTRIjHmSBmmEOogrAdkD9mdmDLr924ehH8NwF6mwyotBUz2XVNmJepd84Y Ns+H5Inu5XNo2Y1KYWc9LZrFThZRfUSDMg5HFe1JD617O0OVueR9dS4moy1oDEfy9JRm O+4C/5FZZa5nvwEsJR9YgMao7ThCnyE/0gQczMpBIblvoZjDp6XkrHotdApJ2ocgQjF/ g2D90qVhNyDJbBRCJt1F2Ii8al1KQ8ryh1VDYzxZXbLunN3tzg87inrFU5aeubYyiU9H t2q3IHutTy/V2VG1xaCQEi0Sk94XNJVGvoJXKT96kkzkvtR8c1tO6GusXAarCZ8LIg+u N4dA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711838793; x=1712443593; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=V0gDHTxpd/4lujMbr9UO7uvM+ApEAO4Xxdgj4x9P7BU=; b=vyOjMUkDfQA4Y3pIStgT+rLELD/HTu04Mc6v++rK59aj2Oq7RBN2ed2cvVEFeyjJcv 1IKTAmFeyqLpX0FwSn8OYTldQAEa2yXZKaDOvCKcP0Vgvkk1HaMehEPEYwa4n3OrAXtn cNHgtQ+4C8aT9R1VzNzIJeUXsgXwdK8QXEcyV88hfMh9a/jD2TsesI10rFW/KndhHFPk hOAN4mWFZz0M7OQZzLMdg7WIN5gkRVPKA8jTvbXXV7GPH5S+42R/MsraBun3bOZdmdR/ 8VPlKcNvh9wb6K2X+vWTqVnW3v0PAyFbfhOrXgG99xFJ7o0xAwoKdsndbboW50dd+Iww fr6g== X-Gm-Message-State: AOJu0YyKF3hBBtxg9BLUWhlN1qUuKl9vo5oaXCq40WSSkeVd1DbuWpKa 8bLAkBZ+tTT/4S0n9fB4w2ONaJtjZ4kFyfmzx32/8lgv3AwXnN1tmqSTodp0gOg= X-Google-Smtp-Source: AGHT+IHxEF8VbApaAmyflVqnl1PwHF6qsYxsi3zj7eLroAHPrRswtUHW34jiVMQJVK8iAIvmywczwg== X-Received: by 2002:a17:907:72c6:b0:a46:13d5:46fe with SMTP id du6-20020a17090772c600b00a4613d546femr5435904ejc.11.1711838792800; Sat, 30 Mar 2024 15:46:32 -0700 (PDT) Received: from laptop.fritz.box ([2a02:2455:826e:4900:9f01:e67d:4cf3:c3bc]) by smtp.gmail.com with ESMTPSA id o19-20020a17090637d300b00a46cc48ab07sm3533580ejc.221.2024.03.30.15.46.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Mar 2024 15:46:32 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: git@vger.kernel.org Cc: ps@pks.im, Karthik Nayak Subject: [PATCH 6/8] refs/reftable-backend: add support for symref updates Date: Sat, 30 Mar 2024 23:46:21 +0100 Message-ID: <20240330224623.579457-7-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240330224623.579457-1-knayak@gitlab.com> References: <20240330224623.579457-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Karthik Nayak Add support for transactional symbolic reference updates in the reftable backend. While this commit is setting up the reftable-backend to support symrefs in transaction's. It will only be used in a consequent commit, when we wire up the `update-symref` option for `git-update-ref`. Signed-off-by: Karthik Nayak --- refs/reftable-backend.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index 92f2803e90..35f2e8e050 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -884,7 +884,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, new_update = ref_transaction_add_update( transaction, "HEAD", u->flags | REF_LOG_ONLY | REF_NO_DEREF, - &u->new_oid, &u->old_oid, u->msg, NULL); + &u->new_oid, &u->old_oid, u->msg, u->symref_target); string_list_insert(&affected_refnames, new_update->refname); } @@ -909,9 +909,11 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, /* * There is no need to write the reference deletion - * when the reference in question doesn't exist. + * when the reference in question doesn't exist except + * when we want to create new symrefs. */ - if (u->flags & REF_HAVE_NEW && !is_null_oid(&u->new_oid)) { + if ((u->flags & REF_HAVE_NEW && !is_null_oid(&u->new_oid)) || + u->flags & REF_UPDATE_SYMREF) { ret = queue_transaction_update(refs, tx_data, u, ¤t_oid, err); if (ret) @@ -963,7 +965,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, */ new_update = ref_transaction_add_update( transaction, referent.buf, new_flags, - &u->new_oid, &u->old_oid, u->msg, NULL); + &u->new_oid, &u->old_oid, u->msg, u->symref_target); new_update->parent_update = u; /* @@ -1026,6 +1028,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, */ if ((u->type & REF_ISSYMREF) || (u->flags & REF_LOG_ONLY) || + (u->flags & REF_UPDATE_SYMREF) || (u->flags & REF_HAVE_NEW && !oideq(¤t_oid, &u->new_oid))) { ret = queue_transaction_update(refs, tx_data, u, ¤t_oid, err); @@ -1187,6 +1190,18 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data ret = reftable_writer_add_ref(writer, &ref); if (ret < 0) goto done; + } else if (u->flags & REF_UPDATE_SYMREF) { + struct write_create_symref_arg create = { + .refs = arg->refs, + .stack = arg->stack, + .refname = u->refname, + .target = u->symref_target, + .logmsg = u->msg, + }; + + write_symref_with_log(writer, &create, ts); + if (ret < 0) + goto done; } } From patchwork Sat Mar 30 22:46:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13611777 Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C8D7F381DF for ; Sat, 30 Mar 2024 22:46:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838798; cv=none; b=O67p/oiENe5d8yBZf/u910n9Kogwd7DvXc2yBSsfyHHq/dWw4GTuFNMRcIDlgQL7hNOC6Nu/uYq0htW4Scj3w93IUPUbBfR+SfSC3mcSq/VP0sFMXIOyDEfonx1L+wChw9TiGkI20JyXnINI/8p70uvhphaTSlRg6qNn/CidDQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838798; c=relaxed/simple; bh=ZHtKWNvTzyz+uBNJTBK8fx8Iz7WdsutD6e381kTMRrs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dXuN3olfr8iRZp15rnYAg/XeIzzZJlug617h/L/GyYzXWxod4949aQJBRk19QztybYkbR3ZTYcn2FCFRiS5Wnoh3HUdIQGureCXmc8IZ9C8FYatv+qqv8+HiWTYQJgr1imaGZQGbY44L6B6C13gnegRv/bVm9Ff3xIL6HyF2WXA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=QWrbTMMn; arc=none smtp.client-ip=209.85.218.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QWrbTMMn" Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-a46ba938de0so396168566b.3 for ; Sat, 30 Mar 2024 15:46:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711838794; x=1712443594; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PlaWPzTeMegfBmCCEyFm1Ddhz/DJL8YRXMernyZs1pI=; b=QWrbTMMnZbgEa8vpgH2gbMGCMN9W78jApGpzsMOtftXVUmljVKMy6f3zXvWolQ1EQs ZQptcF5qbbruIiuXeVOrH+o33bIyfmFwnoPIj4qRHQKMRwhgDv0yHhaCU0R13De1gRf+ MpiuZZOh8dyfKnb+Rcdt3x3vTlG84osNeRl39dRerXR6FQo7tsfXoaICr7NTuqEpbG78 a138yfvkJLpJqvHYitUrNXRcEozn/kEV5KYkHAERlqWWTy5vVhiFR4umTr959TBDGTS/ xcqyvd1W8cx9cO+pWpDRNo//HkaE7OHvb4D/HfNNrnRxXdwMXQ5AyTCD4S/nQyg1hgWQ cKXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711838794; x=1712443594; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PlaWPzTeMegfBmCCEyFm1Ddhz/DJL8YRXMernyZs1pI=; b=mLRIne8pKkuCNerJ92QL6B5xLe3hwOM4DpghE+tybVjkr8uMAfwYCH09DJq4RJ5+9y Av621YmD3XezY2Zs7c4PRsV3jxnGDAF86rYmmlOgRNPqDC9GwHkRISR+KcmeTItA+EG5 8mbkAxdXfoMQYPNt4QcMIuE2ZBDOuSDFep8MSeOoqrrQytNabq0QBhWWkRHRJJboc5A4 GhgafjCivl7PtGREXrcDJ2OlFitr6Q5R4i0JctcnS27WWDaw9sjasXiMeUuO/TwAPZad TYxNnROXUcVCA7kH7BpW6t7bMaGe3Qtqdkn4E6Bqj7RAZqUZRLoqWT/rS4fuLnFZaAa/ bnWQ== X-Gm-Message-State: AOJu0YxQvViFmrVi0JvVQj7T0ARN9njsSyDgWn1QCBulCQ+QwLdS2HZV 26AafJU7X7zhQKQWBK7NpDlTprXRrwVaCL5rhA0tsbYpi3nXHGFzQAae14rYyKc= X-Google-Smtp-Source: AGHT+IEzmFyqcF8GptS4+GXeaWnkQjxtswA44lzQDkXas2OFseVyhZQgwXix7qm/IfI+4aVsh4PQxw== X-Received: by 2002:a17:906:3152:b0:a46:8e02:19c4 with SMTP id e18-20020a170906315200b00a468e0219c4mr2929853eje.77.1711838793873; Sat, 30 Mar 2024 15:46:33 -0700 (PDT) Received: from laptop.fritz.box ([2a02:2455:826e:4900:9f01:e67d:4cf3:c3bc]) by smtp.gmail.com with ESMTPSA id o19-20020a17090637d300b00a46cc48ab07sm3533580ejc.221.2024.03.30.15.46.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Mar 2024 15:46:33 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: git@vger.kernel.org Cc: ps@pks.im, Karthik Nayak Subject: [PATCH 7/8] refs: add 'update-symref' command to 'update-ref' Date: Sat, 30 Mar 2024 23:46:22 +0100 Message-ID: <20240330224623.579457-8-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240330224623.579457-1-knayak@gitlab.com> References: <20240330224623.579457-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Karthik Nayak The 'git-update-ref(1)' command allows transactional reference updates. But currently only supports regular reference updates. Meaning, if one wants to update HEAD (symbolic ref) in a transaction, there is no tool to do so. One option to obtain transactional updates for the HEAD ref is to manually create the HEAD.lock file and commit. This is intrusive, where the user needs to mimic internal git behavior. Also, this only works when using the files backend. To allow users to update the HEAD ref in a transaction, we introduce 'update-symref' command for 'git-update-ref(1)'. This command allows the user to create symref in a transaction similar to the 'update' command of 'git-update-ref(1)'. This command also works well with the existing 'no-deref' option. The option can also be used to create new symrefs too. This means we don't need a dedicated `create-symref` option. This is also because we don't verify the old symref value when updating a symref. So in this case update and create hold the same meaning. The regular `delete` option can also be used to delete symrefs. So we don't add a dedicated `delete-symref` option. Signed-off-by: Karthik Nayak --- Documentation/git-update-ref.txt | 11 ++- builtin/update-ref.c | 61 ++++++++++++--- refs.c | 10 +++ t/t0600-reffiles-backend.sh | 30 ++++++++ t/t1400-update-ref.sh | 127 +++++++++++++++++++++++++++++++ 5 files changed, 229 insertions(+), 10 deletions(-) diff --git a/Documentation/git-update-ref.txt b/Documentation/git-update-ref.txt index 0561808cca..2ea8bc8167 100644 --- a/Documentation/git-update-ref.txt +++ b/Documentation/git-update-ref.txt @@ -65,6 +65,7 @@ performs all modifications together. Specify commands of the form: create SP SP LF delete SP [SP ] LF verify SP [SP ] LF + update-symref SP SP LF option SP LF start LF prepare LF @@ -86,6 +87,7 @@ quoting: create SP NUL NUL delete SP NUL [] NUL verify SP NUL [] NUL + update-symref NUL NUL NUL option SP NUL start NUL prepare NUL @@ -111,12 +113,19 @@ create:: delete:: Delete after verifying it exists with , if - given. If given, may not be zero. + given. If given, may not be zero. Can also delete + symrefs. verify:: Verify against but do not change it. If is zero or missing, the ref must not exist. +update-symref:: + Update as a symbolic reference to point to the given + reference . By default, will be recursively + de-referenced, unless the `no-deref` option is used. Can also + be used to create new symrefs. + option:: Modify the behavior of the next command naming a . The only valid option is `no-deref` to avoid dereferencing diff --git a/builtin/update-ref.c b/builtin/update-ref.c index 3807cf4106..357daf31b8 100644 --- a/builtin/update-ref.c +++ b/builtin/update-ref.c @@ -213,6 +213,48 @@ static void parse_cmd_update(struct ref_transaction *transaction, strbuf_release(&err); } +static void parse_cmd_update_symref(struct ref_transaction *transaction, + const char *next, const char *end) +{ + struct strbuf err = STRBUF_INIT; + char *refname, *symref; + + refname = parse_refname(&next); + if (!refname) + die("update-symref: missing "); + + if (line_termination) { + /* Without -z, consume SP and use next argument */ + if (!*next || *next == line_termination) + die("update-symref %s: missing ", refname); + if (*next != ' ') + die("update-symref %s: expected SP but got: %s", + refname, next); + } else { + /* With -z, read the next NUL-terminated line */ + if (*next) + die("update-symref %s: missing ", refname); + } + next++; + + symref = parse_refname(&next); + if (!symref) + die("update-symref %s: missing ", refname); + + if (*next != line_termination) + die("update-symref %s: extra input: %s", refname, next); + + if (ref_transaction_update(transaction, refname, NULL, NULL, + update_flags | create_reflog_flag | REF_UPDATE_SYMREF, + msg, symref, &err)) + die("%s", err.buf); + + update_flags = default_flags; + free(symref); + free(refname); + strbuf_release(&err); +} + static void parse_cmd_create(struct ref_transaction *transaction, const char *next, const char *end) { @@ -379,15 +421,16 @@ static const struct parse_cmd { unsigned args; enum update_refs_state state; } command[] = { - { "update", parse_cmd_update, 3, UPDATE_REFS_OPEN }, - { "create", parse_cmd_create, 2, UPDATE_REFS_OPEN }, - { "delete", parse_cmd_delete, 2, UPDATE_REFS_OPEN }, - { "verify", parse_cmd_verify, 2, UPDATE_REFS_OPEN }, - { "option", parse_cmd_option, 1, UPDATE_REFS_OPEN }, - { "start", parse_cmd_start, 0, UPDATE_REFS_STARTED }, - { "prepare", parse_cmd_prepare, 0, UPDATE_REFS_PREPARED }, - { "abort", parse_cmd_abort, 0, UPDATE_REFS_CLOSED }, - { "commit", parse_cmd_commit, 0, UPDATE_REFS_CLOSED }, + { "update", parse_cmd_update, 3, UPDATE_REFS_OPEN }, + { "update-symref", parse_cmd_update_symref, 2, UPDATE_REFS_OPEN }, + { "create", parse_cmd_create, 2, UPDATE_REFS_OPEN }, + { "delete", parse_cmd_delete, 2, UPDATE_REFS_OPEN }, + { "verify", parse_cmd_verify, 2, UPDATE_REFS_OPEN }, + { "option", parse_cmd_option, 1, UPDATE_REFS_OPEN }, + { "start", parse_cmd_start, 0, UPDATE_REFS_STARTED }, + { "prepare", parse_cmd_prepare, 0, UPDATE_REFS_PREPARED }, + { "abort", parse_cmd_abort, 0, UPDATE_REFS_CLOSED }, + { "commit", parse_cmd_commit, 0, UPDATE_REFS_CLOSED }, }; static void update_refs_stdin(void) diff --git a/refs.c b/refs.c index 69b89a1aa2..706dcd6deb 100644 --- a/refs.c +++ b/refs.c @@ -1216,6 +1216,7 @@ void ref_transaction_free(struct ref_transaction *transaction) } for (i = 0; i < transaction->nr; i++) { + free(transaction->updates[i]->symref_target); free(transaction->updates[i]->msg); free(transaction->updates[i]); } @@ -1235,6 +1236,9 @@ struct ref_update *ref_transaction_add_update( if (transaction->state != REF_TRANSACTION_OPEN) BUG("update called for transaction that is not open"); + if ((flags & (REF_HAVE_NEW | REF_UPDATE_SYMREF)) == (REF_HAVE_NEW | REF_UPDATE_SYMREF)) + BUG("cannot create regular ref and symref at once"); + FLEX_ALLOC_STR(update, refname, refname); ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc); transaction->updates[transaction->nr++] = update; @@ -1245,6 +1249,8 @@ struct ref_update *ref_transaction_add_update( oidcpy(&update->new_oid, new_oid); if (flags & REF_HAVE_OLD) oidcpy(&update->old_oid, old_oid); + if (flags & REF_UPDATE_SYMREF) + update->symref_target = xstrdup(symref); update->msg = normalize_reflog_message(msg); return update; } @@ -2337,6 +2343,10 @@ static int run_transaction_hook(struct ref_transaction *transaction, for (i = 0; i < transaction->nr; i++) { struct ref_update *update = transaction->updates[i]; + // Reference transaction does not support symbolic updates. + if (update->flags & REF_UPDATE_SYMREF) + continue; + strbuf_reset(&buf); strbuf_addf(&buf, "%s %s %s\n", oid_to_hex(&update->old_oid), diff --git a/t/t0600-reffiles-backend.sh b/t/t0600-reffiles-backend.sh index 64214340e7..6d334cb477 100755 --- a/t/t0600-reffiles-backend.sh +++ b/t/t0600-reffiles-backend.sh @@ -472,4 +472,34 @@ test_expect_success POSIXPERM 'git reflog expire honors core.sharedRepository' ' esac ' +test_expect_success SYMLINKS 'symref transaction supports symlinks' ' + git update-ref refs/heads/new @ && + test_config core.prefersymlinkrefs true && + cat >stdin <<-EOF && + start + update-symref TESTSYMREFONE refs/heads/new + prepare + commit + EOF + git update-ref --no-deref --stdin stdin <<-EOF && + start + update-symref TESTSYMREFONE refs/heads/new + prepare + commit + EOF + git update-ref --no-deref --stdin actual && + echo refs/heads/new >expect && + test_cmp expect actual +' + test_done diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh index 6ebc3ef945..2a6036471b 100755 --- a/t/t1400-update-ref.sh +++ b/t/t1400-update-ref.sh @@ -868,6 +868,105 @@ test_expect_success 'stdin delete symref works flag --no-deref' ' test_cmp expect actual ' +test_expect_success 'stdin update-symref creates symref with --no-deref' ' + # ensure that the symref does not already exist + test_must_fail git symbolic-ref --no-recurse refs/heads/symref && + cat >stdin <<-EOF && + update-symref refs/heads/symref $b + EOF + git update-ref --no-deref --stdin expect && + git symbolic-ref --no-recurse refs/heads/symref >actual && + test_cmp expect actual && + test-tool ref-store main for-each-reflog-ent refs/heads/symref >actual && + grep "$Z $(git rev-parse $b)" actual +' + +test_expect_success 'stdin update-symref updates symref with --no-deref' ' + # ensure that the symref already exists + git symbolic-ref --no-recurse refs/heads/symref && + cat >stdin <<-EOF && + update-symref refs/heads/symref $a + EOF + git update-ref --no-deref --stdin expect && + git symbolic-ref --no-recurse refs/heads/symref >actual && + test_cmp expect actual && + test-tool ref-store main for-each-reflog-ent refs/heads/symref >actual && + grep "$(git rev-parse $b) $(git rev-parse $a)" actual +' + +test_expect_success 'stdin update-symref noop update with --no-deref' ' + git symbolic-ref --no-recurse refs/heads/symref >actual && + echo $a >expect && + test_cmp expect actual && + cat >stdin <<-EOF && + update-symref refs/heads/symref $a + EOF + git update-ref --no-deref --stdin actual && + test_cmp expect actual && + test-tool ref-store main for-each-reflog-ent refs/heads/symref >actual && + grep "$(git rev-parse $a) $(git rev-parse $a)" actual +' + +test_expect_success 'stdin update-symref regular ref with --no-deref' ' + git update-ref refs/heads/regularref $a && + cat >stdin <<-EOF && + update-symref refs/heads/regularref $a + EOF + git update-ref --no-deref --stdin expect && + git symbolic-ref --no-recurse refs/heads/regularref >actual && + test_cmp expect actual && + test-tool ref-store main for-each-reflog-ent refs/heads/regularref >actual && + grep "$(git rev-parse $a) $(git rev-parse $a)" actual +' + +test_expect_success 'stdin update-symref creates symref' ' + # delete the ref since it already exists from previous tests + git update-ref --no-deref -d refs/heads/symref && + cat >stdin <<-EOF && + update-symref refs/heads/symref $b + EOF + git update-ref --stdin expect && + git symbolic-ref --no-recurse refs/heads/symref >actual && + test_cmp expect actual && + test-tool ref-store main for-each-reflog-ent refs/heads/symref >actual && + grep "$Z $(git rev-parse $b)" actual +' + +test_expect_success 'stdin update-symref updates symref' ' + git update-ref refs/heads/symref2 $b && + git symbolic-ref --no-recurse refs/heads/symref refs/heads/symref2 && + cat >stdin <<-EOF && + update-symref refs/heads/symref $a + EOF + git update-ref --stdin expect && + git symbolic-ref --no-recurse refs/heads/symref2 >actual && + test_cmp expect actual && + echo refs/heads/symref2 >expect && + git symbolic-ref --no-recurse refs/heads/symref >actual && + test_cmp expect actual && + test-tool ref-store main for-each-reflog-ent refs/heads/symref >actual && + grep "$(git rev-parse $b) $(git rev-parse $b)" actual +' + +test_expect_success 'stdin update-symref regular ref' ' + git update-ref --no-deref refs/heads/regularref $a && + cat >stdin <<-EOF && + update-symref refs/heads/regularref $a + EOF + git update-ref --stdin expect && + git symbolic-ref --no-recurse refs/heads/regularref >actual && + test_cmp expect actual && + test-tool ref-store main for-each-reflog-ent refs/heads/regularref >actual && + grep "$(git rev-parse $a) $(git rev-parse $a)" actual +' + test_expect_success 'stdin delete ref works with right old value' ' echo "delete $b $m~1" >stdin && git update-ref --stdin stdin <<-EOF && + start + update-symref TESTSYMREFONE refs/heads/branch + prepare + commit + EOF + git update-ref --no-deref --stdin expect && + git symbolic-ref TESTSYMREFONE >actual && + test_cmp expect actual +' + +test_expect_success 'transaction can abort symref update' ' + git symbolic-ref TESTSYMREFONE $a && + cat >stdin <<-EOF && + start + update-symref TESTSYMREFONE refs/heads/branch + prepare + abort + EOF + git update-ref --no-deref --stdin expect && + git symbolic-ref TESTSYMREFONE >actual && + test_cmp expect actual +' + test_done From patchwork Sat Mar 30 22:46:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13611778 Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F3FF053812 for ; Sat, 30 Mar 2024 22:46:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838799; cv=none; b=qTE6PGNXlt60qzeS3VPhtc+ndmhL9w7JppU0UxalKMmMinK3riGHodJUo9iGtYLFWXsrKcJ7+XF3EILRjPb0/fr1K0lz313reUQ/2wcFFenLVd0mMyC+NKpwEl8sCframwpup8ccYGXj1emuYxUZuvUA8ivBEsqUSWBPlYGtL0Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711838799; c=relaxed/simple; bh=yXMYdiTeEYmnS8E10alPu7HOgtgBmeEDhAjV4KfLwkI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WfjgWvlRKGeP3OfmNo9/8JJQP6L9cEdEMiTs1unWdwnsslm4+yNciynCYTeuI/jzDFgQc/hVSQA/+TUUO23tZXRk1J1jIO6kDS+DLiAb3Cub3BmdbzBE2ENk+eKxo2M6vIkYdDE5oIlvFA4vL8hx8YxpbAaNvS/XtdVW60+6HSI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jKz03yA/; arc=none smtp.client-ip=209.85.167.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jKz03yA/" Received: by mail-lf1-f51.google.com with SMTP id 2adb3069b0e04-515c198e835so3281298e87.3 for ; Sat, 30 Mar 2024 15:46:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711838795; x=1712443595; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=t7whyboC5hIwRKKWtx1/0kVWea8p6fzKsGI3H7EjAOY=; b=jKz03yA/5zEhYUg4xGPG9X7Hq86gj5RS3ulVKPshoMtJ/gTJ1pw8B95+3/dJlPQRyU yWmLX73j5L8kO1PgP7/cKyPmxpsmyJoOFpD3dR/s1hjzm8hVnNUtuGVZfM//P/q7zBer LswnMaoVPVofFIvQCGmnF1q6STu7zGatV6Int8cCbXtsJ7xeHFOgnLG7OVgas5lSww1+ dZaP/7HAbzD/A+WqLGyrj9Oq2qvepswFukj4l77YJ5wwQF7OIW63C/15tTTXXW9eHoBF UkzaBvbB+XcHXfJFxmTWdGHsmh4rW8ITwZgbEppSxLVSJu5xuQIMe7VLV1FjPjbtbBdD x2RA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711838795; x=1712443595; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=t7whyboC5hIwRKKWtx1/0kVWea8p6fzKsGI3H7EjAOY=; b=c2Z8/R/FO3UBOQ6jAGXNC5HymPJ+M3m2JwR3ro86RLtsGf+lRZeEu+cZSWiBj0EZx0 eDUZB05yOX3iObd/5S53sJqnjGTP4z0+hDub4i+RjjjRkLTlaf9vRZErbz2aBVs94Tz0 alU3dQKMTEvj3iFBmH/BhUZ8HULVCotEKAuOtDXBX6yv0I5lL8He9HMpXTQSn/xh3x0R WyqEqR8oSK9RdbvjojPiipCQa2y6wyXNde3rZfQmi81FdnmngzkG4GV3fMmT66xtKR+1 2BXZlOWA6UjbeSvHFlIvn0kkAWtUDgbYgzBAZVD7jkVOifZ4trVkJoeEP4n5z2bMUp58 xOCA== X-Gm-Message-State: AOJu0Yy4sLKGO68HTl+tEuKkpYdWdrbPotGIWJhoGHAYJjSG5MJQfiLb zqWFZ1bKN7Jsw/MwlaMmMvlmuZ3qmkd9j9uqwOt2fIkcNYyxQRAFYWEIoD3fy0o= X-Google-Smtp-Source: AGHT+IHvhe5kTBPoQfa2sGhLoTQRfN9j4DdkLyFcpLfQeCt75De18erCkOMrSds6TZhSln6vrxYi0w== X-Received: by 2002:a05:6512:40d:b0:513:c195:1c56 with SMTP id u13-20020a056512040d00b00513c1951c56mr3895043lfk.16.1711838794814; Sat, 30 Mar 2024 15:46:34 -0700 (PDT) Received: from laptop.fritz.box ([2a02:2455:826e:4900:9f01:e67d:4cf3:c3bc]) by smtp.gmail.com with ESMTPSA id o19-20020a17090637d300b00a46cc48ab07sm3533580ejc.221.2024.03.30.15.46.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Mar 2024 15:46:34 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: git@vger.kernel.org Cc: ps@pks.im, Karthik Nayak Subject: [PATCH 8/8] refs: support symrefs in 'reference-transaction' hook Date: Sat, 30 Mar 2024 23:46:23 +0100 Message-ID: <20240330224623.579457-9-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240330224623.579457-1-knayak@gitlab.com> References: <20240330224623.579457-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Karthik Nayak The 'reference-transaction' hook runs whenever a reference update is made to the system. In the previous commit, we added support for the `update-symref` command in `git-update-ref`. While it allowed us to now create symbolic refs via `git-update-ref`, it didn't activate the 'reference-transaction' hook. Let's activate the hook for symbolic reference updates too. This takes the form ` SP LF`, which deviates from the form for regular updates since this only contains two fields. While this seems to be backward incompatible, it is okay, since the only way the `reference-transaction` hook outputs this syntax is when `git-update-ref` is used with `update-symref` command. The command was only introduced in the previous commit and hence only users of this command will face this incompatibility. Signed-off-by: Karthik Nayak --- Documentation/githooks.txt | 13 +++++++++++-- refs.c | 17 +++++++++-------- t/t1416-ref-transaction-hooks.sh | 27 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt index 37f91d5b50..ae9f02974d 100644 --- a/Documentation/githooks.txt +++ b/Documentation/githooks.txt @@ -485,8 +485,7 @@ reference-transaction This hook is invoked by any Git command that performs reference updates. It executes whenever a reference transaction is prepared, -committed or aborted and may thus get called multiple times. The hook -does not cover symbolic references (but that may change in the future). +committed or aborted and may thus get called multiple times. The hook takes exactly one argument, which is the current state the given reference transaction is in: @@ -513,6 +512,16 @@ to be created anew, `` is the all-zeroes object name. To distinguish these cases, you can inspect the current value of `` via `git rev-parse`. +For each symbolic reference update that was added to the transaction, +the hook receives on standard input a line of the format: + + SP LF + +where `` is the target of the symbolic reference update +passed into the reference transaction, `` is the full name of +the ref being updated. To distinguish from the regular updates, we can +note that there are only two fields. + The exit status of the hook is ignored for any state except for the "prepared" state. In the "prepared" state, a non-zero exit status will cause the transaction to be aborted. The hook will not be called with diff --git a/refs.c b/refs.c index 706dcd6deb..d0929c5684 100644 --- a/refs.c +++ b/refs.c @@ -2342,16 +2342,17 @@ static int run_transaction_hook(struct ref_transaction *transaction, for (i = 0; i < transaction->nr; i++) { struct ref_update *update = transaction->updates[i]; + strbuf_reset(&buf); - // Reference transaction does not support symbolic updates. if (update->flags & REF_UPDATE_SYMREF) - continue; - - strbuf_reset(&buf); - strbuf_addf(&buf, "%s %s %s\n", - oid_to_hex(&update->old_oid), - oid_to_hex(&update->new_oid), - update->refname); + strbuf_addf(&buf, "%s %s\n", + update->symref_target, + update->refname); + else + strbuf_addf(&buf, "%s %s %s\n", + oid_to_hex(&update->old_oid), + oid_to_hex(&update->new_oid), + update->refname); if (write_in_full(proc.in, buf.buf, buf.len) < 0) { if (errno != EPIPE) { diff --git a/t/t1416-ref-transaction-hooks.sh b/t/t1416-ref-transaction-hooks.sh index 2092488090..fac5d5fc6d 100755 --- a/t/t1416-ref-transaction-hooks.sh +++ b/t/t1416-ref-transaction-hooks.sh @@ -83,6 +83,33 @@ test_expect_success 'hook gets all queued updates in committed state' ' test_cmp expect actual ' +test_expect_success 'hook gets all queued symref updates' ' + test_when_finished "rm actual" && + test_hook reference-transaction <<-\EOF && + echo "$*" >>actual + while read -r line + do + printf "%s\n" "$line" + done >>actual + EOF + cat >expect <<-EOF && + prepared + refs/heads/test TESTSYMREF + refs/heads/test refs/heads/symref + committed + refs/heads/test TESTSYMREF + refs/heads/test refs/heads/symref + EOF + git update-ref --no-deref --stdin <<-EOF && + start + update-symref TESTSYMREF refs/heads/test + update-symref refs/heads/symref refs/heads/test + prepare + commit + EOF + test_cmp expect actual +' + test_expect_success 'hook gets all queued updates in aborted state' ' test_when_finished "rm actual" && git reset --hard PRE &&