Alex S
Alex S

Reputation: 66

Create local ".git" repository programmatically

Problem: I have root folder with subfolders / files and I need to create git repository inside this root folder programmatically (without using git tool). The general structure should be simple - new repo and only 1 commit with all files.

I've tried to find specification for .git folder but haven't found good explanation how to encode and generate objects folder and index file inside the .git folder. Could you please guide me how to do that?

Complication: If I solve this problem there is 1 additional complication - some files could be changed. E.g. I have the original file data in memory and I need to use this data + file path.

Thanks :)

Upvotes: 0

Views: 379

Answers (2)

Mikko Rantalainen
Mikko Rantalainen

Reputation: 16045

I definitely would suggest just using git init && git add -m "Initial commit" . but if you insist not using the git binary but have e.g. python version 3, you can follow instructions at https://wyag.thb.lt/

Basically you create whole git repository (.git directory) using home made code to create commits, tree objects and blob objects plus a few metadata files. Your home made repository will end up being bigger than what git would do automatically but it will work with the official git binary.

If you don't have python either, you should include full list of limitations in the question.

Upvotes: 0

bk2204
bk2204

Reputation: 76964

You can do this by hand by examining the contents and format of a normal repository and reading the Pro Git book linked on the website, but I would strongly recommend against doing that. Git repositories are reasonably complex, and you would be better off either using the git binary or a binding of libgit2 (which has bindings for several languages).

Doing it yourself runs the risk of creating corrupt objects, and if you do that your repositories generally will not be able to be pushed anywhere, since most hosting providers reject malformed data. You will also run the risk of data loss.

libgit2 is a good approach if you don't want to use Git, but do note that it lacks some features (like SHA-256 repository support) compared to the Git binary.

Upvotes: 4

Related Questions