Attributes to export an archive

While exporting a snapshot of a Git repository with the archive command (refer to Chapter 10, Patching and Offline Sharing), it is possible to change the way the archive is made.

Getting ready

We'll use the attributes_example repository:

$ git clone https://github.com/dvaske/attributes_example.git
$ cd attributes_example

How to do it...

First, we'll set up the attributes needed in .gitattributes and commit the file on the exif branch:

$ git checkout exif
Branch exif set up to track remote branch exif from origin by rebasing.
Switched to a new branch 'exif'
$ echo 'europe_needles.jpg export-ignore' >> .gitattributes
$ git add .gitattributes
$ git commit -m 'Add .gitattributes'
[exif 783b7f7] Add .gitattributes
 1 file changed, 1 insertion(+)
 create mode 100644 .gitattributes

Now, we can create an archive from the tip of the exif branch, and the europe_needles.jpg file shouldn't be included, as shown in the following snippet:

$ git archive -o attr.zip exif
$ unzip -l attr.zip
Archive:  attr.zip
783b7f73110e23f56675f0014ab3f1d0aba21d7f
  Length     Date   Time    Name
 --------    ----   ----    ----
       33  05-06-14 21:33   .gitattributes
      325  05-06-14 21:33   README.md
   272509  05-06-14 21:33   hello_world.jpg
      543  05-06-14 21:33   pic_credits.txt
 --------                   -------
   273410                   4 files

The europe_needles.jpg file isn't there! This is very useful when creating archives of the source code without including test, the proprietary code, IPR, and so on.

There's more…

We can also do keyword substitution while exporting an archive. We need to use the export-subst attribute. We can set it up on the README.md file in the following way:

$ echo "README.md export-subst" >> .gitattributes
$ echo "Last commit: $Format:%H$" >> README.md
$ echo "Last commit date: $Format:%cd$" >> README.md
$ git add .gitattributes README.md
$ git commit -m "Commit info for git archive"
[exif 8c01a48] Commit info for git archive
 2 files changed, 3 insertions(+)

Create the archive. Check the content of the README.md file in the archive and the last commit on the exif branch using the following command:

$ git archive -o attr.zip exif
$ unzip -p attr.zip README.md
Git Attributes
==============

A few examples on using git attributes.

Pictures used found on flickr.com,
check pic_credits.txt for details.

Pictures only changes in size, not altered
only exif data is used as examples of diff'ing
pictures based on exif data.

Example repository for the book: Git Version Control Cookbook
Last commit: d3dda23601a3cc16295bdd7f4f9812544ea69d53
Last commit date: Tue May 6 21:52:25 2014 +0200 $
$
$ git log -1 --format=Commit: %H%nDate: %cd
Commit: d3dda23601a3cc16295bdd7f4f9812544ea69d53
Date: Tue May 6 21:52:25 2014 +0200

Recording the commit ID in the archive is very useful. This is especially the case if you are using the archive for testing so you know which revision or ID of the repository you are using, and you can report issues against this revision or ID.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset