You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseReceivePack.java 49KB

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