Mike Hommey: Finding where a tarball came from with git
I got the idea reading this question on the webkit-dev list, and from my recollection of the git documentation. Well, for the webkit case, the basic script I put up would be of no help because the tarballs don’t contain all the files (plus, they use subversion, not git, so it would also require a long importing process). Anyways…
Considering how SHA-1 hashes of objects are created with git, it is actually pretty easy to generate a SHA-1 hash from a random (non-git) tree, and then, the corresponding commit. First, you have
git-hash-object that helps you creating a hash for a particular object (though it’s also trivial to do with sha1sum). For regular files, git-hash-object -t blob $filename is enough. For symbolic links, you have to read the link destination, and give it without a trailing character (be it the NULL character or a carriage return) to git-hash-object -t blob --stdin. For directories, you have to generate a tree “structure” by yourself and pass it to git-hash-object -t tree --stdin. I haven’t bothered looking at other file types.
The tree structure can be guessed by either looking at mktree.c or at the output of git-cat-file tree $sha1 where $sha1 is the SHA-1 hash for a tree object. It contains 3 informations for each node in the tree : the file mode, with the same format as what stat() returns, except for some reason, permissions are 000 for directories and symbolic links ; the file name ; and the SHA-1 hash. These informations are written with the following format : file mode in octal ascii and no padding zero (”%o”) followed by a space character, then the filename followed by a NULL character, and the binary form of the SHA-1 hash.
Nodes are sorted in a not-so-quite lexical order (take a look at base_name_compare in read-cache.c) and are not separated by any special character: the mode of a file just follows the SHA-1 hash of its predecessor.
With all this new knowledge, you should be able to write some code that would return the SHA-1 from an arbitrary directory. Okay, since you must be at least as lazy as I am, you can take the script I wrote.
Now, let’s take a look at a real life case : what commit is the latest nightly snapshot for the linux kernel from ? First, download the latest snapshot patch and its baseline, and extract the whole. Then, run my git-hash-tree.pl script with the directory containing the extracted kernel as an argument. It will return, after a while, the SHA-1 hash for the whole tree. During this long process, you also have plenty of time to git clone linus’s tree.
Once you’re all done, you can search for the commit corresponding to the tree hash (let’s call it $hash) with the following command :
git-rev-list --all while read h; do git-cat-file commit $h grep -q "^tree $hash" && echo $h && break; done
If you just followed these steps, you should just have spent a great moment having no result at all. There are actually 2 things that prevent this method to properly work with the linux kernel nightlies :
- The snapshot patches contain a change to the top Makefile that doesn’t exist in the repository. You need to remove the
-gitnfrom theEXTRAVERSIONvariable in the Makefile. git diffonly includes diff headers for removal of empty files, so if you apply the snapshot patch with thepatchutility (and you can’t apply it withgit-applysince you don’t have a.gitdirectory), empty files that were marked as deleted will still be on your tree. It happens with the current snapshot patch (2.6.22-rc7-git6): it doesn’t removeinclude/asm-blackfin/macros.h.
This graph shows the percentage of the total download that used HTTP
from a mirror. The optimal line shows the minimum possible (i.e. all
peers are connected), which corresponds to only one copy (the first)
of every package being downloaded using HTTP.
This verifies my previous thinking, which is that fracturing the
download population into multiple small swarms results in lots of
inefficiencies, and lots of HTTP downloading. All efforts will need
to be taken to keep the downloading populations together. (This is
especially difficult for unstable, when a new swarm could be created
twice a day due to archive updates.) Swarms of 1,000 peers or more
seem to be sufficient to minimize the HTTP downloading.
This graph shows the amount of unnecessary HTTP downloading that
occurred, which is just the difference of each line from the optimal
one in the previous plot. This shows the danger of selective
downloading, as all of this unnecessary HTTP downloading occurs
because the swarm is too big to find peers that have the desired
package (they do exist). Fortunately, this unnecessary downloading
seems to approach a maximum as the swarm size increases.
For the large swarm sizes needed to make the downloading efficient,
and assuming a reasonable number of connections of 100 (bittorrent
defaults to maintaining at least 40, and large swarms usually means
a large number of connections), we can expect to be using HTTP for
about 4% of the download. This number is surprisingly low due to the
popularity distribution of packages (only rare ones are hard to
find, but they aren't downloaded very often). I think 4% is
manageable in our situation, given that there is a backup method to
find the rare packages, though it does show the need to have that
backup method available. This is probably more of a problem for
regular bittorrent clients, in which there is no backup method.