How to implement Git in pure Javascript to create

2020-07-27 19:43发布

问题:

after searching quite a lot without findind nothing about it I'm here to ask you if there is a way to implement Git in a pure Javascript web application. I already know about Git.js but it implements just some basic things and I also wanted to build my own library to learn more in depth about Git.

What I'm not looking for is an API or a lib that could help me.

What I'm looking for is something like:

var command = {{git commit -m "Hello world"}} // Also pure git implementation
gitExecute(command);

I'm still a junior developer and maybe this could be impossible...thanks for the reply :)

回答1:

What you are asking for may be difficult to do in a browser (because you will need access to the file system to run git commands). What you may need to do is create a NodeJS server which exposes REST endpoints which can be accessed by code in the browser which provides the GUI. The NodeJS server code can run commands as needed and respond to the REST HTTP requests which can be then used by your code in the browser to show/update the GUI.

The disadvantage with this method is that you will need to run your NodeJS server on the computer which has the repository and will not work if the repo is not local to the server.

Another alternative is to use the REST APIs exposed by popular GIT providers like GitHub.

EDIT:

Come to think of it, your usecase may be a good fit for an Electron App. That will allow you to build a desktop app (with access to the filesystem and privileges to execute commands) using Javascript.



回答2:

For this purpose, NodeJS is mandatory.

You could install git on your server machine, and thene execute your cmds through nodejs' child processes (DOCs)