18
18
*/
19
19
20
20
/*
21
- * Copyright (c) 2008, 2023 , Oracle and/or its affiliates. All rights reserved.
21
+ * Copyright (c) 2008, 2024 , Oracle and/or its affiliates. All rights reserved.
22
22
* Portions Copyright (c) 2017, 2020, Chris Fraire <[email protected] >.
23
23
* Portions Copyright (c) 2019, Krystof Tulinger <[email protected] >.
24
24
* Portions Copyright (c) 2023, Ric Harris <[email protected] >.
36
36
import java .util .List ;
37
37
import java .util .HashMap ;
38
38
import java .util .Map ;
39
+ import java .util .Objects ;
39
40
import java .util .Optional ;
40
41
import java .util .Scanner ;
41
42
import java .util .Set ;
@@ -458,6 +459,11 @@ public int getPerPartesCount() {
458
459
return MAX_CHANGESETS ;
459
460
}
460
461
462
+ private boolean isRepositoryEmpty () {
463
+ File headsFile = Paths .get (getDirectoryName (), Constants .DOT_GIT , "refs" , "heads" ).toFile ();
464
+ return headsFile .isDirectory () && (Objects .requireNonNull (headsFile .listFiles ()).length == 0 );
465
+ }
466
+
461
467
@ Override
462
468
public void accept (String sinceRevision , Consumer <BoundaryChangesets .IdWithProgress > visitor , Progress progress )
463
469
throws HistoryException {
@@ -473,7 +479,11 @@ public void accept(String sinceRevision, Consumer<BoundaryChangesets.IdWithProgr
473
479
walk .markUninteresting (walk .lookupCommit (objId ));
474
480
}
475
481
ObjectId objId = repository .resolve (Constants .HEAD );
476
- if (objId == null ) {
482
+ if (Objects .isNull (objId )) {
483
+ if (isRepositoryEmpty ()) {
484
+ LOGGER .log (Level .FINEST , "ignoring empty repository {}" , this );
485
+ return ;
486
+ }
477
487
throw new HistoryException ("cannot resolve HEAD" );
478
488
}
479
489
walk .markStart (walk .parseCommit (objId ));
@@ -512,6 +522,11 @@ public void traverseHistory(File file, String sinceRevision, String tillRevision
512
522
try (org .eclipse .jgit .lib .Repository repository = getJGitRepository (getDirectoryName ());
513
523
RevWalk walk = new RevWalk (repository )) {
514
524
525
+ if (Objects .isNull (repository .resolve (Constants .HEAD )) && isRepositoryEmpty ()) {
526
+ LOGGER .log (Level .FINEST , "ignoring empty repository {}" , this );
527
+ return ;
528
+ }
529
+
515
530
setupWalk (file , sinceRevision , tillRevision , repository , walk );
516
531
517
532
int num = 0 ;
@@ -544,17 +559,18 @@ public void traverseHistory(File file, String sinceRevision, String tillRevision
544
559
}
545
560
546
561
private void setupWalk (File file , String sinceRevision , String tillRevision , Repository repository , RevWalk walk )
547
- throws IOException , ForbiddenSymlinkException {
562
+ throws IOException , ForbiddenSymlinkException , HistoryException {
548
563
549
564
if (sinceRevision != null ) {
550
565
walk .markUninteresting (walk .lookupCommit (repository .resolve (sinceRevision )));
551
566
}
552
567
553
- if ( tillRevision != null ) {
554
- walk . markStart ( walk . lookupCommit ( repository .resolve (tillRevision )) );
555
- } else {
556
- walk . markStart ( walk . parseCommit ( repository . resolve ( Constants . HEAD )) );
568
+ final String revStr = Objects . requireNonNullElse ( tillRevision , Constants . HEAD );
569
+ ObjectId objectId = repository .resolve (revStr );
570
+ if ( Objects . isNull ( objectId )) {
571
+ throw new HistoryException ( "failed to lookup revision " + revStr );
557
572
}
573
+ walk .markStart (walk .lookupCommit (objectId ));
558
574
559
575
String relativePath = RuntimeEnvironment .getInstance ().getPathRelativeToSourceRoot (file );
560
576
if (!getDirectoryNameRelative ().equals (relativePath )) {
0 commit comments