user5754
user5754

Reputation: 147

Store custom metadata against individual files in a Git repository

I'm working with a Git repository and would like to define custom properties to apply to each file in the repository. These properties could then be updated on each commit. Ideally the properties should be easily searchable and eventually exportable.

Is there an easy way to do this in Git? I've looked into Git notes, but as far as I can tell that's per commit not per file.

Thanks.

Upvotes: 8

Views: 5085

Answers (2)

1615903
1615903

Reputation: 34770

You can in fact use git notes:

Adds, removes, or reads notes attached to objects, without touching the objects themselves.

Objects include single files too. As explained in this answer, you would list the files in your current HEAD with git ls-tree HEAD, and then add a note with git notes add -m "Some text, key-value pairs maybe?" <hash of the file>. To view the notes later, use git notes show <hash of the file>.

The obvious downside is that when you change the file contents, the hash changes. You would have to write your own scripts to traverse the file history and print all the notes.

Upvotes: 8

max630
max630

Reputation: 9238

I'm afraid there is no built-in functionality.

You could put attributes with custom names to .gitattributes but probably it would not give much benefit compared to just placing the data to one or several files in more mature format like json or xml and commiting those files alongside your project.

Upvotes: 3

Related Questions