Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

BaseReceivePack.java 52KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_ATOMIC;
  45. import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_DELETE_REFS;
  46. import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_OFS_DELTA;
  47. import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_PUSH_OPTIONS;
  48. import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_QUIET;
  49. import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_REPORT_STATUS;
  50. import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_SIDE_BAND_64K;
  51. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT;
  52. import static org.eclipse.jgit.transport.SideBandOutputStream.CH_DATA;
  53. import static org.eclipse.jgit.transport.SideBandOutputStream.CH_ERROR;
  54. import static org.eclipse.jgit.transport.SideBandOutputStream.CH_PROGRESS;
  55. import static org.eclipse.jgit.transport.SideBandOutputStream.MAX_BUF;
  56. import java.io.EOFException;
  57. import java.io.IOException;
  58. import java.io.InputStream;
  59. import java.io.OutputStream;
  60. import java.text.MessageFormat;
  61. import java.util.ArrayList;
  62. import java.util.Collections;
  63. import java.util.HashSet;
  64. import java.util.List;
  65. import java.util.Map;
  66. import java.util.Set;
  67. import java.util.concurrent.TimeUnit;
  68. import org.eclipse.jgit.errors.InvalidObjectIdException;
  69. import org.eclipse.jgit.errors.MissingObjectException;
  70. import org.eclipse.jgit.errors.PackProtocolException;
  71. import org.eclipse.jgit.errors.TooLargePackException;
  72. import org.eclipse.jgit.internal.JGitText;
  73. import org.eclipse.jgit.internal.storage.file.PackLock;
  74. import org.eclipse.jgit.lib.BatchRefUpdate;
  75. import org.eclipse.jgit.lib.Config;
  76. import org.eclipse.jgit.lib.Config.SectionParser;
  77. import org.eclipse.jgit.lib.Constants;
  78. import org.eclipse.jgit.lib.NullProgressMonitor;
  79. import org.eclipse.jgit.lib.ObjectChecker;
  80. import org.eclipse.jgit.lib.ObjectId;
  81. import org.eclipse.jgit.lib.ObjectIdSubclassMap;
  82. import org.eclipse.jgit.lib.ObjectInserter;
  83. import org.eclipse.jgit.lib.PersonIdent;
  84. import org.eclipse.jgit.lib.ProgressMonitor;
  85. import org.eclipse.jgit.lib.Ref;
  86. import org.eclipse.jgit.lib.Repository;
  87. import org.eclipse.jgit.revwalk.ObjectWalk;
  88. import org.eclipse.jgit.revwalk.RevBlob;
  89. import org.eclipse.jgit.revwalk.RevCommit;
  90. import org.eclipse.jgit.revwalk.RevFlag;
  91. import org.eclipse.jgit.revwalk.RevObject;
  92. import org.eclipse.jgit.revwalk.RevSort;
  93. import org.eclipse.jgit.revwalk.RevTree;
  94. import org.eclipse.jgit.revwalk.RevWalk;
  95. import org.eclipse.jgit.transport.ReceiveCommand.Result;
  96. import org.eclipse.jgit.util.io.InterruptTimer;
  97. import org.eclipse.jgit.util.io.LimitedInputStream;
  98. import org.eclipse.jgit.util.io.TimeoutInputStream;
  99. import org.eclipse.jgit.util.io.TimeoutOutputStream;
  100. /**
  101. * Base implementation of the side of a push connection that receives objects.
  102. * <p>
  103. * Contains high-level operations for initializing and closing streams,
  104. * advertising refs, reading commands, and receiving and applying a pack.
  105. * Subclasses compose these operations into full service implementations.
  106. */
  107. public abstract class BaseReceivePack {
  108. /** Data in the first line of a request, the line itself plus capabilities. */
  109. public static class FirstLine {
  110. private final String line;
  111. private final Set<String> capabilities;
  112. /**
  113. * Parse the first line of a receive-pack request.
  114. *
  115. * @param line
  116. * line from the client.
  117. */
  118. public FirstLine(String line) {
  119. final HashSet<String> caps = new HashSet<String>();
  120. final int nul = line.indexOf('\0');
  121. if (nul >= 0) {
  122. for (String c : line.substring(nul + 1).split(" ")) //$NON-NLS-1$
  123. caps.add(c);
  124. this.line = line.substring(0, nul);
  125. } else
  126. this.line = line;
  127. this.capabilities = Collections.unmodifiableSet(caps);
  128. }
  129. /** @return non-capabilities part of the line. */
  130. public String getLine() {
  131. return line;
  132. }
  133. /** @return capabilities parsed from the line. */
  134. public Set<String> getCapabilities() {
  135. return capabilities;
  136. }
  137. }
  138. /** Database we write the stored objects into. */
  139. private final Repository db;
  140. /** Revision traversal support over {@link #db}. */
  141. private final RevWalk walk;
  142. /**
  143. * Is the client connection a bi-directional socket or pipe?
  144. * <p>
  145. * If true, this class assumes it can perform multiple read and write cycles
  146. * with the client over the input and output streams. This matches the
  147. * functionality available with a standard TCP/IP connection, or a local
  148. * operating system or in-memory pipe.
  149. * <p>
  150. * If false, this class runs in a read everything then output results mode,
  151. * making it suitable for single round-trip systems RPCs such as HTTP.
  152. */
  153. private boolean biDirectionalPipe = true;
  154. /** Expecting data after the pack footer */
  155. private boolean expectDataAfterPackFooter;
  156. /** Should an incoming transfer validate objects? */
  157. private ObjectChecker objectChecker;
  158. /** Should an incoming transfer permit create requests? */
  159. private boolean allowCreates;
  160. /** Should an incoming transfer permit delete requests? */
  161. private boolean allowAnyDeletes;
  162. private boolean allowBranchDeletes;
  163. /** Should an incoming transfer permit non-fast-forward requests? */
  164. private boolean allowNonFastForwards;
  165. /** Should an incoming transfer permit push options? **/
  166. private boolean allowPushOptions;
  167. /**
  168. * Should the requested ref updates be performed as a single atomic
  169. * transaction?
  170. */
  171. private boolean atomic;
  172. private boolean allowOfsDelta;
  173. private boolean allowQuiet = true;
  174. /** Identity to record action as within the reflog. */
  175. private PersonIdent refLogIdent;
  176. /** Hook used while advertising the refs to the client. */
  177. private AdvertiseRefsHook advertiseRefsHook;
  178. /** Filter used while advertising the refs to the client. */
  179. private RefFilter refFilter;
  180. /** Timeout in seconds to wait for client interaction. */
  181. private int timeout;
  182. /** Timer to manage {@link #timeout}. */
  183. private InterruptTimer timer;
  184. private TimeoutInputStream timeoutIn;
  185. // Original stream passed to init(), since rawOut may be wrapped in a
  186. // sideband.
  187. private OutputStream origOut;
  188. /** Raw input stream. */
  189. protected InputStream rawIn;
  190. /** Raw output stream. */
  191. protected OutputStream rawOut;
  192. /** Optional message output stream. */
  193. protected OutputStream msgOut;
  194. private SideBandOutputStream errOut;
  195. /** Packet line input stream around {@link #rawIn}. */
  196. protected PacketLineIn pckIn;
  197. /** Packet line output stream around {@link #rawOut}. */
  198. protected PacketLineOut pckOut;
  199. private final MessageOutputWrapper msgOutWrapper = new MessageOutputWrapper();
  200. private PackParser parser;
  201. /** The refs we advertised as existing at the start of the connection. */
  202. private Map<String, Ref> refs;
  203. /** All SHA-1s shown to the client, which can be possible edges. */
  204. private Set<ObjectId> advertisedHaves;
  205. /** Capabilities requested by the client. */
  206. private Set<String> enabledCapabilities;
  207. String userAgent;
  208. private Set<ObjectId> clientShallowCommits;
  209. private List<ReceiveCommand> commands;
  210. private StringBuilder advertiseError;
  211. /** If {@link BasePackPushConnection#CAPABILITY_SIDE_BAND_64K} is enabled. */
  212. private boolean sideBand;
  213. private boolean quiet;
  214. /** Lock around the received pack file, while updating refs. */
  215. private PackLock packLock;
  216. private boolean checkReferencedIsReachable;
  217. /** Git object size limit */
  218. private long maxObjectSizeLimit;
  219. /** Total pack size limit */
  220. private long maxPackSizeLimit = -1;
  221. /** The size of the received pack, including index size */
  222. private Long packSize;
  223. private PushCertificateParser pushCertificateParser;
  224. private SignedPushConfig signedPushConfig;
  225. private PushCertificate pushCert;
  226. /**
  227. * Get the push certificate used to verify the pusher's identity.
  228. * <p>
  229. * Only valid after commands are read from the wire.
  230. *
  231. * @return the parsed certificate, or null if push certificates are disabled
  232. * or no cert was presented by the client.
  233. * @since 4.1
  234. */
  235. public PushCertificate getPushCertificate() {
  236. return pushCert;
  237. }
  238. /**
  239. * Set the push certificate used to verify the pusher's identity.
  240. * <p>
  241. * Should only be called if reconstructing an instance without going through
  242. * the normal {@link #recvCommands()} flow.
  243. *
  244. * @param cert
  245. * the push certificate to set.
  246. * @since 4.1
  247. */
  248. public void setPushCertificate(PushCertificate cert) {
  249. pushCert = cert;
  250. }
  251. /**
  252. * Create a new pack receive for an open repository.
  253. *
  254. * @param into
  255. * the destination repository.
  256. */
  257. protected BaseReceivePack(final Repository into) {
  258. db = into;
  259. walk = new RevWalk(db);
  260. TransferConfig tc = db.getConfig().get(TransferConfig.KEY);
  261. objectChecker = tc.newReceiveObjectChecker();
  262. ReceiveConfig rc = db.getConfig().get(ReceiveConfig.KEY);
  263. allowCreates = rc.allowCreates;
  264. allowAnyDeletes = true;
  265. allowBranchDeletes = rc.allowDeletes;
  266. allowNonFastForwards = rc.allowNonFastForwards;
  267. allowOfsDelta = rc.allowOfsDelta;
  268. allowPushOptions = rc.allowPushOptions;
  269. advertiseRefsHook = AdvertiseRefsHook.DEFAULT;
  270. refFilter = RefFilter.DEFAULT;
  271. advertisedHaves = new HashSet<ObjectId>();
  272. clientShallowCommits = new HashSet<ObjectId>();
  273. signedPushConfig = rc.signedPush;
  274. }
  275. /** Configuration for receive operations. */
  276. protected static class ReceiveConfig {
  277. static final SectionParser<ReceiveConfig> KEY = new SectionParser<ReceiveConfig>() {
  278. public ReceiveConfig parse(final Config cfg) {
  279. return new ReceiveConfig(cfg);
  280. }
  281. };
  282. final boolean allowCreates;
  283. final boolean allowDeletes;
  284. final boolean allowNonFastForwards;
  285. final boolean allowOfsDelta;
  286. final boolean allowPushOptions;
  287. final SignedPushConfig signedPush;
  288. ReceiveConfig(final Config config) {
  289. allowCreates = true;
  290. allowDeletes = !config.getBoolean("receive", "denydeletes", false); //$NON-NLS-1$ //$NON-NLS-2$
  291. allowNonFastForwards = !config.getBoolean("receive", //$NON-NLS-1$
  292. "denynonfastforwards", false); //$NON-NLS-1$
  293. allowOfsDelta = config.getBoolean("repack", "usedeltabaseoffset", //$NON-NLS-1$ //$NON-NLS-2$
  294. true);
  295. allowPushOptions = config.getBoolean("receive", "pushoptions", //$NON-NLS-1$ //$NON-NLS-2$
  296. false);
  297. signedPush = SignedPushConfig.KEY.parse(config);
  298. }
  299. }
  300. /**
  301. * Output stream that wraps the current {@link #msgOut}.
  302. * <p>
  303. * We don't want to expose {@link #msgOut} directly because it can change
  304. * several times over the course of a session.
  305. */
  306. class MessageOutputWrapper extends OutputStream {
  307. @Override
  308. public void write(int ch) {
  309. if (msgOut != null) {
  310. try {
  311. msgOut.write(ch);
  312. } catch (IOException e) {
  313. // Ignore write failures.
  314. }
  315. }
  316. }
  317. @Override
  318. public void write(byte[] b, int off, int len) {
  319. if (msgOut != null) {
  320. try {
  321. msgOut.write(b, off, len);
  322. } catch (IOException e) {
  323. // Ignore write failures.
  324. }
  325. }
  326. }
  327. @Override
  328. public void write(byte[] b) {
  329. write(b, 0, b.length);
  330. }
  331. @Override
  332. public void flush() {
  333. if (msgOut != null) {
  334. try {
  335. msgOut.flush();
  336. } catch (IOException e) {
  337. // Ignore write failures.
  338. }
  339. }
  340. }
  341. }
  342. /** @return the process name used for pack lock messages. */
  343. protected abstract String getLockMessageProcessName();
  344. /** @return the repository this receive completes into. */
  345. public final Repository getRepository() {
  346. return db;
  347. }
  348. /** @return the RevWalk instance used by this connection. */
  349. public final RevWalk getRevWalk() {
  350. return walk;
  351. }
  352. /**
  353. * Get refs which were advertised to the client.
  354. *
  355. * @return all refs which were advertised to the client, or null if
  356. * {@link #setAdvertisedRefs(Map, Set)} has not been called yet.
  357. */
  358. public final Map<String, Ref> getAdvertisedRefs() {
  359. return refs;
  360. }
  361. /**
  362. * Set the refs advertised by this ReceivePack.
  363. * <p>
  364. * Intended to be called from a {@link PreReceiveHook}.
  365. *
  366. * @param allRefs
  367. * explicit set of references to claim as advertised by this
  368. * ReceivePack instance. This overrides any references that
  369. * may exist in the source repository. The map is passed
  370. * to the configured {@link #getRefFilter()}. If null, assumes
  371. * all refs were advertised.
  372. * @param additionalHaves
  373. * explicit set of additional haves to claim as advertised. If
  374. * null, assumes the default set of additional haves from the
  375. * repository.
  376. */
  377. public void setAdvertisedRefs(Map<String, Ref> allRefs, Set<ObjectId> additionalHaves) {
  378. refs = allRefs != null ? allRefs : db.getAllRefs();
  379. refs = refFilter.filter(refs);
  380. Ref head = refs.get(Constants.HEAD);
  381. if (head != null && head.isSymbolic())
  382. refs.remove(Constants.HEAD);
  383. for (Ref ref : refs.values()) {
  384. if (ref.getObjectId() != null)
  385. advertisedHaves.add(ref.getObjectId());
  386. }
  387. if (additionalHaves != null)
  388. advertisedHaves.addAll(additionalHaves);
  389. else
  390. advertisedHaves.addAll(db.getAdditionalHaves());
  391. }
  392. /**
  393. * Get objects advertised to the client.
  394. *
  395. * @return the set of objects advertised to the as present in this repository,
  396. * or null if {@link #setAdvertisedRefs(Map, Set)} has not been called
  397. * yet.
  398. */
  399. public final Set<ObjectId> getAdvertisedObjects() {
  400. return advertisedHaves;
  401. }
  402. /**
  403. * @return true if this instance will validate all referenced, but not
  404. * supplied by the client, objects are reachable from another
  405. * reference.
  406. */
  407. public boolean isCheckReferencedObjectsAreReachable() {
  408. return checkReferencedIsReachable;
  409. }
  410. /**
  411. * Validate all referenced but not supplied objects are reachable.
  412. * <p>
  413. * If enabled, this instance will verify that references to objects not
  414. * contained within the received pack are already reachable through at least
  415. * one other reference displayed as part of {@link #getAdvertisedRefs()}.
  416. * <p>
  417. * This feature is useful when the application doesn't trust the client to
  418. * not provide a forged SHA-1 reference to an object, in an attempt to
  419. * access parts of the DAG that they aren't allowed to see and which have
  420. * been hidden from them via the configured {@link AdvertiseRefsHook} or
  421. * {@link RefFilter}.
  422. * <p>
  423. * Enabling this feature may imply at least some, if not all, of the same
  424. * functionality performed by {@link #setCheckReceivedObjects(boolean)}.
  425. * Applications are encouraged to enable both features, if desired.
  426. *
  427. * @param b
  428. * {@code true} to enable the additional check.
  429. */
  430. public void setCheckReferencedObjectsAreReachable(boolean b) {
  431. this.checkReferencedIsReachable = b;
  432. }
  433. /**
  434. * @return true if this class expects a bi-directional pipe opened between
  435. * the client and itself. The default is true.
  436. */
  437. public boolean isBiDirectionalPipe() {
  438. return biDirectionalPipe;
  439. }
  440. /**
  441. * @param twoWay
  442. * if true, this class will assume the socket is a fully
  443. * bidirectional pipe between the two peers and takes advantage
  444. * of that by first transmitting the known refs, then waiting to
  445. * read commands. If false, this class assumes it must read the
  446. * commands before writing output and does not perform the
  447. * initial advertising.
  448. */
  449. public void setBiDirectionalPipe(final boolean twoWay) {
  450. biDirectionalPipe = twoWay;
  451. }
  452. /** @return true if there is data expected after the pack footer. */
  453. public boolean isExpectDataAfterPackFooter() {
  454. return expectDataAfterPackFooter;
  455. }
  456. /**
  457. * @param e
  458. * true if there is additional data in InputStream after pack.
  459. */
  460. public void setExpectDataAfterPackFooter(boolean e) {
  461. expectDataAfterPackFooter = e;
  462. }
  463. /**
  464. * @return true if this instance will verify received objects are formatted
  465. * correctly. Validating objects requires more CPU time on this side
  466. * of the connection.
  467. */
  468. public boolean isCheckReceivedObjects() {
  469. return objectChecker != null;
  470. }
  471. /**
  472. * @param check
  473. * true to enable checking received objects; false to assume all
  474. * received objects are valid.
  475. * @see #setObjectChecker(ObjectChecker)
  476. */
  477. public void setCheckReceivedObjects(final boolean check) {
  478. if (check && objectChecker == null)
  479. setObjectChecker(new ObjectChecker());
  480. else if (!check && objectChecker != null)
  481. setObjectChecker(null);
  482. }
  483. /**
  484. * @param impl if non-null the object checking instance to verify each
  485. * received object with; null to disable object checking.
  486. * @since 3.4
  487. */
  488. public void setObjectChecker(ObjectChecker impl) {
  489. objectChecker = impl;
  490. }
  491. /** @return true if the client can request refs to be created. */
  492. public boolean isAllowCreates() {
  493. return allowCreates;
  494. }
  495. /**
  496. * @param canCreate
  497. * true to permit create ref commands to be processed.
  498. */
  499. public void setAllowCreates(final boolean canCreate) {
  500. allowCreates = canCreate;
  501. }
  502. /** @return true if the client can request refs to be deleted. */
  503. public boolean isAllowDeletes() {
  504. return allowAnyDeletes;
  505. }
  506. /**
  507. * @param canDelete
  508. * true to permit delete ref commands to be processed.
  509. */
  510. public void setAllowDeletes(final boolean canDelete) {
  511. allowAnyDeletes = canDelete;
  512. }
  513. /**
  514. * @return true if the client can delete from {@code refs/heads/}.
  515. * @since 3.6
  516. */
  517. public boolean isAllowBranchDeletes() {
  518. return allowBranchDeletes;
  519. }
  520. /**
  521. * @param canDelete
  522. * true to permit deletion of branches from the
  523. * {@code refs/heads/} namespace.
  524. * @since 3.6
  525. */
  526. public void setAllowBranchDeletes(boolean canDelete) {
  527. allowBranchDeletes = canDelete;
  528. }
  529. /**
  530. * @return true if the client can request non-fast-forward updates of a ref,
  531. * possibly making objects unreachable.
  532. */
  533. public boolean isAllowNonFastForwards() {
  534. return allowNonFastForwards;
  535. }
  536. /**
  537. * @param canRewind
  538. * true to permit the client to ask for non-fast-forward updates
  539. * of an existing ref.
  540. */
  541. public void setAllowNonFastForwards(final boolean canRewind) {
  542. allowNonFastForwards = canRewind;
  543. }
  544. /**
  545. * @return true if the client's commands should be performed as a single
  546. * atomic transaction.
  547. * @since 4.4
  548. */
  549. public boolean isAtomic() {
  550. return atomic;
  551. }
  552. /**
  553. * @param atomic
  554. * true to perform the client's commands as a single atomic
  555. * transaction.
  556. * @since 4.4
  557. */
  558. public void setAtomic(boolean atomic) {
  559. this.atomic = atomic;
  560. }
  561. /** @return identity of the user making the changes in the reflog. */
  562. public PersonIdent getRefLogIdent() {
  563. return refLogIdent;
  564. }
  565. /**
  566. * Set the identity of the user appearing in the affected reflogs.
  567. * <p>
  568. * The timestamp portion of the identity is ignored. A new identity with the
  569. * current timestamp will be created automatically when the updates occur
  570. * and the log records are written.
  571. *
  572. * @param pi
  573. * identity of the user. If null the identity will be
  574. * automatically determined based on the repository
  575. * configuration.
  576. */
  577. public void setRefLogIdent(final PersonIdent pi) {
  578. refLogIdent = pi;
  579. }
  580. /** @return the hook used while advertising the refs to the client */
  581. public AdvertiseRefsHook getAdvertiseRefsHook() {
  582. return advertiseRefsHook;
  583. }
  584. /** @return the filter used while advertising the refs to the client */
  585. public RefFilter getRefFilter() {
  586. return refFilter;
  587. }
  588. /**
  589. * Set the hook used while advertising the refs to the client.
  590. * <p>
  591. * If the {@link AdvertiseRefsHook} chooses to call
  592. * {@link #setAdvertisedRefs(Map,Set)}, only refs set by this hook
  593. * <em>and</em> selected by the {@link RefFilter} will be shown to the client.
  594. * Clients may still attempt to create or update a reference not advertised by
  595. * the configured {@link AdvertiseRefsHook}. These attempts should be rejected
  596. * by a matching {@link PreReceiveHook}.
  597. *
  598. * @param advertiseRefsHook
  599. * the hook; may be null to show all refs.
  600. */
  601. public void setAdvertiseRefsHook(final AdvertiseRefsHook advertiseRefsHook) {
  602. if (advertiseRefsHook != null)
  603. this.advertiseRefsHook = advertiseRefsHook;
  604. else
  605. this.advertiseRefsHook = AdvertiseRefsHook.DEFAULT;
  606. }
  607. /**
  608. * Set the filter used while advertising the refs to the client.
  609. * <p>
  610. * Only refs allowed by this filter will be shown to the client.
  611. * The filter is run against the refs specified by the
  612. * {@link AdvertiseRefsHook} (if applicable).
  613. *
  614. * @param refFilter
  615. * the filter; may be null to show all refs.
  616. */
  617. public void setRefFilter(final RefFilter refFilter) {
  618. this.refFilter = refFilter != null ? refFilter : RefFilter.DEFAULT;
  619. }
  620. /** @return timeout (in seconds) before aborting an IO operation. */
  621. public int getTimeout() {
  622. return timeout;
  623. }
  624. /**
  625. * Set the timeout before willing to abort an IO call.
  626. *
  627. * @param seconds
  628. * number of seconds to wait (with no data transfer occurring)
  629. * before aborting an IO read or write operation with the
  630. * connected client.
  631. */
  632. public void setTimeout(final int seconds) {
  633. timeout = seconds;
  634. }
  635. /**
  636. * Set the maximum allowed Git object size.
  637. * <p>
  638. * If an object is larger than the given size the pack-parsing will throw an
  639. * exception aborting the receive-pack operation.
  640. *
  641. * @param limit
  642. * the Git object size limit. If zero then there is not limit.
  643. */
  644. public void setMaxObjectSizeLimit(final long limit) {
  645. maxObjectSizeLimit = limit;
  646. }
  647. /**
  648. * Set the maximum allowed pack size.
  649. * <p>
  650. * A pack exceeding this size will be rejected.
  651. *
  652. * @param limit
  653. * the pack size limit, in bytes
  654. *
  655. * @since 3.3
  656. */
  657. public void setMaxPackSizeLimit(final long limit) {
  658. if (limit < 0)
  659. throw new IllegalArgumentException(MessageFormat.format(
  660. JGitText.get().receivePackInvalidLimit, Long.valueOf(limit)));
  661. maxPackSizeLimit = limit;
  662. }
  663. /**
  664. * Check whether the client expects a side-band stream.
  665. *
  666. * @return true if the client has advertised a side-band capability, false
  667. * otherwise.
  668. * @throws RequestNotYetReadException
  669. * if the client's request has not yet been read from the wire, so
  670. * we do not know if they expect side-band. Note that the client
  671. * may have already written the request, it just has not been
  672. * read.
  673. */
  674. public boolean isSideBand() throws RequestNotYetReadException {
  675. checkRequestWasRead();
  676. return enabledCapabilities.contains(CAPABILITY_SIDE_BAND_64K);
  677. }
  678. /**
  679. * @return true if clients may request avoiding noisy progress messages.
  680. * @since 4.0
  681. */
  682. public boolean isAllowQuiet() {
  683. return allowQuiet;
  684. }
  685. /**
  686. * Configure if clients may request the server skip noisy messages.
  687. *
  688. * @param allow
  689. * true to allow clients to request quiet behavior; false to
  690. * refuse quiet behavior and send messages anyway. This may be
  691. * necessary if processing is slow and the client-server network
  692. * connection can timeout.
  693. * @since 4.0
  694. */
  695. public void setAllowQuiet(boolean allow) {
  696. allowQuiet = allow;
  697. }
  698. /**
  699. * @return true if the server supports receiving push options.
  700. * @since 4.5
  701. */
  702. public boolean isAllowPushOptions() {
  703. return allowPushOptions;
  704. }
  705. /**
  706. * Configure if the server supports receiving push options.
  707. *
  708. * @param allow
  709. * true to optionally accept option strings from the client.
  710. * @since 4.5
  711. */
  712. public void setAllowPushOptions(boolean allow) {
  713. allowPushOptions = allow;
  714. }
  715. /**
  716. * True if the client wants less verbose output.
  717. *
  718. * @return true if the client has requested the server to be less verbose.
  719. * @throws RequestNotYetReadException
  720. * if the client's request has not yet been read from the wire,
  721. * so we do not know if they expect side-band. Note that the
  722. * client may have already written the request, it just has not
  723. * been read.
  724. * @since 4.0
  725. */
  726. public boolean isQuiet() throws RequestNotYetReadException {
  727. checkRequestWasRead();
  728. return quiet;
  729. }
  730. /**
  731. * Set the configuration for push certificate verification.
  732. *
  733. * @param cfg
  734. * new configuration; if this object is null or its {@link
  735. * SignedPushConfig#getCertNonceSeed()} is null, push certificate
  736. * verification will be disabled.
  737. * @since 4.1
  738. */
  739. public void setSignedPushConfig(SignedPushConfig cfg) {
  740. signedPushConfig = cfg;
  741. }
  742. private PushCertificateParser getPushCertificateParser() {
  743. if (pushCertificateParser == null) {
  744. pushCertificateParser = new PushCertificateParser(db, signedPushConfig);
  745. }
  746. return pushCertificateParser;
  747. }
  748. /**
  749. * Get the user agent of the client.
  750. * <p>
  751. * If the client is new enough to use {@code agent=} capability that value
  752. * will be returned. Older HTTP clients may also supply their version using
  753. * the HTTP {@code User-Agent} header. The capability overrides the HTTP
  754. * header if both are available.
  755. * <p>
  756. * When an HTTP request has been received this method returns the HTTP
  757. * {@code User-Agent} header value until capabilities have been parsed.
  758. *
  759. * @return user agent supplied by the client. Available only if the client
  760. * is new enough to advertise its user agent.
  761. * @since 4.0
  762. */
  763. public String getPeerUserAgent() {
  764. return UserAgent.getAgent(enabledCapabilities, userAgent);
  765. }
  766. /** @return all of the command received by the current request. */
  767. public List<ReceiveCommand> getAllCommands() {
  768. return Collections.unmodifiableList(commands);
  769. }
  770. /**
  771. * Send an error message to the client.
  772. * <p>
  773. * If any error messages are sent before the references are advertised to
  774. * the client, the errors will be sent instead of the advertisement and the
  775. * receive operation will be aborted. All clients should receive and display
  776. * such early stage errors.
  777. * <p>
  778. * If the reference advertisements have already been sent, messages are sent
  779. * in a side channel. If the client doesn't support receiving messages, the
  780. * message will be discarded, with no other indication to the caller or to
  781. * the client.
  782. * <p>
  783. * {@link PreReceiveHook}s should always try to use
  784. * {@link ReceiveCommand#setResult(Result, String)} with a result status of
  785. * {@link Result#REJECTED_OTHER_REASON} to indicate any reasons for
  786. * rejecting an update. Messages attached to a command are much more likely
  787. * to be returned to the client.
  788. *
  789. * @param what
  790. * string describing the problem identified by the hook. The
  791. * string must not end with an LF, and must not contain an LF.
  792. */
  793. public void sendError(final String what) {
  794. if (refs == null) {
  795. if (advertiseError == null)
  796. advertiseError = new StringBuilder();
  797. advertiseError.append(what).append('\n');
  798. } else {
  799. msgOutWrapper.write(Constants.encode("error: " + what + "\n")); //$NON-NLS-1$ //$NON-NLS-2$
  800. }
  801. }
  802. private void fatalError(String msg) {
  803. if (errOut != null) {
  804. try {
  805. errOut.write(Constants.encode(msg));
  806. errOut.flush();
  807. } catch (IOException e) {
  808. // Ignore write failures
  809. }
  810. } else {
  811. sendError(msg);
  812. }
  813. }
  814. /**
  815. * Send a message to the client, if it supports receiving them.
  816. * <p>
  817. * If the client doesn't support receiving messages, the message will be
  818. * discarded, with no other indication to the caller or to the client.
  819. *
  820. * @param what
  821. * string describing the problem identified by the hook. The
  822. * string must not end with an LF, and must not contain an LF.
  823. */
  824. public void sendMessage(final String what) {
  825. msgOutWrapper.write(Constants.encode(what + "\n")); //$NON-NLS-1$
  826. }
  827. /** @return an underlying stream for sending messages to the client. */
  828. public OutputStream getMessageOutputStream() {
  829. return msgOutWrapper;
  830. }
  831. /**
  832. * Get the size of the received pack file including the index size.
  833. *
  834. * This can only be called if the pack is already received.
  835. *
  836. * @return the size of the received pack including index size
  837. * @throws IllegalStateException
  838. * if called before the pack has been received
  839. * @since 3.3
  840. */
  841. public long getPackSize() {
  842. if (packSize != null)
  843. return packSize.longValue();
  844. throw new IllegalStateException(JGitText.get().packSizeNotSetYet);
  845. }
  846. /**
  847. * Get the commits from the client's shallow file.
  848. *
  849. * @return if the client is a shallow repository, the list of edge commits
  850. * that define the client's shallow boundary. Empty set if the client
  851. * is earlier than Git 1.9, or is a full clone.
  852. * @since 3.5
  853. */
  854. protected Set<ObjectId> getClientShallowCommits() {
  855. return clientShallowCommits;
  856. }
  857. /** @return true if any commands to be executed have been read. */
  858. protected boolean hasCommands() {
  859. return !commands.isEmpty();
  860. }
  861. /** @return true if an error occurred that should be advertised. */
  862. protected boolean hasError() {
  863. return advertiseError != null;
  864. }
  865. /**
  866. * Initialize the instance with the given streams.
  867. *
  868. * @param input
  869. * raw input to read client commands and pack data from. Caller
  870. * must ensure the input is buffered, otherwise read performance
  871. * may suffer.
  872. * @param output
  873. * response back to the Git network client. Caller must ensure
  874. * the output is buffered, otherwise write performance may
  875. * suffer.
  876. * @param messages
  877. * secondary "notice" channel to send additional messages out
  878. * through. When run over SSH this should be tied back to the
  879. * standard error channel of the command execution. For most
  880. * other network connections this should be null.
  881. */
  882. protected void init(final InputStream input, final OutputStream output,
  883. final OutputStream messages) {
  884. origOut = output;
  885. rawIn = input;
  886. rawOut = output;
  887. msgOut = messages;
  888. if (timeout > 0) {
  889. final Thread caller = Thread.currentThread();
  890. timer = new InterruptTimer(caller.getName() + "-Timer"); //$NON-NLS-1$
  891. timeoutIn = new TimeoutInputStream(rawIn, timer);
  892. TimeoutOutputStream o = new TimeoutOutputStream(rawOut, timer);
  893. timeoutIn.setTimeout(timeout * 1000);
  894. o.setTimeout(timeout * 1000);
  895. rawIn = timeoutIn;
  896. rawOut = o;
  897. }
  898. if (maxPackSizeLimit >= 0)
  899. rawIn = new LimitedInputStream(rawIn, maxPackSizeLimit) {
  900. @Override
  901. protected void limitExceeded() throws TooLargePackException {
  902. throw new TooLargePackException(limit);
  903. }
  904. };
  905. pckIn = new PacketLineIn(rawIn);
  906. pckOut = new PacketLineOut(rawOut);
  907. pckOut.setFlushOnEnd(false);
  908. enabledCapabilities = new HashSet<String>();
  909. commands = new ArrayList<ReceiveCommand>();
  910. }
  911. /** @return advertised refs, or the default if not explicitly advertised. */
  912. protected Map<String, Ref> getAdvertisedOrDefaultRefs() {
  913. if (refs == null)
  914. setAdvertisedRefs(null, null);
  915. return refs;
  916. }
  917. /**
  918. * Receive a pack from the stream and check connectivity if necessary.
  919. *
  920. * @throws IOException
  921. * an error occurred during unpacking or connectivity checking.
  922. */
  923. protected void receivePackAndCheckConnectivity() throws IOException {
  924. receivePack();
  925. if (needCheckConnectivity())
  926. checkConnectivity();
  927. parser = null;
  928. }
  929. /**
  930. * Unlock the pack written by this object.
  931. *
  932. * @throws IOException
  933. * the pack could not be unlocked.
  934. */
  935. protected void unlockPack() throws IOException {
  936. if (packLock != null) {
  937. packLock.unlock();
  938. packLock = null;
  939. }
  940. }
  941. /**
  942. * Generate an advertisement of available refs and capabilities.
  943. *
  944. * @param adv
  945. * the advertisement formatter.
  946. * @throws IOException
  947. * the formatter failed to write an advertisement.
  948. * @throws ServiceMayNotContinueException
  949. * the hook denied advertisement.
  950. */
  951. public void sendAdvertisedRefs(final RefAdvertiser adv)
  952. throws IOException, ServiceMayNotContinueException {
  953. if (advertiseError != null) {
  954. adv.writeOne("ERR " + advertiseError); //$NON-NLS-1$
  955. return;
  956. }
  957. try {
  958. advertiseRefsHook.advertiseRefs(this);
  959. } catch (ServiceMayNotContinueException fail) {
  960. if (fail.getMessage() != null) {
  961. adv.writeOne("ERR " + fail.getMessage()); //$NON-NLS-1$
  962. fail.setOutput();
  963. }
  964. throw fail;
  965. }
  966. adv.init(db);
  967. adv.advertiseCapability(CAPABILITY_SIDE_BAND_64K);
  968. adv.advertiseCapability(CAPABILITY_DELETE_REFS);
  969. adv.advertiseCapability(CAPABILITY_REPORT_STATUS);
  970. if (allowQuiet)
  971. adv.advertiseCapability(CAPABILITY_QUIET);
  972. String nonce = getPushCertificateParser().getAdvertiseNonce();
  973. if (nonce != null) {
  974. adv.advertiseCapability(nonce);
  975. }
  976. if (db.getRefDatabase().performsAtomicTransactions())
  977. adv.advertiseCapability(CAPABILITY_ATOMIC);
  978. if (allowOfsDelta)
  979. adv.advertiseCapability(CAPABILITY_OFS_DELTA);
  980. if (allowPushOptions) {
  981. adv.advertiseCapability(CAPABILITY_PUSH_OPTIONS);
  982. }
  983. adv.advertiseCapability(OPTION_AGENT, UserAgent.get());
  984. adv.send(getAdvertisedOrDefaultRefs());
  985. for (ObjectId obj : advertisedHaves)
  986. adv.advertiseHave(obj);
  987. if (adv.isEmpty())
  988. adv.advertiseId(ObjectId.zeroId(), "capabilities^{}"); //$NON-NLS-1$
  989. adv.end();
  990. }
  991. /**
  992. * Receive a list of commands from the input.
  993. *
  994. * @throws IOException
  995. */
  996. protected void recvCommands() throws IOException {
  997. PushCertificateParser certParser = getPushCertificateParser();
  998. boolean firstPkt = true;
  999. try {
  1000. for (;;) {
  1001. String line;
  1002. try {
  1003. line = pckIn.readString();
  1004. } catch (EOFException eof) {
  1005. if (commands.isEmpty())
  1006. return;
  1007. throw eof;
  1008. }
  1009. if (line == PacketLineIn.END) {
  1010. break;
  1011. }
  1012. if (line.length() >= 48 && line.startsWith("shallow ")) { //$NON-NLS-1$
  1013. parseShallow(line.substring(8, 48));
  1014. continue;
  1015. }
  1016. if (firstPkt) {
  1017. firstPkt = false;
  1018. FirstLine firstLine = new FirstLine(line);
  1019. enabledCapabilities = firstLine.getCapabilities();
  1020. line = firstLine.getLine();
  1021. enableCapabilities();
  1022. if (line.equals(GitProtocolConstants.OPTION_PUSH_CERT)) {
  1023. certParser.receiveHeader(pckIn, !isBiDirectionalPipe());
  1024. continue;
  1025. }
  1026. }
  1027. if (line.equals(PushCertificateParser.BEGIN_SIGNATURE)) {
  1028. certParser.receiveSignature(pckIn);
  1029. continue;
  1030. }
  1031. ReceiveCommand cmd = parseCommand(line);
  1032. if (cmd.getRefName().equals(Constants.HEAD)) {
  1033. cmd.setResult(Result.REJECTED_CURRENT_BRANCH);
  1034. } else {
  1035. cmd.setRef(refs.get(cmd.getRefName()));
  1036. }
  1037. commands.add(cmd);
  1038. if (certParser.enabled()) {
  1039. certParser.addCommand(cmd);
  1040. }
  1041. }
  1042. pushCert = certParser.build();
  1043. } catch (PackProtocolException e) {
  1044. if (sideBand) {
  1045. try {
  1046. pckIn.discardUntilEnd();
  1047. } catch (IOException e2) {
  1048. // Ignore read failures attempting to discard.
  1049. }
  1050. }
  1051. fatalError(e.getMessage());
  1052. throw e;
  1053. }
  1054. }
  1055. private void parseShallow(String idStr) throws PackProtocolException {
  1056. ObjectId id;
  1057. try {
  1058. id = ObjectId.fromString(idStr);
  1059. } catch (InvalidObjectIdException e) {
  1060. throw new PackProtocolException(e.getMessage(), e);
  1061. }
  1062. clientShallowCommits.add(id);
  1063. }
  1064. static ReceiveCommand parseCommand(String line) throws PackProtocolException {
  1065. if (line == null || line.length() < 83) {
  1066. throw new PackProtocolException(
  1067. JGitText.get().errorInvalidProtocolWantedOldNewRef);
  1068. }
  1069. String oldStr = line.substring(0, 40);
  1070. String newStr = line.substring(41, 81);
  1071. ObjectId oldId, newId;
  1072. try {
  1073. oldId = ObjectId.fromString(oldStr);
  1074. newId = ObjectId.fromString(newStr);
  1075. } catch (InvalidObjectIdException e) {
  1076. throw new PackProtocolException(
  1077. JGitText.get().errorInvalidProtocolWantedOldNewRef, e);
  1078. }
  1079. String name = line.substring(82);
  1080. if (!Repository.isValidRefName(name)) {
  1081. throw new PackProtocolException(
  1082. JGitText.get().errorInvalidProtocolWantedOldNewRef);
  1083. }
  1084. return new ReceiveCommand(oldId, newId, name);
  1085. }
  1086. /** Enable capabilities based on a previously read capabilities line. */
  1087. protected void enableCapabilities() {
  1088. sideBand = isCapabilityEnabled(CAPABILITY_SIDE_BAND_64K);
  1089. quiet = allowQuiet && isCapabilityEnabled(CAPABILITY_QUIET);
  1090. if (sideBand) {
  1091. OutputStream out = rawOut;
  1092. rawOut = new SideBandOutputStream(CH_DATA, MAX_BUF, out);
  1093. msgOut = new SideBandOutputStream(CH_PROGRESS, MAX_BUF, out);
  1094. errOut = new SideBandOutputStream(CH_ERROR, MAX_BUF, out);
  1095. pckOut = new PacketLineOut(rawOut);
  1096. pckOut.setFlushOnEnd(false);
  1097. }
  1098. }
  1099. /**
  1100. * Check if the peer requested a capability.
  1101. *
  1102. * @param name
  1103. * protocol name identifying the capability.
  1104. * @return true if the peer requested the capability to be enabled.
  1105. */
  1106. protected boolean isCapabilityEnabled(String name) {
  1107. return enabledCapabilities.contains(name);
  1108. }
  1109. void checkRequestWasRead() {
  1110. if (enabledCapabilities == null)
  1111. throw new RequestNotYetReadException();
  1112. }
  1113. /** @return true if a pack is expected based on the list of commands. */
  1114. protected boolean needPack() {
  1115. for (final ReceiveCommand cmd : commands) {
  1116. if (cmd.getType() != ReceiveCommand.Type.DELETE)
  1117. return true;
  1118. }
  1119. return false;
  1120. }
  1121. /**
  1122. * Receive a pack from the input and store it in the repository.
  1123. *
  1124. * @throws IOException
  1125. * an error occurred reading or indexing the pack.
  1126. */
  1127. private void receivePack() throws IOException {
  1128. // It might take the client a while to pack the objects it needs
  1129. // to send to us. We should increase our timeout so we don't
  1130. // abort while the client is computing.
  1131. //
  1132. if (timeoutIn != null)
  1133. timeoutIn.setTimeout(10 * timeout * 1000);
  1134. ProgressMonitor receiving = NullProgressMonitor.INSTANCE;
  1135. ProgressMonitor resolving = NullProgressMonitor.INSTANCE;
  1136. if (sideBand && !quiet)
  1137. resolving = new SideBandProgressMonitor(msgOut);
  1138. try (ObjectInserter ins = db.newObjectInserter()) {
  1139. String lockMsg = "jgit receive-pack"; //$NON-NLS-1$
  1140. if (getRefLogIdent() != null)
  1141. lockMsg += " from " + getRefLogIdent().toExternalString(); //$NON-NLS-1$
  1142. parser = ins.newPackParser(rawIn);
  1143. parser.setAllowThin(true);
  1144. parser.setNeedNewObjectIds(checkReferencedIsReachable);
  1145. parser.setNeedBaseObjectIds(checkReferencedIsReachable);
  1146. parser.setCheckEofAfterPackFooter(!biDirectionalPipe
  1147. && !isExpectDataAfterPackFooter());
  1148. parser.setExpectDataAfterPackFooter(isExpectDataAfterPackFooter());
  1149. parser.setObjectChecker(objectChecker);
  1150. parser.setLockMessage(lockMsg);
  1151. parser.setMaxObjectSizeLimit(maxObjectSizeLimit);
  1152. packLock = parser.parse(receiving, resolving);
  1153. packSize = Long.valueOf(parser.getPackSize());
  1154. ins.flush();
  1155. }
  1156. if (timeoutIn != null)
  1157. timeoutIn.setTimeout(timeout * 1000);
  1158. }
  1159. private boolean needCheckConnectivity() {
  1160. return isCheckReceivedObjects()
  1161. || isCheckReferencedObjectsAreReachable()
  1162. || !getClientShallowCommits().isEmpty();
  1163. }
  1164. private void checkConnectivity() throws IOException {
  1165. ObjectIdSubclassMap<ObjectId> baseObjects = null;
  1166. ObjectIdSubclassMap<ObjectId> providedObjects = null;
  1167. ProgressMonitor checking = NullProgressMonitor.INSTANCE;
  1168. if (sideBand && !quiet) {
  1169. SideBandProgressMonitor m = new SideBandProgressMonitor(msgOut);
  1170. m.setDelayStart(750, TimeUnit.MILLISECONDS);
  1171. checking = m;
  1172. }
  1173. if (checkReferencedIsReachable) {
  1174. baseObjects = parser.getBaseObjectIds();
  1175. providedObjects = parser.getNewObjectIds();
  1176. }
  1177. parser = null;
  1178. try (final ObjectWalk ow = new ObjectWalk(db)) {
  1179. if (baseObjects != null) {
  1180. ow.sort(RevSort.TOPO);
  1181. if (!baseObjects.isEmpty())
  1182. ow.sort(RevSort.BOUNDARY, true);
  1183. }
  1184. for (final ReceiveCommand cmd : commands) {
  1185. if (cmd.getResult() != Result.NOT_ATTEMPTED)
  1186. continue;
  1187. if (cmd.getType() == ReceiveCommand.Type.DELETE)
  1188. continue;
  1189. ow.markStart(ow.parseAny(cmd.getNewId()));
  1190. }
  1191. for (final ObjectId have : advertisedHaves) {
  1192. RevObject o = ow.parseAny(have);
  1193. ow.markUninteresting(o);
  1194. if (baseObjects != null && !baseObjects.isEmpty()) {
  1195. o = ow.peel(o);
  1196. if (o instanceof RevCommit)
  1197. o = ((RevCommit) o).getTree();
  1198. if (o instanceof RevTree)
  1199. ow.markUninteresting(o);
  1200. }
  1201. }
  1202. checking.beginTask(JGitText.get().countingObjects,
  1203. ProgressMonitor.UNKNOWN);
  1204. RevCommit c;
  1205. while ((c = ow.next()) != null) {
  1206. checking.update(1);
  1207. if (providedObjects != null //
  1208. && !c.has(RevFlag.UNINTERESTING) //
  1209. && !providedObjects.contains(c))
  1210. throw new MissingObjectException(c, Constants.TYPE_COMMIT);
  1211. }
  1212. RevObject o;
  1213. while ((o = ow.nextObject()) != null) {
  1214. checking.update(1);
  1215. if (o.has(RevFlag.UNINTERESTING))
  1216. continue;
  1217. if (providedObjects != null) {
  1218. if (providedObjects.contains(o))
  1219. continue;
  1220. else
  1221. throw new MissingObjectException(o, o.getType());
  1222. }
  1223. if (o instanceof RevBlob && !db.hasObject(o))
  1224. throw new MissingObjectException(o, Constants.TYPE_BLOB);
  1225. }
  1226. checking.endTask();
  1227. if (baseObjects != null) {
  1228. for (ObjectId id : baseObjects) {
  1229. o = ow.parseAny(id);
  1230. if (!o.has(RevFlag.UNINTERESTING))
  1231. throw new MissingObjectException(o, o.getType());
  1232. }
  1233. }
  1234. }
  1235. }
  1236. /** Validate the command list. */
  1237. protected void validateCommands() {
  1238. for (final ReceiveCommand cmd : commands) {
  1239. final Ref ref = cmd.getRef();
  1240. if (cmd.getResult() != Result.NOT_ATTEMPTED)
  1241. continue;
  1242. if (cmd.getType() == ReceiveCommand.Type.DELETE) {
  1243. if (!isAllowDeletes()) {
  1244. // Deletes are not supported on this repository.
  1245. cmd.setResult(Result.REJECTED_NODELETE);
  1246. continue;
  1247. }
  1248. if (!isAllowBranchDeletes()
  1249. && ref.getName().startsWith(Constants.R_HEADS)) {
  1250. // Branches cannot be deleted, but other refs can.
  1251. cmd.setResult(Result.REJECTED_NODELETE);
  1252. continue;
  1253. }
  1254. }
  1255. if (cmd.getType() == ReceiveCommand.Type.CREATE) {
  1256. if (!isAllowCreates()) {
  1257. cmd.setResult(Result.REJECTED_NOCREATE);
  1258. continue;
  1259. }
  1260. if (ref != null && !isAllowNonFastForwards()) {
  1261. // Creation over an existing ref is certainly not going
  1262. // to be a fast-forward update. We can reject it early.
  1263. //
  1264. cmd.setResult(Result.REJECTED_NONFASTFORWARD);
  1265. continue;
  1266. }
  1267. if (ref != null) {
  1268. // A well behaved client shouldn't have sent us a
  1269. // create command for a ref we advertised to it.
  1270. //
  1271. cmd.setResult(Result.REJECTED_OTHER_REASON,
  1272. JGitText.get().refAlreadyExists);
  1273. continue;
  1274. }
  1275. }
  1276. if (cmd.getType() == ReceiveCommand.Type.DELETE && ref != null) {
  1277. ObjectId id = ref.getObjectId();
  1278. if (id == null) {
  1279. id = ObjectId.zeroId();
  1280. }
  1281. if (!ObjectId.zeroId().equals(cmd.getOldId())
  1282. && !id.equals(cmd.getOldId())) {
  1283. // Delete commands can be sent with the old id matching our
  1284. // advertised value, *OR* with the old id being 0{40}. Any
  1285. // other requested old id is invalid.
  1286. //
  1287. cmd.setResult(Result.REJECTED_OTHER_REASON,
  1288. JGitText.get().invalidOldIdSent);
  1289. continue;
  1290. }
  1291. }
  1292. if (cmd.getType() == ReceiveCommand.Type.UPDATE) {
  1293. if (ref == null) {
  1294. // The ref must have been advertised in order to be updated.
  1295. //
  1296. cmd.setResult(Result.REJECTED_OTHER_REASON, JGitText.get().noSuchRef);
  1297. continue;
  1298. }
  1299. ObjectId id = ref.getObjectId();
  1300. if (id == null) {
  1301. // We cannot update unborn branch
  1302. cmd.setResult(Result.REJECTED_OTHER_REASON,
  1303. JGitText.get().cannotUpdateUnbornBranch);
  1304. continue;
  1305. }
  1306. if (!id.equals(cmd.getOldId())) {
  1307. // A properly functioning client will send the same
  1308. // object id we advertised.
  1309. //
  1310. cmd.setResult(Result.REJECTED_OTHER_REASON,
  1311. JGitText.get().invalidOldIdSent);
  1312. continue;
  1313. }
  1314. // Is this possibly a non-fast-forward style update?
  1315. //
  1316. RevObject oldObj, newObj;
  1317. try {
  1318. oldObj = walk.parseAny(cmd.getOldId());
  1319. } catch (IOException e) {
  1320. cmd.setResult(Result.REJECTED_MISSING_OBJECT, cmd
  1321. .getOldId().name());
  1322. continue;
  1323. }
  1324. try {
  1325. newObj = walk.parseAny(cmd.getNewId());
  1326. } catch (IOException e) {
  1327. cmd.setResult(Result.REJECTED_MISSING_OBJECT, cmd
  1328. .getNewId().name());
  1329. continue;
  1330. }
  1331. if (oldObj instanceof RevCommit && newObj instanceof RevCommit) {
  1332. try {
  1333. if (walk.isMergedInto((RevCommit) oldObj,
  1334. (RevCommit) newObj))
  1335. cmd.setTypeFastForwardUpdate();
  1336. else
  1337. cmd.setType(ReceiveCommand.Type.UPDATE_NONFASTFORWARD);
  1338. } catch (MissingObjectException e) {
  1339. cmd.setResult(Result.REJECTED_MISSING_OBJECT, e
  1340. .getMessage());
  1341. } catch (IOException e) {
  1342. cmd.setResult(Result.REJECTED_OTHER_REASON);
  1343. }
  1344. } else {
  1345. cmd.setType(ReceiveCommand.Type.UPDATE_NONFASTFORWARD);
  1346. }
  1347. if (cmd.getType() == ReceiveCommand.Type.UPDATE_NONFASTFORWARD
  1348. && !isAllowNonFastForwards()) {
  1349. cmd.setResult(Result.REJECTED_NONFASTFORWARD);
  1350. continue;
  1351. }
  1352. }
  1353. if (!cmd.getRefName().startsWith(Constants.R_REFS)
  1354. || !Repository.isValidRefName(cmd.getRefName())) {
  1355. cmd.setResult(Result.REJECTED_OTHER_REASON, JGitText.get().funnyRefname);
  1356. }
  1357. }
  1358. }
  1359. /**
  1360. * @return if any commands have been rejected so far.
  1361. * @since 3.6
  1362. */
  1363. protected boolean anyRejects() {
  1364. for (ReceiveCommand cmd : commands) {
  1365. if (cmd.getResult() != Result.NOT_ATTEMPTED && cmd.getResult() != Result.OK)
  1366. return true;
  1367. }
  1368. return false;
  1369. }
  1370. /**
  1371. * Set the result to fail for any command that was not processed yet.
  1372. * @since 3.6
  1373. */
  1374. protected void failPendingCommands() {
  1375. ReceiveCommand.abort(commands);
  1376. }
  1377. /**
  1378. * Filter the list of commands according to result.
  1379. *
  1380. * @param want
  1381. * desired status to filter by.
  1382. * @return a copy of the command list containing only those commands with the
  1383. * desired status.
  1384. */
  1385. protected List<ReceiveCommand> filterCommands(final Result want) {
  1386. return ReceiveCommand.filter(commands, want);
  1387. }
  1388. /** Execute commands to update references. */
  1389. protected void executeCommands() {
  1390. List<ReceiveCommand> toApply = filterCommands(Result.NOT_ATTEMPTED);
  1391. if (toApply.isEmpty())
  1392. return;
  1393. ProgressMonitor updating = NullProgressMonitor.INSTANCE;
  1394. if (sideBand) {
  1395. SideBandProgressMonitor pm = new SideBandProgressMonitor(msgOut);
  1396. pm.setDelayStart(250, TimeUnit.MILLISECONDS);
  1397. updating = pm;
  1398. }
  1399. BatchRefUpdate batch = db.getRefDatabase().newBatchUpdate();
  1400. batch.setAllowNonFastForwards(isAllowNonFastForwards());
  1401. batch.setAtomic(isAtomic());
  1402. batch.setRefLogIdent(getRefLogIdent());
  1403. batch.setRefLogMessage("push", true); //$NON-NLS-1$
  1404. batch.addCommand(toApply);
  1405. try {
  1406. batch.setPushCertificate(getPushCertificate());
  1407. batch.execute(walk, updating);
  1408. } catch (IOException err) {
  1409. for (ReceiveCommand cmd : toApply) {
  1410. if (cmd.getResult() == Result.NOT_ATTEMPTED)
  1411. cmd.reject(err);
  1412. }
  1413. }
  1414. }
  1415. /**
  1416. * Send a status report.
  1417. *
  1418. * @param forClient
  1419. * true if this report is for a Git client, false if it is for an
  1420. * end-user.
  1421. * @param unpackError
  1422. * an error that occurred during unpacking, or {@code null}
  1423. * @param out
  1424. * the reporter for sending the status strings.
  1425. * @throws IOException
  1426. * an error occurred writing the status report.
  1427. */
  1428. protected void sendStatusReport(final boolean forClient,
  1429. final Throwable unpackError, final Reporter out) throws IOException {
  1430. if (unpackError != null) {
  1431. out.sendString("unpack error " + unpackError.getMessage()); //$NON-NLS-1$
  1432. if (forClient) {
  1433. for (final ReceiveCommand cmd : commands) {
  1434. out.sendString("ng " + cmd.getRefName() //$NON-NLS-1$
  1435. + " n/a (unpacker error)"); //$NON-NLS-1$
  1436. }
  1437. }
  1438. return;
  1439. }
  1440. if (forClient)
  1441. out.sendString("unpack ok"); //$NON-NLS-1$
  1442. for (final ReceiveCommand cmd : commands) {
  1443. if (cmd.getResult() == Result.OK) {
  1444. if (forClient)
  1445. out.sendString("ok " + cmd.getRefName()); //$NON-NLS-1$
  1446. continue;
  1447. }
  1448. final StringBuilder r = new StringBuilder();
  1449. if (forClient)
  1450. r.append("ng ").append(cmd.getRefName()).append(" "); //$NON-NLS-1$ //$NON-NLS-2$
  1451. else
  1452. r.append(" ! [rejected] ").append(cmd.getRefName()).append(" ("); //$NON-NLS-1$ //$NON-NLS-2$
  1453. switch (cmd.getResult()) {
  1454. case NOT_ATTEMPTED:
  1455. r.append("server bug; ref not processed"); //$NON-NLS-1$
  1456. break;
  1457. case REJECTED_NOCREATE:
  1458. r.append("creation prohibited"); //$NON-NLS-1$
  1459. break;
  1460. case REJECTED_NODELETE:
  1461. r.append("deletion prohibited"); //$NON-NLS-1$
  1462. break;
  1463. case REJECTED_NONFASTFORWARD:
  1464. r.append("non-fast forward"); //$NON-NLS-1$
  1465. break;
  1466. case REJECTED_CURRENT_BRANCH:
  1467. r.append("branch is currently checked out"); //$NON-NLS-1$
  1468. break;
  1469. case REJECTED_MISSING_OBJECT:
  1470. if (cmd.getMessage() == null)
  1471. r.append("missing object(s)"); //$NON-NLS-1$
  1472. else if (cmd.getMessage().length() == Constants.OBJECT_ID_STRING_LENGTH) {
  1473. r.append("object "); //$NON-NLS-1$
  1474. r.append(cmd.getMessage());
  1475. r.append(" missing"); //$NON-NLS-1$
  1476. } else
  1477. r.append(cmd.getMessage());
  1478. break;
  1479. case REJECTED_OTHER_REASON:
  1480. if (cmd.getMessage() == null)
  1481. r.append("unspecified reason"); //$NON-NLS-1$
  1482. else
  1483. r.append(cmd.getMessage());
  1484. break;
  1485. case LOCK_FAILURE:
  1486. r.append("failed to lock"); //$NON-NLS-1$
  1487. break;
  1488. case OK:
  1489. // We shouldn't have reached this case (see 'ok' case above).
  1490. continue;
  1491. }
  1492. if (!forClient)
  1493. r.append(")"); //$NON-NLS-1$
  1494. out.sendString(r.toString());
  1495. }
  1496. }
  1497. /**
  1498. * Close and flush (if necessary) the underlying streams.
  1499. *
  1500. * @throws IOException
  1501. */
  1502. protected void close() throws IOException {
  1503. if (sideBand) {
  1504. // If we are using side band, we need to send a final
  1505. // flush-pkt to tell the remote peer the side band is
  1506. // complete and it should stop decoding. We need to
  1507. // use the original output stream as rawOut is now the
  1508. // side band data channel.
  1509. //
  1510. ((SideBandOutputStream) msgOut).flushBuffer();
  1511. ((SideBandOutputStream) rawOut).flushBuffer();
  1512. PacketLineOut plo = new PacketLineOut(origOut);
  1513. plo.setFlushOnEnd(false);
  1514. plo.end();
  1515. }
  1516. if (biDirectionalPipe) {
  1517. // If this was a native git connection, flush the pipe for
  1518. // the caller. For smart HTTP we don't do this flush and
  1519. // instead let the higher level HTTP servlet code do it.
  1520. //
  1521. if (!sideBand && msgOut != null)
  1522. msgOut.flush();
  1523. rawOut.flush();
  1524. }
  1525. }
  1526. /**
  1527. * Release any resources used by this object.
  1528. *
  1529. * @throws IOException
  1530. * the pack could not be unlocked.
  1531. */
  1532. protected void release() throws IOException {
  1533. walk.close();
  1534. unlockPack();
  1535. timeoutIn = null;
  1536. rawIn = null;
  1537. rawOut = null;
  1538. msgOut = null;
  1539. pckIn = null;
  1540. pckOut = null;
  1541. refs = null;
  1542. // Keep the capabilities. If responses are sent after this release
  1543. // we need to remember at least whether sideband communication has to be
  1544. // used
  1545. commands = null;
  1546. if (timer != null) {
  1547. try {
  1548. timer.terminate();
  1549. } finally {
  1550. timer = null;
  1551. }
  1552. }
  1553. }
  1554. /** Interface for reporting status messages. */
  1555. static abstract class Reporter {
  1556. abstract void sendString(String s) throws IOException;
  1557. }
  1558. }