Q:

How do I create git tags for legacy gcc releases?

I've cloned the Official GCC git repository, which includes a full GCC development history, but git branch -r lists only a few recent branches, and 'git tag' reports no tag.  git fetch --tags didn't help.  git svn fetch will recreate tags/branches that were in SVN, but will take about 10 days to complete.
I know I could use SVN directly, but would prefer to leverage git's superior merge capabilities to forward-port changes from gcc-3.3.2.
So, is there a relatively quick way to automatically retrieve git tags or branches for old GCC releases?

A:

To create git tags for GCC releases, you must first clone the git repository with git clone git://gcc.gnu.org/git/gcc.git ; cd git and then fetch the release branches, following the instructions in the gcc wiki:
~/gcc> for f in 2_95 3_0 3_1 3_2 3_3 3_4 ; do git config --add remote.origin.fetch refs/remotes/gcc-$f-branch:refs/remotes/origin/gcc-$f-branch; done
~/gcc> for f in 1_00 1_1; do git config --add remote.origin.fetch refs/remotes/egcs_${f}_branch:refs/remotes/origin/egcs_${f}_branch; done
~/gcc> git remote update

Then you can create tags for the various releases as follows:
#! /bin/sh
# These commits removed "prerelease" from DEV-PHASE
git tag gcc-4.5.0  72d56ebb66ff343f779686a70c7be429238f5d5d 
git tag gcc-4.4.4  fdaa572e16a4a3ed881c9daa357c933c06dc31cb 
git tag gcc-4.4.3  51a24d9c5dbd1c57edd1e424e9e3d2ff7d51c69b 
git tag gcc-4.4.2  36b1f6c832fa33bee30bff27673c0dbdc395c176 
git tag gcc-4.4.1  d380ec468c73de5d574a447eafafc2459c7cf30b 
git tag gcc-4.4.0  2728d6e41532213f6aeb4389279ea4bfb67fc630 
git tag gcc-4.3.5  16d5e336556f03096929f6dde24f512b54a461ec 
git tag gcc-4.3.4  cbbc992fb4715f214e3cdde8772a7a689e73b93f 
git tag gcc-4.3.3  7c2c8f392d84e82a0486f4620f144d30a7f5b23b 
git tag gcc-4.3.2  d68c00434de71550eaf302687e50f25de9f54a7f 
git tag gcc-4.3.1  1394445da1475b1bf4f38d021e87f40f45ce89f3 
git tag gcc-4.3.0  16c0ea36c9d625d2ae7a101163c8bde9f760a021 
git tag gcc-4.2.4  274499b7a967415ae994d93dbe23f30845885aaf 
git tag gcc-4.2.3  e5e549f0d8c2f02bf7785f1330c4ee5c5c764d62 
git tag gcc-4.2.2  541a9a5982606677fe5bb7068000ba603de6caa9 
git tag gcc-4.2.1  13cfe4cbd201e7390fe0f74f87aada6d6c1f9d81 
git tag gcc-4.2.0  18187566601443fc139424f6f0563d49ac1760e9 
git tag gcc-4.1.2  fd3c8e16b8724e10f1de95bbc157f2ac838ecf6a 
git tag gcc-4.1.1  a393db9348d55e6b889d4b87a8728af8fa22f90c
git tag gcc-4.1.0  abe98e32d2e56516eacbfe8aad3dcec9750e4d50
# These commits created unadorned gcc/version.c except for
# gcc-3.3.6, which includes the subsequent parse.y fix.
git tag gcc-4.0.4  e3a4cd335ea4a9fc2c9e2c8f7f9f47b8d104cd6b 
git tag gcc-4.0.3  89ef6ca7fc65f4bd4328a7ed92073787767108e1 
git tag gcc-4.0.2  ba94c4d3347470baad33d33da0aab6c466d3a76d 
git tag gcc-4.0.1  dc4eedf60db8034421294df88f4006758d0a3fee 
git tag gcc-4.0.0  81a28974b35a40ea0f9e49ebcf461808f79cd1c9
git tag gcc-3.4.6  359e576cf4a70392fda974114498ba451310ab05 
git tag gcc-3.4.5  31f752f38d95c8d5b8f4368ab7f6630dd8569190 
git tag gcc-3.4.4  8e095405a5fe8adf3f01361c05e7010a9b94e8cc 
git tag gcc-3.4.3  5625cbd10a9d3a1fbe7541f1776f8a3efdbee1e8 
git tag gcc-3.4.2  7db743b07c1d46b879c0cb1a43597ed5e9feae2c 
git tag gcc-3.4.1  a1d11c1349ec7908053ab5d2c6200652e4d8c700 
git tag gcc-3.4.0  3b3ea0678785edcb024c8fb6c2a870a1260bd407 
git tag gcc-3.3.6  67be34f080d258b9accdc9b2dc60e522a071b24d 
git tag gcc-3.3.5  34ae055bd7b7f51982f981c9e039899e948d4a79 
git tag gcc-3.3.4  fbd67149061d692b9da2dbabd782367cdc429551 
git tag gcc-3.3.3  46f1268b65c1b8634a5bd1e10b9c8410d1387010 
git tag gcc-3.3.2  79d8c6c28fc8d439fe84d65a2fbfe23df3527d93 
git tag gcc-3.3.1  7650ad7cf3c4148167ec46b700ef164b9cc96dbf 
git tag gcc-3.3    b96376930b5c43b42d357a269de640596dc0fe34 
git tag gcc-3.2.3  d79068c19439ded5d3c025455c7d56dd33b968fb 
git tag gcc-3.2.2  bfac687d15cfc11828f9d8a0a64616dfb9cfa58d 
git tag gcc-3.2.1  85fed4a8c013f5532a7ac6a831fc3a86b20045f9 
git tag gcc-3.2    0fc80fd06dd97851b2b63e09becd3cb9f4e0fffc 
git tag gcc-3.1.1  941e96525189b92bc056913f51f362b96ea5cb45 
git tag gcc-3.1    b7ba1b367cf7cedf834b83cc47594bac9549fab0
# These commits set "(release)" in gcc/versionc.c
git tag gcc-2.95.3 35ab1cbf431d6c6cefe00721286cfce1737d11c7
git tag gcc-2.95.2 980266352750ab98bbc2a4fa2da04a6e4c2af019
git tag gcc-2.95.1 ee41387a5663e9239605bf48259227016944dbf1
git tag gcc-2.95   100d7691ebd6455c709c5ceae36bdd6baa4a2087

Note that the commit IDs above were derived manually, but have been verified against SVN where possible with the script below.
I did not provide tags for EGCS releases, but the trick is to check out the release branch and use git log -p gcc/version.c to reveal the relevant commit IDs.
I tried to find a way to do fast automatic tag-setting, and even succeeded for recent (gcc-4.1+) releases, but it seems that the gcc-3.X and early gcc-4.0.X tags in the SVN repository were not imported into the official git repository, so I could not generate correct results for those.  The script to do this follows, but again it generates incorrect tags for older releases:
#! /bin/bash
# git clone git://gcc.gnu.org/git/gcc.git && cd gcc
s=svn://gcc.gnu.org/svn/gcc
# git svn init $s

echo 'Creating tags.db (This takes several seconds.)'
git log --grep=git-svn-id: --remotes \
| egrep '(^commit |git-svn-id: )' \
| sed -e 's/^ *git-svn-id:[^@]*@\([0-9]*\).*/\1/' \
| awk 'BEGIN { RS = "commit " ; FS = "\n" } { print $1, $2 }' \
> tags.db

echo 'Reading tags.db'
while read cid rev ; do commit[$rev]=$cid ; done < tags.db

echo 'Fetching SVN revisions and tagging'
echo '                                          SVN rev'
echo ' Git commit ID                            in git  SVN rev Tag'
echo ' ======================================== ======= ======= ======= = = ='
echo ' (Waiting for remote svn info.  This takes a few seconds.)'
for t in `svn ls $s/tags | fgrep release | sed -e 's@/$@@'`
do
  rev=`svn info $s/tags/$t | grep '^Last Changed Rev: ' | awk '{ print $4 }'`
  # Scan the svn log for the first SVN revision mirrored in GIT.
  # This is required because git-svn does not mirror SVN tag/branch revisions.
  for r in `svn log -l 2 $s/tags/$t \
            | grep '^r[0-9][0-9]* | ' \
            | awk '{ print $1 }' \
            | sed -e 's/r//'`
  do 
    if [ x"${commit[$r]}" = x ] ; then continue ; fi
    printf " %40s %7s %7s %s\n" "${commit[$r]}" "$r" "$rev" "$t"
    if [ x"${commit[$r]}" != x ] ; then 
        git tag -f "$t" "${commit[$r]}"
    fi
    break
  done
done

echo 'Removing tags.db'
rm tags.db
echo 'Done.'

