Skip to content

Commit

Permalink
Merge pull request #6528 from mbien/git-initial-branch
Browse files Browse the repository at this point in the history
Read initial git branch name from user config.
  • Loading branch information
mbien authored Oct 14, 2023
2 parents 9805027 + e7f236a commit 887e47e
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions ide/libs.git/src/org/netbeans/libs/git/jgit/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.jgit.api.errors.InvalidRefNameException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.RawText;
import org.eclipse.jgit.diff.RenameDetector;
import org.eclipse.jgit.errors.AmbiguousObjectException;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.errors.RevisionSyntaxException;
Expand All @@ -59,6 +61,7 @@
import org.eclipse.jgit.treewalk.filter.OrTreeFilter;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.eclipse.jgit.util.SystemReader;
import org.netbeans.libs.git.GitBranch;
import org.netbeans.libs.git.GitException;
import org.netbeans.libs.git.GitObjectType;
Expand All @@ -76,9 +79,28 @@ private Utils () {
}

public static Repository getRepositoryForWorkingDir (File workDir) throws IOException, IllegalArgumentException {
Repository repo = new FileRepositoryBuilder().setWorkTree(workDir).build();
repo.getConfig().setBoolean("pack", null, "buildbitmaps", false);
return repo;
FileRepositoryBuilder builder = new FileRepositoryBuilder().setWorkTree(workDir);
// Sets the initial ref name (in .git/HEAD), this is only written when repo.create() is called
// and doesn't affect existing repos. Initial ref is essentially the branch name after first commit.
try {
builder.setInitialBranch(getInitBranchName());
} catch (InvalidRefNameException | ConfigInvalidException | IOException ex) {
Logger.getLogger(Utils.class.getName()).log(Level.INFO, "can not set initial branch name", ex);
}
Repository repo = builder.build();
repo.getConfig().setBoolean("pack", null, "buildbitmaps", false);
return repo;
}

private static String getInitBranchName() throws IOException, ConfigInvalidException {
return getGitSettingString(ConfigConstants.CONFIG_INIT_SECTION, null, ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH);
}

private static String getGitSettingString(String section, String subsection, String name) throws IOException, ConfigInvalidException {
SystemReader reader = SystemReader.getInstance();
String value = reader.getUserConfig().getString(section, subsection, name);
return value != null && !value.isEmpty() ? value
: reader.getSystemConfig().getString(section, subsection, name);
}

public static File getMetadataFolder (File workDir) {
Expand All @@ -99,7 +121,7 @@ public static TreeFilter getExcludeExactPathsFilter (File workDir, File[] roots)
TreeFilter filter = null;
if (relativePaths.size() > 0) {
Collection<PathFilter> filters = getPathFilters(relativePaths);
List<TreeFilter> exactPathFilters = new LinkedList<TreeFilter>();
List<TreeFilter> exactPathFilters = new LinkedList<>();
for (PathFilter f : filters) {
exactPathFilters.add(ExactPathFilter.create(f));
}
Expand All @@ -109,7 +131,7 @@ public static TreeFilter getExcludeExactPathsFilter (File workDir, File[] roots)
}

public static List<GitFileInfo> getDiffEntries (Repository repository, TreeWalk walk, GitClassFactory fac) throws IOException {
List<GitFileInfo> result = new ArrayList<GitFileInfo>();
List<GitFileInfo> result = new ArrayList<>();
List<DiffEntry> entries = DiffEntry.scan(walk);
RenameDetector rd = new RenameDetector(repository);
rd.addAll(entries);
Expand Down Expand Up @@ -167,7 +189,7 @@ public static Collection<PathFilter> getPathFilters (Collection<String> relative
}

public static List<String> getRelativePaths(File workDir, File[] roots) {
List<String> paths = new ArrayList<String>(roots.length);
List<String> paths = new ArrayList<>(roots.length);
for (File root : roots) {
if (workDir.equals(root)) {
paths.clear();
Expand Down Expand Up @@ -228,7 +250,7 @@ public static boolean isUnderOrEqual (TreeWalk treeWalk, Collection<PathFilter>
}

public static Collection<byte[]> getPaths (Collection<PathFilter> pathFilters) {
Collection<byte[]> paths = new LinkedList<byte[]>();
Collection<byte[]> paths = new LinkedList<>();
for (PathFilter filter : pathFilters) {
paths.add(Constants.encode(filter.getPath()));
}
Expand Down Expand Up @@ -301,7 +323,7 @@ public static void deleteRecursively(File file) {
}

/**
* Eliminates part of the ref's name that equals knon prefixes such as refs/heads/, refs/remotes/ etc.
* Eliminates part of the ref's name that equals known prefixes such as refs/heads/, refs/remotes/ etc.
* @param ref
* @return
*/
Expand All @@ -322,7 +344,7 @@ public static String getRefName (Ref ref) {
* @return
*/
public static Map<String, GitBranch> refsToBranches (Collection<Ref> allRefs, String prefix, GitClassFactory factory) {
Map<String, GitBranch> branches = new LinkedHashMap<String, GitBranch>();
Map<String, GitBranch> branches = new LinkedHashMap<>();

// try to find the head first - it usually is the active remote branch
Ref head = null;
Expand Down Expand Up @@ -363,7 +385,7 @@ public static Map<String, GitBranch> refsToBranches (Collection<Ref> allRefs, St
* @return
*/
public static Map<String, String> refsToTags (Collection<Ref> allRefs) {
Map<String, String> tags = new LinkedHashMap<String, String>();
Map<String, String> tags = new LinkedHashMap<>();

// get all refs/tags
for (final Ref ref : RefComparator.sort(allRefs)) {
Expand Down

0 comments on commit 887e47e

Please sign in to comment.