git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "John Cai via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org, John Cai <johncai86@gmail.com>
Subject: Re: [PATCH 1/2] diff: use HEAD for attributes when using bare repository
Date: Tue, 14 Mar 2023 10:43:49 -0700	[thread overview]
Message-ID: <xmqqa60fqld6.fsf@gitster.g> (raw)
In-Reply-To: <xmqqttynqnnj.fsf@gitster.g> (Junio C. Hamano's message of "Tue, 14 Mar 2023 09:54:24 -0700")

Junio C Hamano <gitster@pobox.com> writes:

> Since referring to in-tree attributes is often useful with any
> command, not limited to "diff", I wonder if it is feasible to add
> support for the --attr-source=<tree> option globally, instead of
> implementing such an option piecemeal.  If your "git diff" in a bare
> repository starts honoring attributes recorded in HEAD, don't your
> users expect your "git log" and "git show" to also honor them?

Just for illustration, here is one way to do so.

The implementation goes in the opposite direction from the more
recent trend, which is why I am not making it an official patch, but
with this you can do things like:

  $ git --attr-source=e83c5163 check-attr whitespace cache.h
  cache.h: whitespace: unspecified
  $ git --attr-source=e2f6331a142^ check-attr whitespace cache.h
  cache.h: whitespace: set
  $ git --attr-source=HEAD check-attr whitespace cache.h
  cache.h: whitespace: indent,trail,space

where e83c5163 is the very first version of Git from 2005 where
.gitattributes did not exist, e2f6331a142^ is the last version
before we set whitespace to indent,trail,space, and HEAD is a more
recent version of our source tree.

In the following illustration patch, the attr-source tree object
name is kept as a string as long as possible and at the very last
minute when we call git_check_attr() for the first time we turn it
into an object id.  This is because at the very early stage when we
parse the global option we may not even know where our repository is
(hence do not have enough information to parse HEAD).  We also figure
out is_bare_repository() late in the process for the same reason.



 attr.c | 29 +++++++++++++++++++++++++++++
 attr.h |  6 ++++++
 git.c  |  3 +++
 3 files changed, 38 insertions(+)

diff --git c/attr.c w/attr.c
index 1053dfcd4b..2fd6e0eab2 100644
--- c/attr.c
+++ w/attr.c
@@ -1165,12 +1165,41 @@ static void collect_some_attrs(struct index_state *istate,
 	fill(path, pathlen, basename_offset, check->stack, check->all_attrs, rem);
 }
 
+static const char *default_attr_source_tree_object_name;
+
+void set_git_attr_source(const char *tree_object_name)
+{
+	default_attr_source_tree_object_name = xstrdup(tree_object_name);
+}
+
+
+static struct object_id *default_attr_source(void)
+{
+	static struct object_id attr_source;
+
+	if (is_null_oid(&attr_source)) {
+		if (!default_attr_source_tree_object_name &&
+		    is_bare_repository())
+			default_attr_source_tree_object_name = "HEAD";
+
+		if (default_attr_source_tree_object_name &&
+		    is_null_oid(&attr_source))
+			get_oid(default_attr_source_tree_object_name, &attr_source);
+	}
+	if (is_null_oid(&attr_source))
+		return NULL;
+	return &attr_source;
+}
+
 void git_check_attr(struct index_state *istate,
 		    const struct object_id *tree_oid, const char *path,
 		    struct attr_check *check)
 {
 	int i;
 
+	if (!tree_oid)
+		tree_oid = default_attr_source();
+
 	collect_some_attrs(istate, tree_oid, path, check);
 
 	for (i = 0; i < check->nr; i++) {
diff --git c/attr.h w/attr.h
index 9884ea2bc6..a77e3713b2 100644
--- c/attr.h
+++ w/attr.h
@@ -135,6 +135,12 @@ struct git_attr;
 struct all_attrs_item;
 struct attr_stack;
 
+/*
+ * The textual object name for the tree-ish used by git_check_attr()
+ * when NULL is given to tree_oid.
+ */
+void set_git_attr_source(const char *);
+
 /*
  * Given a string, return the gitattribute object that
  * corresponds to it.
diff --git c/git.c w/git.c
index 6171fd6769..21bddc5718 100644
--- c/git.c
+++ w/git.c
@@ -4,6 +4,7 @@
 #include "help.h"
 #include "run-command.h"
 #include "alias.h"
+#include "attr.h"
 #include "shallow.h"
 
 #define RUN_SETUP		(1<<0)
@@ -307,6 +308,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
 			} else {
 				exit(list_cmds(cmd));
 			}
+		} else if (skip_prefix(cmd, "--attr-source=", &cmd)) {
+			set_git_attr_source(cmd);
 		} else {
 			fprintf(stderr, _("unknown option: %s\n"), cmd);
 			usage(git_usage_string);

  reply	other threads:[~2023-03-14 17:44 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-14  1:53 [PATCH 0/2] diff: support bare repositories when reading gitattributes for diff algorithm John Cai via GitGitGadget
2023-03-14  1:53 ` [PATCH 1/2] diff: use HEAD for attributes when using bare repository John Cai via GitGitGadget
2023-03-14 16:54   ` Junio C Hamano
2023-03-14 17:43     ` Junio C Hamano [this message]
2023-03-14 19:38       ` John Cai
2023-03-14 20:37         ` Junio C Hamano
2023-03-16 16:46           ` John Cai
2023-03-16 22:56             ` Junio C Hamano
2023-03-17 14:11               ` John Cai
2023-03-14  1:53 ` [PATCH 2/2] diff: add --attr-source to read gitattributes from a commit John Cai via GitGitGadget
2023-03-14 17:21 ` [PATCH 0/2] diff: support bare repositories when reading gitattributes for diff algorithm Philippe Blain
2023-03-14 19:18   ` John Cai
2023-03-14 20:44     ` Junio C Hamano
2023-03-16 14:29       ` Jeff King
2023-03-16 16:44         ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqa60fqld6.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=johncai86@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).