Search code examples
node.jsgitgift

How can I get the contents of a file of a git commit with the gift library?


I am using the https://github.com/notatestuser/gift library and here's what I have:

gitCommit.tree().contents (err, gitTreeContents) ->
  console.log gitTreeContents

This gives me the tree contents, which is an array with objects that look like:

{ repo: 
   { path: '/mygitrepo',
     bare: false,
     dot_git: '/mygitrepo/.git',
     git: 
      { [Function]
        cmd: [Function],
        streamCmd: [Function],
        list_remotes: [Function],
        refs: [Function] } },
  id: 'ed38d79b10503a4e7e68910f37f387f24dedd5fa',
  name: 'address.js',
  mode: '100644' }

So two questions. First, what is that id referring to? It's not a commit sha, so I assume it's a treeish? Second, how can I see what the file looked like at that point in time?

Thanks


Solution

    1. Yes, it's a tree-ish ID, that is sha-1 of a given filesystem object, in case of a plain file, sha-1 of its content.
    2. To get an object at a given point of time, you should choose an appropriate commit first. Please note, that in a project there might be more than one commit at a given time with a file with a given name, because of possible branches. So you should choose a branch and then query that branch state at that time. And once again there might be more than one commit, because of possible merges in the branch.
    3. gift is a simple wrapper over command line git. In command line git you should run a command like this: git cat-file blob <commit-sha-1>:</path/to/file>. Take a look at http://github.com/notatestuser/gift/blob/master/src/blob.coffee it seems, it does exactly what you need.