简体   繁体   English

智能HTTP git客户端

[英]Smart HTTP git client

Here, this part I can't make it work. 在这里,这部分我无法使其发挥作用。

https://gist.github.com/schacon/6092633#file-git-http-proto-txt-L313 https://gist.github.com/schacon/6092633#file-git-http-proto-txt-L313

# -*- coding: utf-8 -*-
import requests                              
url = "https://github.com/llacroix/node-nfc.git/git-upload-pack"

def make_line(line):
    prefix = "%0.4x" % (len(line) + 4)
    return "%s%s" % (prefix, line)

data = ""
data += make_line("want ba9007f12771f6fddf2b1b22888af90352588197\0 \n")
data += make_line("have 5cec4d0c777aa2bda27c6c9be8e6108ef0f08d62\n")
data += "0000"

req = requests.post(url, data, headers={'Content-Type': 'application/x-git-upload-pack-request'})
print req.text

I don't understand how I can make it work. 我不明白我怎么能让它发挥作用。 I tried the command locally with: 我在本地尝试了以下命令:

git-upload-pack --stateless-rpc .

But I can't figure out to make the protocol work correctly. 但我无法弄清楚协议是否正常工作。 It either fail or return nothing locally. 它要么失败要么在本地没有返回。 And Remotely, it always fails with: 而且远程地,它始终失败:

IncompleteRead(0 bytes read)', ... IncompleteRead(读取0个字节)',...

Ok, first off the solution is pretty much based on this answer for a similar question. 好的,首先解决方案几乎是基于类似问题的答案。

https://stackoverflow.com/a/31953327/54606 https://stackoverflow.com/a/31953327/54606

The documentation I linked is either wrong or really outdated. 我链接的文档错误或过时。 The NUL delimiter isn't "\\0" but "0000". NUL分隔符不是“\\ 0”而是“0000”。 "0000" Isn't actually ending the communication but "done" is effectively doing that. “0000”实际上并没有结束通信,但“完成”实际上正在这样做。

The four char before the line are actually the length of the line including a newline and the 4 char except the 0000 is a line starts with them. 该行前面的四个字符实际上是包含换行符的行的长度,除了0000之外的4个字符串是以它们开头的行。 Without looking at the source code, I'd expect the "0000" to behave like a reset or a flush. 在不查看源代码的情况下,我希望“0000”的行为类似于重置或刷新。

The done line also have to be prefixed with the length which will be 9 chars including a newline. 完成的行也必须以9个字符为前缀,包括换行符。

The end result looks like this: 最终结果如下:

upload_pack = '%s/git-upload-pack'
url = upload_pack % (options.url)

def make_line(line):
    prefix = "%0.4x" % (len(line) + 5)
    return "%s%s\n" % (prefix, line)

def make_want(obj):
    line = make_line("want %s" % obj)
    return line + "0000"

def make_have(obj):
    return make_line("have %s" % obj)

def make_request(from_obj, to_obj):
    data = ""
    data += make_want(to_obj)
    data += make_have(from_obj)
    data += make_line("done")
    return data

headers = {
    'Content-Type': 'application/x-git-upload-pack-request',
}

req_data = make_request(options.from_obj, options.to_obj)
req = requests.post(url, req_data, headers=headers)

print req.content

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM