From patchwork Thu Sep 10 07:32:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denton Liu X-Patchwork-Id: 11766921 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 39F5313B1 for ; Thu, 10 Sep 2020 07:33:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 17B8C207EA for ; Thu, 10 Sep 2020 07:33:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KTKd1doC" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730021AbgIJHdF (ORCPT ); Thu, 10 Sep 2020 03:33:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726932AbgIJHcn (ORCPT ); Thu, 10 Sep 2020 03:32:43 -0400 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19E6DC061756 for ; Thu, 10 Sep 2020 00:32:43 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id o20so4110276pfp.11 for ; Thu, 10 Sep 2020 00:32:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=y6rcRpyp6oEtNjMcJ5TzPBWNEijRXeIaMMkmND3iSGo=; b=KTKd1doCeACEtINOh91wmIwGs32V9IRgNhTw7nwNcjt7UGSghwwsSFMMaSRAttZjP5 tdFJW8Aju9Gwkhn9e+ku2BXBRWdleqEx61PANS82+ntO+QfybZ5Xa8VfY/fUltJMi6hp 6M5JWYw+RXod9SI5fwROW/naUlLrkhap04fLrp8L5LnHeRfpFEOR1OCrqCAXiYBtFmJl FGHRVKaYLu/SRdL6F6rYVjoasS3Shejr2cBC4/XvWNVx8YWwAZa4AyRKGavg35E1u8WZ k5p41hpZnsmPyFe1xMlU2Ceo9sGOAX0qfxq7GBA/B+WkwHxPjBlIhNF8ufXBkNM0uXu0 Z7sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=y6rcRpyp6oEtNjMcJ5TzPBWNEijRXeIaMMkmND3iSGo=; b=QQQVBKVBP8TpODpZ37i9am1U5w0zVtC6rX9kJMJSR4QJzqOI2rOhIrxgRr+9Ct7pvy D04quPspGCdtC7QHuoojCV8elaeXElVmNazdp7uGHmB/5NNX7gmDMQrrMvfd57WWDTym tj152/kz7v3rOGG5vnkZbweWEOrjRD2AG8JhkHVyYZI6gaA3/Li/MPCt0jOX8+jh5dRq W5BViG03jXCicnFoxMWk6tu59DLH24SjYcuwYMy75L64MEv7U8Sz57qfhJE3NSCo33Tz yCcXIcvd9AXgMlmOMhOaPdlOouT7hd2xRnstENOZvgvPPL1I6oSrXlVoJhElNQ01XeE5 NYuQ== X-Gm-Message-State: AOAM533nvgjVeCEN/8GAUSwBXG/UBBq9kNkn/TxJHxX5dagD4UhN33D+ gTKgLiX8GhLgoz+nrGSfjpCxi9J8U38= X-Google-Smtp-Source: ABdhPJydQ+3f84fFACIHtjgsxMZiXTTxN++nR9X0xWSCKlYUi4jRf68uOq2l9kFV1wMoRZe5sIGbjg== X-Received: by 2002:a62:7c82:0:b029:13c:1611:6525 with SMTP id x124-20020a627c820000b029013c16116525mr4366954pfc.5.1599723162420; Thu, 10 Sep 2020 00:32:42 -0700 (PDT) Received: from archbookpro.hsd1.ca.comcast.net ([2601:647:5900:d670::1ab5]) by smtp.gmail.com with ESMTPSA id q127sm4990550pfb.61.2020.09.10.00.32.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 00:32:41 -0700 (PDT) From: Denton Liu To: Git Mailing List Cc: Junio C Hamano Subject: [PATCH v2 1/4] t4068: remove unnecessary >tmp Date: Thu, 10 Sep 2020 00:32:17 -0700 Message-Id: <80e9066a59524ededbb0d0191a93e6e3154c5623.1599723087.git.liu.denton@gmail.com> X-Mailer: git-send-email 2.28.0.618.gf4bc123cb7 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The many `git diff` invocations have a `>tmp` redirection even though the file is not being used afterwards. Remove these unnecessary redirections. Signed-off-by: Denton Liu --- t/t4068-diff-symmetric.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/t/t4068-diff-symmetric.sh b/t/t4068-diff-symmetric.sh index 31d17a5af0..60c506c2b2 100755 --- a/t/t4068-diff-symmetric.sh +++ b/t/t4068-diff-symmetric.sh @@ -64,27 +64,27 @@ test_expect_success 'diff with two merge bases' ' ' test_expect_success 'diff with no merge bases' ' - test_must_fail git diff br2...br3 >tmp 2>err && + test_must_fail git diff br2...br3 2>err && test_i18ngrep "fatal: br2...br3: no merge base" err ' test_expect_success 'diff with too many symmetric differences' ' - test_must_fail git diff br1...master br2...br3 >tmp 2>err && + test_must_fail git diff br1...master br2...br3 2>err && test_i18ngrep "usage" err ' test_expect_success 'diff with symmetric difference and extraneous arg' ' - test_must_fail git diff master br1...master >tmp 2>err && + test_must_fail git diff master br1...master 2>err && test_i18ngrep "usage" err ' test_expect_success 'diff with two ranges' ' - test_must_fail git diff master br1..master br2..br3 >tmp 2>err && + test_must_fail git diff master br1..master br2..br3 2>err && test_i18ngrep "usage" err ' test_expect_success 'diff with ranges and extra arg' ' - test_must_fail git diff master br1..master commit-D >tmp 2>err && + test_must_fail git diff master br1..master commit-D 2>err && test_i18ngrep "usage" err ' From patchwork Thu Sep 10 07:32:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denton Liu X-Patchwork-Id: 11766923 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AA78414F6 for ; Thu, 10 Sep 2020 07:33:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 856AC207EA for ; Thu, 10 Sep 2020 07:33:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="pBYu3AY2" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730104AbgIJHdI (ORCPT ); Thu, 10 Sep 2020 03:33:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727770AbgIJHco (ORCPT ); Thu, 10 Sep 2020 03:32:44 -0400 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80B65C061757 for ; Thu, 10 Sep 2020 00:32:44 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id k15so4110605pfc.12 for ; Thu, 10 Sep 2020 00:32:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rupAiabRDgtPF1NJ4d/pjYnNSXyE4EjbhLrxZBEG168=; b=pBYu3AY2UQFcZMwFhiIl0yfPLd7FHXNXejZMqdjD6HM31NqcS3IAOT7wT/jyU6z1SO 9sAn8B+31IK0Z4lnvbAz3xg/dfvr2j28T+QpW2BgAe+9vMH3VjvNuyMA8xECt07JImWA r3OGnQdIJL2THGvDMGGUgolfZshMKFpbVe+wX85Sir/+BY8ZG1E9GFXyrLGK/nToCb4L 1ZWibw2otgh2VjpgrOt2a7ubE19E6l7AbAC0iWZoRrsUClRmUoNMWIcC9LdUg65aeT0i rHqwm58SN67GHVG6SPhm8zKU1Rd3uOerQYaI8cxMfgr+YAMCW6IRQx5ZklFZcEiicoFt zTmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rupAiabRDgtPF1NJ4d/pjYnNSXyE4EjbhLrxZBEG168=; b=MUulbIiUgHQrCTYuq2pkZWdi1mJlrV1ExpgNOFd85COLzyrXeioi4fMLexfebCSQ0K B/7Y6TquUUPZQ89kpHDPS2Zk6ZHvULlut20ubLiqiN8LjDCyB/9QbBvenaUt5r6Y1jR1 N6iQveH45/g/H6INXSooMmLnivWWdi0rmYjVoF0EAbae0FwEF8v/R/hfrG+8iB6J7f8E 4rJdJlzFDbZ2D9Kv8BP6Op0Tz52FaLuvndiDT3pNBlprCkNCSD4JRqoKABeyszmMvsmz EQp4j3+ubVkQLf/gzRUC5RrVd0rZtamfPxiM9Pbn6stEHpgg22OXIcQL0ShNXcKFZgfy 6hAg== X-Gm-Message-State: AOAM532IqFFAJllCVFPZArmXYx6A+i6h5gzh1PuadsODq2eDXMC8R0S6 Rs39jrljFZEAQC5aBYeEZT7jWP2lEIg= X-Google-Smtp-Source: ABdhPJzV39MXWpFKDfAi3ZN/n0tkWj6p5WrZwdesPbSLJ2eO/HmLTnC7Bxl2C5wUliFS6GphexnSpA== X-Received: by 2002:a63:4611:: with SMTP id t17mr3449103pga.225.1599723163373; Thu, 10 Sep 2020 00:32:43 -0700 (PDT) Received: from archbookpro.hsd1.ca.comcast.net ([2601:647:5900:d670::1ab5]) by smtp.gmail.com with ESMTPSA id q127sm4990550pfb.61.2020.09.10.00.32.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 00:32:42 -0700 (PDT) From: Denton Liu To: Git Mailing List Cc: Junio C Hamano Subject: [PATCH v2 2/4] git-diff.txt: backtick quote command text Date: Thu, 10 Sep 2020 00:32:18 -0700 Message-Id: <8e72bd8fea276d40729705eb8dcc63bcc9faef2c.1599723087.git.liu.denton@gmail.com> X-Mailer: git-send-email 2.28.0.618.gf4bc123cb7 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The modern way to quote commands in the documentation is to use backticks instead of double-quotes as this renders the text with the code style. Convert double-quoted command text to backtick-quoted commands. While we're at it, quote one instance of `^@`. Signed-off-by: Denton Liu --- Documentation/git-diff.txt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Documentation/git-diff.txt b/Documentation/git-diff.txt index 727f24d16e..8f7b4ed3ca 100644 --- a/Documentation/git-diff.txt +++ b/Documentation/git-diff.txt @@ -68,13 +68,13 @@ files on disk. This form is to view the results of a merge commit. The first listed must be the merge itself; the remaining two or more commits should be its parents. A convenient way to produce - the desired set of revisions is to use the {caret}@ suffix. + the desired set of revisions is to use the `^@` suffix. For instance, if `master` names a merge commit, `git diff master master^@` gives the same combined diff as `git show master`. 'git diff' [] .. [--] [...]:: - This is synonymous to the earlier form (without the "..") for + This is synonymous to the earlier form (without the `..`) for viewing the changes between two arbitrary . If on one side is omitted, it will have the same effect as using HEAD instead. @@ -83,20 +83,20 @@ files on disk. This form is to view the changes on the branch containing and up to the second , starting at a common ancestor - of both . "git diff A\...B" is equivalent to - "git diff $(git merge-base A B) B". You can omit any one + of both . `git diff A...B` is equivalent to + `git diff $(git merge-base A B) B`. You can omit any one of , which has the same effect as using HEAD instead. Just in case you are doing something exotic, it should be noted that all of the in the above description, except -in the last two forms that use ".." notations, can be any +in the last two forms that use `..` notations, can be any . For a more complete list of ways to spell , see "SPECIFYING REVISIONS" section in linkgit:gitrevisions[7]. However, "diff" is about comparing two _endpoints_, not ranges, -and the range notations (".." and -"\...") do not mean a range as defined in the +and the range notations (`..` and +`...`) do not mean a range as defined in the "SPECIFYING RANGES" section in linkgit:gitrevisions[7]. 'git diff' [] :: @@ -144,9 +144,9 @@ $ git diff HEAD <3> + <1> Changes in the working tree not yet staged for the next commit. <2> Changes between the index and your last commit; what you - would be committing if you run "git commit" without "-a" option. + would be committing if you run `git commit` without `-a` option. <3> Changes in the working tree since your last commit; what you - would be committing if you run "git commit -a" + would be committing if you run `git commit -a` Comparing with arbitrary commits:: + From patchwork Thu Sep 10 07:32:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denton Liu X-Patchwork-Id: 11766927 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A0B6213B1 for ; Thu, 10 Sep 2020 07:33:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 81306207EA for ; Thu, 10 Sep 2020 07:33:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mSlaYCZI" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730128AbgIJHdX (ORCPT ); Thu, 10 Sep 2020 03:33:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728405AbgIJHcp (ORCPT ); Thu, 10 Sep 2020 03:32:45 -0400 Received: from mail-pl1-x643.google.com (mail-pl1-x643.google.com [IPv6:2607:f8b0:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 13EF1C0613ED for ; Thu, 10 Sep 2020 00:32:45 -0700 (PDT) Received: by mail-pl1-x643.google.com with SMTP id e4so493783pln.10 for ; Thu, 10 Sep 2020 00:32:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=S/QifQe57MOrI7Xj9erN8l7NhwVR7X9un0ZWB+8/n3Q=; b=mSlaYCZIRxrEpkX85W72soK9gpahUaOEIygTOXkX2WX1U8euzBmYM6VCKg8qO1n1Wd XFCE1nS2j0FtgWbTGnVsFzKiCFhMb0I9zmOlLMYDuQmeXIiEzXzDqwTHK7lnGw8YhWZi 2X/RVy10zXqy6OnQJs8Q80xX4cWNvpeCSTNN8PHn4X81mIu1qJv7dOm/uR3mREFePxEN 4Tvd1EzlVBqeu11wgzwCj9UOmih6MJngfQosIEeeIfmNZgoGb9ghh5kk0kmBhnWPlv5D yp76TnA9YqNzNT1nfCVAYgppgnFW2aX6PwDQaSo0Ub7VtgTokKxDjGqHgtI5/izDgd+U Bn1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=S/QifQe57MOrI7Xj9erN8l7NhwVR7X9un0ZWB+8/n3Q=; b=m/esgKsGJMh0XUcS1zreUBTvHPQMLmnsk13qk1II03PPzoCaugSGpBqQmg7H5yk0JT 0oNbtjsJlzs5SVsOc/LDwR0wZy8Gl1VluJZulOUE07m2R4jvUzcWM+pzfM3BpmW2LcZG 8LAGrt6DP5Mmems0+C6IHaJdYS55KYKcMj5qt62MbpvgGEPbe1qxbawnExK9uuCh35Zw Ca3EVv7jpxS3lsXBaQ/bkzjUDGwZTChpmpxnzHNPyChjEsgWbPQli/MSfiIcE14k9tsF oq+Am/7T5Wv3+rkDfNZczZQ5Wi4j1+E8WzIMudojZbmIIuLY2FxjijwZQWqZFbhdzae3 +ApQ== X-Gm-Message-State: AOAM531U02PsAR1JmwooQbsAfBFwz6a63I0IGgs9JE7XM/Tk0MlekU+h YGUMJ/aq5yMHy1bxlHfsv0TYBE5lovU= X-Google-Smtp-Source: ABdhPJxv2ZjfHCBpo3czQh8KPfwDgS/BCis0g5Mz1bLW3e2G2ZGxQiiPkV2krf0a2IFAbCwJ3Y5ptg== X-Received: by 2002:a17:902:b105:b029:d0:cbe1:e7b1 with SMTP id q5-20020a170902b105b02900d0cbe1e7b1mr4392183plr.34.1599723164300; Thu, 10 Sep 2020 00:32:44 -0700 (PDT) Received: from archbookpro.hsd1.ca.comcast.net ([2601:647:5900:d670::1ab5]) by smtp.gmail.com with ESMTPSA id q127sm4990550pfb.61.2020.09.10.00.32.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 00:32:43 -0700 (PDT) From: Denton Liu To: Git Mailing List Cc: Junio C Hamano Subject: [PATCH v2 3/4] builtin/diff: parse --no-index using parse_options() Date: Thu, 10 Sep 2020 00:32:19 -0700 Message-Id: X-Mailer: git-send-email 2.28.0.618.gf4bc123cb7 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Instead of parsing the `--no-index` option with a plain strcmp, use parse_options() to parse options. This allows us to easily add more options in a future commit. As a result of this change, `--no-index` is removed from `argv` so, as a result, we no longer need to handle it in diff_no_index(). Signed-off-by: Denton Liu --- builtin/diff.c | 33 ++++++++++++++++++++++----------- diff-no-index.c | 15 +++------------ 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/builtin/diff.c b/builtin/diff.c index cb98811c21..0e086ed7c4 100644 --- a/builtin/diff.c +++ b/builtin/diff.c @@ -373,6 +373,13 @@ int cmd_diff(int argc, const char **argv, const char *prefix) int nongit = 0, no_index = 0; int result = 0; struct symdiff sdiff; + struct option options[] = { + OPT_SET_INT_F(0, "no-index", &no_index, + N_("compare the given paths on the filesystem"), + DIFF_NO_INDEX_EXPLICIT, + PARSE_OPT_NONEG), + OPT_END(), + }; /* * We could get N tree-ish in the rev.pending_objects list. @@ -406,21 +413,25 @@ int cmd_diff(int argc, const char **argv, const char *prefix) * Other cases are errors. */ - /* Were we asked to do --no-index explicitly? */ - for (i = 1; i < argc; i++) { - if (!strcmp(argv[i], "--")) { - i++; - break; - } - if (!strcmp(argv[i], "--no-index")) - no_index = DIFF_NO_INDEX_EXPLICIT; - if (argv[i][0] != '-') - break; - } + argc = parse_options(argc, argv, prefix, options, NULL, + PARSE_OPT_KEEP_DASHDASH | + PARSE_OPT_KEEP_ARGV0 | + PARSE_OPT_KEEP_UNKNOWN | + PARSE_OPT_NO_INTERNAL_HELP); prefix = setup_git_directory_gently(&nongit); if (!no_index) { + int i; + for (i = 1; i < argc; i++) { + if (!strcmp(argv[i], "--")) { + i++; + break; + } + if (argv[i][0] != '-') + break; + } + /* * Treat git diff with at least one path outside of the * repo the same as if the command would have been executed diff --git a/diff-no-index.c b/diff-no-index.c index 7814eabfe0..da82e2d090 100644 --- a/diff-no-index.c +++ b/diff-no-index.c @@ -243,28 +243,19 @@ int diff_no_index(struct rev_info *revs, int implicit_no_index, int argc, const char **argv) { - int i, no_index; + int i; const char *paths[2]; struct strbuf replacement = STRBUF_INIT; const char *prefix = revs->prefix; - struct option no_index_options[] = { - OPT_BOOL_F(0, "no-index", &no_index, "", - PARSE_OPT_NONEG | PARSE_OPT_HIDDEN), - OPT_END(), - }; - struct option *options; - options = parse_options_concat(no_index_options, - revs->diffopt.parseopts); - argc = parse_options(argc, argv, revs->prefix, options, + argc = parse_options(argc, argv, revs->prefix, revs->diffopt.parseopts, diff_no_index_usage, 0); if (argc != 2) { if (implicit_no_index) warning(_("Not a git repository. Use --no-index to " "compare two paths outside a working tree")); - usage_with_options(diff_no_index_usage, options); + usage_with_options(diff_no_index_usage, revs->diffopt.parseopts); } - FREE_AND_NULL(options); for (i = 0; i < 2; i++) { const char *p = argv[argc - 2 + i]; if (!strcmp(p, "-")) From patchwork Thu Sep 10 07:32:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denton Liu X-Patchwork-Id: 11766929 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B4BC1618 for ; Thu, 10 Sep 2020 07:33:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8A60C20795 for ; Thu, 10 Sep 2020 07:33:33 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JRzf35hz" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730176AbgIJHdc (ORCPT ); Thu, 10 Sep 2020 03:33:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729005AbgIJHcq (ORCPT ); Thu, 10 Sep 2020 03:32:46 -0400 Received: from mail-pl1-x643.google.com (mail-pl1-x643.google.com [IPv6:2607:f8b0:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39667C061786 for ; Thu, 10 Sep 2020 00:32:46 -0700 (PDT) Received: by mail-pl1-x643.google.com with SMTP id y6so495230plt.9 for ; Thu, 10 Sep 2020 00:32:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=5OXj3SZsQtQHk6AwZhnaJeMu8d5OYb9ooBMofSGcouA=; b=JRzf35hzFczUIw3q4BwMKtn9I7XWkjOfZJz8dSI5RhNhavwumQucsSm4sATRoGbBtK NjIdM2lZ5PPBXtGIN4ryQKvdP8TCn2Tp2OBngjr2z3ZtWsXANdkLLSoqVjPKmoXDrcke Sf2zTTTW5QdpbS5S0fMSHfYGSeu+wb98WVNYasXTXH0yKR+nfkb9ZEEAXKOJjCozRY3a cQGCntigiw/bYXbMd8JOyHVq6i8zK/51wv6LN09SV8wdt7WAgh1vXMU8MuUUVunoXous A7xubwpwtKDQUZOHxfLdv03rBERprcKaXCd3Faj+WQBPdzCcLEfbUQH9Bgl+n6KZaaas 0JxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5OXj3SZsQtQHk6AwZhnaJeMu8d5OYb9ooBMofSGcouA=; b=rbsitH5vsYWR2hz03Zq2pJR2Ck+99qQLoRee6pfldqVlNGq/FG2cQGql+Ae/hws6Xe 34lUY9iQRPGzPKsFJ09M9zVuDlLK19KP7oPxXTTbqhHW0XiTkk6JQSbMvfcLhSNlFN90 +nuyJpYWJZ4dC6U1sgV+aWtN9gSF89z0fyIm3+aG8LkJYENCF+HVSr8405c5Sw0GDGnG Vrqiv/zBxCXvogFbOHogm3yweigeq7s2HWIhGyGxbZuP+T3xTUVytWCG9tBnk+fUQEvG TPQjnvdXe4sVcF7nbRJSfz9bt1jaXaqgwmP0ezsKaIh5dvpHBlIcD7yl+ETU6gUQ9ggA MYnA== X-Gm-Message-State: AOAM533+OFeJ/TUMkArTIBsBUKPNv6DqUVghCKxES/WCAI/r5zCF+R6I KF/OTLWZqXwO5bVbB/nxOXaZP+s+Yvo= X-Google-Smtp-Source: ABdhPJyXTIt0QHi6wwxlQaVs1/45/8i4AmNzcLLMidiv3GPF5iNYRvVhLVca2ZMW9We+SMWy7bx13g== X-Received: by 2002:a17:90a:df91:: with SMTP id p17mr4224349pjv.29.1599723165357; Thu, 10 Sep 2020 00:32:45 -0700 (PDT) Received: from archbookpro.hsd1.ca.comcast.net ([2601:647:5900:d670::1ab5]) by smtp.gmail.com with ESMTPSA id q127sm4990550pfb.61.2020.09.10.00.32.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 00:32:44 -0700 (PDT) From: Denton Liu To: Git Mailing List Cc: Junio C Hamano , Jonathan Nieder Subject: [PATCH v2 4/4] builtin/diff: learn --merge-base Date: Thu, 10 Sep 2020 00:32:20 -0700 Message-Id: <4f219cf0d1df1b92d2bd49e3449a4c387056a6cd.1599723087.git.liu.denton@gmail.com> X-Mailer: git-send-email 2.28.0.618.gf4bc123cb7 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In order to get the diff between a commit and its merge base, the currently preferred method is to use `git diff A...B`. However, the range-notation with diff has, time and time again, been noted as a point of confusion and thus, it should be avoided. Although we have a substitute for the double-dot notation, we don't have any replacement for the triple-dot notation. Introduce the `--merge-base` flag as a replacement for triple-dot notation. Thus, we would be able to write the above as `git diff --merge-base A B`, allowing us to gently deprecate range-notation completely. Suggested-by: Jonathan Nieder Signed-off-by: Denton Liu --- Notes: The `--merge-base` name isn't very satisfying. If anyone has any suggestions for alternative names, please let me know. Documentation/git-diff.txt | 24 ++++++++++-- builtin/diff.c | 61 ++++++++++++++++++++++++++++- t/t4068-diff-symmetric.sh | 79 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+), 4 deletions(-) diff --git a/Documentation/git-diff.txt b/Documentation/git-diff.txt index 8f7b4ed3ca..2cab6eabe1 100644 --- a/Documentation/git-diff.txt +++ b/Documentation/git-diff.txt @@ -12,7 +12,7 @@ SYNOPSIS 'git diff' [] [] [--] [...] 'git diff' [] --cached [] [--] [...] 'git diff' [] [...] [--] [...] -'git diff' [] ... [--] [...] +'git diff' [] --merge-base [--cached] [ []] [--] [...] 'git diff' [] 'git diff' [] --no-index [--] @@ -63,6 +63,24 @@ files on disk. This is to view the changes between two arbitrary . +'git diff' [] --merge-base [--cached] [ []] [--] [...]:: + + In this form, the "before" side will be the merge base of the + two given commits. If either commit is omitted, it will default + to HEAD. ++ +In the case where two commits are given, a diff is displayed between the +merge base and the second commit. `git diff --merge-base A B` is +equivalent to `git diff $(git merge-base A B) B`. ++ +In the case where one commit is given, a diff is displayed between the +merge base of the commit and the HEAD and the working tree or the index +if `--cached` is given. `git diff --merge-base A` is equivalent to `git +diff $(git merge-base A HEAD)`. ++ +In the case where no commits are given, this form behaves identically to +as if no `--merge-base` were supplied. + 'git diff' [] ... [--] [...]:: This form is to view the results of a merge commit. The first @@ -89,8 +107,8 @@ files on disk. Just in case you are doing something exotic, it should be noted that all of the in the above description, except -in the last two forms that use `..` notations, can be any -. +in the `--merge-base` case and the last two forms that use `..` +notations, can be any . For a more complete list of ways to spell , see "SPECIFYING REVISIONS" section in linkgit:gitrevisions[7]. diff --git a/builtin/diff.c b/builtin/diff.c index 0e086ed7c4..448d2dd69a 100644 --- a/builtin/diff.c +++ b/builtin/diff.c @@ -19,6 +19,7 @@ #include "builtin.h" #include "submodule.h" #include "oid-array.h" +#include "commit-reach.h" #define DIFF_NO_INDEX_EXPLICIT 1 #define DIFF_NO_INDEX_IMPLICIT 2 @@ -27,7 +28,7 @@ static const char builtin_diff_usage[] = "git diff [] [] [--] [...]\n" " or: git diff [] --cached [] [--] [...]\n" " or: git diff [] [...] [--] [...]\n" -" or: git diff [] ...] [--] [...]\n" +" or: git diff [] --merge-base [ []] [--] [...]\n" " or: git diff [] ]\n" " or: git diff [] --no-index [--] ]\n" COMMON_DIFF_OPTIONS_HELP; @@ -371,6 +372,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix) int blobs = 0, paths = 0; struct object_array_entry *blob[2]; int nongit = 0, no_index = 0; + int merge_base = 0; int result = 0; struct symdiff sdiff; struct option options[] = { @@ -378,6 +380,8 @@ int cmd_diff(int argc, const char **argv, const char *prefix) N_("compare the given paths on the filesystem"), DIFF_NO_INDEX_EXPLICIT, PARSE_OPT_NONEG), + OPT_BOOL(0, "merge-base", &merge_base, + N_("compare with the merge base between two commits")), OPT_END(), }; @@ -457,6 +461,9 @@ int cmd_diff(int argc, const char **argv, const char *prefix) rev.diffopt.flags.allow_external = 1; rev.diffopt.flags.allow_textconv = 1; + if (no_index && merge_base) + die(_("--no-index and --merge-base are mutually exclusive")); + /* If this is a no-index diff, just run it and exit there. */ if (no_index) exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT, @@ -513,6 +520,58 @@ int cmd_diff(int argc, const char **argv, const char *prefix) } symdiff_prepare(&rev, &sdiff); + + if (merge_base && rev.pending.nr) { + int i; + struct commit *mb_child[2] = {0}; + struct commit_list *merge_bases; + int old_nr; + + for (i = 0; i < rev.pending.nr; i++) { + struct object *obj = rev.pending.objects[i].item; + if (obj->flags) + die(_("--merge-base does not work with ranges")); + if (obj->type != OBJ_COMMIT) + die(_("--merge-base only works with commits")); + } + + /* + * This check must go after the for loop above because A...B + * ranges produce three pending commits, resulting in a + * misleading error message. + */ + if (rev.pending.nr > ARRAY_SIZE(mb_child)) + die(_("--merge-base does not work with more than two commits")); + + for (i = 0; i < rev.pending.nr; i++) + mb_child[i] = lookup_commit_reference(the_repository, &rev.pending.objects[i].item->oid); + if (rev.pending.nr < ARRAY_SIZE(mb_child)) { + struct object_id oid; + + if (rev.pending.nr != 1) + BUG("unexpected rev.pending.nr: %d", rev.pending.nr); + + if (get_oid("HEAD", &oid)) + die(_("unable to get HEAD")); + + mb_child[1] = lookup_commit_reference(the_repository, &oid); + } + + merge_bases = repo_get_merge_bases(the_repository, mb_child[0], mb_child[1]); + if (!merge_bases) + die(_("no merge base found")); + if (merge_bases->next) + die(_("multiple merge bases found")); + + old_nr = rev.pending.nr; + rev.pending.nr = 1; + object_array_pop(&rev.pending); + add_object_array(&merge_bases->item->object, oid_to_hex(&merge_bases->item->object.oid), &rev.pending); + rev.pending.nr = old_nr; + + free_commit_list(merge_bases); + } + for (i = 0; i < rev.pending.nr; i++) { struct object_array_entry *entry = &rev.pending.objects[i]; struct object *obj = entry->item; diff --git a/t/t4068-diff-symmetric.sh b/t/t4068-diff-symmetric.sh index 60c506c2b2..0e43ed7660 100755 --- a/t/t4068-diff-symmetric.sh +++ b/t/t4068-diff-symmetric.sh @@ -88,4 +88,83 @@ test_expect_success 'diff with ranges and extra arg' ' test_i18ngrep "usage" err ' +test_expect_success 'diff --merge-base with two commits' ' + git diff commit-C master >expect && + git diff --merge-base br2 master >actual && + test_cmp expect actual +' + +test_expect_success 'diff --merge-base with no commits' ' + git diff --merge-base >actual && + test_must_be_empty actual +' + +test_expect_success 'diff --merge-base with one commit' ' + git checkout master && + git diff commit-C >expect && + git diff --merge-base br2 >actual && + test_cmp expect actual +' + +test_expect_success 'diff --merge-base with one commit and unstaged changes' ' + git checkout master && + test_when_finished git reset --hard && + echo unstaged >>c && + git diff commit-C >expect && + git diff --merge-base br2 >actual && + test_cmp expect actual +' + +test_expect_success 'diff --merge-base with one commit and staged and unstaged changes' ' + git checkout master && + test_when_finished git reset --hard && + echo staged >>c && + git add c && + echo unstaged >>c && + git diff commit-C >expect && + git diff --merge-base br2 >actual && + test_cmp expect actual +' + +test_expect_success 'diff --merge-base --cached with one commit and staged and unstaged changes' ' + git checkout master && + test_when_finished git reset --hard && + echo staged >>c && + git add c && + echo unstaged >>c && + git diff --cached commit-C >expect && + git diff --cached --merge-base br2 >actual && + test_cmp expect actual +' + +test_expect_success 'diff --merge-base with --no-index' ' + test_must_fail git diff --merge-base --no-index expect actual 2>err && + test_i18ngrep "fatal: --no-index and --merge-base are mutually exclusive" err +' + +test_expect_success 'diff --merge-base with range' ' + test_must_fail git diff --merge-base br2..br3 2>err && + test_i18ngrep "fatal: --merge-base does not work with ranges" err +' + +test_expect_success 'diff --merge-base non-commit' ' + test_must_fail git diff --merge-base master^{tree} 2>err && + test_i18ngrep "fatal: --merge-base only works with commits" err +' + +test_expect_success 'diff --merge-base with three commits' ' + test_must_fail git diff --merge-base br1 br2 master 2>err && + test_i18ngrep "fatal: --merge-base does not work with more than two commits" err +' + +test_expect_success 'diff --merge-base with no merge bases' ' + test_must_fail git diff --merge-base br2 br3 2>err && + test_i18ngrep "fatal: no merge base found" err +' + +test_expect_success 'diff --merge-base with multiple merge bases' ' + test_must_fail git diff --merge-base master br1 2>err && + test_i18ngrep "fatal: multiple merge bases found" err +' + test_done