Nodegit: guides/links to read for how to do tasks with this API?

Created on 30 Jan 2018  路  6Comments  路  Source: nodegit/nodegit

Beyond the API docs on the website, I'm looking to do a few (I think, basic) tasks, and I'm not quite sure where to start with the API methods provided. I've never worked with the git API, only with the git CLI, so that's basically the level of understanding I have of git at this point. I vaguely understand bits of how git works internally, but certainly not enough to know how to fully utilize this API.

I was wondering if there were any guides or example code people have created that could help me?

What I want to do is:

  1. do a bare clone of a remote repository (from on github, basically) with only one branch
  2. traverse the contents of the repo (aka, the files in it), and pull out their names and contents
  3. add a file, or edit a file, and commit the change to the repo.
  4. push the commit to remote

That's basically the most complicated it'll get for me. I'll almost certainly never be doing anything advanced like multiple branches, merging, rebasing, conflicts, etc.

I know doing the above steps when there's a full clone in the file system are fairly straightforward, with a lot of the work done as file I/O happening via the fs module.

But I'm thinking I want to do this with a bare clone, where there won't actually be files present, and where I'll have to sort of "virtually" traverse the repo's contents instead of using fs operations.

The reason I was thinking I wanted to do a bare clone instead of a full clone, is my perception (which may be incorrect!) that doing so effectively doubles the size-on-disk (storage used for .git folder + storage used for all the files). As this repository will get quite large, having a significant overhead (> 1.5-2 X of original content size) is undesirable. So I was thinking going the bare clone route saves on that file storage overhead.

Honestly, maybe I'm totally off here, and if so, I apologize for a noisy issue. Mostly, I'm just looking for some extra guides or examples to help me figure out how to sort out my options and proceed. Thanks in advance!

Most helpful comment

  1. insert is meant to add or update an entry so there is nothing weird about it.

  2. 33188 is a decimal number that translates to the standard octal 0100644 which is one of the values expected by libgit2.

  3. I believe you have to create a new tree and insert it into your existing tree.

  4. I believe that is correct.

  5. This continues to be an issue that plagues users. In theory it should not be necessary to call free but leaks have happened in the past and you can find such issues on GitHub if you search for them.

All 6 comments

I should have mentioned, I definitely have already looked at the guides and examples in this repo, and I didn't find what I was looking for. But maybe it's there and I just am too inexperienced with the API to understand what I'm looking at.

Just for posterity sake, I've hacked together some working code (for some of my tasks) after scouring around. Here's that code:

https://gist.github.com/getify/f5b111381413f9d9f4b2571c7d5822ce

Disclaimer: I'm not sure if I'm doing this right or most optimally, but it seems to be working. Hope it's helpful.

Still would like to know if anyone has any other resources on using this utility?

From looking at your Gist, it seems like you are interacting with and using the repository in the way I would envision it (using TreeBuilder and so on to construct a Tree without using physical files).

@rcjsuen thanks so much for looking at my code. very helpful to have more experienced eyes on it. glad i'm not doing something wildly off base.

several quick questions:

  1. is "modifying a file in a git repo" essentially just insert(..)ing a file with the same path, but with modified contents? or is there a more correct way to do that? just seems weird/hacky to "insert" when the file already exists, but maybe that's by design.

  2. can you tell me what the file-mode of 33188 represents? I saw that in code and it works, but I wasn't able to change it to other values (like 0644) like I would have expected. I can't find any documentation on what this value expects?

  3. how do you create a directory with treebuilder? is it just insert(..) with a file whose path implies a parent directory already exists, or do you have to do a specific step to make a directory (tree) to insert files into?

  4. how do you rename a file or directory using treebuilder? is it just remove(..)ing the old entry and insert(..)ing it at a different name?

  5. couldn't find any documentation on whether I need to be using free(..) on all these references I'm working with? is JS's garbage collection sufficient, or do I need to be manually freeing up all this stuff to avoid memory leaks?

  1. insert is meant to add or update an entry so there is nothing weird about it.

  2. 33188 is a decimal number that translates to the standard octal 0100644 which is one of the values expected by libgit2.

  3. I believe you have to create a new tree and insert it into your existing tree.

  4. I believe that is correct.

  5. This continues to be an issue that plagues users. In theory it should not be necessary to call free but leaks have happened in the past and you can find such issues on GitHub if you search for them.

@rcjsuen

Thanks so much! BTW, I've been updating that gist with more code as I've figured out other tasks. I now have working:

  • authentication (ssh)
  • cloning bare repo
  • fetching from remote to update
  • adding/updating the tree
  • committing
  • pushing, including enumerating all refs to use, since the API apparently doesn't support refspec wildcards for push.

Hopefully that gist is helpful to others.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DinkDonk picture DinkDonk  路  4Comments

monkeyboy64 picture monkeyboy64  路  7Comments

Means88 picture Means88  路  5Comments

Sergeeeek picture Sergeeeek  路  9Comments

fatso83 picture fatso83  路  7Comments