Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable the ident string for a Git repository?

Tags:

git

ident

How do I enable ident $Id$ on files in a Git repository?

like image 488
pean Avatar asked Nov 24 '09 20:11

pean


People also ask

How do I find my git repository string?

Git includes a grep command to search through commits to a repo as well as the local files in the repo directory: git grep. Sometimes it is useful to search for a string throughout an entire repo, e.g. to find where an error message is produced.

How do you add a text file to a repository?

To add and commit files to a Git repository Create your new files or edit existing files in your local project directory. Enter git add --all at the command line prompt in your local project directory to add the files or changes to the repository. Enter git status to see the changes to be committed.

How do I commit to a repository?

Commit the files staged in your local repository by writing a commit message. You can create a commit message by git commit -m 'your message' , which adds the change to the local repository.

Where do I put a .git file?

In a . git directory in the root of the project.


2 Answers

Summary: The recommended way of embedding version information in a product is to use the build system for that; see below for details and alternate approaches.


In Git (and I think usually also in other VCS systems with atomic commits) there is no such thing like version of a single file.

Git does support on-demand expansion of $Id:$ keyword, but:

  1. It is done on request only. You have to specify (perhaps using globbing pattern) that a file (or a set of files) has an ident attribute set (in '.gitattributes' file in tree, or in '.git/info/attributes' for local repository settings).
  2. It expands to the SHA-1 of file contents (or to be more exact to $Id:<sha-1 of blob>$). The reason for this choice is that Git does not touch files that have not changed during branch switching or rewinding; if '$Id:$' expanded to revision info it would require to update every version-controlled file e.g. when switching branches.

Git supports quite a wide set of $Format:...$ placeholders which expands to commit information (e.g. $Format:%H$ replaced by a commit hash) but:

  1. Expansion is done only when running git archive, in its output file.
  2. It is done on request, controlled via export-subst attribute.

The recommended way of embedding version information is to do it via the build system (in a build stage); see for example Git Makefile and GIT-VERSION-GEN script used by Makefile in the Git web interface for the git.git repository.

You can however (ab)use a clean/smudge filter driver (via filter attribute) to get CVS-like keyword expansion, expanding keywords on checkout, and cleaning them up on entering contents to the repository.

like image 173
Jakub Narębski Avatar answered Oct 20 '22 12:10

Jakub Narębski


You can do this by adding a pattern for which files you want this functionality followed by ident in the .gitattributes file. This will replace $Id$ with $Id:<40-digit SHA>$ on checkout of the file. Notice though that it won't give you a revision number of the file as in CVS/SVN.

Example:

$ echo '*.txt ident' >> .gitattributes $ echo '$Id$' > test.txt $ git commit -a -m "test"  $ rm test.txt $ git checkout -- test.txt $ cat test.txt 

Link to gitattributes(5) Manual Page

like image 45
hallski Avatar answered Oct 20 '22 12:10

hallski