git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 13/23] Allow querying all attributes on a file

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Aug 4, 2011, 04:36 UTC
Message-ID
<1312432593-9841-14-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1312432593-9841-1-git-send-email-mhagger@alum.mit.edu>

Add a function, git_all_attrs(), that reports on all attributes that are set on a path.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 Documentation/technical/api-gitattributes.txt |   46 +++++++++++++++++-------
 attr.c                                        |   28 +++++++++++++++
 attr.h                                        |    9 +++++
 3 files changed, 69 insertions(+), 14 deletions(-)
diff --git a/Documentation/technical/api-gitattributes.txt b/Documentation/technical/api-gitattributes.txt
index ab3a84d..3bd2f57 100644
--- a/Documentation/technical/api-gitattributes.txt
+++ b/Documentation/technical/api-gitattributes.txt
@@ -22,19 +22,6 @@ Data Structure
 	to `git_checkattr()` function, and receives the results.
 
 
-Calling Sequence
-----------------
-
-* Prepare an array of `struct git_attr_check` to define the list of
-  attributes you would want to check.  To populate this array, you would
-  need to define necessary attributes by calling `git_attr()` function.
-
-* Call git_checkattr() to check the attributes for the path.
-
-* Inspect `git_attr_check` structure to see how each of the attribute in
-  the array is defined for the path.
-
-
 Attribute Values
 ----------------
 
@@ -58,6 +45,19 @@ If none of the above returns true, `.value` member points at a string
 value of the attribute for the path.
 
 
+Querying Specific Attributes
+----------------------------
+
+* Prepare an array of `struct git_attr_check` to define the list of
+  attributes you would want to check.  To populate this array, you would
+  need to define necessary attributes by calling `git_attr()` function.
+
+* Call `git_checkattr()` to check the attributes for the path.
+
+* Inspect `git_attr_check` structure to see how each of the attribute in
+  the array is defined for the path.
+
+
 Example
 -------
 
@@ -109,4 +109,22 @@ static void setup_check(void)
 	}
 ------------
 
-(JC)
+
+Querying All Attributes
+-----------------------
+
+To get the values of all attributes associated with a file:
+
+* Call `git_all_attrs()`, which returns an array of `git_attr_check`
+  structures.
+
+* Iterate over the `git_attr_check` array to examine the attribute
+  names and values.  The name of the attribute described by a
+  `git_attr_check` object can be retrieved via
+  `git_attr_name(check[i].attr)`.  (Please note that no items will be
+  returned for unset attributes, so `ATTR_UNSET()` will return false
+  for all returned `git_array_check` objects.)
+
+* Free the `git_array_check` array.
+
+
diff --git a/attr.c b/attr.c
index ab30c81..658112e 100644
--- a/attr.c
+++ b/attr.c
@@ -747,6 +747,34 @@ int git_checkattr(const char *path, int num, struct git_attr_check *check)
 	return 0;
 }
 
+int git_all_attrs(const char *path, int *num, struct git_attr_check **check)
+{
+	int i, count, j;
+
+	collect_all_attrs(path);
+
+	/* Count the number of attributes that are set. */
+	count = 0;
+	for (i = 0; i < attr_nr; i++) {
+		const char *value = check_all_attr[i].value;
+		if (value != ATTR__UNSET && value != ATTR__UNKNOWN)
+			++count;
+	}
+	*num = count;
+	*check = xmalloc(sizeof(**check) * count);
+	j = 0;
+	for (i = 0; i < attr_nr; i++) {
+		const char *value = check_all_attr[i].value;
+		if (value != ATTR__UNSET && value != ATTR__UNKNOWN) {
+			(*check)[j].attr = check_all_attr[i].attr;
+			(*check)[j].value = value;
+			++j;
+		}
+	}
+
+	return 0;
+}
+
 void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
 {
 	enum git_attr_direction old = direction;
diff --git a/attr.h b/attr.h
index d4f875a..9e22893 100644
--- a/attr.h
+++ b/attr.h
@@ -38,6 +38,15 @@ char *git_attr_name(struct git_attr *);
 
 int git_checkattr(const char *path, int, struct git_attr_check *);
 
+/*
+ * Retrieve all attributes that apply to the specified path.  *num
+ * will be set the the number of attributes on the path; **check will
+ * be set to point at a newly-allocated array of git_attr_check
+ * objects describing the attributes and their values.  *check must be
+ * free()ed by the caller.
+ */
+int git_all_attrs(const char *path, int *num, struct git_attr_check **check);
+
 enum git_attr_direction {
 	GIT_ATTR_CHECKIN,
 	GIT_ATTR_CHECKOUT,
-- 
1.7.6.8.gd2879
Previous: Michael HaggertyNext: Michael Haggerty
Message 14 of 24 in “Add --all option to git-check-attr”
  1. 00/23 Add --all option to git-check-attrMichael Haggerty, Aug 4, 2011
  2. 01/23 doc: Add a link from gitattributes(5) to git-check-attr(1)Michael Haggerty, Aug 4, 2011
  3. 02/23 doc: Correct git_attr() calls in example codeMichael Haggerty, Aug 4, 2011
  4. 03/23 Remove anachronism from commentMichael Haggerty, Aug 4, 2011
  5. 04/23 Disallow the empty string as an attribute nameMichael Haggerty, Aug 4, 2011
  6. 05/23 git-check-attr: Add missing "&&"Michael Haggerty, Aug 4, 2011
  7. 06/23 git-check-attr: Add tests of command-line parsingMichael Haggerty, Aug 4, 2011
  8. 07/23 Provide access to the name attribute of git_attrMichael Haggerty, Aug 4, 2011
  9. 08/23 git-check-attr: Use git_attr_name()Michael Haggerty, Aug 4, 2011
  10. 09/23 Teach prepare_attr_stack() to figure out dirlen itselfMichael Haggerty, Aug 4, 2011
  11. 10/23 Extract a function collect_all_attrs()Michael Haggerty, Aug 4, 2011
  12. 11/23 Remove redundant call to bootstrap_attr_stack()Michael Haggerty, Aug 4, 2011
  13. 12/23 Remove redundant checkMichael Haggerty, Aug 4, 2011
  14. 13/23 Allow querying all attributes on a fileMichael Haggerty, Aug 4, 2011
  15. 14/23 git-check-attr: Extract a function output_attr()Michael Haggerty, Aug 4, 2011
  16. 15/23 git-check-attr: Introduce a new variableMichael Haggerty, Aug 4, 2011
  17. 16/23 git-check-attr: Extract a function error_with_usage()Michael Haggerty, Aug 4, 2011
  18. 17/23 git-check-attr: Handle each error separatelyMichael Haggerty, Aug 4, 2011
  19. 18/23 git-check-attr: Process command-line args more systematicallyMichael Haggerty, Aug 4, 2011
  20. 19/23 git-check-attr: Error out if no pathnames are specifiedMichael Haggerty, Aug 4, 2011
  21. 20/23 git-check-attr: Add an --all option to show all attributesMichael Haggerty, Aug 4, 2011
  22. 21/23 git-check-attr: Drive two tests using the same raw dataMichael Haggerty, Aug 4, 2011
  23. 22/23 git-check-attr: Fix command-line handling to match docsMichael Haggerty, Aug 4, 2011
  24. 23/23 Rename git_checkattr() to git_check_attr()Michael Haggerty, Aug 4, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.