org.conqat.engine.index.shared.GitRefUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-commons Show documentation
Show all versions of teamscale-commons Show documentation
Provides common DTOs for Teamscale
The newest version!
/*
* Copyright (c) CQSE GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.conqat.engine.index.shared;
import java.util.regex.Pattern;
/**
* Utility methods for working with Git refs.
*/
public class GitRefUtils {
/**
* The Git ref name for the HEAD ref. Note that it's very important to distinguish the meaning of
* this ref for different kinds of Git repositories when determining the default branch (i.e. branch
* checked out upon cloning the repository).
*
* For a regular local Git clone, which doesn't use the {@code --bare} or {@code --mirror} command
* line flags, the HEAD ref points to the revision of the currently checked out branch, tag or
* specific revision.
*
* For a Git checkout with the command line flags {@code --bare} or {@code --mirror} the HEAD points
* to the revision of the default branch.
*
* If refs are loaded from a remote Git using the JGit LsRemoteCommand the (remote) HEAD points to
* the revision of the default branch.
*
* However, for any of the different kinds of Git repositories there may be multiple branch head
* refs, pointing to the same revision as the HEAD ref. Hence the ref for the default branch isn't
* always uniquely defined and there may be multiple options for choosing it for a Git
* repository.
*
* There exists a Git capability (see
* StackOverFlow,
* Official
* Github Release Notes) for determining the branch head ref to which the remote HEAD also
* points. However JGit doesn't support this capability and we're currently limited to guessing the
* default branch names.
*/
public static final String HEAD_REF_NAME = "HEAD";
/** Prefix for the ref name for Git branch heads. */
public static final String BRANCH_HEAD_REF_PREFIX = "refs/heads/";
/** Prefix for refs to remote branches */
public static final String REFERENCE_REMOTES_PREFIX = "refs/remotes/origin/";
/** Name of the master branch. */
public static final String MASTER_BRANCH = "master";
/** Pattern for a SHA-1 hash which identifies a Git commit uniquely. */
private static final Pattern COMMIT_HASH_PATTERN = Pattern.compile("^[\\da-f]{40}$");
/** Base name for anonymous branches. */
private static final String ANONYMOUS_BRANCH_NAME = "_anonymous_";
/**
* Returns whether the given revision is a valid commit hash or the HEAD ref.
*
* @see #HEAD_REF_NAME
*/
public static boolean isCommitHashOrHeadRef(String revision) {
return revision.equals(HEAD_REF_NAME) || COMMIT_HASH_PATTERN.matcher(revision).matches();
}
/**
* Returns true, if and only if the branch name starts with the {@link #ANONYMOUS_BRANCH_NAME}.
*/
public static boolean isAnonymousBranchName(String branchName) {
return branchName != null && branchName.startsWith(ANONYMOUS_BRANCH_NAME);
}
/** Creates the name of a ref for a branch name. */
public static String createBranchHeadRefName(String branchName) {
return BRANCH_HEAD_REF_PREFIX + branchName;
}
/** Creates an anonymous branch name based on the given commit hash. */
public static String createAnonymousBranchName(String commitHash) {
return ANONYMOUS_BRANCH_NAME + commitHash;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy