Accessing old version of file via commit


#1

I have an oid representing a commit object, and I have a path to a file that I know exists in that commit. It there any way to directly get the object that represents the historic file other splitting out the path and traversing the commit’s tree object one node at a time?

I can easily get the object representing the most current version of the file by doing this:

{
  repository(owner: "me", name: "example-repo") {
    object(expression: "master:path/to/deep/file.txt") {
      ... on Blob {
        text
        oid
      }
    }
  }
}

But I would need some way to feed an expression to a Tree object instead of just a repository.

Is there more information in the Commit that I’m neglecting that might be useful?

Thanks for any help!


#2

Hey @sjml!

If I’m reading this right, you’re very close with the object connection - if you have the OID and file path, you should be able to fetch the blob by replacing master with that oid.

Something like this seems to work for me:

query {
  repository(owner: "nickvanw", name: "ircx") {
    object(expression: "410de84adaf4ed5202d7a15bef0e625c9d8dcc2e:bot.go") {
      __typename
      ... on Blob {
        text
      }
    }
  }
}

Does this solve your problem? Let me know!


#3

Hmm, that doesn’t seem to work for me – does your OID reference the commit or the file itself?

I get “Argument ‘oid’ on Field ‘object’ has an invalid value. Expected type ‘GitObjectID’.” when I try to combine them like that. :confused:


#4

Hey @sjml!

Interesting! The object connection takes an expression in the same style as the rev-parse command, and then return an object corresponding to the hash that’s returned.

I can’t say for certain what the issue is, but I’d be curious what happens if you did a git rev-parse $message with the expression you’re trying to use? That might be more helpful!