選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

BaseReceivePack.java 48KB

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