From patchwork Mon Jun 3 20:38:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Rub=C3=A9n_Justo?= X-Patchwork-Id: 13684340 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.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 0A33D13B5AF for ; Mon, 3 Jun 2024 20:38:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717447088; cv=none; b=lkZNNjG/PgilOs/AwkmsvTz2r33h2aObsgcZm/f3wpTv25J3VehpOgZYxbFuUKrr6A41X9y9fGsQV9ilLsVWIbiNpoNP6U0gORZAPVJMtVcy8nO0NlxHFTNCWMN5QZ0An7BvDJM1+710GYNKskVRQVs+14gtoaxLvJdO/AwsCUM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717447088; c=relaxed/simple; bh=Y6CDrVEANeofyF3gYzYnrJBcyUmy+fDe9pkxNHJweXU=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=MqWzEhVf0g4Nj8Ua5rvJHwS1024WHEeJBTn4JDgQ5bAlul3jHDc8e7/vUXjisSoJamCCDbQZ0zDBsYHVqGf6h9TqWdASpQAoy11JYlIw3y3VZYgqnZrSEnYscJrNBTeX5CE7yapdL23IiRTI5p5WgxjBKhCEpRy3j51KCiWbLc4= 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=NFEp3nBt; arc=none smtp.client-ip=209.85.128.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="NFEp3nBt" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-4214aa43a66so746765e9.1 for ; Mon, 03 Jun 2024 13:38:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717447085; x=1718051885; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=ayZgIbNzVwcE6oDv9YFWqhEMm253yLcstl5OPbScquE=; b=NFEp3nBt4iQYNczEnBnrlwZ7kgV42lkHC/0FsyoZqvJDCsUtaj4lFlLNtgTW4t+aDb WmnY3vnRgreodQjv/djJoXp2tdJQU64SVYoblnu6sAHGilr+s4gzGhK2RR1VZnvliazP GFBLDn9tPixim2A438z2hYswll/mUnTd6P7x3XMEOUPnbcgKaME2Z8h2y0qP+ZdEBYa1 wvyYYsFDWKtm0Zg8eaVQzKcJbYq/WismF22I5Qz+GQWPsvPIotI6Z9BDr/kQTA88by1X z76ki6VavcLpaDl/WysEAYz9A44Iy5p6CZc7P5YYvnW9Wu5mFwscuizeoe0/ktGyu3mf Gg7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717447085; x=1718051885; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ayZgIbNzVwcE6oDv9YFWqhEMm253yLcstl5OPbScquE=; b=i82KO/p3kYnsianZ815xXSEH2ZWPalEwBD0kg1SrH3VLyctaibvdzltvjG8S1UHo2r 5sy9csoL8MgEUHC7xmv5iSfmeG/zVY8Ggxf7x7q5k34JaBRqFRsXe2cutgNC7UhIE3hh 2Q7bzp75jnwomXNQXX1ZQSnrFq4c7s0W7YS/lk/Vrs+It9OCnJ6k+q5f08fg8hxiPn4n VN8qy2DoPqboE6UDt62+CTriCXQUfnJHH1dng/kAKW8yxAAMp3sd8xHO1Gz9NzYALoFB V4SMld5N030+X0p1nkKTSffgk2URTtYZxsl/bgEnvn3MoL9mCcJdcVuMFzkwC5rt70ql Qoxw== X-Gm-Message-State: AOJu0YzlqyOGWkXTQFjhro93ADYQRSIOGA2Ns7+3lJJgxa60MejH7n2D qnYzM1BHWPy2X7lFQAQRJwSbhCfXw3jEYTrn37mJQ5lFHU1cb3if316xHg== X-Google-Smtp-Source: AGHT+IGDnwsKCrnfrtWk57AmNBhi8W3yE0hqh5gP4mWZTUF2/qgayiqXnKI+snElfHpiMrhE15dh9A== X-Received: by 2002:a05:600c:a4b:b0:41f:fb7b:bc53 with SMTP id 5b1f17b1804b1-4212e066be2mr82929265e9.22.1717447085271; Mon, 03 Jun 2024 13:38:05 -0700 (PDT) Received: from gmail.com (20.red-88-14-43.dynamicip.rima-tde.net. [88.14.43.20]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4212b8a758csm129443445e9.36.2024.06.03.13.38.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 03 Jun 2024 13:38:04 -0700 (PDT) Message-ID: <28cc803a-1885-49fc-81b2-4ced1fbb8236@gmail.com> Date: Mon, 3 Jun 2024 22:38:04 +0200 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v4 1/6] add-patch: test for 'p' command From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , Dragan Simic , Jeff King References: <1d0cb55c-5f32-419a-b593-d5f0969a51fd@gmail.com> <199072a9-a3fb-4c8d-b867-b0717a10bacc@gmail.com> <1ef0ac3a-3be5-4fc2-93f8-46610f3d1880@gmail.com> Content-Language: en-US In-Reply-To: <1ef0ac3a-3be5-4fc2-93f8-46610f3d1880@gmail.com> Add a test for the 'p' command, which was introduced in 66c14ab592 (add-patch: introduce 'p' in interactive-patch, 2024-03-29). Signed-off-by: Rubén Justo --- t/t3701-add-interactive.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh index 6624a4f7c0..6f6d174687 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -590,6 +590,22 @@ test_expect_success 'navigate to hunk via regex / pattern' ' test_cmp expect actual.trimmed ' +test_expect_success 'print again the hunk' ' + test_when_finished "git reset" && + tr _ " " >expect <<-EOF && + +15 + 20 + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? @@ -1,2 +1,3 @@ + 10 + +15 + 20 + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_ + EOF + test_write_lines s y g 1 p | git add -p >actual && + tail -n 7 actual.trimmed && + test_cmp expect actual.trimmed +' + test_expect_success 'split hunk "add -p (edit)"' ' # Split, say Edit and do nothing. Then: # From patchwork Mon Jun 3 20:38:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Rub=C3=A9n_Justo?= X-Patchwork-Id: 13684341 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 73D1F13B58D for ; Mon, 3 Jun 2024 20:38:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717447097; cv=none; b=GaKyVnX9EtStoGa+B3h2/mCdy8DqPED1k5Nsnk2u2ozzywt8PoxTbEYSVBASMePnuzLTAPTufExvv1I3P9DVSgLOnLsQWNq12kadccFhbSAcJXjHA+30zGgcYI8bNhRGpL+6grbgOBOQPT0yhC81xG7ybOP7FBL+G5TZGqug6go= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717447097; c=relaxed/simple; bh=l1hFnB7hjMo+R9NFQ1wAFHjh/EWSDSrKSZjZES1OWWo=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=YiPS6qaReRUwisTqnz9hd9j0stmwpfJeHyzIA7bWWxLWE/S3/xYo0dUWafuD380Tgi7Y6sDxtrEhP0EekNw+pkRgodZpZpl4FYOVIx+ag0DKoTLc9BVYzlZLvd7H7oZbUvjwVr6Y+9rC08fFd6owSWFJo4sDTAF2+f1VEJ9YJ3M= 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=Ep8CtGCO; arc=none smtp.client-ip=209.85.128.49 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="Ep8CtGCO" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-42120e3911eso43833925e9.0 for ; Mon, 03 Jun 2024 13:38:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717447094; x=1718051894; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=gnwEsJIRw29CX5M5TLsclc2ankgX6+NZGX3GYp/OdlA=; b=Ep8CtGCOKvYL7S94L255xgscqW5YmB1d7/4BEGqQ5yC4OsVXT60iJ1qFm6fn1FH98N krppEQCs81D8cJ6aWRzjwtOn2caRSqdwuiJDlE3dJh8yQRhaoEAcMk20LiZV0rttOteR XWLudcomzvIFT6TzKGj6YaHPfPXYqAUhFB1QOKMfkDpCbf8n9gO7wU4P6srmnRZ1UkXC uHkaFgcRjdyCHCM7h8CcVY6PGYUDRVfdSW5Jx+SVzn211+cMbIAHZmSYTBBp9HAA8uG+ jeCAmG40ep2+VNqHHgL9SpiEz3sg8fjkQreHM4J03HbsPU0RiT24x5U0Jq240SGfn3cZ MbLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717447094; x=1718051894; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gnwEsJIRw29CX5M5TLsclc2ankgX6+NZGX3GYp/OdlA=; b=V96+HBzhWaXFVdI/0y7JJmSGH/wSTkXt5VpgcfcW7wiia0rPPSgFhL/vEEwEL9cYwv 0bb956l173dnDZYIBqRbcFdyK1a0ksv/FVlvjyP5p18RBNuX+ZARRopUPNsZC5OgrKpK FD1RStwi4hAcHx6Y2FYHwRZil/yWWMB5tYpGNrrGpwaUgfvEAsI+kD1q2ANxL7kz48km 7cgYewT1BpHdg+v+EFdmIPdmRHL1B1Qpq6u5HahzbdaXQITOa2hxn5TC6Ysp1IO1XsBX 9hEO8YGfPoBH0sGqHJSyWcu+HNeAUpkPsKggjkLVCwBF/tPK9845QAgmYzmy9uCh+vTT v5hg== X-Gm-Message-State: AOJu0YyMfJVvZup00I988mo39+TP7emE6ioypaUHJ1rLe38i8BHBUQ88 PVfR4B8ERrwr2BcivYOF9KcENLyoPGLSaIViesGxkSecxaV/PeGSfVliRw== X-Google-Smtp-Source: AGHT+IElzvjKe0dz4roz/C8lqKaIRK+lhNC5iRXxHDsR6Z0O9ps5OKgZneLQODdgmE7OqjdHvJtLyw== X-Received: by 2002:a05:600c:35d5:b0:420:fb99:ed02 with SMTP id 5b1f17b1804b1-4212e04492amr77039815e9.6.1717447093610; Mon, 03 Jun 2024 13:38:13 -0700 (PDT) Received: from gmail.com (20.red-88-14-43.dynamicip.rima-tde.net. [88.14.43.20]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4212b83d479sm130835225e9.3.2024.06.03.13.38.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 03 Jun 2024 13:38:13 -0700 (PDT) Message-ID: Date: Mon, 3 Jun 2024 22:38:12 +0200 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v4 2/6] pager: do not close fd 2 unnecessarily From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , Dragan Simic , Jeff King References: <1d0cb55c-5f32-419a-b593-d5f0969a51fd@gmail.com> <199072a9-a3fb-4c8d-b867-b0717a10bacc@gmail.com> <1ef0ac3a-3be5-4fc2-93f8-46610f3d1880@gmail.com> Content-Language: en-US In-Reply-To: <1ef0ac3a-3be5-4fc2-93f8-46610f3d1880@gmail.com> We send errors to the pager since 61b80509e3 (sending errors to stdout under $PAGER, 2008-02-16). In a8335024c2 (pager: do not dup2 stderr if it is already redirected, 2008-12-15) an exception was introduced to avoid redirecting stderr if it is not connected to a terminal. In such exceptional cases, the close(STDERR_FILENO) we're doing in close_pager_fds, is unnecessary. Furthermore, in a subsequent commit we're going to introduce changes that will involve using close_pager_fds multiple times. With this in mind, controlling when we want to close stderr, become sensible. Let's close(STDERR_FILENO) only when necessary, and pave the way for the upcoming changes. Signed-off-by: Rubén Justo --- pager.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pager.c b/pager.c index b8822a9381..b786601074 100644 --- a/pager.c +++ b/pager.c @@ -14,6 +14,7 @@ int pager_use_color = 1; static struct child_process pager_process; static const char *pager_program; +static int close_fd2; /* Is the value coming back from term_columns() just a guess? */ static int term_columns_guessed; @@ -23,7 +24,8 @@ static void close_pager_fds(void) { /* signal EOF to pager */ close(1); - close(2); + if (close_fd2) + close(2); } static void wait_for_pager_atexit(void) @@ -141,8 +143,10 @@ void setup_pager(void) /* original process continues, but writes to the pipe */ dup2(pager_process.in, 1); - if (isatty(2)) + if (isatty(2)) { + close_fd2 = 1; dup2(pager_process.in, 2); + } close(pager_process.in); /* this makes sure that the parent terminates after the pager */ From patchwork Mon Jun 3 20:38:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Rub=C3=A9n_Justo?= X-Patchwork-Id: 13684342 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.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 5498E13B78A for ; Mon, 3 Jun 2024 20:38:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717447104; cv=none; b=siDG2Mt/eoIDohhd3kor5UpxhMBCOZmt7lF/25OJecreYlgGTFIkg9YTNVBnY+8NLxJqLtslGCLxZi2Psow4+LLbSx5WAupO5tMuoCA6uo0nym3hu5eMdA2JJfhwarlcTB8DcDJ1YEK0SPeCXQFj1Oci3Hbqxwlz6VW4jtyNT2Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717447104; c=relaxed/simple; bh=q7MLXEzp+e1hVWI+le4SbfhDrsoCTTlIu34x6EM7iHQ=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=XhRIzRXRuspE0JcXFbxx2T142K5NPHcLCwyHP+thjocBTe0yI82Cab+MYL9T51kJOpqEdf5GihZqze0QbOhyJ91Jy1EeYOI4l1gNNfG6JgYqTEzmCtcPW7oQtaOT/ieGDAo9OyPspv/YU1CinK9D0AN6CE1uEeNXtvNOPtSzLeI= 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=hATcYlik; arc=none smtp.client-ip=209.85.221.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="hATcYlik" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-35dd0c06577so3540743f8f.2 for ; Mon, 03 Jun 2024 13:38:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717447100; x=1718051900; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=f2aMAmJmJ4CpKAM0B/DGMf81IvLZt+/q3msMuyfNWNM=; b=hATcYlikhnTYafT4BXcozgozFkRKIaLCOe7gbxzcaQQa19lUce2L8Crv3MXxS5zcHU R2E9a60CejpG3O+VXIOQzmzABKZs7ZluDciJuFG5LWxURWxa3RsiuCfIAbE2PFBp+X7p R+iaI+TKJoz2MwBAvmkYJOqGoUJTxTJK4tcQuvX13m21McF2B0p1PFmrdj2o0+0nYAaj O9WdYrLDIYoCoP6489IQXrRYG2NCyYSsRzc/Y+MgkbglMD1zZlIZcIwmHLJkIAQ5U9oC 9lAWjvGhye4QCKrAno154WP3E+yDKRmvBIEIi9nVlk7IkdOTXXhR6KkuCETWfzjDjmIY h5Xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717447100; x=1718051900; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=f2aMAmJmJ4CpKAM0B/DGMf81IvLZt+/q3msMuyfNWNM=; b=ITGTEuQLGmANXQptbsnLX9ejGDAqDvmPGjJxGpwzTT1XWiC9mejkApXsa3EaeA01C6 KKpF01JQ3XR4OBthmINKugE1hU/WmAL6CRmMAidS+DEPcAjTY8vBKLT06uxcG8zu5AKU aKHaEex0qHSy/vLp0FoOsl5aJCpu9LRcZR8sfqrXWCdalGLxu8repempfI0VpoFkxAdT 5q6JypUwWu7+huwJ9uhckC4IjTyf3epBOA+2PlDFo50DgNZdhSyZs4f3WfCAYP8mA1CJ WwlvrB4ErTq5PdH4sK79k8nnhcuKOd1mYRUu/ziKiO40f8pVTMSOmP0Asr+wGK9lc0mM Zmaw== X-Gm-Message-State: AOJu0Ywb8YYUr3fXZC4Fd/oCa0ulJNz5LD7XKCMF4T2qVXRBUzQBCTw/ QjlBIC7CzkW5I+eqM+DExrzJSR8wGbuFxBE7AONfC0LhrodEjqjIedG35g== X-Google-Smtp-Source: AGHT+IHnGKjqPkKcnbbWDFqKfnXMoFSchcbGjYJU+hT6Xu4Zc57BP7dnawg4DSljAbULs7Tq1O+KcA== X-Received: by 2002:adf:cf0f:0:b0:355:3b3:f1a3 with SMTP id ffacd0b85a97d-35e0f25e5dcmr6215012f8f.7.1717447100588; Mon, 03 Jun 2024 13:38:20 -0700 (PDT) Received: from gmail.com (20.red-88-14-43.dynamicip.rima-tde.net. [88.14.43.20]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35e52ec4616sm5609316f8f.4.2024.06.03.13.38.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 03 Jun 2024 13:38:20 -0700 (PDT) Message-ID: <76c725b4-1bc4-4916-81d8-98cad8fc4ca0@gmail.com> Date: Mon, 3 Jun 2024 22:38:19 +0200 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v4 3/6] pager: introduce wait_for_pager From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , Dragan Simic , Jeff King References: <1d0cb55c-5f32-419a-b593-d5f0969a51fd@gmail.com> <199072a9-a3fb-4c8d-b867-b0717a10bacc@gmail.com> <1ef0ac3a-3be5-4fc2-93f8-46610f3d1880@gmail.com> Content-Language: en-US In-Reply-To: <1ef0ac3a-3be5-4fc2-93f8-46610f3d1880@gmail.com> Since f67b45f862 (Introduce trivial new pager.c helper infrastructure, 2006-02-28) we have the machinery to send our output to a pager. That machinery, once set up, does not allow us to regain the original stdio streams. In the interactive commands (i.e.: add -p) we want to use the pager for some output, while maintaining the interaction with the user. Modify the pager machinery so that we can use setup_pager and, once we've finished sending the desired output for the pager, wait for the pager termination using a new function wait_for_pager. Make this function reset the pager machinery before returning. Signed-off-by: Rubén Justo --- pager.c | 36 ++++++++++++++++++++++++++++++------ pager.h | 1 + 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/pager.c b/pager.c index b786601074..925f860335 100644 --- a/pager.c +++ b/pager.c @@ -14,7 +14,7 @@ int pager_use_color = 1; static struct child_process pager_process; static const char *pager_program; -static int close_fd2; +static int old_fd1 = -1, old_fd2 = -1; /* Is the value coming back from term_columns() just a guess? */ static int term_columns_guessed; @@ -24,20 +24,41 @@ static void close_pager_fds(void) { /* signal EOF to pager */ close(1); - if (close_fd2) + if (old_fd2 != -1) close(2); } static void wait_for_pager_atexit(void) { + if (old_fd1 == -1) + return; + fflush(stdout); fflush(stderr); close_pager_fds(); finish_command(&pager_process); } +void wait_for_pager(void) +{ + if (old_fd1 == -1) + return; + + wait_for_pager_atexit(); + unsetenv("GIT_PAGER_IN_USE"); + dup2(old_fd1, 1); + old_fd1 = -1; + if (old_fd2 != -1) { + dup2(old_fd2, 2); + old_fd2 = -1; + } +} + static void wait_for_pager_signal(int signo) { + if (old_fd1 == -1) + return; + close_pager_fds(); finish_command_in_signal(&pager_process); sigchain_pop(signo); @@ -113,6 +134,7 @@ void prepare_pager_args(struct child_process *pager_process, const char *pager) void setup_pager(void) { + static int once = 0; const char *pager = git_pager(isatty(1)); if (!pager) @@ -142,16 +164,18 @@ void setup_pager(void) return; /* original process continues, but writes to the pipe */ + old_fd1 = dup(1); dup2(pager_process.in, 1); if (isatty(2)) { - close_fd2 = 1; + old_fd2 = dup(2); dup2(pager_process.in, 2); } close(pager_process.in); - /* this makes sure that the parent terminates after the pager */ - sigchain_push_common(wait_for_pager_signal); - atexit(wait_for_pager_atexit); + if (!once++) { + sigchain_push_common(wait_for_pager_signal); + atexit(wait_for_pager_atexit); + } } int pager_in_use(void) diff --git a/pager.h b/pager.h index b77433026d..103ecac476 100644 --- a/pager.h +++ b/pager.h @@ -5,6 +5,7 @@ struct child_process; const char *git_pager(int stdout_is_tty); void setup_pager(void); +void wait_for_pager(void); int pager_in_use(void); int term_columns(void); void term_clear_line(void); From patchwork Mon Jun 3 20:38:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Rub=C3=A9n_Justo?= X-Patchwork-Id: 13684343 Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.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 89F3113B582 for ; Mon, 3 Jun 2024 20:38:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717447111; cv=none; b=gZCrIoho72uBSsBkOah2ztsnV8b8paTkMTGBRLZZcD+XkW4gGIDHB5Uab9b4ncMTC8fT0A29NZecKNo25MoaM/WzAZH9EeiiYAiHcLnaIHtYFHsFNAmI2kvzYkzXuLwtS+WfMvh9XEhSEg/6XY9YKfwEWqm2JxubxMgyhPychy8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717447111; c=relaxed/simple; bh=EOJjdODlPJhnaMGb18ZE03E/7+Jhm4l9+dtHs8Ls8DE=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=Z2KSCAr/XvuUU9tmQeZlcpoBjSjDP3bMGSZ/JY/NgApiJvOJjrVOKT5KghnG3UTrcEJpl7yqm/0rO8bjO/zw71iUmZlTreqG87Uup6qIDm9gaipBgAnfOpaWHOfa4Yx4SBHIqsbpItKpbvKVOSggFxDn5AGwab3TsaQO08QoFOk= 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=imf6kRb9; arc=none smtp.client-ip=209.85.221.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="imf6kRb9" Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-35dcd5377c4so3854366f8f.2 for ; Mon, 03 Jun 2024 13:38:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717447108; x=1718051908; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=H8T0sbiTMEQG3ThFTq6CKv3Z8+03AZJ2MSll3LSYbEE=; b=imf6kRb9xquiHk+fOHLNScXiS0r7XWxHRcU+wdcgRoinuoQKp2znsrxdxbL2EUKO2/ UhHw1JdKhOgboopblh5bd22MXskmIMAdFfUE7Sp1BkifLWIFrfrsYGJKhihei4kNMfzM LTfEvN5XIxX20qDs6qk0u2hGe7o79FFPxNCLGUE6TwjyuW3loVzFKTWpaFlmRzf2QwVR 5l8WgDdlheZhmlrwPYtc2W0BicntYpJwxy3B3ubTaTP+aMcDWgzR9bFbCxfRu5WRcnXU jjAWHI4v1rPITHTLA7lZkQmCI3RA6kYGbeA/Tz1VuaCQ+1mnf2sxnHIdiIhmjNXjyHAY 8zBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717447108; x=1718051908; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=H8T0sbiTMEQG3ThFTq6CKv3Z8+03AZJ2MSll3LSYbEE=; b=LAJ6o2ytzABN8Nq7yPQ92bO60nYc9QKmsJxKhWgwaRPBgFPFWc7F/IDp+05ir2QRxL jzqVYa/1b/VujcSCvDyTPf/aUkKEWXXD3sdxXNc1Vq1cIbUwExuEoWYrDqr0hxO4g1S6 Rtc/ySSAwPs1TCKv8zZ6y0pbAp9T6BFfPKPhJDuwPO/tpLKLROX+XP9xZyv65HxTp5Yj elc3iFwEAr3cboPNHDsA6z9THq+kV523dwAkn3xlh/jQbklG25AmEe8DpZbvTQX1dD9c h7fuW+hUIGDyu1xlDnYa8QM+OUPY/TNFMYez++x3u3ANxEfEWOsKTU5lvsm8C+99Re3T Ojzw== X-Gm-Message-State: AOJu0YyjLBC55oQycrWk6PTs/G/we54a2ds/6GY+Yo2O3mMgk63DGGOH 6U4TCIHRsAPxb3btGt05tqT9QIf9yiQo6C0oEL0o+WDuB99MGLW4wbKD1w== X-Google-Smtp-Source: AGHT+IFUhzIUizjAt+ndHB410GA/sxAzVoEaIkshKJ338x7JKmmEJRDrLwdp+izFBxlFgxdqRgH3Kw== X-Received: by 2002:adf:f051:0:b0:35a:6422:efc5 with SMTP id ffacd0b85a97d-35e0f34ddafmr6895532f8f.54.1717447107924; Mon, 03 Jun 2024 13:38:27 -0700 (PDT) Received: from gmail.com (20.red-88-14-43.dynamicip.rima-tde.net. [88.14.43.20]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd04d6e5asm9667386f8f.45.2024.06.03.13.38.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 03 Jun 2024 13:38:27 -0700 (PDT) Message-ID: <3fa0ade6-d710-47ce-8bcd-81d7821dfffb@gmail.com> Date: Mon, 3 Jun 2024 22:38:27 +0200 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v4 4/6] pager: introduce setup_custom_pager From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , Dragan Simic , Jeff King References: <1d0cb55c-5f32-419a-b593-d5f0969a51fd@gmail.com> <199072a9-a3fb-4c8d-b867-b0717a10bacc@gmail.com> <1ef0ac3a-3be5-4fc2-93f8-46610f3d1880@gmail.com> Content-Language: en-US In-Reply-To: <1ef0ac3a-3be5-4fc2-93f8-46610f3d1880@gmail.com> Introduce a new function setup_custom_pager() to allow setting up our pager mechanism using a custom pager. If the custom pager specified is NULL or an empty string, use the normal pager as setup_pager() currently does. Signed-off-by: Rubén Justo --- pager.c | 17 +++++++++++------ pager.h | 6 +++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/pager.c b/pager.c index 925f860335..21a7d9cd60 100644 --- a/pager.c +++ b/pager.c @@ -74,14 +74,13 @@ static int core_pager_config(const char *var, const char *value, return 0; } -const char *git_pager(int stdout_is_tty) +static const char *git_pager_custom(int stdout_is_tty, const char* pager) { - const char *pager; - if (!stdout_is_tty) return NULL; - pager = getenv("GIT_PAGER"); + if (!pager || !*pager) + pager = getenv("GIT_PAGER"); if (!pager) { if (!pager_program) read_early_config(core_pager_config, NULL); @@ -97,6 +96,11 @@ const char *git_pager(int stdout_is_tty) return pager; } +const char *git_pager(int stdout_is_tty) +{ + return git_pager_custom(stdout_is_tty, NULL); +} + static void setup_pager_env(struct strvec *env) { const char **argv; @@ -132,10 +136,11 @@ void prepare_pager_args(struct child_process *pager_process, const char *pager) pager_process->trace2_child_class = "pager"; } -void setup_pager(void) +void setup_custom_pager(const char* pager) { static int once = 0; - const char *pager = git_pager(isatty(1)); + + pager = git_pager_custom(isatty(1), pager); if (!pager) return; diff --git a/pager.h b/pager.h index 103ecac476..2166662361 100644 --- a/pager.h +++ b/pager.h @@ -4,7 +4,11 @@ struct child_process; const char *git_pager(int stdout_is_tty); -void setup_pager(void); +void setup_custom_pager(const char*); +static inline void setup_pager(void) +{ + setup_custom_pager(NULL); +} void wait_for_pager(void); int pager_in_use(void); int term_columns(void); From patchwork Mon Jun 3 20:38:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Rub=C3=A9n_Justo?= X-Patchwork-Id: 13684344 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.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 A5A4E13B5B7 for ; Mon, 3 Jun 2024 20:38:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717447118; cv=none; b=lZCMOQwMEW4X00FLuop0lY7r7oXUkmZIUH/PJp117h4QPExwWEVjeSC1WKBKB4wejFM4UJR3/mYC+wPe0Qss4vSIR0n3H6CjGGHN5r7dK+UYjVZJBhUzbxxljUItr6EIyHGO9xAyQ/izfUL3A0ZHVdRPRNkKeEcELLySJHkz8QE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717447118; c=relaxed/simple; bh=7aZ27ysAPiHC/lLUvIXh/g7iOTT9uU08+KxJU0nHESw=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=HKzHuUmo78/MPIX5BdL+DAUwZcH39wixWGv2nkaruV7oBSNX0ISQJCZT0OUMJR8Qn3DIkiIF1pHPPVE/uJznVD/lThFzHuD1IqaQdqsxoN/5qmJUvfXwfQtpNz9rHLwHlvB5MJfCUA5pdh7b5iWooKWbmD+YxC1Bjz7crnToofg= 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=DnGWYjs2; arc=none smtp.client-ip=209.85.128.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="DnGWYjs2" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4212a3e82b6so23019955e9.0 for ; Mon, 03 Jun 2024 13:38:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717447115; x=1718051915; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=lq/b1bUNfdeYcYuytQwJtXRULZtZJnBomfU6UZWsw/w=; b=DnGWYjs2SPHXINySeKiK+/XcpVG4pSh8Klzh1LuUVZRL9srONBVxHJcJK9QpUkM9uE Zj6J9O8PPdXUYKOhd+TOgRMl3wZkdHhADgWZ6yOPdOaT9f1qwzx8joD7nIyi+ja3fCZq LgieH1bA5Nb/Hvlk4d0c6sL31AIN3BzYyJra2i6BWLFG8MGQEKxhsqJ+uykEUHw5MeUk ZS4WUhroTAgCR2JfDq2U2ZLu4Ht+IgVMUkbBElBJ3S7j2cyuYRNiRF0x/4qg0iKC7q9B rSQ16D+gWH7vPzCJ2jTovYGUqT65E4kifD6S/FYo634alcdnwnRruGFq5RGXFR6FfcG+ uWWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717447115; x=1718051915; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lq/b1bUNfdeYcYuytQwJtXRULZtZJnBomfU6UZWsw/w=; b=Bf8pYEe887hIQF68fC2zlo40dz0OWD9p5UoxWTAmYJK1oDyLvGee72mhXxMsubUCVy x/swQOmv+eeG8zg9/zmFQ5+IylOAlg4pdx6oBQ1aG7BlHitPmAzVeQVP9Ewp+DMOimn1 MWyOxFV93KQ6lH6f4jVNq5/W2FfUhkMAqboQoNWQu3m+wXhOHfJemhRLIE2sGF9EYO4b 6Bf6Uywzaj92GeUlonOUOl1sj7KVcKIEG80om65JYzkzQiODKl/ilKZUFyZ/pQc3URTB FhSq8fw2SAtwr4vC9tA6wTBDJCXTHO6PeQ2RKHlEqbd2PAVV0/sSnlnMSKjwrb/VlODb 545w== X-Gm-Message-State: AOJu0Yz2PQjvevDiekdwKM6YqRHlPEqoDLU81PVk6Mra8aTixuH/VUIU 5icSDkQ9cFagmu+8jbmJBtCcuYB0/Pc/LloZOaLNKXN3y2W1Tz1wdf4Zpg== X-Google-Smtp-Source: AGHT+IEU10g+ohvxyQjbhqUl5WullrkJI+UIMgMTWrG9Hr6w6VRTLLk14KamZeA4ej4tM0QU+9Yleg== X-Received: by 2002:a05:600c:3514:b0:421:1b8d:2df0 with SMTP id 5b1f17b1804b1-421451199famr6162915e9.7.1717447114973; Mon, 03 Jun 2024 13:38:34 -0700 (PDT) Received: from gmail.com (20.red-88-14-43.dynamicip.rima-tde.net. [88.14.43.20]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd04c0f57sm9782456f8f.12.2024.06.03.13.38.34 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 03 Jun 2024 13:38:34 -0700 (PDT) Message-ID: Date: Mon, 3 Jun 2024 22:38:33 +0200 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v4 5/6] test-terminal: introduce --no-stdin-pty From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , Dragan Simic , Jeff King References: <1d0cb55c-5f32-419a-b593-d5f0969a51fd@gmail.com> <199072a9-a3fb-4c8d-b867-b0717a10bacc@gmail.com> <1ef0ac3a-3be5-4fc2-93f8-46610f3d1880@gmail.com> Content-Language: en-US In-Reply-To: <1ef0ac3a-3be5-4fc2-93f8-46610f3d1880@gmail.com> In 18d8c26930 (test_terminal: redirect child process' stdin to a pty, 2015-08-04), t/test-terminal.perl learned to connect the child process' stdin to a pty. It works well for what was intended: satisfying an `isatty(STDIN_FILENO)` check. However, the fork introduced, that copies the stdin to the child process, does not always manage to send all the information. To illustrate this behaviour, we can use a function like this: f () { dd if=/dev/zero bs=1 count=10000 status=none | t/test-terminal.perl cat - 2>/dev/null | wc -c; } We do not obtain the expected results when executing this function 100 times: $ for i in $(seq 100); do f; done | sort | uniq -c 36 0 4 1 53 4095 7 4159 If we do the same with a version that does not redirect stdin, a version prior to 18d8c26930, the expected result is obtained: $ git checkout 18d8c26930~1 $ for i in $(seq 100); do f; done | sort | uniq -c 100 10000 In a subsequent commit, a new test is going to rely on test-terminate, and it does not require stdin to be connected to a terminal, but all piped data needs to be successfully transmitted to the child process. To make this possible, add a new parameter "--no-stdin-pty" to allow disabling the stdin redirection though a pty. Signed-off-by: Rubén Justo --- t/test-terminal.perl | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/t/test-terminal.perl b/t/test-terminal.perl index 3810e9bb43..85edc9e8b9 100755 --- a/t/test-terminal.perl +++ b/t/test-terminal.perl @@ -12,10 +12,10 @@ sub start_child { if (not defined $pid) { die "fork failed: $!" } elsif ($pid == 0) { - open STDIN, "<&", $in; + open STDIN, "<&", $in if $in; open STDOUT, ">&", $out; open STDERR, ">&", $err; - close $in; + close $in if $in; close $out; exec(@$argv) or die "cannot exec '$argv->[0]': $!" } @@ -78,28 +78,32 @@ sub copy_stdio { } if ($#ARGV < 1) { - die "usage: test-terminal program args"; + die "usage: test-terminal [--no-stdin-pty] program args"; } +my $no_stdin_pty = $ARGV[0] eq '--no-stdin-pty'; +shift @ARGV if $no_stdin_pty; $ENV{TERM} = 'vt100'; -my $parent_in = new IO::Pty; +my $parent_in = $no_stdin_pty ? undef : IO::Pty->new; my $parent_out = new IO::Pty; my $parent_err = new IO::Pty; -$parent_in->set_raw(); +$parent_in->set_raw() if $parent_in; $parent_out->set_raw(); $parent_err->set_raw(); -$parent_in->slave->set_raw(); +$parent_in->slave->set_raw() if $parent_in; $parent_out->slave->set_raw(); $parent_err->slave->set_raw(); -my $pid = start_child(\@ARGV, $parent_in->slave, $parent_out->slave, $parent_err->slave); -close $parent_in->slave; +my $pid = start_child(\@ARGV,$parent_in ? $parent_in->slave : undef, $parent_out->slave, $parent_err->slave); +close $parent_in->slave if $parent_in; close $parent_out->slave; close $parent_err->slave; -my $in_pid = copy_stdin($parent_in); +my $in_pid = $no_stdin_pty ? 0 : copy_stdin($parent_in); copy_stdio($parent_out, $parent_err); my $ret = finish_child($pid); -# If the child process terminates before our copy_stdin() process is able to -# write all of its data to $parent_in, the copy_stdin() process could stall. -# Send SIGTERM to it to ensure it terminates. -kill 'TERM', $in_pid; -finish_child($in_pid); +if ($in_pid) { + # If the child process terminates before our copy_stdin() process is able to + # write all of its data to $parent_in, the copy_stdin() process could stall. + # Send SIGTERM to it to ensure it terminates. + kill 'TERM', $in_pid; + finish_child($in_pid); +} exit($ret); From patchwork Mon Jun 3 20:38:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Rub=C3=A9n_Justo?= X-Patchwork-Id: 13684345 Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (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 8447A13791B for ; Mon, 3 Jun 2024 20:38:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717447126; cv=none; b=E+MwJyaqV6Nbb63TgL/Ny24WYUL7AZeApMGAObRpPgNTBXdB4lA7KjHi82Nr7xNm+5z6r91dM60glV0VHwzPaXIpQSasuRjd4AJybvWKsIAD/IXiD2Iy52bTFJhiDJr6+c4NK7ogcpHfzYXpbFgQdNSODunAaCSXycm2s+o4GRE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717447126; c=relaxed/simple; bh=LHY27roIN3DCSarVpDprIS9PN9+fkOhsfTZwgcO75ic=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=izwQtB4wmuueVttT3XEzIFV1Q/FewXYka3JeiJG+nNkwx7zCYCGnvuutOUiodBkXhlburQ4wufimJmkwlkUSMcHaTAPVyxDuzI5JtR8OYr8tJt8v4dC08fA/lL3WAz6lYxgdjTJmKwjgHg94OWijnjiZqdmdPUHQEoB7UIOj1hk= 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=NZDiDFFo; arc=none smtp.client-ip=209.85.221.49 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="NZDiDFFo" Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-35dceef429bso2051320f8f.1 for ; Mon, 03 Jun 2024 13:38:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717447123; x=1718051923; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=cD1IpnSq0XIbPuWi3/xONlVhm3J33YeM493u89zh2Jg=; b=NZDiDFFo9A4tXvsoErdKBJLx1wK6Vx8FziDa8kW0kUyBGpeoRfthf4ayXdI5LypiP/ DDKHZkVCoLYhR/v2cZoo8tZ9mhnKrkm8Pg0UhsOYT9Z62q41UHS83Pwq1wfr1INsSSY/ T9JRf/2xSg99JEgX7wRkNLPw1DrsxmK7uifbsdZEyMkiNRDsqscA3VrH0otZ21zonMCo rGsewLRp54SKRmATdMJkJH+ehhUb2h2nWTf/VIdjvIVw7ZHf3e0bEn4dhvO8Kg+6rghj GQNthC4qgBJYChqfQR24ewVxa/55Vv6C2k50GeCisAjeYlpRvAvZM2RZG1CCvT3QDOWl h+mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717447123; x=1718051923; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cD1IpnSq0XIbPuWi3/xONlVhm3J33YeM493u89zh2Jg=; b=j5IvcPU47+eGGoFc+r1F3EomuK280atzklQP6juBdTi5SBFr52h/xyOva04f+R/XTr i3YnPxBwioo9fXc7DAEA8ELYmGFkghTAFdKPhJBAkV8VpqCPaFQvsCr4vxioaFDOYcoS Utot84FOtYpwgKENNCfOZU29SdUIemCW2qRcOVh1kV6qO4b/X86JcwgCUoq629EE3ypJ /18SewPlGNoQ6MkE4S1Cr80wZXwfzsOqdRd+HgUOP3mfOcxo0LbU914DYiGyoG2wvus4 zBUJfttUcOx57jLKi9H5woV7HAk97OiG545iWq9uvUlJ/FJIq7H3iL5puvqAskZ/Ioax my+A== X-Gm-Message-State: AOJu0YzAsxE4lyJ1okbkHIGluhik7oMxKGKZB/kHnyF7ld57kU9x/reJ y2qbv0X5tq8tUo+DYesgKkKfWu8zcQ1nG1cYFGrBhayYhKmMkh1iA8FKyw== X-Google-Smtp-Source: AGHT+IGVdrZzap0+Q0073jNTGnuzrlX6wmWmLbxSriDnWTZhe8lidNtztoHX60XiwE1nTgMiU2qv0Q== X-Received: by 2002:a05:6000:1361:b0:35e:5076:e8ce with SMTP id ffacd0b85a97d-35e7c51a31dmr575119f8f.2.1717447122687; Mon, 03 Jun 2024 13:38:42 -0700 (PDT) Received: from gmail.com (20.red-88-14-43.dynamicip.rima-tde.net. [88.14.43.20]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd04d90a8sm9777670f8f.46.2024.06.03.13.38.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 03 Jun 2024 13:38:42 -0700 (PDT) Message-ID: <75a3cc89-4d23-4eae-b0ad-e52e2c8ba550@gmail.com> Date: Mon, 3 Jun 2024 22:38:41 +0200 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v4 6/6] add-patch: introduce the command '|' From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , Dragan Simic , Jeff King References: <1d0cb55c-5f32-419a-b593-d5f0969a51fd@gmail.com> <199072a9-a3fb-4c8d-b867-b0717a10bacc@gmail.com> <1ef0ac3a-3be5-4fc2-93f8-46610f3d1880@gmail.com> Content-Language: en-US In-Reply-To: <1ef0ac3a-3be5-4fc2-93f8-46610f3d1880@gmail.com> Introduce a new command '|' to send the current hunk to a program. If no program is specified, use the pager. Signed-off-by: Rubén Justo Signed-off-by: Junio C Hamano --- add-patch.c | 25 +++++++++++++++-- t/t3701-add-interactive.sh | 55 ++++++++++++++++++++++++++------------ 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/add-patch.c b/add-patch.c index 814de57c4a..5d8a2f97f9 100644 --- a/add-patch.c +++ b/add-patch.c @@ -5,6 +5,7 @@ #include "environment.h" #include "gettext.h" #include "object-name.h" +#include "pager.h" #include "read-cache-ll.h" #include "repository.h" #include "strbuf.h" @@ -1389,6 +1390,7 @@ N_("j - leave this hunk undecided, see next undecided hunk\n" "s - split the current hunk into smaller hunks\n" "e - manually edit the current hunk\n" "p - print the current hunk\n" + "| - use pager to show the current hunk, or use | to customize\n" "? - print help\n"); static int patch_update_file(struct add_p_state *s, @@ -1401,6 +1403,7 @@ static int patch_update_file(struct add_p_state *s, struct child_process cp = CHILD_PROCESS_INIT; int colored = !!s->colored.len, quit = 0; enum prompt_mode_type prompt_mode_type; + const char* pager = NULL; enum { ALLOW_GOTO_PREVIOUS_HUNK = 1 << 0, ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK = 1 << 1, @@ -1449,9 +1452,15 @@ static int patch_update_file(struct add_p_state *s, strbuf_reset(&s->buf); if (file_diff->hunk_nr) { if (rendered_hunk_index != hunk_index) { + if (pager) + setup_custom_pager(pager); render_hunk(s, hunk, 0, colored, &s->buf); fputs(s->buf.buf, stdout); rendered_hunk_index = hunk_index; + if (pager) { + wait_for_pager(); + pager = NULL; + } } strbuf_reset(&s->buf); @@ -1485,6 +1494,7 @@ static int patch_update_file(struct add_p_state *s, strbuf_addstr(&s->buf, ",e"); } strbuf_addstr(&s->buf, ",p"); + strbuf_addstr(&s->buf, ",|"); } if (file_diff->deleted) prompt_mode_type = PROMPT_DELETION; @@ -1512,8 +1522,8 @@ static int patch_update_file(struct add_p_state *s, continue; ch = tolower(s->answer.buf[0]); - /* 'g' takes a hunk number and '/' takes a regexp */ - if (s->answer.len != 1 && (ch != 'g' && ch != '/')) { + /* 'g' takes a hunk number, '/' takes a regexp and '|' takes a program */ + if (s->answer.len != 1 && (ch != 'g' && ch != '/' && ch != '|')) { err(s, _("Only one letter is expected, got '%s'"), s->answer.buf); continue; } @@ -1674,6 +1684,17 @@ static int patch_update_file(struct add_p_state *s, } } else if (s->answer.buf[0] == 'p') { rendered_hunk_index = -1; + } else if (ch == '|') { + strbuf_remove(&s->answer, 0, 1); + if (s->s.use_single_key && s->answer.len == 0) { + printf("%s", _("program? ")); + fflush(stdout); + strbuf_getline(&s->answer, stdin); + strbuf_trim_trailing_newline(&s->answer); + } + strbuf_trim(&s->answer); + pager = s->answer.buf; + rendered_hunk_index = -1; } else if (s->answer.buf[0] == '?') { const char *p = _(help_patch_remainder), *eol = p; diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh index 6f6d174687..7b3ebb671d 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -64,8 +64,8 @@ test_expect_success 'unknown command' ' git add -N command && git diff command >expect && cat >>expect <<-EOF && - (1/1) Stage addition [y,n,q,a,d,e,p,?]? Unknown command ${SQ}W${SQ} (use ${SQ}?${SQ} for help) - (1/1) Stage addition [y,n,q,a,d,e,p,?]?$SP + (1/1) Stage addition [y,n,q,a,d,e,p,|,?]? Unknown command ${SQ}W${SQ} (use ${SQ}?${SQ} for help) + (1/1) Stage addition [y,n,q,a,d,e,p,|,?]?$SP EOF git add -p -- command actual 2>&1 && test_cmp expect actual @@ -348,9 +348,9 @@ test_expect_success 'different prompts for mode change/deleted' ' git -c core.filemode=true add -p >actual && sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered && cat >expect <<-\EOF && - (1/1) Stage deletion [y,n,q,a,d,p,?]? - (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,p,?]? - (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? + (1/1) Stage deletion [y,n,q,a,d,p,|,?]? + (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,p,|,?]? + (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,|,?]? EOF test_cmp expect actual.filtered ' @@ -537,13 +537,13 @@ test_expect_success 'split hunk setup' ' test_expect_success 'goto hunk 1 with "g 1"' ' test_when_finished "git reset" && tr _ " " >expect <<-EOF && - (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? + 1: -1,2 +1,3 +15 + (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,|,?]? + 1: -1,2 +1,3 +15 _ 2: -2,4 +3,8 +21 go to which hunk? @@ -1,2 +1,3 @@ _10 +15 _20 - (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_ + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,|,?]?_ EOF test_write_lines s y g 1 | git add -p >actual && tail -n 7 actual.trimmed && @@ -556,7 +556,7 @@ test_expect_success 'goto hunk 1 with "g1"' ' _10 +15 _20 - (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_ + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,|,?]?_ EOF test_write_lines s y g1 | git add -p >actual && tail -n 4 actual.trimmed && @@ -566,11 +566,11 @@ test_expect_success 'goto hunk 1 with "g1"' ' test_expect_success 'navigate to hunk via regex /pattern' ' test_when_finished "git reset" && tr _ " " >expect <<-EOF && - (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? @@ -1,2 +1,3 @@ + (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,|,?]? @@ -1,2 +1,3 @@ _10 +15 _20 - (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_ + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,|,?]?_ EOF test_write_lines s y /1,2 | git add -p >actual && tail -n 5 actual.trimmed && @@ -583,7 +583,7 @@ test_expect_success 'navigate to hunk via regex / pattern' ' _10 +15 _20 - (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_ + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,|,?]?_ EOF test_write_lines s y / 1,2 | git add -p >actual && tail -n 4 actual.trimmed && @@ -595,17 +595,38 @@ test_expect_success 'print again the hunk' ' tr _ " " >expect <<-EOF && +15 20 - (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? @@ -1,2 +1,3 @@ + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,|,?]? @@ -1,2 +1,3 @@ 10 +15 20 - (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_ + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,|,?]?_ EOF test_write_lines s y g 1 p | git add -p >actual && tail -n 7 actual.trimmed && test_cmp expect actual.trimmed ' +test_expect_success TTY 'print again the hunk (PAGER)' ' + test_when_finished "git reset" && + cat >expect <<-EOF && + +15 + 20 + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,|,?]? PAGER @@ -1,2 +1,3 @@ + PAGER 10 + PAGER +15 + PAGER 20 + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,|,?]? + EOF + test_write_lines s y g 1 \| | + ( + GIT_PAGER="sed s/^/PAGER\ /" && + export GIT_PAGER && + test_terminal --no-stdin-pty git add -p >actual + ) && + tail -n 7 actual.trimmed && + test_cmp expect actual.trimmed +' + test_expect_success 'split hunk "add -p (edit)"' ' # Split, say Edit and do nothing. Then: # @@ -780,21 +801,21 @@ test_expect_success 'colors can be overridden' ' +new more-context +another-one - (1/1) Stage this hunk [y,n,q,a,d,s,e,p,?]? Split into 2 hunks. + (1/1) Stage this hunk [y,n,q,a,d,s,e,p,|,?]? Split into 2 hunks. @@ -1,3 +1,3 @@ context -old +new more-context - (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? @@ -3 +3,2 @@ + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,|,?]? @@ -3 +3,2 @@ more-context +another-one - (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? @@ -1,3 +1,3 @@ + (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,|,?]? @@ -1,3 +1,3 @@ context -old +new more-context - (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,|,?]? EOF test_cmp expect actual '