From patchwork Mon Jul 15 20:20:58 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: 13733865 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) (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 51FF7282FD for ; Mon, 15 Jul 2024 20:21:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721074863; cv=none; b=L0znRYD6M61NZNQG12NnMh7XxM4vivT78lLaNuTVmm/iaqmaJYta+NLcXwxtnr3K75xDawdly5LI+0Mf9ktKvKXVYxVyl45MqRW+C/ngbKtVs1QnV91g4Ud5kGNYVMzr3mnYpsvfBzbKTxHpJ9Y/n40832Sk8e8ZXqyVWiIHghY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721074863; c=relaxed/simple; bh=gAgdXh/gkFK7hVREhySVsjCZFaZgrSOv80xc1Ytvgi4=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=YCR+a6Vr32iZudGwVYq6ezo9mxqIBTpoV6a/AT6i7Cc5BmJFrPR/6MCKE1Auq88nUKj5HdEBIvVOJEaRFmsfZoDnUScWAMrN6MrhfSUx4uwYvBCVGjcNyk0MicfvffHsFHRPv5mY2fmbMxIpeIisXAYIl8XOS8ZGAZTDNdQk7dk= 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=UsEbZ2C6; arc=none smtp.client-ip=209.85.215.172 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="UsEbZ2C6" Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7611b6a617cso2902231a12.3 for ; Mon, 15 Jul 2024 13:21:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721074861; x=1721679661; 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=5mL8r9nAHoACS7bCrLv1OQbKk0PuFB71iOuVFQjke1s=; b=UsEbZ2C69tZd9APX8+RtegZdjwhTYZlRBe6Itocm4YdtEFcSug9Eu5neHVpvNhallH XVJh6a7obFaUd7W6u97rapc99OhOpRQyDOQL4JXbKUvIE8Ju1lwwpy7Q/AqwKTXCAf+Z GXct/Z8KrFW7tfqKNjI9e/qhIYoKLY9lQ3A4tDJASUzFvBhsSRhYNaSh6gWPo3YDJIDd Oy1hsGdueCOhCBFzOXQPGCilk2VjAaC5Nx4d6GCkH827myLejdxsQDjXsGMiuWaITqId o9OaTGLODwjLQLeFtMKFtLNVartaAcLjmm50+0MHFIUEpYsc1WCHWA60sqewm8w+qSai vl9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721074861; x=1721679661; 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=5mL8r9nAHoACS7bCrLv1OQbKk0PuFB71iOuVFQjke1s=; b=s/PsL/YrVTyl39Xf8yubmCkx4KWje8PRSCDBeDk8atHU/p9ehQobqE5GRfgHPhQtuJ tVFwJsOa7ZvdMrtj9YgJEmfFZxXDk3m1xmhImevaXX5XJc4B1S4FDr2L12M/oKUHn2/L bIqVsTVHD3aMYLRdp75aixFpBozzW/svCHArzumTxLwQfPASud+/FXsBDAI0KbyMOtuf fJ3BlfHbpdUnlyWrttTtlL9gDqG7hVT3k+zd5LWpoTy5JrC5OlTvxIr2T3C3VO45umYa QqY6zZhhFWLtk7nDzstYlLpCeIXamtKlhnuKfoxawWjH9LdQ4knphtouRHWOz7kUnkiL YXyQ== X-Gm-Message-State: AOJu0YxGJ8Iyfer+QhW/axrNSwEfysIOcoM2IGZnOcoXFouNlRZUvAiI dJtn3mX2FVetR9JVmkq7snUH3uq2DrvhsTdnGmyMKRgb/tm69rJMnA+8FA== X-Google-Smtp-Source: AGHT+IFT4MBvKlThp7dEqeCRmp8eiK70wqmGa70nQnzPSoQahUZtr9dMPVO7GXEJxnj7jSGjAMTAwQ== X-Received: by 2002:a05:6a20:748a:b0:1be:c71c:ff26 with SMTP id adf61e73a8af0-1c3f1275fefmr22903637.40.1721074861431; Mon, 15 Jul 2024 13:21:01 -0700 (PDT) Received: from gmail.com (p4453252-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.169.252]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70b7eca75a6sm4782735b3a.164.2024.07.15.13.20.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 15 Jul 2024 13:21:00 -0700 (PDT) Message-ID: <6b21ace7-2abe-4ec0-9a34-09ec45599575@gmail.com> Date: Tue, 16 Jul 2024 05:20:58 +0900 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/4] 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 , Phillip Wood References: <2653fb37-c8a8-49b1-a804-4be6654a2cad@gmail.com> Content-Language: en-US In-Reply-To: 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 5d78868ac1..6daf3a6be0 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -575,6 +575,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 Jul 15 20:21:23 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: 13733866 Received: from mail-oa1-f43.google.com (mail-oa1-f43.google.com [209.85.160.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 9D555282FD for ; Mon, 15 Jul 2024 20:21:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721074889; cv=none; b=S4vv5/o1K+0y2UaiHUAYkfJvJ2L4+urP85GtPs8iVgf60ehIFAHIfvTEo64oMxrBL037UR0CSPGrAmzTZIdKmALuI1vFkMavasPoFNK/hSp6ilwebQm1W+V+rWK2woxZQsrNltmsdD6kywN+P3pH0HwkqhmphHr6rWHzCZXbvEk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721074889; c=relaxed/simple; bh=u32LQG4dN9nfkFEuddyIXoN/3mhVI6NWOOXZDTojG8U=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=MN2JO90I+TCjguqY/XPYXuQlxI4yh2m/AYy9k/L8AWjmh2NNgDIC1x11fB3Q24iZjT/FwJCoZS4vfAkvVQbpzyVJKm8JVDu2YKWWOYYp6PHWy/7DUe1HFD8IcypgwsqVGZOZ2daFAWYIGQWTUpHxMkT1aepLHizDst3q/ik0BYk= 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=HqAPQusb; arc=none smtp.client-ip=209.85.160.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="HqAPQusb" Received: by mail-oa1-f43.google.com with SMTP id 586e51a60fabf-25e1610e359so2200232fac.1 for ; Mon, 15 Jul 2024 13:21:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721074886; x=1721679686; 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=W8/Ekc3sWlNmwWSqhq/+qCoXGlhRecvSYcC1v2Zj1Xc=; b=HqAPQusbOjHTnYpfISreU5IhzTmgbAyEC0a8LMTzskKPO7riXVFeXtz+XQJPxGJKlL 4r/xCfJpTIloS8mdOnXUx40uM+tJo8FtYtgeUJPvjLyvjvzc/RZ4tWKyen+zbvsnqx/R DFAXoIw1hhVo+p3hOiWgx2bxjWoEI+Ta54Apy1FgcXshQo6vbuyuXGpQTGu2XA5/EAUs LAp4IsPaMN/1RO0pQnp3aAHud+SmSDcjGOrLvA0fzAMXPF9PA1wByZGXnSwIxK0QKIeo Xke+1gWuTjYxx4vWzBzMxuG4e1uxocZBuiliC9XYkDp3Nr5hKDa+VLQl1dbEgXqzNRS3 P9Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721074886; x=1721679686; 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=W8/Ekc3sWlNmwWSqhq/+qCoXGlhRecvSYcC1v2Zj1Xc=; b=a+tHbb8Wvq/bxWG0oq/E2yhkXFNhty7MztvAnNwGFlg0h9RJDJR4BrRwQOJTF/jsdv zENe5wn6cFF8CTUVFZRhyaf6sKGzF/bTwzKLFN9x1Lo0Y/vvMYmOUpk2zrx6+Dh43jlG Rz9q3O4itViQ50OXyyzJfjL7xJSh427Xp+wrrCOwVMoMMGuuyWF2ABZU59EUxqzC29n+ uVN6M2lM/EV3UAtB2tUppYHF+z0xZ0JCaYvu5wERmtA6JFy2qx5mQvZg1ldx2vhbbFcv VMskoVGvJHnmKt/iJ22k9D01IP6sCCu9M2jUJ3oC0SheDhrprJXGFxJ2FGUlNPPpReK1 xSdA== X-Gm-Message-State: AOJu0YwY7IJCJXzEUChtF0jheGdJCGre4DM7LE3UBQRpe3TMTMqiDRz1 xgrd9oO7u/U5WXPupgOpuPaahOzY0zT+bQKO+THR5uIdMQWVa6ht0qvZdQ== X-Google-Smtp-Source: AGHT+IHVZxYmnToxNV9We3cDJbjOCo2Im+lWWysDN4Sdty8pAniyTO4sTIbJjezrbs06ljI7siOSIQ== X-Received: by 2002:a05:6871:58f:b0:258:3455:4b02 with SMTP id 586e51a60fabf-260ba82a853mr314129fac.51.1721074886540; Mon, 15 Jul 2024 13:21:26 -0700 (PDT) Received: from gmail.com (p4453252-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.169.252]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70b7ec7eebasm4797789b3a.136.2024.07.15.13.21.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 15 Jul 2024 13:21:26 -0700 (PDT) Message-ID: <5478131d-ed0c-4a0a-832f-39189db07941@gmail.com> Date: Tue, 16 Jul 2024 05:21:23 +0900 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/4] 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 , Phillip Wood References: <2653fb37-c8a8-49b1-a804-4be6654a2cad@gmail.com> Content-Language: en-US In-Reply-To: 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 be6f4ee59f..251adfc2ad 100644 --- a/pager.c +++ b/pager.c @@ -14,6 +14,7 @@ int pager_use_color = 1; static struct child_process pager_process; static 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 Jul 15 20:21:53 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: 13733867 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (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 B7913282FD for ; Mon, 15 Jul 2024 20:21:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721074919; cv=none; b=FzHZo6ajufU/iv1wcttEQcNpuF1GDgKqbG2Y/4ioHUKA/PiGZ73MIFbhzxafDyKesnyMIn1TOV4mwcp4UEJTzmloYo+zkn6+GhxkxNYhH0JDkCmXOL8fJh/nwOAJbd2yLQDHqIgpX+O/JBLOE4t0EaKo101ySRdn2i0AOnXzsfA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721074919; c=relaxed/simple; bh=eybTNNn6GOol/ZFna1blhl6Xq7kTJBAXEbY6ShWv2Bo=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=k1CEK6VT9B06a4kJ+r4zvt6ZxL7LysEEfJkrhxbvVNBnn8kQpRTqXam0i5VRthHgM1gpE7nqZQqzwltt65I4lNEfn/LoI0og1pe6+Ekwp9CkFTqCrHZoUVNnGuyFe2bNtR8gsPPraQcrk5rBz8lWdP84KGHtXOQl0Z/J6SiKn7g= 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=dJadHj9q; arc=none smtp.client-ip=209.85.215.174 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="dJadHj9q" Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-75c3afd7a50so2959128a12.2 for ; Mon, 15 Jul 2024 13:21:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721074917; x=1721679717; 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=jP4uRA8QRVuvZpKig1R/AhI0LJi2scYLcSpl9be2erA=; b=dJadHj9qECN9eHTz+88vLKbvEyD7YVJIh81UQ+qLZD2pToqTDPTOjGsEaoGUDPUpZE xrxxlUCEzSYUFRI9cRYGMdFfEgVzOgZth+sikkLFuyNWysz0088I670/SYe+v/GQYDwL jxh9jRSLEFDSDZcsBfMcnTiGnCUMfUEBL6tIU9/cn+qwdCjE4X+CelRyxBZPy634v+I+ lvekK8F4nNPyGBT1CioGcmU2OdFiXKrNvWW8v7ZVocvVXmObL8vPFCuULCEjTnz+Gy6Y eYrse9C/k5HDt5oUMKmRCDIYShPGhXkIaX2rwdBht05+MUy4klxHclZh+LA5dgChMGwk wx5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721074917; x=1721679717; 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=jP4uRA8QRVuvZpKig1R/AhI0LJi2scYLcSpl9be2erA=; b=IdEfrq/gOrYLNIsEvbQBAIFC8PkH1MhbSAOEaDtPd0+qSGG16tFj9H5r0h8kIKt9cd cWCkLM9yiAvapYT5gkNNWrP9+jN6GJ/DvVBSelFm8NaWDpES7VOqWSd1kKKnG6ERm29i nxF1l7TEIAwuPfDXfw5ITrJrWPA4ySHg0kcuXEOUK8iUV952nqGX5fH7RpzRzCjk0OrH 7Tu13jDObqbUm9fLp7IWdp63+t2hjt+NBTZxHSmoCl23C6zdggEUWHRgDNWU82TxH/An KVCPkubD1X9B0uHojKWTWeFt9wcICBB9VB4l7soaFZTZbWm/NECRhQIR99QKiCCMXJXs v1MQ== X-Gm-Message-State: AOJu0YxDNo6yU8r7olPI7OCCWEkFf8BBKXv+7T68N22i5fMFK4wd+lgn gdigPmUjxEJOPNfzMXu/T4WATqmpkBYG/fGqqDgh2QDnZCF76tFaQpOVHg== X-Google-Smtp-Source: AGHT+IH/5nyX4o0OWpmvb5SaaS1VnPIl/KPeJHcIjfa+pZJDWzpSPWrhCDgoPhL6UOznABK2Gb/kXw== X-Received: by 2002:a05:6a20:43a2:b0:1c2:905c:db0 with SMTP id adf61e73a8af0-1c3f1242119mr45949637.33.1721074916915; Mon, 15 Jul 2024 13:21:56 -0700 (PDT) Received: from gmail.com (p4453252-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.169.252]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fc0bc382dbsm44971435ad.197.2024.07.15.13.21.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 15 Jul 2024 13:21:56 -0700 (PDT) Message-ID: Date: Tue, 16 Jul 2024 05:21:53 +0900 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/4] pager: introduce wait_for_pager From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , Dragan Simic , Jeff King , Phillip Wood References: <2653fb37-c8a8-49b1-a804-4be6654a2cad@gmail.com> Content-Language: en-US In-Reply-To: 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 | 43 +++++++++++++++++++++++++++++++++++++------ pager.h | 1 + 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/pager.c b/pager.c index 251adfc2ad..bea4345f6f 100644 --- a/pager.c +++ b/pager.c @@ -14,7 +14,7 @@ int pager_use_color = 1; static struct child_process pager_process; static 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,11 +24,11 @@ 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) +static void finish_pager(void) { fflush(stdout); fflush(stderr); @@ -36,8 +36,34 @@ static void wait_for_pager_atexit(void) finish_command(&pager_process); } +static void wait_for_pager_atexit(void) +{ + if (old_fd1 == -1) + return; + + finish_pager(); +} + +void wait_for_pager(void) +{ + finish_pager(); + sigchain_pop_common(); + unsetenv("GIT_PAGER_IN_USE"); + dup2(old_fd1, 1); + close(old_fd1); + old_fd1 = -1; + if (old_fd2 != -1) { + dup2(old_fd2, 2); + close(old_fd2); + 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 +139,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 +169,20 @@ void setup_pager(void) die("unable to execute pager '%s'", pager); /* 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) { + once++; + 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 Jul 15 20:22:14 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: 13733868 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 54637282FD for ; Mon, 15 Jul 2024 20:22:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721074939; cv=none; b=kPCs47RHtPboC5+vVwGhwcRBpgZ2UK6OXIiapGGcExDAtMPZG/WB2wzX+pbmPZ76/V4HckemMDL8diRpfWlxsiNiNI6nSUXdfWEQuLY6hcHSlJvXblLmemP0rfY7k/C8KoAtC47XsFHpr+ohPAaQgmITD+lgJSge9AkEaAJEZns= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721074939; c=relaxed/simple; bh=3O1ErOTE2plHq5W6G1NNhtwDA8QxC50/Tox6k4dIpHE=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=nw+99eazlUY3oE+GXpmmCfgek+AUApQJYl7OrpJIN+igA94QIqfKuqPdKjZg/tiAZIUrPsyzJGj5/eWYMSNeyGO98b9lmjd29Ls+fzRwp4uGZgIw+stHetGJv6zIq5XhUus5YSSI08AYpvEbtsAdLo/3b9jFERaJu1uLaXgfQj4= 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=csdX/jYq; arc=none smtp.client-ip=209.85.214.174 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="csdX/jYq" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1fb0d7e4ee9so31330835ad.3 for ; Mon, 15 Jul 2024 13:22:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721074938; x=1721679738; 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=GvPAK4REs1Px4s/o8a09L1Zr+qkB4/XRs6ylZVJnksI=; b=csdX/jYqIZRxEl05DxR8hmK4CZpfxvDQsQHZHZRUok9jVjcJNDM3S+CMo/tZsjL7hr de0lVcS5K8HHA+iE7Dx3+TA0TTM9Y/7mZSM61zVgj11YDKi4uj+AVHubo5k6yKqAhzd0 bTA9zu7WJ7TssvP2uGVY+xugrqVWd3hp0sMtGePuqXuJMYq6hIEjU0qk8idwz3dvAV/5 DzFQ92wDhNKNSHoofynuHQR9zC27BKGLpwx5AeDNziyQvEsNFtih1lCofoNXB8pexjX5 u0i6VAsPOoEWLAEOt0vgLMuPLPoULIlFw3ED3BL6aqWX5inXYsHCUICNKrDuCp/do+Bm zwtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721074938; x=1721679738; 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=GvPAK4REs1Px4s/o8a09L1Zr+qkB4/XRs6ylZVJnksI=; b=pdGZeJgvODDeNlq+OIRLFVKS6PR128zjxoGtxjZ9cqhjxdyUmpcA8utnnf7PZ1vcct YnzZ6FWALwJwbyrCfpGys/hvPNu4pPlOW3QM6m3E68zXN5oZF8FHfPWrwKsd/6q+gx5/ sRDeGaTmwrZW2Zz6fJLdUkBKVde8I/UmHczm8skdxIPE3nb+o7OVvoPvAaVbctkjZy8n JpAnZVFzyLIOAaSF2wvvbsEBNnYEfS0JXjebJ7sR0n/oQeWRyFUImwPiPmRnmimuCw7Z W66qYtLoifGw/Ij9p8w4mB2Jn79lJH3uN633ndfdP0VAFTICFgQi3+O2UHlf19+3JPD2 lylQ== X-Gm-Message-State: AOJu0Yzc93CzffqEi8p6SS4zRiViCJoGM9L1uQPfaBQ9LiUP2hA7Qpb0 tuQslt0Wgdh/oNIVBg8QFOreZpC75Kx+jRlz2MtDOUjhv4D8SA9v2Pj3Zw== X-Google-Smtp-Source: AGHT+IHIN3oy/tpPqaJQ4DuXMCw90GSw6HOdqEYbnroHIEhTpXxGHR25+VD+mJJOMlct61Dytsnx3w== X-Received: by 2002:a17:902:cf03:b0:1f9:c289:737c with SMTP id d9443c01a7336-1fbb6f0a0eemr169949795ad.60.1721074937501; Mon, 15 Jul 2024 13:22:17 -0700 (PDT) Received: from gmail.com (p4453252-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.169.252]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fc0bb6fb1asm44936785ad.23.2024.07.15.13.22.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 15 Jul 2024 13:22:17 -0700 (PDT) Message-ID: <9ad2200b-46b2-40b8-abb6-5bc0c1d1684a@gmail.com> Date: Tue, 16 Jul 2024 05:22:14 +0900 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/4] add-patch: render hunks through the pager From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , Dragan Simic , Jeff King , Phillip Wood References: <2653fb37-c8a8-49b1-a804-4be6654a2cad@gmail.com> Content-Language: en-US In-Reply-To: Make the print command trigger the pager when invoked using a capital 'P', to make it easier for the user to review long hunks. Note that if the PAGER ends unexpectedly before we've been able to send the payload, perhaps because the user is not interested in the whole thing, we might receive a SIGPIPE, which would abruptly and unexpectedly terminate the interactive session for the user. Therefore, we need to ignore a possible SIGPIPE signal. Add a test for this, in addition to the test for normal operation. For the SIGPIPE test, we need to make sure that we completely fill the operating system's buffer, otherwise we might not trigger the SIGPIPE signal. The normal size of this buffer in different OSs varies from a few KBs to 1MB. Use a payload large enough to guarantee that we exceed this limit. Signed-off-by: Rubén Justo --- add-patch.c | 18 +++++++++++++++--- t/t3701-add-interactive.sh | 28 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/add-patch.c b/add-patch.c index 6e176cd21a..f2c76b7d83 100644 --- a/add-patch.c +++ b/add-patch.c @@ -7,9 +7,11 @@ #include "environment.h" #include "gettext.h" #include "object-name.h" +#include "pager.h" #include "read-cache-ll.h" #include "repository.h" #include "strbuf.h" +#include "sigchain.h" #include "run-command.h" #include "strvec.h" #include "pathspec.h" @@ -1391,7 +1393,7 @@ N_("j - leave this hunk undecided, see next undecided hunk\n" "/ - search for a hunk matching the given regex\n" "s - split the current hunk into smaller hunks\n" "e - manually edit the current hunk\n" - "p - print the current hunk\n" + "p - print the current hunk, 'P' to use the pager\n" "? - print help\n"); static int patch_update_file(struct add_p_state *s, @@ -1402,7 +1404,7 @@ static int patch_update_file(struct add_p_state *s, struct hunk *hunk; char ch; struct child_process cp = CHILD_PROCESS_INIT; - int colored = !!s->colored.len, quit = 0; + int colored = !!s->colored.len, quit = 0, use_pager = 0; enum prompt_mode_type prompt_mode_type; enum { ALLOW_GOTO_PREVIOUS_HUNK = 1 << 0, @@ -1452,9 +1454,18 @@ 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 (use_pager) { + setup_pager(); + sigchain_push(SIGPIPE, SIG_IGN); + } render_hunk(s, hunk, 0, colored, &s->buf); fputs(s->buf.buf, stdout); rendered_hunk_index = hunk_index; + if (use_pager) { + sigchain_pop(SIGPIPE); + wait_for_pager(); + use_pager = 0; + } } strbuf_reset(&s->buf); @@ -1675,8 +1686,9 @@ static int patch_update_file(struct add_p_state *s, hunk->use = USE_HUNK; goto soft_increment; } - } else if (s->answer.buf[0] == 'p') { + } else if (ch == 'p') { rendered_hunk_index = -1; + use_pager = (s->answer.buf[0] == 'P') ? 1 : 0; } 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 6daf3a6be0..c60589cb94 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -591,6 +591,34 @@ test_expect_success 'print again the hunk' ' 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 P | + ( + GIT_PAGER="sed s/^/PAGER\ /" && + export GIT_PAGER && + test_terminal git add -p >actual + ) && + tail -n 7 actual.trimmed && + test_cmp expect actual.trimmed +' + +test_expect_success TTY 'P handles SIGPIPE when writing to pager' ' + test_when_finished "rm -f huge_file; git reset" && + printf "\n%2500000s" Y >huge_file && + git add -N huge_file && + test_write_lines P q | GIT_PAGER="head -n 1" test_terminal git add -p +' + test_expect_success 'split hunk "add -p (edit)"' ' # Split, say Edit and do nothing. Then: #