git: refactor capability parsing

this should be a nop, but set us up better
for handling and exposing capabilities in
git/serve.
This commit is contained in:
Ori Bernstein 2024-12-10 23:15:22 +00:00
parent e58df8173f
commit e59b7e28cd
4 changed files with 52 additions and 64 deletions

View file

@ -134,33 +134,6 @@ branchmatch(char *br, char *pat)
return strcmp(br, name) == 0; return strcmp(br, name) == 0;
} }
char *
matchcap(char *s, char *cap, int full)
{
if(strncmp(s, cap, strlen(cap)) == 0)
if(!full || strlen(s) == strlen(cap))
return s + strlen(cap);
return nil;
}
void
handlecaps(char *caps)
{
char *p, *n, *c, *r;
for(p = caps; p != nil; p = n){
n = strchr(p, ' ');
if(n != nil)
*n++ = 0;
if((c = matchcap(p, "symref=", 0)) != nil){
if((r = strchr(c, ':')) != nil){
*r++ = '\0';
print("symref %s %s\n", c, r);
}
}
}
}
void void
fail(char *pack, char *idx, char *msg, ...) fail(char *pack, char *idx, char *msg, ...)
{ {
@ -202,6 +175,7 @@ fetchpack(Conn *c)
int nref, refsz, first, nsent; int nref, refsz, first, nsent;
int i, l, n, req, pfd; int i, l, n, req, pfd;
vlong packsz; vlong packsz;
Capset cs;
Objset hadobj; Objset hadobj;
Object *o; Object *o;
Objq haveq; Objq haveq;
@ -220,8 +194,11 @@ fetchpack(Conn *c)
if(n == 0) if(n == 0)
break; break;
if(first && n > strlen(buf)) if(first && n > strlen(buf)){
handlecaps(buf + strlen(buf) + 1); parsecaps(buf + strlen(buf) + 1, &cs);
if(cs.symfrom[0] != 0)
print("symref %s %s\n", cs.symfrom, cs.symto);
}
first = 0; first = 0;
getfields(buf, sp, nelem(sp), 1, " \t\n\r"); getfields(buf, sp, nelem(sp), 1, " \t\n\r");

View file

@ -82,6 +82,14 @@ struct Hash {
uchar h[20]; uchar h[20];
}; };
struct Capset {
char symfrom[256];
char symto[256];
int sideband;
int sideband64k;
int report;
};
struct Conn { struct Conn {
int type; int type;
int rfd; int rfd;
@ -330,6 +338,7 @@ int gitconnect(Conn *, char *, char *);
int readphase(Conn *); int readphase(Conn *);
int writephase(Conn *); int writephase(Conn *);
void closeconn(Conn *); void closeconn(Conn *);
void parsecaps(char *, Capset *);
/* queues */ /* queues */
void qinit(Objq*); void qinit(Objq*);

View file

@ -17,6 +17,42 @@ enum {
Nbranch = 32, Nbranch = 32,
}; };
char *
matchcap(char *s, char *cap, int full)
{
if(strncmp(s, cap, strlen(cap)) == 0)
if(!full || strlen(s) == strlen(cap))
return s + strlen(cap);
return nil;
}
void
parsecaps(char *caps, Capset *cs)
{
char *p, *n, *c, *t;
memset(cs, 0, sizeof(*cs));
for(p = caps; p != nil; p = n){
n = strchr(p, ' ');
if(n != nil)
*n++ = 0;
if(matchcap(p, "report-status", 1) != nil)
cs->report = 1;
else if(matchcap(p, "side-band", 1) != nil)
cs->sideband = 1;
else if(matchcap(p, "side-band-64k", 1) != nil)
cs->sideband64k = 1;
else if((c = matchcap(p, "symref=", 0)) != nil){
if((t = strchr(c, ':')) == nil)
continue;
*t++ = '\0';
snprint(cs->symfrom, sizeof(cs->symfrom), c);
snprint(cs->symto, sizeof(cs->symto), t);
}
}
}
void void
tracepkt(int v, char *pfx, char *b, int n) tracepkt(int v, char *pfx, char *b, int n)
{ {
@ -158,7 +194,7 @@ parseuri(char *uri, char *proto, char *host, char *port, char *path, char *repo)
snprint(port, Nport, "80"); snprint(port, Nport, "80");
else if(strncmp(proto, "hjgit", 5) == 0) else if(strncmp(proto, "hjgit", 5) == 0)
snprint(port, Nport, "17021"); snprint(port, Nport, "17021");
else if(strncmp(proto, "gits", 4) == 0) else if(strncmp(proto, "gits", 5) == 0)
snprint(port, Nport, "9419"); snprint(port, Nport, "9419");
else else
hasport = 0; hasport = 0;

View file

@ -3,15 +3,8 @@
#include "git.h" #include "git.h"
typedef struct Capset Capset;
typedef struct Map Map; typedef struct Map Map;
struct Capset {
int sideband;
int sideband64k;
int report;
};
struct Map { struct Map {
char *ref; char *ref;
Hash ours; Hash ours;
@ -89,33 +82,6 @@ readours(Hash **tailp, char ***refp)
return nu; return nu;
} }
char *
matchcap(char *s, char *cap, int full)
{
if(strncmp(s, cap, strlen(cap)) == 0)
if(!full || strlen(s) == strlen(cap))
return s + strlen(cap);
return nil;
}
void
parsecaps(char *caps, Capset *cs)
{
char *p, *n;
for(p = caps; p != nil; p = n){
n = strchr(p, ' ');
if(n != nil)
*n++ = 0;
if(matchcap(p, "report-status", 1) != nil)
cs->report = 1;
if(matchcap(p, "side-band", 1) != nil)
cs->sideband = 1;
if(matchcap(p, "side-band-64k", 1) != nil)
cs->sideband64k = 1;
}
}
int int
sendpack(Conn *c) sendpack(Conn *c)
{ {