Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Repository.java 58KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2008-2010, Google Inc.
  4. * Copyright (C) 2006-2010, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2006-2012, Shawn O. Pearce <spearce@spearce.org>
  6. * Copyright (C) 2012, Daniel Megert <daniel_megert@ch.ibm.com>
  7. * and other copyright owners as documented in the project's IP log.
  8. *
  9. * This program and the accompanying materials are made available
  10. * under the terms of the Eclipse Distribution License v1.0 which
  11. * accompanies this distribution, is reproduced below, and is
  12. * available at http://www.eclipse.org/org/documents/edl-v10.php
  13. *
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or
  17. * without modification, are permitted provided that the following
  18. * conditions are met:
  19. *
  20. * - Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. *
  23. * - Redistributions in binary form must reproduce the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer in the documentation and/or other materials provided
  26. * with the distribution.
  27. *
  28. * - Neither the name of the Eclipse Foundation, Inc. nor the
  29. * names of its contributors may be used to endorse or promote
  30. * products derived from this software without specific prior
  31. * written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  34. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  36. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  38. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  40. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  41. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  43. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  44. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  45. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. */
  47. package org.eclipse.jgit.lib;
  48. import java.io.BufferedOutputStream;
  49. import java.io.File;
  50. import java.io.FileNotFoundException;
  51. import java.io.FileOutputStream;
  52. import java.io.IOException;
  53. import java.io.OutputStream;
  54. import java.net.URISyntaxException;
  55. import java.text.MessageFormat;
  56. import java.util.Collection;
  57. import java.util.Collections;
  58. import java.util.HashMap;
  59. import java.util.HashSet;
  60. import java.util.LinkedList;
  61. import java.util.List;
  62. import java.util.Map;
  63. import java.util.Set;
  64. import java.util.concurrent.atomic.AtomicInteger;
  65. import java.util.concurrent.atomic.AtomicLong;
  66. import org.eclipse.jgit.annotations.NonNull;
  67. import org.eclipse.jgit.annotations.Nullable;
  68. import org.eclipse.jgit.attributes.AttributesNodeProvider;
  69. import org.eclipse.jgit.dircache.DirCache;
  70. import org.eclipse.jgit.errors.AmbiguousObjectException;
  71. import org.eclipse.jgit.errors.CorruptObjectException;
  72. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  73. import org.eclipse.jgit.errors.MissingObjectException;
  74. import org.eclipse.jgit.errors.NoWorkTreeException;
  75. import org.eclipse.jgit.errors.RevisionSyntaxException;
  76. import org.eclipse.jgit.events.IndexChangedEvent;
  77. import org.eclipse.jgit.events.IndexChangedListener;
  78. import org.eclipse.jgit.events.ListenerList;
  79. import org.eclipse.jgit.events.RepositoryEvent;
  80. import org.eclipse.jgit.internal.JGitText;
  81. import org.eclipse.jgit.internal.storage.file.GC;
  82. import org.eclipse.jgit.revwalk.RevBlob;
  83. import org.eclipse.jgit.revwalk.RevCommit;
  84. import org.eclipse.jgit.revwalk.RevObject;
  85. import org.eclipse.jgit.revwalk.RevTree;
  86. import org.eclipse.jgit.revwalk.RevWalk;
  87. import org.eclipse.jgit.transport.RefSpec;
  88. import org.eclipse.jgit.transport.RemoteConfig;
  89. import org.eclipse.jgit.treewalk.TreeWalk;
  90. import org.eclipse.jgit.util.FS;
  91. import org.eclipse.jgit.util.FileUtils;
  92. import org.eclipse.jgit.util.IO;
  93. import org.eclipse.jgit.util.RawParseUtils;
  94. import org.eclipse.jgit.util.SystemReader;
  95. import org.slf4j.Logger;
  96. import org.slf4j.LoggerFactory;
  97. /**
  98. * Represents a Git repository.
  99. * <p>
  100. * A repository holds all objects and refs used for managing source code (could
  101. * be any type of file, but source code is what SCM's are typically used for).
  102. * <p>
  103. * This class is thread-safe.
  104. */
  105. public abstract class Repository implements AutoCloseable {
  106. private static final Logger LOG = LoggerFactory.getLogger(Repository.class);
  107. private static final ListenerList globalListeners = new ListenerList();
  108. /** @return the global listener list observing all events in this JVM. */
  109. public static ListenerList getGlobalListenerList() {
  110. return globalListeners;
  111. }
  112. /** Use counter */
  113. final AtomicInteger useCnt = new AtomicInteger(1);
  114. final AtomicLong closedAt = new AtomicLong();
  115. /** Metadata directory holding the repository's critical files. */
  116. private final File gitDir;
  117. /** File abstraction used to resolve paths. */
  118. private final FS fs;
  119. private final ListenerList myListeners = new ListenerList();
  120. /** If not bare, the top level directory of the working files. */
  121. private final File workTree;
  122. /** If not bare, the index file caching the working file states. */
  123. private final File indexFile;
  124. /**
  125. * Initialize a new repository instance.
  126. *
  127. * @param options
  128. * options to configure the repository.
  129. */
  130. protected Repository(final BaseRepositoryBuilder options) {
  131. gitDir = options.getGitDir();
  132. fs = options.getFS();
  133. workTree = options.getWorkTree();
  134. indexFile = options.getIndexFile();
  135. }
  136. /** @return listeners observing only events on this repository. */
  137. @NonNull
  138. public ListenerList getListenerList() {
  139. return myListeners;
  140. }
  141. /**
  142. * Fire an event to all registered listeners.
  143. * <p>
  144. * The source repository of the event is automatically set to this
  145. * repository, before the event is delivered to any listeners.
  146. *
  147. * @param event
  148. * the event to deliver.
  149. */
  150. public void fireEvent(RepositoryEvent<?> event) {
  151. event.setRepository(this);
  152. myListeners.dispatch(event);
  153. globalListeners.dispatch(event);
  154. }
  155. /**
  156. * Create a new Git repository.
  157. * <p>
  158. * Repository with working tree is created using this method. This method is
  159. * the same as {@code create(false)}.
  160. *
  161. * @throws IOException
  162. * @see #create(boolean)
  163. */
  164. public void create() throws IOException {
  165. create(false);
  166. }
  167. /**
  168. * Create a new Git repository initializing the necessary files and
  169. * directories.
  170. *
  171. * @param bare
  172. * if true, a bare repository (a repository without a working
  173. * directory) is created.
  174. * @throws IOException
  175. * in case of IO problem
  176. */
  177. public abstract void create(boolean bare) throws IOException;
  178. /**
  179. * @return local metadata directory; {@code null} if repository isn't local.
  180. */
  181. /*
  182. * TODO This method should be annotated as Nullable, because in some
  183. * specific configurations metadata is not located in the local file system
  184. * (for example in memory databases). In "usual" repositories this
  185. * annotation would only cause compiler errors at places where the actual
  186. * directory can never be null.
  187. */
  188. public File getDirectory() {
  189. return gitDir;
  190. }
  191. /**
  192. * @return the object database which stores this repository's data.
  193. */
  194. @NonNull
  195. public abstract ObjectDatabase getObjectDatabase();
  196. /** @return a new inserter to create objects in {@link #getObjectDatabase()} */
  197. @NonNull
  198. public ObjectInserter newObjectInserter() {
  199. return getObjectDatabase().newInserter();
  200. }
  201. /** @return a new reader to read objects from {@link #getObjectDatabase()} */
  202. @NonNull
  203. public ObjectReader newObjectReader() {
  204. return getObjectDatabase().newReader();
  205. }
  206. /** @return the reference database which stores the reference namespace. */
  207. @NonNull
  208. public abstract RefDatabase getRefDatabase();
  209. /**
  210. * @return the configuration of this repository
  211. */
  212. @NonNull
  213. public abstract StoredConfig getConfig();
  214. /**
  215. * @return a new {@link AttributesNodeProvider}. This
  216. * {@link AttributesNodeProvider} is lazy loaded only once. It means
  217. * that it will not be updated after loading. Prefer creating new
  218. * instance for each use.
  219. * @since 4.2
  220. */
  221. @NonNull
  222. public abstract AttributesNodeProvider createAttributesNodeProvider();
  223. /**
  224. * @return the used file system abstraction, or or {@code null} if
  225. * repository isn't local.
  226. */
  227. /*
  228. * TODO This method should be annotated as Nullable, because in some
  229. * specific configurations metadata is not located in the local file system
  230. * (for example in memory databases). In "usual" repositories this
  231. * annotation would only cause compiler errors at places where the actual
  232. * directory can never be null.
  233. */
  234. public FS getFS() {
  235. return fs;
  236. }
  237. /**
  238. * @param objectId
  239. * @return true if the specified object is stored in this repo or any of the
  240. * known shared repositories.
  241. */
  242. public boolean hasObject(AnyObjectId objectId) {
  243. try {
  244. return getObjectDatabase().has(objectId);
  245. } catch (IOException e) {
  246. // Legacy API, assume error means "no"
  247. return false;
  248. }
  249. }
  250. /**
  251. * Open an object from this repository.
  252. * <p>
  253. * This is a one-shot call interface which may be faster than allocating a
  254. * {@link #newObjectReader()} to perform the lookup.
  255. *
  256. * @param objectId
  257. * identity of the object to open.
  258. * @return a {@link ObjectLoader} for accessing the object.
  259. * @throws MissingObjectException
  260. * the object does not exist.
  261. * @throws IOException
  262. * the object store cannot be accessed.
  263. */
  264. @NonNull
  265. public ObjectLoader open(final AnyObjectId objectId)
  266. throws MissingObjectException, IOException {
  267. return getObjectDatabase().open(objectId);
  268. }
  269. /**
  270. * Open an object from this repository.
  271. * <p>
  272. * This is a one-shot call interface which may be faster than allocating a
  273. * {@link #newObjectReader()} to perform the lookup.
  274. *
  275. * @param objectId
  276. * identity of the object to open.
  277. * @param typeHint
  278. * hint about the type of object being requested, e.g.
  279. * {@link Constants#OBJ_BLOB}; {@link ObjectReader#OBJ_ANY} if
  280. * the object type is not known, or does not matter to the
  281. * caller.
  282. * @return a {@link ObjectLoader} for accessing the object.
  283. * @throws MissingObjectException
  284. * the object does not exist.
  285. * @throws IncorrectObjectTypeException
  286. * typeHint was not OBJ_ANY, and the object's actual type does
  287. * not match typeHint.
  288. * @throws IOException
  289. * the object store cannot be accessed.
  290. */
  291. @NonNull
  292. public ObjectLoader open(AnyObjectId objectId, int typeHint)
  293. throws MissingObjectException, IncorrectObjectTypeException,
  294. IOException {
  295. return getObjectDatabase().open(objectId, typeHint);
  296. }
  297. /**
  298. * Create a command to update, create or delete a ref in this repository.
  299. *
  300. * @param ref
  301. * name of the ref the caller wants to modify.
  302. * @return an update command. The caller must finish populating this command
  303. * and then invoke one of the update methods to actually make a
  304. * change.
  305. * @throws IOException
  306. * a symbolic ref was passed in and could not be resolved back
  307. * to the base ref, as the symbolic ref could not be read.
  308. */
  309. @NonNull
  310. public RefUpdate updateRef(final String ref) throws IOException {
  311. return updateRef(ref, false);
  312. }
  313. /**
  314. * Create a command to update, create or delete a ref in this repository.
  315. *
  316. * @param ref
  317. * name of the ref the caller wants to modify.
  318. * @param detach
  319. * true to create a detached head
  320. * @return an update command. The caller must finish populating this command
  321. * and then invoke one of the update methods to actually make a
  322. * change.
  323. * @throws IOException
  324. * a symbolic ref was passed in and could not be resolved back
  325. * to the base ref, as the symbolic ref could not be read.
  326. */
  327. @NonNull
  328. public RefUpdate updateRef(final String ref, final boolean detach) throws IOException {
  329. return getRefDatabase().newUpdate(ref, detach);
  330. }
  331. /**
  332. * Create a command to rename a ref in this repository
  333. *
  334. * @param fromRef
  335. * name of ref to rename from
  336. * @param toRef
  337. * name of ref to rename to
  338. * @return an update command that knows how to rename a branch to another.
  339. * @throws IOException
  340. * the rename could not be performed.
  341. *
  342. */
  343. @NonNull
  344. public RefRename renameRef(final String fromRef, final String toRef) throws IOException {
  345. return getRefDatabase().newRename(fromRef, toRef);
  346. }
  347. /**
  348. * Parse a git revision string and return an object id.
  349. *
  350. * Combinations of these operators are supported:
  351. * <ul>
  352. * <li><b>HEAD</b>, <b>MERGE_HEAD</b>, <b>FETCH_HEAD</b></li>
  353. * <li><b>SHA-1</b>: a complete or abbreviated SHA-1</li>
  354. * <li><b>refs/...</b>: a complete reference name</li>
  355. * <li><b>short-name</b>: a short reference name under {@code refs/heads},
  356. * {@code refs/tags}, or {@code refs/remotes} namespace</li>
  357. * <li><b>tag-NN-gABBREV</b>: output from describe, parsed by treating
  358. * {@code ABBREV} as an abbreviated SHA-1.</li>
  359. * <li><i>id</i><b>^</b>: first parent of commit <i>id</i>, this is the same
  360. * as {@code id^1}</li>
  361. * <li><i>id</i><b>^0</b>: ensure <i>id</i> is a commit</li>
  362. * <li><i>id</i><b>^n</b>: n-th parent of commit <i>id</i></li>
  363. * <li><i>id</i><b>~n</b>: n-th historical ancestor of <i>id</i>, by first
  364. * parent. {@code id~3} is equivalent to {@code id^1^1^1} or {@code id^^^}.</li>
  365. * <li><i>id</i><b>:path</b>: Lookup path under tree named by <i>id</i></li>
  366. * <li><i>id</i><b>^{commit}</b>: ensure <i>id</i> is a commit</li>
  367. * <li><i>id</i><b>^{tree}</b>: ensure <i>id</i> is a tree</li>
  368. * <li><i>id</i><b>^{tag}</b>: ensure <i>id</i> is a tag</li>
  369. * <li><i>id</i><b>^{blob}</b>: ensure <i>id</i> is a blob</li>
  370. * </ul>
  371. *
  372. * <p>
  373. * The following operators are specified by Git conventions, but are not
  374. * supported by this method:
  375. * <ul>
  376. * <li><b>ref@{n}</b>: n-th version of ref as given by its reflog</li>
  377. * <li><b>ref@{time}</b>: value of ref at the designated time</li>
  378. * </ul>
  379. *
  380. * @param revstr
  381. * A git object references expression
  382. * @return an ObjectId or {@code null} if revstr can't be resolved to any
  383. * ObjectId
  384. * @throws AmbiguousObjectException
  385. * {@code revstr} contains an abbreviated ObjectId and this
  386. * repository contains more than one object which match to the
  387. * input abbreviation.
  388. * @throws IncorrectObjectTypeException
  389. * the id parsed does not meet the type required to finish
  390. * applying the operators in the expression.
  391. * @throws RevisionSyntaxException
  392. * the expression is not supported by this implementation, or
  393. * does not meet the standard syntax.
  394. * @throws IOException
  395. * on serious errors
  396. */
  397. @Nullable
  398. public ObjectId resolve(final String revstr)
  399. throws AmbiguousObjectException, IncorrectObjectTypeException,
  400. RevisionSyntaxException, IOException {
  401. try (RevWalk rw = new RevWalk(this)) {
  402. Object resolved = resolve(rw, revstr);
  403. if (resolved instanceof String) {
  404. final Ref ref = getRef((String)resolved);
  405. return ref != null ? ref.getLeaf().getObjectId() : null;
  406. } else {
  407. return (ObjectId) resolved;
  408. }
  409. }
  410. }
  411. /**
  412. * Simplify an expression, but unlike {@link #resolve(String)} it will not
  413. * resolve a branch passed or resulting from the expression, such as @{-}.
  414. * Thus this method can be used to process an expression to a method that
  415. * expects a branch or revision id.
  416. *
  417. * @param revstr
  418. * @return object id or ref name from resolved expression or {@code null} if
  419. * given expression cannot be resolved
  420. * @throws AmbiguousObjectException
  421. * @throws IOException
  422. */
  423. @Nullable
  424. public String simplify(final String revstr)
  425. throws AmbiguousObjectException, IOException {
  426. try (RevWalk rw = new RevWalk(this)) {
  427. Object resolved = resolve(rw, revstr);
  428. if (resolved != null)
  429. if (resolved instanceof String)
  430. return (String) resolved;
  431. else
  432. return ((AnyObjectId) resolved).getName();
  433. return null;
  434. }
  435. }
  436. @Nullable
  437. private Object resolve(final RevWalk rw, final String revstr)
  438. throws IOException {
  439. char[] revChars = revstr.toCharArray();
  440. RevObject rev = null;
  441. String name = null;
  442. int done = 0;
  443. for (int i = 0; i < revChars.length; ++i) {
  444. switch (revChars[i]) {
  445. case '^':
  446. if (rev == null) {
  447. if (name == null)
  448. if (done == 0)
  449. name = new String(revChars, done, i);
  450. else {
  451. done = i + 1;
  452. break;
  453. }
  454. rev = parseSimple(rw, name);
  455. name = null;
  456. if (rev == null)
  457. return null;
  458. }
  459. if (i + 1 < revChars.length) {
  460. switch (revChars[i + 1]) {
  461. case '0':
  462. case '1':
  463. case '2':
  464. case '3':
  465. case '4':
  466. case '5':
  467. case '6':
  468. case '7':
  469. case '8':
  470. case '9':
  471. int j;
  472. rev = rw.parseCommit(rev);
  473. for (j = i + 1; j < revChars.length; ++j) {
  474. if (!Character.isDigit(revChars[j]))
  475. break;
  476. }
  477. String parentnum = new String(revChars, i + 1, j - i
  478. - 1);
  479. int pnum;
  480. try {
  481. pnum = Integer.parseInt(parentnum);
  482. } catch (NumberFormatException e) {
  483. throw new RevisionSyntaxException(
  484. JGitText.get().invalidCommitParentNumber,
  485. revstr);
  486. }
  487. if (pnum != 0) {
  488. RevCommit commit = (RevCommit) rev;
  489. if (pnum > commit.getParentCount())
  490. rev = null;
  491. else
  492. rev = commit.getParent(pnum - 1);
  493. }
  494. i = j - 1;
  495. done = j;
  496. break;
  497. case '{':
  498. int k;
  499. String item = null;
  500. for (k = i + 2; k < revChars.length; ++k) {
  501. if (revChars[k] == '}') {
  502. item = new String(revChars, i + 2, k - i - 2);
  503. break;
  504. }
  505. }
  506. i = k;
  507. if (item != null)
  508. if (item.equals("tree")) { //$NON-NLS-1$
  509. rev = rw.parseTree(rev);
  510. } else if (item.equals("commit")) { //$NON-NLS-1$
  511. rev = rw.parseCommit(rev);
  512. } else if (item.equals("blob")) { //$NON-NLS-1$
  513. rev = rw.peel(rev);
  514. if (!(rev instanceof RevBlob))
  515. throw new IncorrectObjectTypeException(rev,
  516. Constants.TYPE_BLOB);
  517. } else if (item.equals("")) { //$NON-NLS-1$
  518. rev = rw.peel(rev);
  519. } else
  520. throw new RevisionSyntaxException(revstr);
  521. else
  522. throw new RevisionSyntaxException(revstr);
  523. done = k;
  524. break;
  525. default:
  526. rev = rw.peel(rev);
  527. if (rev instanceof RevCommit) {
  528. RevCommit commit = ((RevCommit) rev);
  529. if (commit.getParentCount() == 0)
  530. rev = null;
  531. else
  532. rev = commit.getParent(0);
  533. } else
  534. throw new IncorrectObjectTypeException(rev,
  535. Constants.TYPE_COMMIT);
  536. }
  537. } else {
  538. rev = rw.peel(rev);
  539. if (rev instanceof RevCommit) {
  540. RevCommit commit = ((RevCommit) rev);
  541. if (commit.getParentCount() == 0)
  542. rev = null;
  543. else
  544. rev = commit.getParent(0);
  545. } else
  546. throw new IncorrectObjectTypeException(rev,
  547. Constants.TYPE_COMMIT);
  548. }
  549. done = i + 1;
  550. break;
  551. case '~':
  552. if (rev == null) {
  553. if (name == null)
  554. if (done == 0)
  555. name = new String(revChars, done, i);
  556. else {
  557. done = i + 1;
  558. break;
  559. }
  560. rev = parseSimple(rw, name);
  561. name = null;
  562. if (rev == null)
  563. return null;
  564. }
  565. rev = rw.peel(rev);
  566. if (!(rev instanceof RevCommit))
  567. throw new IncorrectObjectTypeException(rev,
  568. Constants.TYPE_COMMIT);
  569. int l;
  570. for (l = i + 1; l < revChars.length; ++l) {
  571. if (!Character.isDigit(revChars[l]))
  572. break;
  573. }
  574. int dist;
  575. if (l - i > 1) {
  576. String distnum = new String(revChars, i + 1, l - i - 1);
  577. try {
  578. dist = Integer.parseInt(distnum);
  579. } catch (NumberFormatException e) {
  580. throw new RevisionSyntaxException(
  581. JGitText.get().invalidAncestryLength, revstr);
  582. }
  583. } else
  584. dist = 1;
  585. while (dist > 0) {
  586. RevCommit commit = (RevCommit) rev;
  587. if (commit.getParentCount() == 0) {
  588. rev = null;
  589. break;
  590. }
  591. commit = commit.getParent(0);
  592. rw.parseHeaders(commit);
  593. rev = commit;
  594. --dist;
  595. }
  596. i = l - 1;
  597. done = l;
  598. break;
  599. case '@':
  600. if (rev != null)
  601. throw new RevisionSyntaxException(revstr);
  602. if (i + 1 < revChars.length && revChars[i + 1] != '{')
  603. continue;
  604. int m;
  605. String time = null;
  606. for (m = i + 2; m < revChars.length; ++m) {
  607. if (revChars[m] == '}') {
  608. time = new String(revChars, i + 2, m - i - 2);
  609. break;
  610. }
  611. }
  612. if (time != null) {
  613. if (time.equals("upstream")) { //$NON-NLS-1$
  614. if (name == null)
  615. name = new String(revChars, done, i);
  616. if (name.equals("")) //$NON-NLS-1$
  617. // Currently checked out branch, HEAD if
  618. // detached
  619. name = Constants.HEAD;
  620. if (!Repository.isValidRefName("x/" + name)) //$NON-NLS-1$
  621. throw new RevisionSyntaxException(revstr);
  622. Ref ref = getRef(name);
  623. name = null;
  624. if (ref == null)
  625. return null;
  626. if (ref.isSymbolic())
  627. ref = ref.getLeaf();
  628. name = ref.getName();
  629. RemoteConfig remoteConfig;
  630. try {
  631. remoteConfig = new RemoteConfig(getConfig(),
  632. "origin"); //$NON-NLS-1$
  633. } catch (URISyntaxException e) {
  634. throw new RevisionSyntaxException(revstr);
  635. }
  636. String remoteBranchName = getConfig()
  637. .getString(
  638. ConfigConstants.CONFIG_BRANCH_SECTION,
  639. Repository.shortenRefName(ref.getName()),
  640. ConfigConstants.CONFIG_KEY_MERGE);
  641. List<RefSpec> fetchRefSpecs = remoteConfig
  642. .getFetchRefSpecs();
  643. for (RefSpec refSpec : fetchRefSpecs) {
  644. if (refSpec.matchSource(remoteBranchName)) {
  645. RefSpec expandFromSource = refSpec
  646. .expandFromSource(remoteBranchName);
  647. name = expandFromSource.getDestination();
  648. break;
  649. }
  650. }
  651. if (name == null)
  652. throw new RevisionSyntaxException(revstr);
  653. } else if (time.matches("^-\\d+$")) { //$NON-NLS-1$
  654. if (name != null)
  655. throw new RevisionSyntaxException(revstr);
  656. else {
  657. String previousCheckout = resolveReflogCheckout(-Integer
  658. .parseInt(time));
  659. if (ObjectId.isId(previousCheckout))
  660. rev = parseSimple(rw, previousCheckout);
  661. else
  662. name = previousCheckout;
  663. }
  664. } else {
  665. if (name == null)
  666. name = new String(revChars, done, i);
  667. if (name.equals("")) //$NON-NLS-1$
  668. name = Constants.HEAD;
  669. if (!Repository.isValidRefName("x/" + name)) //$NON-NLS-1$
  670. throw new RevisionSyntaxException(revstr);
  671. Ref ref = getRef(name);
  672. name = null;
  673. if (ref == null)
  674. return null;
  675. // @{n} means current branch, not HEAD@{1} unless
  676. // detached
  677. if (ref.isSymbolic())
  678. ref = ref.getLeaf();
  679. rev = resolveReflog(rw, ref, time);
  680. }
  681. i = m;
  682. } else
  683. throw new RevisionSyntaxException(revstr);
  684. break;
  685. case ':': {
  686. RevTree tree;
  687. if (rev == null) {
  688. if (name == null)
  689. name = new String(revChars, done, i);
  690. if (name.equals("")) //$NON-NLS-1$
  691. name = Constants.HEAD;
  692. rev = parseSimple(rw, name);
  693. name = null;
  694. }
  695. if (rev == null)
  696. return null;
  697. tree = rw.parseTree(rev);
  698. if (i == revChars.length - 1)
  699. return tree.copy();
  700. TreeWalk tw = TreeWalk.forPath(rw.getObjectReader(),
  701. new String(revChars, i + 1, revChars.length - i - 1),
  702. tree);
  703. return tw != null ? tw.getObjectId(0) : null;
  704. }
  705. default:
  706. if (rev != null)
  707. throw new RevisionSyntaxException(revstr);
  708. }
  709. }
  710. if (rev != null)
  711. return rev.copy();
  712. if (name != null)
  713. return name;
  714. if (done == revstr.length())
  715. return null;
  716. name = revstr.substring(done);
  717. if (!Repository.isValidRefName("x/" + name)) //$NON-NLS-1$
  718. throw new RevisionSyntaxException(revstr);
  719. if (getRef(name) != null)
  720. return name;
  721. return resolveSimple(name);
  722. }
  723. private static boolean isHex(char c) {
  724. return ('0' <= c && c <= '9') //
  725. || ('a' <= c && c <= 'f') //
  726. || ('A' <= c && c <= 'F');
  727. }
  728. private static boolean isAllHex(String str, int ptr) {
  729. while (ptr < str.length()) {
  730. if (!isHex(str.charAt(ptr++)))
  731. return false;
  732. }
  733. return true;
  734. }
  735. @Nullable
  736. private RevObject parseSimple(RevWalk rw, String revstr) throws IOException {
  737. ObjectId id = resolveSimple(revstr);
  738. return id != null ? rw.parseAny(id) : null;
  739. }
  740. @Nullable
  741. private ObjectId resolveSimple(final String revstr) throws IOException {
  742. if (ObjectId.isId(revstr))
  743. return ObjectId.fromString(revstr);
  744. if (Repository.isValidRefName("x/" + revstr)) { //$NON-NLS-1$
  745. Ref r = getRefDatabase().getRef(revstr);
  746. if (r != null)
  747. return r.getObjectId();
  748. }
  749. if (AbbreviatedObjectId.isId(revstr))
  750. return resolveAbbreviation(revstr);
  751. int dashg = revstr.indexOf("-g"); //$NON-NLS-1$
  752. if ((dashg + 5) < revstr.length() && 0 <= dashg
  753. && isHex(revstr.charAt(dashg + 2))
  754. && isHex(revstr.charAt(dashg + 3))
  755. && isAllHex(revstr, dashg + 4)) {
  756. // Possibly output from git describe?
  757. String s = revstr.substring(dashg + 2);
  758. if (AbbreviatedObjectId.isId(s))
  759. return resolveAbbreviation(s);
  760. }
  761. return null;
  762. }
  763. @Nullable
  764. private String resolveReflogCheckout(int checkoutNo)
  765. throws IOException {
  766. ReflogReader reader = getReflogReader(Constants.HEAD);
  767. if (reader == null) {
  768. return null;
  769. }
  770. List<ReflogEntry> reflogEntries = reader.getReverseEntries();
  771. for (ReflogEntry entry : reflogEntries) {
  772. CheckoutEntry checkout = entry.parseCheckout();
  773. if (checkout != null)
  774. if (checkoutNo-- == 1)
  775. return checkout.getFromBranch();
  776. }
  777. return null;
  778. }
  779. private RevCommit resolveReflog(RevWalk rw, Ref ref, String time)
  780. throws IOException {
  781. int number;
  782. try {
  783. number = Integer.parseInt(time);
  784. } catch (NumberFormatException nfe) {
  785. throw new RevisionSyntaxException(MessageFormat.format(
  786. JGitText.get().invalidReflogRevision, time));
  787. }
  788. assert number >= 0;
  789. ReflogReader reader = getReflogReader(ref.getName());
  790. if (reader == null) {
  791. throw new RevisionSyntaxException(
  792. MessageFormat.format(JGitText.get().reflogEntryNotFound,
  793. Integer.valueOf(number), ref.getName()));
  794. }
  795. ReflogEntry entry = reader.getReverseEntry(number);
  796. if (entry == null)
  797. throw new RevisionSyntaxException(MessageFormat.format(
  798. JGitText.get().reflogEntryNotFound,
  799. Integer.valueOf(number), ref.getName()));
  800. return rw.parseCommit(entry.getNewId());
  801. }
  802. @Nullable
  803. private ObjectId resolveAbbreviation(final String revstr) throws IOException,
  804. AmbiguousObjectException {
  805. AbbreviatedObjectId id = AbbreviatedObjectId.fromString(revstr);
  806. try (ObjectReader reader = newObjectReader()) {
  807. Collection<ObjectId> matches = reader.resolve(id);
  808. if (matches.size() == 0)
  809. return null;
  810. else if (matches.size() == 1)
  811. return matches.iterator().next();
  812. else
  813. throw new AmbiguousObjectException(id, matches);
  814. }
  815. }
  816. /** Increment the use counter by one, requiring a matched {@link #close()}. */
  817. public void incrementOpen() {
  818. useCnt.incrementAndGet();
  819. }
  820. /** Decrement the use count, and maybe close resources. */
  821. @Override
  822. public void close() {
  823. int newCount = useCnt.decrementAndGet();
  824. if (newCount == 0) {
  825. if (RepositoryCache.isCached(this)) {
  826. closedAt.set(System.currentTimeMillis());
  827. } else {
  828. doClose();
  829. }
  830. } else if (newCount == -1) {
  831. // should not happen, only log when useCnt became negative to
  832. // minimize number of log entries
  833. if (LOG.isDebugEnabled()) {
  834. IllegalStateException e = new IllegalStateException();
  835. LOG.debug(JGitText.get().corruptUseCnt, e);
  836. } else {
  837. LOG.warn(JGitText.get().corruptUseCnt);
  838. }
  839. if (RepositoryCache.isCached(this)) {
  840. closedAt.set(System.currentTimeMillis());
  841. }
  842. }
  843. }
  844. /**
  845. * Invoked when the use count drops to zero during {@link #close()}.
  846. * <p>
  847. * The default implementation closes the object and ref databases.
  848. */
  849. protected void doClose() {
  850. getObjectDatabase().close();
  851. getRefDatabase().close();
  852. }
  853. @SuppressWarnings("nls")
  854. @Override
  855. @NonNull
  856. public String toString() {
  857. String desc;
  858. File directory = getDirectory();
  859. if (directory != null)
  860. desc = directory.getPath();
  861. else
  862. desc = getClass().getSimpleName() + "-" //$NON-NLS-1$
  863. + System.identityHashCode(this);
  864. return "Repository[" + desc + "]"; //$NON-NLS-1$
  865. }
  866. /**
  867. * Get the name of the reference that {@code HEAD} points to.
  868. * <p>
  869. * This is essentially the same as doing:
  870. *
  871. * <pre>
  872. * return exactRef(Constants.HEAD).getTarget().getName()
  873. * </pre>
  874. *
  875. * Except when HEAD is detached, in which case this method returns the
  876. * current ObjectId in hexadecimal string format.
  877. *
  878. * @return name of current branch (for example {@code refs/heads/master}),
  879. * an ObjectId in hex format if the current branch is detached, or
  880. * {@code null} if the repository is corrupt and has no HEAD
  881. * reference.
  882. * @throws IOException
  883. */
  884. @Nullable
  885. public String getFullBranch() throws IOException {
  886. Ref head = exactRef(Constants.HEAD);
  887. if (head == null) {
  888. return null;
  889. }
  890. if (head.isSymbolic()) {
  891. return head.getTarget().getName();
  892. }
  893. ObjectId objectId = head.getObjectId();
  894. if (objectId != null) {
  895. return objectId.name();
  896. }
  897. return null;
  898. }
  899. /**
  900. * Get the short name of the current branch that {@code HEAD} points to.
  901. * <p>
  902. * This is essentially the same as {@link #getFullBranch()}, except the
  903. * leading prefix {@code refs/heads/} is removed from the reference before
  904. * it is returned to the caller.
  905. *
  906. * @return name of current branch (for example {@code master}), an ObjectId
  907. * in hex format if the current branch is detached, or {@code null}
  908. * if the repository is corrupt and has no HEAD reference.
  909. * @throws IOException
  910. */
  911. @Nullable
  912. public String getBranch() throws IOException {
  913. String name = getFullBranch();
  914. if (name != null)
  915. return shortenRefName(name);
  916. return null;
  917. }
  918. /**
  919. * Objects known to exist but not expressed by {@link #getAllRefs()}.
  920. * <p>
  921. * When a repository borrows objects from another repository, it can
  922. * advertise that it safely has that other repository's references, without
  923. * exposing any other details about the other repository. This may help
  924. * a client trying to push changes avoid pushing more than it needs to.
  925. *
  926. * @return unmodifiable collection of other known objects.
  927. */
  928. @NonNull
  929. public Set<ObjectId> getAdditionalHaves() {
  930. return Collections.emptySet();
  931. }
  932. /**
  933. * Get a ref by name.
  934. *
  935. * @param name
  936. * the name of the ref to lookup. May be a short-hand form, e.g.
  937. * "master" which is is automatically expanded to
  938. * "refs/heads/master" if "refs/heads/master" already exists.
  939. * @return the Ref with the given name, or {@code null} if it does not exist
  940. * @throws IOException
  941. * @deprecated Use {@link #exactRef(String)} or {@link #findRef(String)}
  942. * instead.
  943. */
  944. @Deprecated
  945. @Nullable
  946. public Ref getRef(final String name) throws IOException {
  947. return findRef(name);
  948. }
  949. /**
  950. * Get a ref by name.
  951. *
  952. * @param name
  953. * the name of the ref to lookup. Must not be a short-hand
  954. * form; e.g., "master" is not automatically expanded to
  955. * "refs/heads/master".
  956. * @return the Ref with the given name, or {@code null} if it does not exist
  957. * @throws IOException
  958. * @since 4.2
  959. */
  960. @Nullable
  961. public Ref exactRef(String name) throws IOException {
  962. return getRefDatabase().exactRef(name);
  963. }
  964. /**
  965. * Search for a ref by (possibly abbreviated) name.
  966. *
  967. * @param name
  968. * the name of the ref to lookup. May be a short-hand form, e.g.
  969. * "master" which is is automatically expanded to
  970. * "refs/heads/master" if "refs/heads/master" already exists.
  971. * @return the Ref with the given name, or {@code null} if it does not exist
  972. * @throws IOException
  973. * @since 4.2
  974. */
  975. @Nullable
  976. public Ref findRef(String name) throws IOException {
  977. return getRefDatabase().getRef(name);
  978. }
  979. /**
  980. * @return mutable map of all known refs (heads, tags, remotes).
  981. */
  982. @NonNull
  983. public Map<String, Ref> getAllRefs() {
  984. try {
  985. return getRefDatabase().getRefs(RefDatabase.ALL);
  986. } catch (IOException e) {
  987. return new HashMap<String, Ref>();
  988. }
  989. }
  990. /**
  991. * @return mutable map of all tags; key is short tag name ("v1.0") and value
  992. * of the entry contains the ref with the full tag name
  993. * ("refs/tags/v1.0").
  994. */
  995. @NonNull
  996. public Map<String, Ref> getTags() {
  997. try {
  998. return getRefDatabase().getRefs(Constants.R_TAGS);
  999. } catch (IOException e) {
  1000. return new HashMap<String, Ref>();
  1001. }
  1002. }
  1003. /**
  1004. * Peel a possibly unpeeled reference to an annotated tag.
  1005. * <p>
  1006. * If the ref cannot be peeled (as it does not refer to an annotated tag)
  1007. * the peeled id stays null, but {@link Ref#isPeeled()} will be true.
  1008. *
  1009. * @param ref
  1010. * The ref to peel
  1011. * @return <code>ref</code> if <code>ref.isPeeled()</code> is true; else a
  1012. * new Ref object representing the same data as Ref, but isPeeled()
  1013. * will be true and getPeeledObjectId will contain the peeled object
  1014. * (or null).
  1015. */
  1016. @NonNull
  1017. public Ref peel(final Ref ref) {
  1018. try {
  1019. return getRefDatabase().peel(ref);
  1020. } catch (IOException e) {
  1021. // Historical accident; if the reference cannot be peeled due
  1022. // to some sort of repository access problem we claim that the
  1023. // same as if the reference was not an annotated tag.
  1024. return ref;
  1025. }
  1026. }
  1027. /**
  1028. * @return a map with all objects referenced by a peeled ref.
  1029. */
  1030. @NonNull
  1031. public Map<AnyObjectId, Set<Ref>> getAllRefsByPeeledObjectId() {
  1032. Map<String, Ref> allRefs = getAllRefs();
  1033. Map<AnyObjectId, Set<Ref>> ret = new HashMap<AnyObjectId, Set<Ref>>(allRefs.size());
  1034. for (Ref ref : allRefs.values()) {
  1035. ref = peel(ref);
  1036. AnyObjectId target = ref.getPeeledObjectId();
  1037. if (target == null)
  1038. target = ref.getObjectId();
  1039. // We assume most Sets here are singletons
  1040. Set<Ref> oset = ret.put(target, Collections.singleton(ref));
  1041. if (oset != null) {
  1042. // that was not the case (rare)
  1043. if (oset.size() == 1) {
  1044. // Was a read-only singleton, we must copy to a new Set
  1045. oset = new HashSet<Ref>(oset);
  1046. }
  1047. ret.put(target, oset);
  1048. oset.add(ref);
  1049. }
  1050. }
  1051. return ret;
  1052. }
  1053. /**
  1054. * @return the index file location or {@code null} if repository isn't
  1055. * local.
  1056. * @throws NoWorkTreeException
  1057. * if this is bare, which implies it has no working directory.
  1058. * See {@link #isBare()}.
  1059. */
  1060. @NonNull
  1061. public File getIndexFile() throws NoWorkTreeException {
  1062. if (isBare())
  1063. throw new NoWorkTreeException();
  1064. return indexFile;
  1065. }
  1066. /**
  1067. * Create a new in-core index representation and read an index from disk.
  1068. * <p>
  1069. * The new index will be read before it is returned to the caller. Read
  1070. * failures are reported as exceptions and therefore prevent the method from
  1071. * returning a partially populated index.
  1072. *
  1073. * @return a cache representing the contents of the specified index file (if
  1074. * it exists) or an empty cache if the file does not exist.
  1075. * @throws NoWorkTreeException
  1076. * if this is bare, which implies it has no working directory.
  1077. * See {@link #isBare()}.
  1078. * @throws IOException
  1079. * the index file is present but could not be read.
  1080. * @throws CorruptObjectException
  1081. * the index file is using a format or extension that this
  1082. * library does not support.
  1083. */
  1084. @NonNull
  1085. public DirCache readDirCache() throws NoWorkTreeException,
  1086. CorruptObjectException, IOException {
  1087. return DirCache.read(this);
  1088. }
  1089. /**
  1090. * Create a new in-core index representation, lock it, and read from disk.
  1091. * <p>
  1092. * The new index will be locked and then read before it is returned to the
  1093. * caller. Read failures are reported as exceptions and therefore prevent
  1094. * the method from returning a partially populated index.
  1095. *
  1096. * @return a cache representing the contents of the specified index file (if
  1097. * it exists) or an empty cache if the file does not exist.
  1098. * @throws NoWorkTreeException
  1099. * if this is bare, which implies it has no working directory.
  1100. * See {@link #isBare()}.
  1101. * @throws IOException
  1102. * the index file is present but could not be read, or the lock
  1103. * could not be obtained.
  1104. * @throws CorruptObjectException
  1105. * the index file is using a format or extension that this
  1106. * library does not support.
  1107. */
  1108. @NonNull
  1109. public DirCache lockDirCache() throws NoWorkTreeException,
  1110. CorruptObjectException, IOException {
  1111. // we want DirCache to inform us so that we can inform registered
  1112. // listeners about index changes
  1113. IndexChangedListener l = new IndexChangedListener() {
  1114. @Override
  1115. public void onIndexChanged(IndexChangedEvent event) {
  1116. notifyIndexChanged();
  1117. }
  1118. };
  1119. return DirCache.lock(this, l);
  1120. }
  1121. /**
  1122. * @return an important state
  1123. */
  1124. @NonNull
  1125. public RepositoryState getRepositoryState() {
  1126. if (isBare() || getDirectory() == null)
  1127. return RepositoryState.BARE;
  1128. // Pre Git-1.6 logic
  1129. if (new File(getWorkTree(), ".dotest").exists()) //$NON-NLS-1$
  1130. return RepositoryState.REBASING;
  1131. if (new File(getDirectory(), ".dotest-merge").exists()) //$NON-NLS-1$
  1132. return RepositoryState.REBASING_INTERACTIVE;
  1133. // From 1.6 onwards
  1134. if (new File(getDirectory(),"rebase-apply/rebasing").exists()) //$NON-NLS-1$
  1135. return RepositoryState.REBASING_REBASING;
  1136. if (new File(getDirectory(),"rebase-apply/applying").exists()) //$NON-NLS-1$
  1137. return RepositoryState.APPLY;
  1138. if (new File(getDirectory(),"rebase-apply").exists()) //$NON-NLS-1$
  1139. return RepositoryState.REBASING;
  1140. if (new File(getDirectory(),"rebase-merge/interactive").exists()) //$NON-NLS-1$
  1141. return RepositoryState.REBASING_INTERACTIVE;
  1142. if (new File(getDirectory(),"rebase-merge").exists()) //$NON-NLS-1$
  1143. return RepositoryState.REBASING_MERGE;
  1144. // Both versions
  1145. if (new File(getDirectory(), Constants.MERGE_HEAD).exists()) {
  1146. // we are merging - now check whether we have unmerged paths
  1147. try {
  1148. if (!readDirCache().hasUnmergedPaths()) {
  1149. // no unmerged paths -> return the MERGING_RESOLVED state
  1150. return RepositoryState.MERGING_RESOLVED;
  1151. }
  1152. } catch (IOException e) {
  1153. // Can't decide whether unmerged paths exists. Return
  1154. // MERGING state to be on the safe side (in state MERGING
  1155. // you are not allow to do anything)
  1156. }
  1157. return RepositoryState.MERGING;
  1158. }
  1159. if (new File(getDirectory(), "BISECT_LOG").exists()) //$NON-NLS-1$
  1160. return RepositoryState.BISECTING;
  1161. if (new File(getDirectory(), Constants.CHERRY_PICK_HEAD).exists()) {
  1162. try {
  1163. if (!readDirCache().hasUnmergedPaths()) {
  1164. // no unmerged paths
  1165. return RepositoryState.CHERRY_PICKING_RESOLVED;
  1166. }
  1167. } catch (IOException e) {
  1168. // fall through to CHERRY_PICKING
  1169. }
  1170. return RepositoryState.CHERRY_PICKING;
  1171. }
  1172. if (new File(getDirectory(), Constants.REVERT_HEAD).exists()) {
  1173. try {
  1174. if (!readDirCache().hasUnmergedPaths()) {
  1175. // no unmerged paths
  1176. return RepositoryState.REVERTING_RESOLVED;
  1177. }
  1178. } catch (IOException e) {
  1179. // fall through to REVERTING
  1180. }
  1181. return RepositoryState.REVERTING;
  1182. }
  1183. return RepositoryState.SAFE;
  1184. }
  1185. /**
  1186. * Check validity of a ref name. It must not contain character that has
  1187. * a special meaning in a Git object reference expression. Some other
  1188. * dangerous characters are also excluded.
  1189. *
  1190. * For portability reasons '\' is excluded
  1191. *
  1192. * @param refName
  1193. *
  1194. * @return true if refName is a valid ref name
  1195. */
  1196. public static boolean isValidRefName(final String refName) {
  1197. final int len = refName.length();
  1198. if (len == 0)
  1199. return false;
  1200. if (refName.endsWith(".lock")) //$NON-NLS-1$
  1201. return false;
  1202. // Refs may be stored as loose files so invalid paths
  1203. // on the local system must also be invalid refs.
  1204. try {
  1205. SystemReader.getInstance().checkPath(refName);
  1206. } catch (CorruptObjectException e) {
  1207. return false;
  1208. }
  1209. int components = 1;
  1210. char p = '\0';
  1211. for (int i = 0; i < len; i++) {
  1212. final char c = refName.charAt(i);
  1213. if (c <= ' ')
  1214. return false;
  1215. switch (c) {
  1216. case '.':
  1217. switch (p) {
  1218. case '\0': case '/': case '.':
  1219. return false;
  1220. }
  1221. if (i == len -1)
  1222. return false;
  1223. break;
  1224. case '/':
  1225. if (i == 0 || i == len - 1)
  1226. return false;
  1227. if (p == '/')
  1228. return false;
  1229. components++;
  1230. break;
  1231. case '{':
  1232. if (p == '@')
  1233. return false;
  1234. break;
  1235. case '~': case '^': case ':':
  1236. case '?': case '[': case '*':
  1237. case '\\':
  1238. case '\u007F':
  1239. return false;
  1240. }
  1241. p = c;
  1242. }
  1243. return components > 1;
  1244. }
  1245. /**
  1246. * Strip work dir and return normalized repository path.
  1247. *
  1248. * @param workDir Work dir
  1249. * @param file File whose path shall be stripped of its workdir
  1250. * @return normalized repository relative path or the empty
  1251. * string if the file is not relative to the work directory.
  1252. */
  1253. @NonNull
  1254. public static String stripWorkDir(File workDir, File file) {
  1255. final String filePath = file.getPath();
  1256. final String workDirPath = workDir.getPath();
  1257. if (filePath.length() <= workDirPath.length() ||
  1258. filePath.charAt(workDirPath.length()) != File.separatorChar ||
  1259. !filePath.startsWith(workDirPath)) {
  1260. File absWd = workDir.isAbsolute() ? workDir : workDir.getAbsoluteFile();
  1261. File absFile = file.isAbsolute() ? file : file.getAbsoluteFile();
  1262. if (absWd == workDir && absFile == file)
  1263. return ""; //$NON-NLS-1$
  1264. return stripWorkDir(absWd, absFile);
  1265. }
  1266. String relName = filePath.substring(workDirPath.length() + 1);
  1267. if (File.separatorChar != '/')
  1268. relName = relName.replace(File.separatorChar, '/');
  1269. return relName;
  1270. }
  1271. /**
  1272. * @return true if this is bare, which implies it has no working directory.
  1273. */
  1274. public boolean isBare() {
  1275. return workTree == null;
  1276. }
  1277. /**
  1278. * @return the root directory of the working tree, where files are checked
  1279. * out for viewing and editing.
  1280. * @throws NoWorkTreeException
  1281. * if this is bare, which implies it has no working directory.
  1282. * See {@link #isBare()}.
  1283. */
  1284. @NonNull
  1285. public File getWorkTree() throws NoWorkTreeException {
  1286. if (isBare())
  1287. throw new NoWorkTreeException();
  1288. return workTree;
  1289. }
  1290. /**
  1291. * Force a scan for changed refs.
  1292. *
  1293. * @throws IOException
  1294. */
  1295. public abstract void scanForRepoChanges() throws IOException;
  1296. /**
  1297. * Notify that the index changed
  1298. */
  1299. public abstract void notifyIndexChanged();
  1300. /**
  1301. * @param refName
  1302. *
  1303. * @return a more user friendly ref name
  1304. */
  1305. @NonNull
  1306. public static String shortenRefName(String refName) {
  1307. if (refName.startsWith(Constants.R_HEADS))
  1308. return refName.substring(Constants.R_HEADS.length());
  1309. if (refName.startsWith(Constants.R_TAGS))
  1310. return refName.substring(Constants.R_TAGS.length());
  1311. if (refName.startsWith(Constants.R_REMOTES))
  1312. return refName.substring(Constants.R_REMOTES.length());
  1313. return refName;
  1314. }
  1315. /**
  1316. * @param refName
  1317. * @return the remote branch name part of <code>refName</code>, i.e. without
  1318. * the <code>refs/remotes/&lt;remote&gt;</code> prefix, if
  1319. * <code>refName</code> represents a remote tracking branch;
  1320. * otherwise {@code null}.
  1321. * @since 3.4
  1322. */
  1323. @Nullable
  1324. public String shortenRemoteBranchName(String refName) {
  1325. for (String remote : getRemoteNames()) {
  1326. String remotePrefix = Constants.R_REMOTES + remote + "/"; //$NON-NLS-1$
  1327. if (refName.startsWith(remotePrefix))
  1328. return refName.substring(remotePrefix.length());
  1329. }
  1330. return null;
  1331. }
  1332. /**
  1333. * @param refName
  1334. * @return the remote name part of <code>refName</code>, i.e. without the
  1335. * <code>refs/remotes/&lt;remote&gt;</code> prefix, if
  1336. * <code>refName</code> represents a remote tracking branch;
  1337. * otherwise {@code null}.
  1338. * @since 3.4
  1339. */
  1340. @Nullable
  1341. public String getRemoteName(String refName) {
  1342. for (String remote : getRemoteNames()) {
  1343. String remotePrefix = Constants.R_REMOTES + remote + "/"; //$NON-NLS-1$
  1344. if (refName.startsWith(remotePrefix))
  1345. return remote;
  1346. }
  1347. return null;
  1348. }
  1349. /**
  1350. * Read the {@code GIT_DIR/description} file for gitweb.
  1351. *
  1352. * @return description text; null if no description has been configured.
  1353. * @throws IOException
  1354. * description cannot be accessed.
  1355. * @since 4.6
  1356. */
  1357. @Nullable
  1358. public String getGitwebDescription() throws IOException {
  1359. return null;
  1360. }
  1361. /**
  1362. * Set the {@code GIT_DIR/description} file for gitweb.
  1363. *
  1364. * @param description
  1365. * new description; null to clear the description.
  1366. * @throws IOException
  1367. * description cannot be persisted.
  1368. * @since 4.6
  1369. */
  1370. public void setGitwebDescription(@Nullable String description)
  1371. throws IOException {
  1372. throw new IOException(JGitText.get().unsupportedRepositoryDescription);
  1373. }
  1374. /**
  1375. * @param refName
  1376. * @return a {@link ReflogReader} for the supplied refname, or {@code null}
  1377. * if the named ref does not exist.
  1378. * @throws IOException
  1379. * the ref could not be accessed.
  1380. * @since 3.0
  1381. */
  1382. @Nullable
  1383. public abstract ReflogReader getReflogReader(String refName)
  1384. throws IOException;
  1385. /**
  1386. * Return the information stored in the file $GIT_DIR/MERGE_MSG. In this
  1387. * file operations triggering a merge will store a template for the commit
  1388. * message of the merge commit.
  1389. *
  1390. * @return a String containing the content of the MERGE_MSG file or
  1391. * {@code null} if this file doesn't exist
  1392. * @throws IOException
  1393. * @throws NoWorkTreeException
  1394. * if this is bare, which implies it has no working directory.
  1395. * See {@link #isBare()}.
  1396. */
  1397. @Nullable
  1398. public String readMergeCommitMsg() throws IOException, NoWorkTreeException {
  1399. return readCommitMsgFile(Constants.MERGE_MSG);
  1400. }
  1401. /**
  1402. * Write new content to the file $GIT_DIR/MERGE_MSG. In this file operations
  1403. * triggering a merge will store a template for the commit message of the
  1404. * merge commit. If <code>null</code> is specified as message the file will
  1405. * be deleted.
  1406. *
  1407. * @param msg
  1408. * the message which should be written or <code>null</code> to
  1409. * delete the file
  1410. *
  1411. * @throws IOException
  1412. */
  1413. public void writeMergeCommitMsg(String msg) throws IOException {
  1414. File mergeMsgFile = new File(gitDir, Constants.MERGE_MSG);
  1415. writeCommitMsg(mergeMsgFile, msg);
  1416. }
  1417. /**
  1418. * Return the information stored in the file $GIT_DIR/COMMIT_EDITMSG. In
  1419. * this file hooks triggered by an operation may read or modify the current
  1420. * commit message.
  1421. *
  1422. * @return a String containing the content of the COMMIT_EDITMSG file or
  1423. * {@code null} if this file doesn't exist
  1424. * @throws IOException
  1425. * @throws NoWorkTreeException
  1426. * if this is bare, which implies it has no working directory.
  1427. * See {@link #isBare()}.
  1428. * @since 4.0
  1429. */
  1430. @Nullable
  1431. public String readCommitEditMsg() throws IOException, NoWorkTreeException {
  1432. return readCommitMsgFile(Constants.COMMIT_EDITMSG);
  1433. }
  1434. /**
  1435. * Write new content to the file $GIT_DIR/COMMIT_EDITMSG. In this file hooks
  1436. * triggered by an operation may read or modify the current commit message.
  1437. * If {@code null} is specified as message the file will be deleted.
  1438. *
  1439. * @param msg
  1440. * the message which should be written or {@code null} to delete
  1441. * the file
  1442. *
  1443. * @throws IOException
  1444. * @since 4.0
  1445. */
  1446. public void writeCommitEditMsg(String msg) throws IOException {
  1447. File commiEditMsgFile = new File(gitDir, Constants.COMMIT_EDITMSG);
  1448. writeCommitMsg(commiEditMsgFile, msg);
  1449. }
  1450. /**
  1451. * Return the information stored in the file $GIT_DIR/MERGE_HEAD. In this
  1452. * file operations triggering a merge will store the IDs of all heads which
  1453. * should be merged together with HEAD.
  1454. *
  1455. * @return a list of commits which IDs are listed in the MERGE_HEAD file or
  1456. * {@code null} if this file doesn't exist. Also if the file exists
  1457. * but is empty {@code null} will be returned
  1458. * @throws IOException
  1459. * @throws NoWorkTreeException
  1460. * if this is bare, which implies it has no working directory.
  1461. * See {@link #isBare()}.
  1462. */
  1463. @Nullable
  1464. public List<ObjectId> readMergeHeads() throws IOException, NoWorkTreeException {
  1465. if (isBare() || getDirectory() == null)
  1466. throw new NoWorkTreeException();
  1467. byte[] raw = readGitDirectoryFile(Constants.MERGE_HEAD);
  1468. if (raw == null)
  1469. return null;
  1470. LinkedList<ObjectId> heads = new LinkedList<ObjectId>();
  1471. for (int p = 0; p < raw.length;) {
  1472. heads.add(ObjectId.fromString(raw, p));
  1473. p = RawParseUtils
  1474. .nextLF(raw, p + Constants.OBJECT_ID_STRING_LENGTH);
  1475. }
  1476. return heads;
  1477. }
  1478. /**
  1479. * Write new merge-heads into $GIT_DIR/MERGE_HEAD. In this file operations
  1480. * triggering a merge will store the IDs of all heads which should be merged
  1481. * together with HEAD. If <code>null</code> is specified as list of commits
  1482. * the file will be deleted
  1483. *
  1484. * @param heads
  1485. * a list of commits which IDs should be written to
  1486. * $GIT_DIR/MERGE_HEAD or <code>null</code> to delete the file
  1487. * @throws IOException
  1488. */
  1489. public void writeMergeHeads(List<? extends ObjectId> heads) throws IOException {
  1490. writeHeadsFile(heads, Constants.MERGE_HEAD);
  1491. }
  1492. /**
  1493. * Return the information stored in the file $GIT_DIR/CHERRY_PICK_HEAD.
  1494. *
  1495. * @return object id from CHERRY_PICK_HEAD file or {@code null} if this file
  1496. * doesn't exist. Also if the file exists but is empty {@code null}
  1497. * will be returned
  1498. * @throws IOException
  1499. * @throws NoWorkTreeException
  1500. * if this is bare, which implies it has no working directory.
  1501. * See {@link #isBare()}.
  1502. */
  1503. @Nullable
  1504. public ObjectId readCherryPickHead() throws IOException,
  1505. NoWorkTreeException {
  1506. if (isBare() || getDirectory() == null)
  1507. throw new NoWorkTreeException();
  1508. byte[] raw = readGitDirectoryFile(Constants.CHERRY_PICK_HEAD);
  1509. if (raw == null)
  1510. return null;
  1511. return ObjectId.fromString(raw, 0);
  1512. }
  1513. /**
  1514. * Return the information stored in the file $GIT_DIR/REVERT_HEAD.
  1515. *
  1516. * @return object id from REVERT_HEAD file or {@code null} if this file
  1517. * doesn't exist. Also if the file exists but is empty {@code null}
  1518. * will be returned
  1519. * @throws IOException
  1520. * @throws NoWorkTreeException
  1521. * if this is bare, which implies it has no working directory.
  1522. * See {@link #isBare()}.
  1523. */
  1524. @Nullable
  1525. public ObjectId readRevertHead() throws IOException, NoWorkTreeException {
  1526. if (isBare() || getDirectory() == null)
  1527. throw new NoWorkTreeException();
  1528. byte[] raw = readGitDirectoryFile(Constants.REVERT_HEAD);
  1529. if (raw == null)
  1530. return null;
  1531. return ObjectId.fromString(raw, 0);
  1532. }
  1533. /**
  1534. * Write cherry pick commit into $GIT_DIR/CHERRY_PICK_HEAD. This is used in
  1535. * case of conflicts to store the cherry which was tried to be picked.
  1536. *
  1537. * @param head
  1538. * an object id of the cherry commit or <code>null</code> to
  1539. * delete the file
  1540. * @throws IOException
  1541. */
  1542. public void writeCherryPickHead(ObjectId head) throws IOException {
  1543. List<ObjectId> heads = (head != null) ? Collections.singletonList(head)
  1544. : null;
  1545. writeHeadsFile(heads, Constants.CHERRY_PICK_HEAD);
  1546. }
  1547. /**
  1548. * Write revert commit into $GIT_DIR/REVERT_HEAD. This is used in case of
  1549. * conflicts to store the revert which was tried to be picked.
  1550. *
  1551. * @param head
  1552. * an object id of the revert commit or <code>null</code> to
  1553. * delete the file
  1554. * @throws IOException
  1555. */
  1556. public void writeRevertHead(ObjectId head) throws IOException {
  1557. List<ObjectId> heads = (head != null) ? Collections.singletonList(head)
  1558. : null;
  1559. writeHeadsFile(heads, Constants.REVERT_HEAD);
  1560. }
  1561. /**
  1562. * Write original HEAD commit into $GIT_DIR/ORIG_HEAD.
  1563. *
  1564. * @param head
  1565. * an object id of the original HEAD commit or <code>null</code>
  1566. * to delete the file
  1567. * @throws IOException
  1568. */
  1569. public void writeOrigHead(ObjectId head) throws IOException {
  1570. List<ObjectId> heads = head != null ? Collections.singletonList(head)
  1571. : null;
  1572. writeHeadsFile(heads, Constants.ORIG_HEAD);
  1573. }
  1574. /**
  1575. * Return the information stored in the file $GIT_DIR/ORIG_HEAD.
  1576. *
  1577. * @return object id from ORIG_HEAD file or {@code null} if this file
  1578. * doesn't exist. Also if the file exists but is empty {@code null}
  1579. * will be returned
  1580. * @throws IOException
  1581. * @throws NoWorkTreeException
  1582. * if this is bare, which implies it has no working directory.
  1583. * See {@link #isBare()}.
  1584. */
  1585. @Nullable
  1586. public ObjectId readOrigHead() throws IOException, NoWorkTreeException {
  1587. if (isBare() || getDirectory() == null)
  1588. throw new NoWorkTreeException();
  1589. byte[] raw = readGitDirectoryFile(Constants.ORIG_HEAD);
  1590. return raw != null ? ObjectId.fromString(raw, 0) : null;
  1591. }
  1592. /**
  1593. * Return the information stored in the file $GIT_DIR/SQUASH_MSG. In this
  1594. * file operations triggering a squashed merge will store a template for the
  1595. * commit message of the squash commit.
  1596. *
  1597. * @return a String containing the content of the SQUASH_MSG file or
  1598. * {@code null} if this file doesn't exist
  1599. * @throws IOException
  1600. * @throws NoWorkTreeException
  1601. * if this is bare, which implies it has no working directory.
  1602. * See {@link #isBare()}.
  1603. */
  1604. @Nullable
  1605. public String readSquashCommitMsg() throws IOException {
  1606. return readCommitMsgFile(Constants.SQUASH_MSG);
  1607. }
  1608. /**
  1609. * Write new content to the file $GIT_DIR/SQUASH_MSG. In this file
  1610. * operations triggering a squashed merge will store a template for the
  1611. * commit message of the squash commit. If <code>null</code> is specified as
  1612. * message the file will be deleted.
  1613. *
  1614. * @param msg
  1615. * the message which should be written or <code>null</code> to
  1616. * delete the file
  1617. *
  1618. * @throws IOException
  1619. */
  1620. public void writeSquashCommitMsg(String msg) throws IOException {
  1621. File squashMsgFile = new File(gitDir, Constants.SQUASH_MSG);
  1622. writeCommitMsg(squashMsgFile, msg);
  1623. }
  1624. @Nullable
  1625. private String readCommitMsgFile(String msgFilename) throws IOException {
  1626. if (isBare() || getDirectory() == null)
  1627. throw new NoWorkTreeException();
  1628. File mergeMsgFile = new File(getDirectory(), msgFilename);
  1629. try {
  1630. return RawParseUtils.decode(IO.readFully(mergeMsgFile));
  1631. } catch (FileNotFoundException e) {
  1632. if (mergeMsgFile.exists()) {
  1633. throw e;
  1634. }
  1635. // the file has disappeared in the meantime ignore it
  1636. return null;
  1637. }
  1638. }
  1639. private void writeCommitMsg(File msgFile, String msg) throws IOException {
  1640. if (msg != null) {
  1641. FileOutputStream fos = new FileOutputStream(msgFile);
  1642. try {
  1643. fos.write(msg.getBytes(Constants.CHARACTER_ENCODING));
  1644. } finally {
  1645. fos.close();
  1646. }
  1647. } else {
  1648. FileUtils.delete(msgFile, FileUtils.SKIP_MISSING);
  1649. }
  1650. }
  1651. /**
  1652. * Read a file from the git directory.
  1653. *
  1654. * @param filename
  1655. * @return the raw contents or {@code null} if the file doesn't exist or is
  1656. * empty
  1657. * @throws IOException
  1658. */
  1659. @Nullable
  1660. private byte[] readGitDirectoryFile(String filename) throws IOException {
  1661. File file = new File(getDirectory(), filename);
  1662. try {
  1663. byte[] raw = IO.readFully(file);
  1664. return raw.length > 0 ? raw : null;
  1665. } catch (FileNotFoundException notFound) {
  1666. if (file.exists()) {
  1667. throw notFound;
  1668. }
  1669. return null;
  1670. }
  1671. }
  1672. /**
  1673. * Write the given heads to a file in the git directory.
  1674. *
  1675. * @param heads
  1676. * a list of object ids to write or null if the file should be
  1677. * deleted.
  1678. * @param filename
  1679. * @throws FileNotFoundException
  1680. * @throws IOException
  1681. */
  1682. private void writeHeadsFile(List<? extends ObjectId> heads, String filename)
  1683. throws FileNotFoundException, IOException {
  1684. File headsFile = new File(getDirectory(), filename);
  1685. if (heads != null) {
  1686. try (OutputStream bos = new BufferedOutputStream(
  1687. new FileOutputStream(headsFile))) {
  1688. for (ObjectId id : heads) {
  1689. id.copyTo(bos);
  1690. bos.write('\n');
  1691. }
  1692. }
  1693. } else {
  1694. FileUtils.delete(headsFile, FileUtils.SKIP_MISSING);
  1695. }
  1696. }
  1697. /**
  1698. * Read a file formatted like the git-rebase-todo file. The "done" file is
  1699. * also formatted like the git-rebase-todo file. These files can be found in
  1700. * .git/rebase-merge/ or .git/rebase-append/ folders.
  1701. *
  1702. * @param path
  1703. * path to the file relative to the repository's git-dir. E.g.
  1704. * "rebase-merge/git-rebase-todo" or "rebase-append/done"
  1705. * @param includeComments
  1706. * <code>true</code> if also comments should be reported
  1707. * @return the list of steps
  1708. * @throws IOException
  1709. * @since 3.2
  1710. */
  1711. @NonNull
  1712. public List<RebaseTodoLine> readRebaseTodo(String path,
  1713. boolean includeComments)
  1714. throws IOException {
  1715. return new RebaseTodoFile(this).readRebaseTodo(path, includeComments);
  1716. }
  1717. /**
  1718. * Write a file formatted like a git-rebase-todo file.
  1719. *
  1720. * @param path
  1721. * path to the file relative to the repository's git-dir. E.g.
  1722. * "rebase-merge/git-rebase-todo" or "rebase-append/done"
  1723. * @param steps
  1724. * the steps to be written
  1725. * @param append
  1726. * whether to append to an existing file or to write a new file
  1727. * @throws IOException
  1728. * @since 3.2
  1729. */
  1730. public void writeRebaseTodoFile(String path, List<RebaseTodoLine> steps,
  1731. boolean append)
  1732. throws IOException {
  1733. new RebaseTodoFile(this).writeRebaseTodoFile(path, steps, append);
  1734. }
  1735. /**
  1736. * @return the names of all known remotes
  1737. * @since 3.4
  1738. */
  1739. @NonNull
  1740. public Set<String> getRemoteNames() {
  1741. return getConfig()
  1742. .getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION);
  1743. }
  1744. /**
  1745. * Check whether any housekeeping is required; if yes, run garbage
  1746. * collection; if not, exit without performing any work. Some JGit commands
  1747. * run autoGC after performing operations that could create many loose
  1748. * objects.
  1749. * <p/>
  1750. * Currently this option is supported for repositories of type
  1751. * {@code FileRepository} only. See {@link GC#setAuto(boolean)} for
  1752. * configuration details.
  1753. *
  1754. * @param monitor
  1755. * to report progress
  1756. * @since 4.6
  1757. */
  1758. public void autoGC(ProgressMonitor monitor) {
  1759. // default does nothing
  1760. }
  1761. }