Repository navigation
Description
I'm trying to port libgit2 to be able to do repo cloning and pushing from browser, having a credible git client in browser would be pretty neat.
Compilation went fine. First I tried to directly clone a GitHub repo by git protocol but it failed with WebSocket connection to 'ws://github.com/' failed: WebSocket is closed before the connection is established (Chrome console message; I could also see [SYN, SYN-ACK, ACK, PSH-ACK, ACK, FIN-ACK, ACK, FIN-ACK, ACK] sequence on Wireshark). I wondered if it's a problem with absence of HTTPS/SSH, and tried to build a native libgit2 without those protocols and clone something simpler. Native libgit2 clones the dash repo fine, but the assert of emscripten'ed one fails (testing code below): Error: failed to connect to git.kernel.org: Host is unreachable (error seems to be coming from https://github.com/libgit2/libgit2/blob/a6d833a29e100cae66d5144367e9102d093d4dbd/src/socket_stream.c#L124).
It's my first attempt at using emscripten and I don't know the debug/logging hooks yet. Should I try using https://kripken.github.io/emscripten-site/docs/api_reference/emscripten.h.html#socket-event-registration? Enabling -s SOCKET_DEBUG=1 printed only connect: ws://git.kernel.org:9418/, binary. Anything else?
What would be your thoughts?
[ -d libgit2 ] || git clone https://github.com/libgit2/libgit2
cd libgit2
[ -d build ] && rm -rf build
mkdir -p build && cd build
cat <<EOF > mytest.c
#include <stdio.h>
#include <git2.h>
int main()
{
git_repository* repo = NULL;
git_libgit2_init();
if(0 != git_clone(&repo, "git://git.kernel.org/pub/scm/utils/dash/dash.git", "dash", NULL))
printf("Error: %s\n", giterr_last()->message);
}
EOF
function build_test_emscripten
{
cmake .. -DCMAKE_SIZEOF_VOID_P=4 -DCMAKE_C_COMPILER="${EMSCRIPTEN_ROOT_PATH}/emcc" -DCMAKE_AR="${EMSCRIPTEN_ROOT_PATH}/emar" -DCMAKE_RANLIB="${EMSCRIPTEN_ROOT_PATH}/emranlib" -DCMAKE_C_FLAGS=$'-s EXPORTED_FUNCTIONS="[\'_git_clone\', \'_git_libgit2_init\',\'giterr_last\']" -O2' -DBUILD_CLAR=OFF -DUSE_SSH=OFF -DCURL=OFF -DUSE_OPENSSL=OFF
MAKEFLAGS=8 cmake --build .
"${EMSCRIPTEN_ROOT_PATH}/emcc" -s EXPORTED_FUNCTIONS="['_git_clone','_git_libgit2_init','_giterr_last']" -O2 -s LINKABLE=1 -o ../../libgit2.js libgit2.so
"${EMSCRIPTEN_ROOT_PATH}/emcc" -o mytest.js mytest.c -I../include libgit2.so
nodejs ./mytest.js
}
function build_test_native
{
cmake .. -DCMAKE_C_FLAGS=$'-O2' -DBUILD_CLAR=OFF -DUSE_SSH=OFF -DCURL=OFF -DUSE_OPENSSL=OFF
MAKEFLAGS=8 cmake --build .
gcc -o mytest mytest.c -I../include -lgit2
./mytest
}
build_test_emscriptenThanks!
UPD: My current understanding is that it can't work, because the Git servers don't support WebSockets as transport. Sorry for the long text :)
Activity
CMCDragonkai commentedon Aug 31, 2017
What do you lose by not compiling in openssl, libssh2 and curl?
vadimkantorov commentedon Aug 31, 2017
I thought of first getting pure git HTTP protocol working
petersalomonsen commentedon Sep 13, 2017
I solved this by bypassing the libgit http parser and just passing on data directly from the browser xhr. Created a new stream type in tls_stream.c that uses EM_ASM to create an XMLHttpRequest. The data from the xhr is already parsed so I removed the parser code in transports/http.c ( http_stream_read ) and passing data directly from the xhr stream:
*(bytes_read) = git_stream_read(t->io, buffer, buf_size);Here's the changes in tls_stream.c - first of all connect the new stream type:
and then the stream functions:
CMCDragonkai commentedon Sep 13, 2017
@petersalomonsen Does your setup allow http cloning + https cloning? I always thought github had some cross origin constraint issue. What about ssh cloning?
Also interesting way of passing pointers between EM_ASM and C/C++ code!
vadimkantorov commentedon Sep 13, 2017
@petersalomonsen Wow, cool. Do I understand correctly that we still can't clone a repo from GitHub in browser context because of CORS and inability to do a HTTPS post to a foreign domain?
Meanwhile I was implementing my own libgit transport relying on GitHub API, and while it would eventually work it has a lot of nasty details (and not very practical because of gazillion HTTP requests needed to fully clone a repo given that libgit doesn't support yet shallow repos libgit2/libgit2#4331).
Also,
xhrstreamvariable seems undeclared.petersalomonsen commentedon Sep 13, 2017
CORS prevents from cloning from github - so I had to use an Apache proxy (ProxyPass). Tested HTTPS cloning in the browser with same origin.
SSH cloning not supported in the browser. When relying on browser XMLHttpRequest there is no support for SSH sockets.
@vadimkantorov declaration: git_stream xhrstream;
Attaching my modified files.
http.c.txt
tls_stream.c.txt
The problem I'm facing with clone now is on "resolving deltas":
ok up to here, but then I'm getting:
failed to truncate pack file '/workdir/testrepo/.git/objects/pack/pack_git2_iJoMdK': Permission deniedWhich seems to be related to a filedescriptor on locked files after they've been saved and renambed (in indexer.c) . Still hacking into that.
petersalomonsen commentedon Sep 13, 2017
@vadimkantorov @CMCDragonkai
Successfully completed a git clone in the browser from https.
The last problem I mentioned in the previous comment was solved by adding:
fchmod(idx->pack->mwf.fd,0777);before
/* Truncate file to undo rounding up to next page_size in append_to_pack */in the file indexer.c
Screenshot is the output from the web console in chrome.
vadimkantorov commentedon Sep 13, 2017
Cool stuff :) In my understanding, the
tsl_stream.cmodifications can be sidestepped by callinggit_stream_register_tls(git_open_emscripten_stream)before cloning. The only thing to be potentially upstreamed would be thehttp.candindexer.cmods, right?petersalomonsen commentedon Sep 14, 2017
@vadimkantorov Yes, moving out the modifications in
tls_stream.cand instead usinggit_stream_register_tls(git_open_emscripten_stream)would be cleaner.Also I would add a configuration option to bypass the http parser as I did in
http.c. That one line changed inindexer.cshouldn't cause any problems to other platforms, so that could be upstreamed as is.So have to do some code cleanup after this hacking session, and would be good to either have emscripten build support in the original repository or a fork project here at github.
CMCDragonkai commentedon Sep 14, 2017
@petersalomonsen My preliminary google search found that people were able to compile libssh via emscripten. Not entirely sure if SSH works however, I suppose it should be using websockets in the browser, while in Node it should have an easier time.
Could you expand on what you did with the Apache proxy, are you saying that you ran the proxy server-side, and made your xhr method proxy through it?
On other gitjs implementations, some have investigated going through the actual Github OAuth API to do cloning.
CMCDragonkai commentedon Sep 14, 2017
@petersalomonsen Do you have a git repo/fork of your current progress?
petersalomonsen commentedon Sep 14, 2017
@CMCDragonkai Yes the Apache proxy is on the serverside using ProxyPass directives, and the xhr method goes through it.
About SSH you will run into the socket problem. As this thread started with I also faced that libgit2 tried to open a websocket - which is the default behavior of emscripten when trying to open a socket in the browser. I guess you could implement a websocket server that proxies to a tcp socket and make ssh work that way.
Would be easier in node where you don't have the browser sandbox restrictions.
I have a git repo, but have to clean it for passwords/hostnames before posting publicly.
CMCDragonkai commentedon Sep 14, 2017
@petersalomonsen Is the socket problem the same problem as CORS which requires a TCP proxy on server side, or is it a different socket problem where websockets can't handle libssh functionality?
petersalomonsen commentedon Sep 15, 2017
@CMCDragonkai Only "guessing" here - but I think that emscripten makes libgit think it's dealing with a TCP socket while in reality it is a websocket. So I would believe it should work if you make a websocket server that forwards traffic to a tcp socket. Websocket still have to obey CORS just as XMLHttpRequest.
petersalomonsen commentedon Sep 19, 2017
@CMCDragonkai @vadimkantorov Just added a public fork with my emscripten hack:
https://github.com/fintechneo/libgit2
Does this work for you?
CMCDragonkai commentedon Nov 8, 2017
I'm just looking at some of your commits, would it possible that they be pushed back to libgit2 upstream to make the original source more portable (to platforms like emscripten)?
petersalomonsen commentedon Nov 8, 2017
Yes it would be very good to push it back to libgit2 upstream if they want it there. Probably need a better config option on disabling the HTTP parser but otherwise should be possible to integrate this. Do you see any other issues?
Maybe best approach would be to start with registering an issue on libgit2 to start discussion on requirements for pushing back to upstream? @CMCDragonkai
vadimkantorov commentedon Nov 8, 2017
There are those nasty pwrite and chmod, right? (I summarized your changes here, but it sparked no discussion)
petersalomonsen commentedon Nov 8, 2017
Yes that's right. Those hacks for permissions (chmod) and mmap (pwrite) needs a better solution as they're redundant in other environments.
I've upvoted your issue @vadimkantorov - but we need more votes ( @CMCDragonkai )
CMCDragonkai commentedon Nov 8, 2017
On my vfs that I'm integrating into emscripten, there won't be the problem of 1, because the vfs will be read/write by default. As for 2, there's an issue with mmap, for the existing library_memfs, mmap using
MAP_SHAREDis actually done only when the underlying buffer is the same as in HEAPU8, that means the files are already in the HEAPU8 buffer. And all mmap is doing is giving back a pointer to the subarray. There is an object callSYSCALLS.mappingsthat maintains a list of mappings to be synchronised using msync or munmap, however it does not work under the situations where files get resized. In all other cases the file's memory is just copied into a new allocation equivalent toMAP_PRIVATE.On my implementation of
library_fs.js,MAP_SHAREDis simply not possible without a full page table virtual memory management system. Because the files in VirtualFS (https://github.com/MatrixAI/js-virtualfs) are not stored in the same Emscripten heap, onlyMAP_PRIVATEis possible. So with regards to problem 3. it seems that the best course of action is just to disable MMAP. I can see that an emulated mmap is supplied if this constant is defined: https://github.com/libgit2/libgit2/search?q=NO_MMAP&type=Code&utf8=%E2%9C%93CMCDragonkai commentedon Nov 8, 2017
Libgit's emulation of mmap also denies the use of
MAP_SHARED:https://github.com/libgit2/libgit2/blob/28c89507d1b9e1013e690adc1ef16f30bd3fa4f5/src/posix.c#L245-L248
stale commentedon Sep 19, 2019
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.