[C #] implement a git server with pure hosting

Source: Internet
Author: User

It is rumored that git is not used in the past few years.ProgramMember. It is rumored that git has been in full swing in recent years. Except for some public git platforms, most git servers are on Linux, and there are few windows options. As A. Net coders, I certainly hope to have a pure managed service.Code. After one night of study, I wrote a git server with pure hosting code for your reference.

 

Learning Materials: none.

 

Next, start the code. First add reference: gitsharp. Core, gitsharp. You can obtain it from git: // github.com/henon/gitsharp.git. Then,

 
1:UsingGitsharp. Core. Transport;
 
2:UsingSystem;
 
3:UsingSystem. IO;
 
4:UsingSystem. net;
5:UsingSystem. text;
 
6:UsingSystem. Text. regularexpressions;
 
7: 
 
8:NamespaceSamplegitserver
 
9:{
 
10:ClassProgram
 
11:{
 
12:Const StringPrefix =@ "Http: // localhost: 2034 /";
13:Const StringRepository_path =@ "F: \ repositories \ Git-debug-only";
 
14: 
 
15:Readonly StaticRegEx getinforefsregex =NewRegEx (prefix +@ "\ W {3, 10}/INFO/refs \? Service = .*");
 
16:Readonly StaticRegEx gituploadpackregex =NewRegEx (prefix +@ "\ W {3, 10}/Git-upload-pack");
17:Readonly StaticRegEx gitrecivepackregex =NewRegEx (prefix +@ "\ W {3, 10}/Git-receive-pack");
 
18: 
 
19:Static VoidMain (String[] ARGs)
 
20:{
 
21:VaR listener =NewHttplistener ();
 
22:Listener. prefixes. Add (prefix );
 
23: 
24:Console. writeline ("Listening :"+ Prefix );
 
25:Listener. Start ();
 
26: 
 
27:While(True)
 
28:{
 
29:VaR context = listener. getcontext ();
 
30:VaR url = context. Request. url. tostring ();
 
31: 
 
32:Console. writeline (URL );
 
33: 
34:If(Getinforefsregex. Match (URL). Success)
 
35:Getinforefs (context );
 
36:Else If(Gituploadpackregex. Match (URL). Success)
 
37:Gituploadpack (context );
 
38:Else If(Gitrecivepackregex. Match (URL). Success)
 
39:Gitrecivepack (context );
 
40:}
 
41:}
42: 
 
43:Private Static VoidGetinforefs (httplistenercontext context)
 
44:{
 
45:VaR request = context. request;
 
46:VaR response = context. response;
 
47:VaR project = request. url. pathandquery. Split ('/') [1];
 
48:VaR service = request. querystring ["Service"];
 
49: 
50:VaR directory = getdirectoryinfo (project );
 
51:If(Gitsharp. repository. isvalid (directory. fullname,True))
 
52:{
 
53:Response. statuscode = 200;
 
54:Response. contenttype = string. Format ("Application/X-{0}-advertisement", Service );
 
55:Setnocache (response );
 
56: 
 
57:VaR sb =NewStringbuilder ();
58:SB. append (formatmessage (string. Format ("# Service = {0} \ n", Service )));
 
59:SB. append (flushmessage ());
 
60:VaR bytes = encoding. ASCII. getbytes (sb. tostring ());
 
61:Response. outputstream. Write (bytes, 0, bytes. Length );
 
62: 
 
63:Using(VAR repository =NewGitsharp. Repository (directory. fullname ))
 
64:{
65:If(String. Equals ("Git-receive-pack", Service, stringcomparison. invariantcultureignorecase ))
 
66:{
 
67:Using(VAR pack =NewReceivepack (repository ))
 
68:{
 
69:Pack. sendadvertisedrefs (NewRefadvertiser. packetlineoutrefadvertiser (NewPacketlineout (response. outputstream )));
 
70:}
 
71: 
72:}
 
73:Else If(String. Equals ("Git-upload-pack", Service, stringcomparison. invariantcultureignorecase ))
 
74:{
 
75:Using(VAR pack =NewUploadpack (repository ))
 
76:{
 
77:Pack. sendadvertisedrefs (NewRefadvertiser. packetlineoutrefadvertiser (NewPacketlineout (response. outputstream )));
78:}
 
79:}
 
80:}
 
81:}
 
82:Else
 
83:{
 
84:Response. statuscode = 404;
 
85:}
 
86:Response. Close ();
 
87:}
 
88: 
89:Private Static VoidGituploadpack (httplistenercontext context)
 
90:{
 
91:VaR request = context. request;
 
92:VaR response = context. response;
 
93:VaR project = request. url. pathandquery. Split ('/') [1];
 
94: 
 
95:Response. contenttype ="Application/X-Git-upload-pack-result";
 
96:Setnocache (response );
97: 
 
98:VaR directory = getdirectoryinfo (project );
 
99:If(Gitsharp. repository. isvalid (directory. fullname,True))
 
100:{
 
101:Using(VAR repository =NewGitsharp. Repository (directory. fullname ))
 
102:Using(VAR pack =NewUploadpack (repository ))
 
103:{
104:Pack. setbidirectionalpipe (False);
 
105:Pack. Upload (request. inputstream, response. outputstream, response. outputstream );
 
106:}
 
107:}
 
108:Else
 
109:{
 
110:Response. statuscode = 404;
 
111:}
 
112:Response. Close ();
 
113:}
114: 
 
115:Private Static VoidGitrecivepack (httplistenercontext context)
 
116:{
 
117:VaR request = context. request;
 
118:VaR response = context. response;
 
119:VaR project = request. url. pathandquery. Split ('/') [1];
 
120: 
 
121:Response. contenttype ="Application/X-Git-receive-pack-result";
122:Setnocache (response );
 
123: 
 
124:VaR directory = getdirectoryinfo (project );
 
125:If(Gitsharp. repository. isvalid (directory. fullname,True))
 
126:{
 
127:Using(VAR repository =NewGitsharp. Repository (directory. fullname ))
 
128:Using(VAR pack =NewReceivepack (repository ))
 
129:{
130:Pack. setbidirectionalpipe (False);
 
131:Pack. Receive (request. inputstream, response. outputstream, response. outputstream );
 
132:}
 
133:}
 
134:Else
 
135:{
 
136:Response. statuscode = 404;
 
137:}
 
138:Response. Close ();
 
139:}
140: 
 
141:Private StaticString formatmessage (string input)
 
142:{
 
143:Return(Input. Length + 4). tostring ("X"). Padleft (4,'0') + Input;
 
144:}
 
145: 
 
146:Private StaticString flushmessage ()
 
147:{
148:Return "0000";
 
149:}
 
150: 
 
151:Private StaticDirectoryinfo getdirectoryinfo (string Project)
 
152:{
 
153:Return NewDirectoryinfo (path. Combine (repository_path, Project ));
 
154:}
 
155: 
156:Private Static VoidSetnocache (httplistenerresponse response)
 
157:{
 
158:Response. addheader ("Expires","Fri, 01 Jan 1980 00:00:00 GMT");
 
159:Response. addheader ("Pragma","No-Cache");
 
160:Response. addheader ("Cache-control","No-cache, Max-age = 0, must-revalidate");
 
161:}
 
162:}
163:}

Well, you may feel fooled after reading the code. I must admit that I only implement the server interface. PS, I won't tell you this code is copied.

 

the point is that all code is hosted. On github.com, there is a developing git server implemented by ASP. net mvc + EF + SQLite (Dudu recommendation: Use the Open Source ASP. net mvc program bonobo git server to build a git server ). After a few days of trial, I felt stable. The key point is that this service is in its early stages of development. You can create a git server that suits you best according to your needs. PS, the master of this repo is very active recently.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.