Skip to content

Get contents of index entry? #94

Closed

Description

@papandreou

Hi!

I'm trying to use nodegit to build a virtual fs module that fetches its data from a commit in a git repository: https://github.com/papandreou/node-gitfakefs -- it's going fine so far, I've implemented readFile, readdir, and stat.

Next up is adding support for the index. I found repo.openIndex and can get the entries out of it, but from the docs I can't find a way to get the contents of an index entry -- the actual staged contents. Is there a way to accomplish that? Otherwise consider this a feature request :)

Activity

  1. nkallen commented on Sep 23, 2013

    @nkallen
    Contributor

    Can you research the libgit2 functions which would provide this functionality, then find them in the v0.18.json file and copy/paste those entries here?

  2. papandreou commented on Sep 23, 2013

    @papandreou
    ContributorAuthor

    Thanks for your reply.

    The libgit2 docs (http://libgit2.github.com/libgit2/#HEAD/type/git_index_entry) list a bunch of properties for the git_index_entry type:

    git_index_time ctime
    git_index_time mtime
    unsigned int dev
    unsigned int ino
    unsigned int mode
    unsigned int uid
    unsigned int gid
    git_off_t file_size
    git_oid oid
    unsigned short flags
    unsigned short flags_extended
    char *path
    

    whereas nodegit's equivalent has much fewer:

    require('nodegit')
        .Repo.open('test/testrepo.git', function (err, repo) {
            repo.openIndex(function (err, index) {
                var e = index.entry(0);
                for (var p in e) {
                    console.warn(p, e[p]);
                }
            })
        });
    
    // ctime function ctime() { [native code] }
    // mtime function mtime() { [native code] }
    // path function path() { [native code] }

    If the index entry object had the oid method, I believe I could use repo.getBlob to get the staged contents. Unfortunately that is missing from v0.18.0.json, which only has:

      {
        "filename": "index_entry.h",
        "dependencies": [
          "../include/index_time.h"
        ],
        "jsClassName": "IndexEntry",
        "cppClassName": "GitIndexEntry",
        "cType": "git_index_entry",
        "freeFunctionName": "free",
        "fields": [
          {
            "jsFunctionName": "ctime",
            "cppFunctionName": "Ctime",
            "name": "ctime",
            "cType": "git_index_time",
            "cppClassName": "GitIndexTime",
            "jsClassName": "IndexTime",
            "copy": "git_index_time_dup"
          },
          {
            "jsFunctionName": "mtime",
            "cppFunctionName": "Mtime",
            "name": "mtime",
            "cType": "git_index_time",
            "cppClassName": "GitIndexTime",
            "jsClassName": "IndexTime",
            "copy": "git_index_time_dup"
          },
          {
            "jsFunctionName": "path",
            "cppFunctionName": "Path",
            "name": "path",
            "cType": "char *",
            "cppClassName": "String",
            "jsClassName": "String"
          }
        ]
      },
    
  3. nkallen commented on Sep 23, 2013

    @nkallen
    Contributor

    Thanks for the info! Here is a request. Since I'd like to encourage more people to make contributions to nodegit (so the code never rots), I'd like to walk you through how to make this change yourself. In theory it's fairly easy.

    Take all the fields that are missing (i.e., everything minus ctime, mtime, and path) and add them to the fields section you pasted above. For each field, you'll need to populate the appropriate attributes. For example, for the field unsigned int dev, you would add

      {
        "jsFunctionName": "dev",
        "cppFunctionName": "Dev",
        "name": "dev",
        "cType": "unsigned int",
        "cppClassName": "UInt32",
        "jsClassName": "Number"
      }
    

    The trickiest bit is knowing the cppClassName given a cType. If you search around in the v0.18 json file, you should see things you can copy by analogy -- remember to check parameters and return types for functions as well as other fields. The jsFunctionName is the name it will be exposed as in javascript; by convention the cppFunctionName is just the titlecased version of that. You might, in the example above, want to call dev something more descriptive.

    Once you've done that for everything, you should be able to build the project using

    npm run-script gen && npm install && npm test

    Double check that it works in your project (perhaps using the npm link command), and if everything goes smoothly, submit a pull request.

    Thanks for your help!

  4. added a commit that references this issue on Sep 26, 2013
    c372842
  5. papandreou commented on Sep 26, 2013

    @papandreou
    ContributorAuthor

    What an exemplary way to handle a request like this -- thanks a lot for walking me through the process. I got it working and added a pull request here: #95

  6. nkallen commented on Sep 26, 2013

    @nkallen
    Contributor

    Awesome! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions