Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read initial git branch name from user config. #6528

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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