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.

JGitUtils.java 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. package com.gitblit.utils;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.RandomAccessFile;
  7. import java.text.DateFormat;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.ArrayList;
  11. import java.util.Collections;
  12. import java.util.Date;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.Set;
  17. import java.util.concurrent.atomic.AtomicInteger;
  18. import org.eclipse.jgit.diff.DiffEntry;
  19. import org.eclipse.jgit.diff.DiffFormatter;
  20. import org.eclipse.jgit.diff.RawTextComparator;
  21. import org.eclipse.jgit.diff.DiffEntry.ChangeType;
  22. import org.eclipse.jgit.errors.ConfigInvalidException;
  23. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  24. import org.eclipse.jgit.errors.MissingObjectException;
  25. import org.eclipse.jgit.errors.StopWalkException;
  26. import org.eclipse.jgit.lib.AnyObjectId;
  27. import org.eclipse.jgit.lib.Constants;
  28. import org.eclipse.jgit.lib.FileMode;
  29. import org.eclipse.jgit.lib.ObjectId;
  30. import org.eclipse.jgit.lib.ObjectLoader;
  31. import org.eclipse.jgit.lib.PersonIdent;
  32. import org.eclipse.jgit.lib.Ref;
  33. import org.eclipse.jgit.lib.Repository;
  34. import org.eclipse.jgit.lib.StoredConfig;
  35. import org.eclipse.jgit.revwalk.RevBlob;
  36. import org.eclipse.jgit.revwalk.RevCommit;
  37. import org.eclipse.jgit.revwalk.RevObject;
  38. import org.eclipse.jgit.revwalk.RevSort;
  39. import org.eclipse.jgit.revwalk.RevTree;
  40. import org.eclipse.jgit.revwalk.RevWalk;
  41. import org.eclipse.jgit.revwalk.filter.RevFilter;
  42. import org.eclipse.jgit.treewalk.TreeWalk;
  43. import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
  44. import org.eclipse.jgit.treewalk.filter.PathFilter;
  45. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  46. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  47. import org.eclipse.jgit.util.io.DisabledOutputStream;
  48. import org.slf4j.Logger;
  49. import org.slf4j.LoggerFactory;
  50. import com.gitblit.wicket.models.Metric;
  51. import com.gitblit.wicket.models.PathModel;
  52. import com.gitblit.wicket.models.PathModel.PathChangeModel;
  53. import com.gitblit.wicket.models.RefModel;
  54. import com.gitblit.wicket.models.TicketModel;
  55. import com.gitblit.wicket.models.TicketModel.Comment;
  56. public class JGitUtils {
  57. /** Prefix for notes refs */
  58. public static final String R_NOTES = "refs/notes/";
  59. /** Standard notes ref */
  60. public static final String R_NOTES_COMMITS = R_NOTES + "commits";
  61. private final static Logger LOGGER = LoggerFactory.getLogger(JGitUtils.class);
  62. public static List<String> getRepositoryList(File repositoriesFolder, boolean exportAll, boolean readNested) {
  63. List<String> list = new ArrayList<String>();
  64. list.addAll(getNestedRepositories(repositoriesFolder, repositoriesFolder, exportAll, readNested));
  65. Collections.sort(list);
  66. return list;
  67. }
  68. public static List<String> getNestedRepositories(File repositoriesFolder, File folder, boolean exportAll, boolean readNested) {
  69. String basefile = repositoriesFolder.getAbsolutePath();
  70. List<String> list = new ArrayList<String>();
  71. for (File file : folder.listFiles()) {
  72. if (file.isDirectory() && !file.getName().equalsIgnoreCase(Constants.DOT_GIT)) {
  73. // if this is a git repository add it to the list
  74. File gitFolder = new File(file, Constants.DOT_GIT);
  75. boolean isGitRepository = gitFolder.exists() && gitFolder.isDirectory();
  76. boolean exportRepository = isGitRepository && (exportAll || new File(gitFolder, "git-daemon-export-ok").exists());
  77. if (exportRepository) {
  78. // determine repository name relative to repositories folder
  79. String filename = file.getAbsolutePath();
  80. String repo = filename.substring(basefile.length()).replace('\\', '/');
  81. if (repo.charAt(0) == '/') {
  82. repo = repo.substring(1);
  83. }
  84. list.add(repo);
  85. }
  86. // look for nested repositories
  87. if (readNested) {
  88. list.addAll(getNestedRepositories(repositoriesFolder, file, exportAll, readNested));
  89. }
  90. }
  91. }
  92. return list;
  93. }
  94. public static RevCommit getFirstCommit(Repository r, String branch) {
  95. if (StringUtils.isEmpty(branch)) {
  96. branch = Constants.HEAD;
  97. }
  98. try {
  99. RevWalk walk = new RevWalk(r);
  100. walk.sort(RevSort.REVERSE);
  101. RevCommit head = walk.parseCommit(r.resolve(branch));
  102. walk.markStart(head);
  103. RevCommit commit = walk.next();
  104. walk.dispose();
  105. return commit;
  106. } catch (Throwable t) {
  107. LOGGER.error("Failed to determine first commit", t);
  108. }
  109. return null;
  110. }
  111. public static Date getFirstChange(Repository r, String branch) {
  112. try {
  113. RevCommit commit = getFirstCommit(r, branch);
  114. return getCommitDate(commit);
  115. } catch (Throwable t) {
  116. LOGGER.error("Failed to determine first change", t);
  117. }
  118. return null;
  119. }
  120. public static Date getLastChange(Repository r) {
  121. RevCommit commit = getCommit(r, Constants.HEAD);
  122. return getCommitDate(commit);
  123. }
  124. public static RevCommit getCommit(Repository r, String objectId) {
  125. RevCommit commit = null;
  126. try {
  127. if (objectId == null || objectId.trim().length() == 0) {
  128. objectId = Constants.HEAD;
  129. }
  130. ObjectId object = r.resolve(objectId);
  131. RevWalk walk = new RevWalk(r);
  132. RevCommit rev = walk.parseCommit(object);
  133. commit = rev;
  134. walk.dispose();
  135. } catch (Throwable t) {
  136. LOGGER.error("Failed to determine last change", t);
  137. }
  138. return commit;
  139. }
  140. public static Map<ObjectId, List<String>> getAllRefs(Repository r) {
  141. Map<ObjectId, List<String>> refs = new HashMap<ObjectId, List<String>>();
  142. Map<AnyObjectId, Set<Ref>> allRefs = r.getAllRefsByPeeledObjectId();
  143. for (AnyObjectId id : allRefs.keySet()) {
  144. List<String> list = new ArrayList<String>();
  145. for (Ref setRef : allRefs.get(id)) {
  146. String name = setRef.getName();
  147. list.add(name);
  148. }
  149. refs.put(id.toObjectId(), list);
  150. }
  151. return refs;
  152. }
  153. public static Map<ObjectId, List<String>> getRefs(Repository r, String baseRef) {
  154. Map<ObjectId, List<String>> refs = new HashMap<ObjectId, List<String>>();
  155. Map<AnyObjectId, Set<Ref>> allRefs = r.getAllRefsByPeeledObjectId();
  156. for (AnyObjectId id : allRefs.keySet()) {
  157. List<String> list = new ArrayList<String>();
  158. for (Ref setRef : allRefs.get(id)) {
  159. String name = setRef.getName();
  160. if (name.startsWith(baseRef)) {
  161. list.add(name);
  162. }
  163. }
  164. refs.put(id.toObjectId(), list);
  165. }
  166. return refs;
  167. }
  168. /**
  169. * Lookup an entry stored in a tree, failing if not present.
  170. *
  171. * @param tree
  172. * the tree to search.
  173. * @param path
  174. * the path to find the entry of.
  175. * @return the parsed object entry at this path
  176. * @throws Exception
  177. */
  178. public static RevObject getRevObject(Repository r, final RevTree tree, final String path) {
  179. RevObject ro = null;
  180. RevWalk rw = new RevWalk(r);
  181. TreeWalk tw = new TreeWalk(r);
  182. tw.setFilter(PathFilterGroup.createFromStrings(Collections.singleton(path)));
  183. try {
  184. tw.reset(tree);
  185. while (tw.next()) {
  186. if (tw.isSubtree() && !path.equals(tw.getPathString())) {
  187. tw.enterSubtree();
  188. continue;
  189. }
  190. ObjectId entid = tw.getObjectId(0);
  191. FileMode entmode = tw.getFileMode(0);
  192. ro = rw.lookupAny(entid, entmode.getObjectType());
  193. rw.parseBody(ro);
  194. }
  195. } catch (Throwable t) {
  196. LOGGER.error("Can't find " + path + " in tree " + tree.name(), t);
  197. } finally {
  198. if (rw != null) {
  199. rw.dispose();
  200. }
  201. }
  202. return ro;
  203. }
  204. public static byte[] getRawContent(Repository r, RevBlob blob) {
  205. ByteArrayOutputStream os = new ByteArrayOutputStream();
  206. try {
  207. ObjectLoader ldr = r.open(blob.getId(), Constants.OBJ_BLOB);
  208. byte[] tmp = new byte[1024];
  209. InputStream in = ldr.openStream();
  210. int n;
  211. while ((n = in.read(tmp)) > 0) {
  212. os.write(tmp, 0, n);
  213. }
  214. in.close();
  215. } catch (Throwable t) {
  216. LOGGER.error("Failed to read raw content", t);
  217. }
  218. return os.toByteArray();
  219. }
  220. public static String getRawContentAsString(Repository r, RevBlob blob) {
  221. return new String(getRawContent(r, blob));
  222. }
  223. public static String getRawContentAsString(Repository r, RevCommit commit, String blobPath) {
  224. RevObject obj = getRevObject(r, commit.getTree(), blobPath);
  225. return new String(getRawContent(r, (RevBlob) obj));
  226. }
  227. public static List<PathModel> getFilesInPath(Repository r, String basePath, String objectId) {
  228. RevCommit commit = getCommit(r, objectId);
  229. return getFilesInPath(r, basePath, commit);
  230. }
  231. public static List<PathModel> getFilesInPath(Repository r, String basePath, RevCommit commit) {
  232. List<PathModel> list = new ArrayList<PathModel>();
  233. final TreeWalk walk = new TreeWalk(r);
  234. try {
  235. walk.addTree(commit.getTree());
  236. if (basePath != null && basePath.length() > 0) {
  237. PathFilter f = PathFilter.create(basePath);
  238. walk.setFilter(f);
  239. walk.setRecursive(false);
  240. boolean foundFolder = false;
  241. while (walk.next()) {
  242. if (!foundFolder && walk.isSubtree()) {
  243. walk.enterSubtree();
  244. }
  245. if (walk.getPathString().equals(basePath)) {
  246. foundFolder = true;
  247. continue;
  248. }
  249. if (foundFolder) {
  250. list.add(getPathModel(walk, basePath, commit));
  251. }
  252. }
  253. } else {
  254. walk.setRecursive(false);
  255. while (walk.next()) {
  256. list.add(getPathModel(walk, null, commit));
  257. }
  258. }
  259. } catch (IOException e) {
  260. LOGGER.error("Failed to get files for commit " + commit.getName(), e);
  261. } finally {
  262. walk.release();
  263. }
  264. Collections.sort(list);
  265. return list;
  266. }
  267. public static List<PathChangeModel> getFilesInCommit(Repository r, String commitId) {
  268. RevCommit commit = getCommit(r, commitId);
  269. return getFilesInCommit(r, commit);
  270. }
  271. public static List<PathChangeModel> getFilesInCommit(Repository r, RevCommit commit) {
  272. List<PathChangeModel> list = new ArrayList<PathChangeModel>();
  273. try {
  274. final RevWalk rw = new RevWalk(r);
  275. RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
  276. RevTree parentTree = parent.getTree();
  277. RevTree commitTree = commit.getTree();
  278. final TreeWalk walk = new TreeWalk(r);
  279. walk.reset();
  280. walk.setRecursive(true);
  281. walk.addTree(parentTree);
  282. walk.addTree(commitTree);
  283. walk.setFilter(TreeFilter.ANY_DIFF);
  284. RawTextComparator cmp = RawTextComparator.DEFAULT;
  285. DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE);
  286. df.setRepository(r);
  287. df.setDiffComparator(cmp);
  288. df.setDetectRenames(true);
  289. List<DiffEntry> diffs = df.scan(parentTree, commitTree);
  290. for (DiffEntry diff : diffs) {
  291. if (diff.getChangeType().equals(ChangeType.DELETE)) {
  292. list.add(new PathChangeModel(diff.getOldPath(), diff.getOldPath(), 0, diff.getNewMode().getBits(), commit.getId().getName(), diff.getChangeType()));
  293. } else {
  294. list.add(new PathChangeModel(diff.getNewPath(), diff.getNewPath(), 0, diff.getNewMode().getBits(), commit.getId().getName(), diff.getChangeType()));
  295. }
  296. }
  297. } catch (Throwable t) {
  298. LOGGER.error("failed to determine files in commit!", t);
  299. }
  300. return list;
  301. }
  302. public static Map<ChangeType, AtomicInteger> getChangedPathsStats(List<PathChangeModel> paths) {
  303. Map<ChangeType, AtomicInteger> stats = new HashMap<ChangeType, AtomicInteger>();
  304. for (PathChangeModel path : paths) {
  305. if (!stats.containsKey(path.changeType)) {
  306. stats.put(path.changeType, new AtomicInteger(0));
  307. }
  308. stats.get(path.changeType).incrementAndGet();
  309. }
  310. return stats;
  311. }
  312. public static enum DiffOutputType {
  313. PLAIN, GITWEB, GITBLIT;
  314. public static DiffOutputType forName(String name) {
  315. for (DiffOutputType type : values()) {
  316. if (type.name().equalsIgnoreCase(name)) {
  317. return type;
  318. }
  319. }
  320. return null;
  321. }
  322. }
  323. public static String getCommitDiff(Repository r, RevCommit commit, DiffOutputType outputType) {
  324. return getCommitDiff(r, null, commit, null, outputType);
  325. }
  326. public static String getCommitDiff(Repository r, RevCommit commit, String path, DiffOutputType outputType) {
  327. return getCommitDiff(r, null, commit, path, outputType);
  328. }
  329. public static String getCommitDiff(Repository r, RevCommit baseCommit, RevCommit commit, DiffOutputType outputType) {
  330. return getCommitDiff(r, baseCommit, commit, null, outputType);
  331. }
  332. public static String getCommitDiff(Repository r, RevCommit baseCommit, RevCommit commit, String path, DiffOutputType outputType) {
  333. try {
  334. RevTree baseTree;
  335. if (baseCommit == null) {
  336. final RevWalk rw = new RevWalk(r);
  337. RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
  338. rw.dispose();
  339. baseTree = parent.getTree();
  340. } else {
  341. baseTree = baseCommit.getTree();
  342. }
  343. RevTree commitTree = commit.getTree();
  344. final TreeWalk walk = new TreeWalk(r);
  345. walk.reset();
  346. walk.setRecursive(true);
  347. walk.addTree(baseTree);
  348. walk.addTree(commitTree);
  349. walk.setFilter(TreeFilter.ANY_DIFF);
  350. final ByteArrayOutputStream os = new ByteArrayOutputStream();
  351. RawTextComparator cmp = RawTextComparator.DEFAULT;
  352. DiffFormatter df;
  353. switch (outputType) {
  354. case GITWEB:
  355. df = new GitWebDiffFormatter(os);
  356. break;
  357. case GITBLIT:
  358. df = new GitBlitDiffFormatter(os);
  359. break;
  360. case PLAIN:
  361. default:
  362. df = new DiffFormatter(os);
  363. break;
  364. }
  365. df.setRepository(r);
  366. df.setDiffComparator(cmp);
  367. df.setDetectRenames(true);
  368. List<DiffEntry> diffs = df.scan(baseTree, commitTree);
  369. if (path != null && path.length() > 0) {
  370. for (DiffEntry diff : diffs) {
  371. if (diff.getNewPath().equalsIgnoreCase(path)) {
  372. df.format(diff);
  373. break;
  374. }
  375. }
  376. } else {
  377. df.format(diffs);
  378. }
  379. String diff;
  380. if (df instanceof GitWebDiffFormatter) {
  381. // workaround for complex private methods in DiffFormatter
  382. diff = ((GitWebDiffFormatter) df).getHtml();
  383. } else {
  384. diff = os.toString();
  385. }
  386. df.flush();
  387. return diff;
  388. } catch (Throwable t) {
  389. LOGGER.error("failed to generate commit diff!", t);
  390. }
  391. return null;
  392. }
  393. public static String getCommitPatch(Repository r, RevCommit commit) {
  394. return getCommitPatch(r, commit);
  395. }
  396. public static String getCommitPatch(Repository r, RevCommit commit, String path) {
  397. return getCommitPatch(r, null, commit, path);
  398. }
  399. public static String getCommitPatch(Repository r, RevCommit baseCommit, RevCommit commit, String path) {
  400. try {
  401. RevTree baseTree;
  402. if (baseCommit == null) {
  403. final RevWalk rw = new RevWalk(r);
  404. RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
  405. baseTree = parent.getTree();
  406. } else {
  407. baseTree = baseCommit.getTree();
  408. }
  409. RevTree commitTree = commit.getTree();
  410. final TreeWalk walk = new TreeWalk(r);
  411. walk.reset();
  412. walk.setRecursive(true);
  413. walk.addTree(baseTree);
  414. walk.addTree(commitTree);
  415. walk.setFilter(TreeFilter.ANY_DIFF);
  416. final ByteArrayOutputStream os = new ByteArrayOutputStream();
  417. RawTextComparator cmp = RawTextComparator.DEFAULT;
  418. PatchFormatter df = new PatchFormatter(os);
  419. df.setRepository(r);
  420. df.setDiffComparator(cmp);
  421. df.setDetectRenames(true);
  422. List<DiffEntry> diffs = df.scan(baseTree, commitTree);
  423. if (path != null && path.length() > 0) {
  424. for (DiffEntry diff : diffs) {
  425. if (diff.getNewPath().equalsIgnoreCase(path)) {
  426. df.format(diff);
  427. break;
  428. }
  429. }
  430. } else {
  431. df.format(diffs);
  432. }
  433. String diff = df.getPatch(commit);
  434. df.flush();
  435. return diff;
  436. } catch (Throwable t) {
  437. LOGGER.error("failed to generate commit diff!", t);
  438. }
  439. return null;
  440. }
  441. private static PathModel getPathModel(TreeWalk walk, String basePath, RevCommit commit) {
  442. String name;
  443. long size = 0;
  444. if (basePath == null) {
  445. name = walk.getPathString();
  446. } else {
  447. try {
  448. name = walk.getPathString().substring(basePath.length() + 1);
  449. } catch (Throwable t) {
  450. name = walk.getPathString();
  451. }
  452. }
  453. try {
  454. if (!walk.isSubtree()) {
  455. size = walk.getObjectReader().getObjectSize(walk.getObjectId(0), Constants.OBJ_BLOB);
  456. }
  457. } catch (Throwable t) {
  458. LOGGER.error("Failed to retrieve blob size", t);
  459. }
  460. return new PathModel(name, walk.getPathString(), size, walk.getFileMode(0).getBits(), commit.getName());
  461. }
  462. public static String getPermissionsFromMode(int mode) {
  463. if (FileMode.TREE.equals(mode)) {
  464. return "drwxr-xr-x";
  465. } else if (FileMode.REGULAR_FILE.equals(mode)) {
  466. return "-rw-r--r--";
  467. } else if (FileMode.EXECUTABLE_FILE.equals(mode)) {
  468. return "-rwxr-xr-x";
  469. } else if (FileMode.SYMLINK.equals(mode)) {
  470. // FIXME symlink permissions
  471. return "symlink";
  472. } else if (FileMode.GITLINK.equals(mode)) {
  473. // FIXME gitlink permissions
  474. return "gitlink";
  475. } else if (FileMode.MISSING.equals(mode)) {
  476. // FIXME missing permissions
  477. return "missing";
  478. }
  479. return "" + mode;
  480. }
  481. public static boolean isTreeFromMode(int mode) {
  482. return FileMode.TREE.equals(mode);
  483. }
  484. public static List<RevCommit> getRevLog(Repository r, int maxCount) {
  485. return getRevLog(r, Constants.HEAD, 0, maxCount);
  486. }
  487. public static List<RevCommit> getRevLog(Repository r, String objectId, int offset, int maxCount) {
  488. return getRevLog(r, objectId, null, offset, maxCount);
  489. }
  490. public static List<RevCommit> getRevLog(Repository r, String objectId, String path, int offset, int maxCount) {
  491. List<RevCommit> list = new ArrayList<RevCommit>();
  492. try {
  493. if (objectId == null || objectId.trim().length() == 0) {
  494. objectId = Constants.HEAD;
  495. }
  496. RevWalk walk = new RevWalk(r);
  497. ObjectId object = r.resolve(objectId);
  498. walk.markStart(walk.parseCommit(object));
  499. if (!StringUtils.isEmpty(path)) {
  500. TreeFilter filter = AndTreeFilter.create(PathFilterGroup.createFromStrings(Collections.singleton(path)), TreeFilter.ANY_DIFF);
  501. walk.setTreeFilter(filter);
  502. }
  503. Iterable<RevCommit> revlog = walk;
  504. if (offset > 0) {
  505. int count = 0;
  506. for (RevCommit rev : revlog) {
  507. count++;
  508. if (count > offset) {
  509. list.add(rev);
  510. if (maxCount > 0 && list.size() == maxCount) {
  511. break;
  512. }
  513. }
  514. }
  515. } else {
  516. for (RevCommit rev : revlog) {
  517. list.add(rev);
  518. if (maxCount > 0 && list.size() == maxCount) {
  519. break;
  520. }
  521. }
  522. }
  523. walk.dispose();
  524. } catch (Throwable t) {
  525. LOGGER.error("Failed to determine last change", t);
  526. }
  527. return list;
  528. }
  529. public static enum SearchType {
  530. AUTHOR, COMMITTER, COMMIT;
  531. public static SearchType forName(String name) {
  532. for (SearchType type : values()) {
  533. if (type.name().equalsIgnoreCase(name)) {
  534. return type;
  535. }
  536. }
  537. return null;
  538. }
  539. public String toString() {
  540. return name().toLowerCase();
  541. }
  542. }
  543. public static List<RevCommit> searchRevlogs(Repository r, String objectId, String value, final SearchType type, int offset, int maxCount) {
  544. final String lcValue = value.toLowerCase();
  545. List<RevCommit> list = new ArrayList<RevCommit>();
  546. try {
  547. if (objectId == null || objectId.trim().length() == 0) {
  548. objectId = Constants.HEAD;
  549. }
  550. RevWalk walk = new RevWalk(r);
  551. walk.setRevFilter(new RevFilter() {
  552. @Override
  553. public RevFilter clone() {
  554. return this;
  555. }
  556. @Override
  557. public boolean include(RevWalk walker, RevCommit commit) throws StopWalkException, MissingObjectException, IncorrectObjectTypeException, IOException {
  558. switch (type) {
  559. case AUTHOR:
  560. return (commit.getAuthorIdent().getName().toLowerCase().indexOf(lcValue) > -1) || (commit.getAuthorIdent().getEmailAddress().toLowerCase().indexOf(lcValue) > -1);
  561. case COMMITTER:
  562. return (commit.getCommitterIdent().getName().toLowerCase().indexOf(lcValue) > -1) || (commit.getCommitterIdent().getEmailAddress().toLowerCase().indexOf(lcValue) > -1);
  563. case COMMIT:
  564. return commit.getFullMessage().toLowerCase().indexOf(lcValue) > -1;
  565. }
  566. return false;
  567. }
  568. });
  569. ObjectId object = r.resolve(objectId);
  570. walk.markStart(walk.parseCommit(object));
  571. Iterable<RevCommit> revlog = walk;
  572. if (offset > 0) {
  573. int count = 0;
  574. for (RevCommit rev : revlog) {
  575. count++;
  576. if (count > offset) {
  577. list.add(rev);
  578. if (maxCount > 0 && list.size() == maxCount) {
  579. break;
  580. }
  581. }
  582. }
  583. } else {
  584. for (RevCommit rev : revlog) {
  585. list.add(rev);
  586. if (maxCount > 0 && list.size() == maxCount) {
  587. break;
  588. }
  589. }
  590. }
  591. walk.dispose();
  592. } catch (Throwable t) {
  593. LOGGER.error("Failed to determine last change", t);
  594. }
  595. return list;
  596. }
  597. public static List<RefModel> getTags(Repository r, int maxCount) {
  598. return getRefs(r, Constants.R_TAGS, maxCount);
  599. }
  600. public static List<RefModel> getLocalBranches(Repository r, int maxCount) {
  601. return getRefs(r, Constants.R_HEADS, maxCount);
  602. }
  603. public static List<RefModel> getRemoteBranches(Repository r, int maxCount) {
  604. return getRefs(r, Constants.R_REMOTES, maxCount);
  605. }
  606. public static List<RefModel> getRefs(Repository r, String refs, int maxCount) {
  607. List<RefModel> list = new ArrayList<RefModel>();
  608. try {
  609. Map<String, Ref> map = r.getRefDatabase().getRefs(refs);
  610. for (String name : map.keySet()) {
  611. Ref ref = map.get(name);
  612. RevCommit commit = getCommit(r, ref.getObjectId().getName());
  613. list.add(new RefModel(name, ref, commit));
  614. }
  615. Collections.sort(list);
  616. Collections.reverse(list);
  617. if (maxCount > 0 && list.size() > maxCount) {
  618. list = new ArrayList<RefModel>(list.subList(0, maxCount));
  619. }
  620. } catch (IOException e) {
  621. LOGGER.error("Failed to retrieve " + refs, e);
  622. }
  623. return list;
  624. }
  625. public static Ref getRef(Repository r, String id) {
  626. try {
  627. Map<String, Ref> map = r.getRefDatabase().getRefs(id);
  628. for (String name : map.keySet()) {
  629. return map.get(name);
  630. }
  631. } catch (IOException e) {
  632. LOGGER.error("Failed to retrieve ref " + id, e);
  633. }
  634. return null;
  635. }
  636. public static Date getCommitDate(RevCommit commit) {
  637. return new Date(commit.getCommitTime() * 1000l);
  638. }
  639. public static String getDisplayName(PersonIdent person) {
  640. final StringBuilder r = new StringBuilder();
  641. r.append(person.getName());
  642. r.append(" <");
  643. r.append(person.getEmailAddress());
  644. r.append(">");
  645. return r.toString();
  646. }
  647. public static String getRepositoryDescription(Repository r) {
  648. File dir = r.getDirectory();
  649. if (dir.exists()) {
  650. File description = new File(dir, "description");
  651. if (description.exists() && description.length() > 0) {
  652. RandomAccessFile raf = null;
  653. try {
  654. raf = new RandomAccessFile(description, "r");
  655. byte[] buffer = new byte[(int) description.length()];
  656. raf.readFully(buffer);
  657. return new String(buffer);
  658. } catch (Throwable t) {
  659. } finally {
  660. try {
  661. raf.close();
  662. } catch (Throwable t) {
  663. }
  664. }
  665. }
  666. }
  667. return "";
  668. }
  669. public static String getRepositoryOwner(Repository r) {
  670. StoredConfig c = readConfig(r);
  671. if (c == null) {
  672. return "";
  673. }
  674. String o = c.getString("gitweb", null, "owner");
  675. return o == null ? "" : o;
  676. }
  677. private static StoredConfig readConfig(Repository r) {
  678. StoredConfig c = r.getConfig();
  679. if (c != null) {
  680. try {
  681. c.load();
  682. } catch (ConfigInvalidException cex) {
  683. LOGGER.error("Repository configuration is invalid!", cex);
  684. } catch (IOException cex) {
  685. LOGGER.error("Could not open repository configuration!", cex);
  686. }
  687. return c;
  688. }
  689. return null;
  690. }
  691. public static List<Metric> getDateMetrics(Repository r) {
  692. final List<RefModel> tags = getTags(r, -1);
  693. final Map<ObjectId, RefModel> tagMap = new HashMap<ObjectId, RefModel>();
  694. for (RefModel tag : tags) {
  695. tagMap.put(tag.getCommitId(), tag);
  696. }
  697. Metric total = new Metric("TOTAL");
  698. final Map<String, Metric> metricMap = new HashMap<String, Metric>();
  699. try {
  700. RevWalk walk = new RevWalk(r);
  701. ObjectId object = r.resolve(Constants.HEAD);
  702. RevCommit firstCommit = getFirstCommit(r, Constants.HEAD);
  703. RevCommit lastCommit = walk.parseCommit(object);
  704. int diffDays = (lastCommit.getCommitTime() - firstCommit.getCommitTime()) / (60 * 60 * 24);
  705. total.duration = diffDays;
  706. DateFormat df;
  707. if (diffDays <= 90) {
  708. // Days
  709. df = new SimpleDateFormat("yyyy-MM-dd");
  710. } else if (diffDays > 90 && diffDays < 365) {
  711. // Weeks
  712. df = new SimpleDateFormat("yyyy-MM (w)");
  713. } else {
  714. // Months
  715. df = new SimpleDateFormat("yyyy-MM");
  716. }
  717. walk.markStart(lastCommit);
  718. Iterable<RevCommit> revlog = walk;
  719. for (RevCommit rev : revlog) {
  720. Date d = getCommitDate(rev);
  721. String p = df.format(d);
  722. if (!metricMap.containsKey(p))
  723. metricMap.put(p, new Metric(p));
  724. Metric m = metricMap.get(p);
  725. m.count++;
  726. total.count++;
  727. if (tagMap.containsKey(rev.getId())) {
  728. m.tag++;
  729. total.tag++;
  730. }
  731. }
  732. } catch (Throwable t) {
  733. LOGGER.error("Failed to mine log history for metrics", t);
  734. }
  735. List<String> keys = new ArrayList<String>(metricMap.keySet());
  736. Collections.sort(keys);
  737. List<Metric> metrics = new ArrayList<Metric>();
  738. for (String key : keys) {
  739. metrics.add(metricMap.get(key));
  740. }
  741. metrics.add(0, total);
  742. return metrics;
  743. }
  744. public static RefModel getTicketsBranch(Repository r) {
  745. RefModel ticgitBranch = null;
  746. try {
  747. // search for ticgit branch in local heads
  748. for (RefModel ref : getLocalBranches(r, -1)) {
  749. if (ref.getDisplayName().endsWith("ticgit")) {
  750. ticgitBranch = ref;
  751. break;
  752. }
  753. }
  754. // search for ticgit branch in remote heads
  755. if (ticgitBranch == null) {
  756. for (RefModel ref : getRemoteBranches(r, -1)) {
  757. if (ref.getDisplayName().endsWith("ticgit")) {
  758. ticgitBranch = ref;
  759. break;
  760. }
  761. }
  762. }
  763. } catch (Throwable t) {
  764. LOGGER.error("Failed to find ticgit branch!", t);
  765. }
  766. return ticgitBranch;
  767. }
  768. public static List<TicketModel> getTickets(Repository r) {
  769. RefModel ticgitBranch = getTicketsBranch(r);
  770. List<PathModel> paths = getFilesInPath(r, null, ticgitBranch.getCommit());
  771. List<TicketModel> tickets = new ArrayList<TicketModel>();
  772. for (PathModel ticketFolder : paths) {
  773. if (ticketFolder.isTree()) {
  774. try {
  775. TicketModel t = new TicketModel(ticketFolder.name);
  776. readTicketContents(r, ticgitBranch, t);
  777. tickets.add(t);
  778. } catch (Throwable t) {
  779. LOGGER.error("Failed to get a ticket!", t);
  780. }
  781. }
  782. }
  783. Collections.sort(tickets);
  784. Collections.reverse(tickets);
  785. return tickets;
  786. }
  787. public static TicketModel getTicket(Repository r, String ticketFolder) {
  788. RefModel ticketsBranch = getTicketsBranch(r);
  789. if (ticketsBranch != null) {
  790. try {
  791. TicketModel ticket = new TicketModel(ticketFolder);
  792. readTicketContents(r, ticketsBranch, ticket);
  793. return ticket;
  794. } catch (Throwable t) {
  795. LOGGER.error("Failed to get ticket " + ticketFolder, t);
  796. }
  797. }
  798. return null;
  799. }
  800. private static void readTicketContents(Repository r, RefModel ticketsBranch, TicketModel ticket) {
  801. List<PathModel> ticketFiles = getFilesInPath(r, ticket.name, ticketsBranch.getCommit());
  802. for (PathModel file : ticketFiles) {
  803. String content = getRawContentAsString(r, ticketsBranch.getCommit(), file.path).trim();
  804. if (file.name.equals("TICKET_ID")) {
  805. ticket.id = content;
  806. } else if (file.name.equals("TITLE")) {
  807. ticket.title = content;
  808. } else {
  809. String[] chunks = file.name.split("_");
  810. if (chunks[0].equals("ASSIGNED")) {
  811. ticket.handler = content;
  812. } else if (chunks[0].equals("COMMENT")) {
  813. try {
  814. Comment c = new Comment(file.name, content);
  815. ticket.comments.add(c);
  816. } catch (ParseException e) {
  817. e.printStackTrace();
  818. }
  819. } else if (chunks[0].equals("TAG")) {
  820. if (content.startsWith("TAG_")) {
  821. ticket.tags.add(content.substring(4));
  822. } else {
  823. ticket.tags.add(content);
  824. }
  825. } else if (chunks[0].equals("STATE")) {
  826. ticket.state = content;
  827. }
  828. }
  829. }
  830. Collections.sort(ticket.comments);
  831. }
  832. public static String getTicketContent(Repository r, String filePath) {
  833. RefModel ticketsBranch = getTicketsBranch(r);
  834. if (ticketsBranch != null) {
  835. return getRawContentAsString(r, ticketsBranch.getCommit(), filePath);
  836. }
  837. return "";
  838. }
  839. }