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

Unabbreviate cl variable names #230

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public final class GitCommandLineUtils {

private GitCommandLineUtils() {}

public static void addTarget(Commandline cl, List<File> files) {
public static void addTarget(Commandline commandLine, List<File> files) {
if (files == null || files.isEmpty()) {
return;
}
final File workingDirectory = cl.getWorkingDirectory();
final File workingDirectory = commandLine.getWorkingDirectory();
try {
final String canonicalWorkingDirectory = workingDirectory.getCanonicalPath();
for (File file : files) {
Expand All @@ -73,7 +73,7 @@ public static void addTarget(Commandline cl, List<File> files) {
}

// no setFile() since this screws up the working directory!
cl.createArg().setValue(FilenameUtils.separatorsToUnix(relativeFile));
commandLine.createArg().setValue(FilenameUtils.separatorsToUnix(relativeFile));
}
} catch (IOException ex) {
throw new IllegalArgumentException(
Expand All @@ -84,77 +84,72 @@ public static void addTarget(Commandline cl, List<File> files) {

/**
* Use this only for commands not requiring environment variables (i.e. local commands).
* @param workingDirectory
* @param command
* @return TODO
*/
public static Commandline getBaseGitCommandLine(File workingDirectory, String command) {
return getBaseGitCommandLine(workingDirectory, command, null, null);
}

/**
* Use this for commands requiring environment variables (i.e. remote commands).
* @param workingDirectory
* @param command
* @param environment
* @return TODO
*/
public static Commandline getBaseGitCommandLine(
File workingDirectory,
String command,
GitScmProviderRepository repository,
Map<String, String> environment) {
Commandline cl = getAnonymousBaseGitCommandLine(workingDirectory, command);
Commandline commandLine = getAnonymousBaseGitCommandLine(workingDirectory, command);
if (repository != null) {
prepareEnvVariablesForRepository(repository, environment).forEach(cl::addEnvironment);
prepareEnvVariablesForRepository(repository, environment).forEach(commandLine::addEnvironment);
} else if (environment != null) {
environment.forEach(cl::addEnvironment);
environment.forEach(commandLine::addEnvironment);
}
return cl;
return commandLine;
}

/**
* Creates a {@link Commandline} for which the toString() do not display
* password.
* Creates a {@link Commandline} for which toString() does not display
* the password.
*
* @param workingDirectory
* @param command
* @return CommandLine with anonymous output.
* @return CommandLine with anonymous output
*/
private static Commandline getAnonymousBaseGitCommandLine(File workingDirectory, String command) {
if (command == null || command.length() == 0) {
if (command == null || command.isEmpty()) {
return null;
}

Commandline cl = new AnonymousCommandLine();
Commandline commandLine = new AnonymousCommandLine();

composeCommand(workingDirectory, command, cl);
composeCommand(workingDirectory, command, commandLine);

return cl;
return commandLine;
}

private static void composeCommand(File workingDirectory, String command, Commandline cl) {
private static void composeCommand(File workingDirectory, String command, Commandline commandLine) {
Settings settings = GitUtil.getSettings();

cl.setExecutable(settings.getGitCommand());
commandLine.setExecutable(settings.getGitCommand());

cl.createArg().setValue(command);
commandLine.createArg().setValue(command);

if (workingDirectory != null) {
cl.setWorkingDirectory(workingDirectory.getAbsolutePath());
commandLine.setWorkingDirectory(workingDirectory.getAbsolutePath());
}
}

public static int execute(Commandline cl, StreamConsumer consumer, CommandLineUtils.StringStreamConsumer stderr)
public static int execute(
Commandline commandline, StreamConsumer consumer, CommandLineUtils.StringStreamConsumer stderr)
throws ScmException {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Executing: " + cl);
LOGGER.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
LOGGER.info("Executing: " + commandline);
LOGGER.info(
"Working directory: " + commandline.getWorkingDirectory().getAbsolutePath());
}

int exitCode;
try {
exitCode = CommandLineUtils.executeCommandLine(cl, consumer, stderr);
exitCode = CommandLineUtils.executeCommandLine(commandline, consumer, stderr);
} catch (CommandLineException ex) {
throw new ScmException("Error while executing command.", ex);
}
Expand All @@ -163,16 +158,19 @@ public static int execute(Commandline cl, StreamConsumer consumer, CommandLineUt
}

public static int execute(
Commandline cl, CommandLineUtils.StringStreamConsumer stdout, CommandLineUtils.StringStreamConsumer stderr)
Commandline commandLine,
CommandLineUtils.StringStreamConsumer stdout,
CommandLineUtils.StringStreamConsumer stderr)
throws ScmException {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Executing: " + cl);
LOGGER.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
LOGGER.info("Executing: " + commandLine);
LOGGER.info(
"Working directory: " + commandLine.getWorkingDirectory().getAbsolutePath());
}

int exitCode;
try {
exitCode = CommandLineUtils.executeCommandLine(cl, stdout, stderr);
exitCode = CommandLineUtils.executeCommandLine(commandLine, stdout, stderr);
} catch (CommandLineException ex) {
throw new ScmException("Error while executing command.", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ public ScmResult executeCommand(ScmProviderRepository repo, ScmFileSet fileSet,
}

// no git repo seems to exist, let's clone the original repo
Commandline clClone = createCloneCommand(repository, fileSet.getBasedir(), version, binary, shallow);
Commandline gitClone = createCloneCommand(repository, fileSet.getBasedir(), version, binary, shallow);

exitCode = GitCommandLineUtils.execute(clClone, stdout, stderr);
exitCode = GitCommandLineUtils.execute(gitClone, stdout, stderr);
if (exitCode != 0) {
return new CheckOutScmResult(
clClone.toString(), "The git-clone command failed.", stderr.getOutput(), false);
gitClone.toString(), "The git clone command failed.", stderr.getOutput(), false);
}
lastCommandLine = clClone.toString();
lastCommandLine = gitClone.toString();
}

GitRemoteInfoCommand gitRemoteInfoCommand = new GitRemoteInfoCommand(environmentVariables);
Expand All @@ -115,35 +115,34 @@ public ScmResult executeCommand(ScmProviderRepository repo, ScmFileSet fileSet,
&& new File(fileSet.getBasedir(), ".git").exists()
&& result.getBranches().size() > 0) {
// git repo exists, so we must git-pull the changes
Commandline clPull = createPullCommand(repository, fileSet.getBasedir(), version);
Commandline gitPull = createPullCommand(repository, fileSet.getBasedir(), version);

exitCode = GitCommandLineUtils.execute(clPull, stdout, stderr);
exitCode = GitCommandLineUtils.execute(gitPull, stdout, stderr);
if (exitCode != 0) {
return new CheckOutScmResult(
clPull.toString(), "The git-pull command failed.", stderr.getOutput(), false);
gitPull.toString(), "The git pull command failed.", stderr.getOutput(), false);
}
lastCommandLine = clPull.toString();

// and now lets do the git-checkout itself
Commandline clCheckout = createCommandLine(repository, fileSet.getBasedir(), version);
// and now let's do the git-checkout itself
Commandline gitCheckout = createCommandLine(repository, fileSet.getBasedir(), version);

exitCode = GitCommandLineUtils.execute(clCheckout, stdout, stderr);
exitCode = GitCommandLineUtils.execute(gitCheckout, stdout, stderr);
if (exitCode != 0) {
return new CheckOutScmResult(
clCheckout.toString(), "The git-checkout command failed.", stderr.getOutput(), false);
gitCheckout.toString(), "The git checkout command failed.", stderr.getOutput(), false);
}
lastCommandLine = clCheckout.toString();
lastCommandLine = gitCheckout.toString();
}

// and now search for the files
GitListConsumer listConsumer = new GitListConsumer(fileSet.getBasedir(), ScmFileStatus.CHECKED_IN);

Commandline clList = GitListCommand.createCommandLine(repository, fileSet.getBasedir());
Commandline gitList = GitListCommand.createCommandLine(repository, fileSet.getBasedir());

exitCode = GitCommandLineUtils.execute(clList, listConsumer, stderr);
exitCode = GitCommandLineUtils.execute(gitList, listConsumer, stderr);
if (exitCode != 0) {
return new CheckOutScmResult(
clList.toString(), "The git-ls-files command failed.", stderr.getOutput(), false);
gitList.toString(), "The git ls-files command failed.", stderr.getOutput(), false);
}

return new CheckOutScmResult(lastCommandLine, listConsumer.getListedFiles());
Expand All @@ -155,13 +154,13 @@ && new File(fileSet.getBasedir(), ".git").exists()

public static Commandline createCommandLine(
GitScmProviderRepository repository, File workingDirectory, ScmVersion version) {
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "checkout");
Commandline gitCheckout = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "checkout");

if (version != null && StringUtils.isNotEmpty(version.getName())) {
cl.createArg().setValue(version.getName());
gitCheckout.createArg().setValue(version.getName());
}

return cl;
return gitCheckout;
}

/**
Expand All @@ -173,44 +172,43 @@ private Commandline createCloneCommand(
ScmVersion version,
boolean binary,
boolean shallow) {
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(
Commandline gitClone = GitCommandLineUtils.getBaseGitCommandLine(
workingDirectory.getParentFile(), "clone", repository, environmentVariables);

forceBinary(cl, binary);
forceBinary(gitClone, binary);

if (shallow) {
cl.createArg().setValue("--depth");
gitClone.createArg().setValue("--depth");

cl.createArg().setValue("1");
gitClone.createArg().setValue("1");
}

if (version != null && (version instanceof ScmBranch)) {

cl.createArg().setValue("--branch");
gitClone.createArg().setValue("--branch");

cl.createArg().setValue(version.getName());
gitClone.createArg().setValue(version.getName());
}

cl.createArg().setValue(repository.getFetchUrl());
gitClone.createArg().setValue(repository.getFetchUrl());

cl.createArg().setValue(workingDirectory.getName());
gitClone.createArg().setValue(workingDirectory.getName());

return cl;
return gitClone;
}

private void forceBinary(Commandline cl, boolean binary) {
private void forceBinary(Commandline commandLine, boolean binary) {
if (binary) {
cl.createArg().setValue("-c");
cl.createArg().setValue("core.autocrlf=false");
commandLine.createArg().setValue("-c");
commandLine.createArg().setValue("core.autocrlf=false");
}
}

/**
* create a git-pull repository command
* Create a git fetch or git pull repository command.
*/
private Commandline createPullCommand(
GitScmProviderRepository repository, File workingDirectory, ScmVersion version) {
Commandline cl;

if (version != null && StringUtils.isNotEmpty(version.getName())) {
if (version instanceof ScmTag) {
Expand All @@ -219,33 +217,34 @@ private Commandline createPullCommand(
// but create a 'detached HEAD'.
// In fact, a tag in git may be in multiple branches. This occurs if
// you create a branch after the tag has been created
cl = GitCommandLineUtils.getBaseGitCommandLine(
Commandline gitFetch = GitCommandLineUtils.getBaseGitCommandLine(
workingDirectory, "fetch", repository, environmentVariables);

cl.createArg().setValue(repository.getFetchUrl());
gitFetch.createArg().setValue(repository.getFetchUrl());
return gitFetch;
} else {
cl = GitCommandLineUtils.getBaseGitCommandLine(
Commandline gitPull = GitCommandLineUtils.getBaseGitCommandLine(
workingDirectory, "pull", repository, environmentVariables);

cl.createArg().setValue(repository.getFetchUrl());

cl.createArg().setValue(version.getName() + ":" + version.getName());
gitPull.createArg().setValue(repository.getFetchUrl());
gitPull.createArg().setValue(version.getName() + ":" + version.getName());
return gitPull;
}
} else {
cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "pull", repository, environmentVariables);

cl.createArg().setValue(repository.getFetchUrl());
cl.createArg().setValue("master");
Commandline gitPull = GitCommandLineUtils.getBaseGitCommandLine(
workingDirectory, "pull", repository, environmentVariables);
gitPull.createArg().setValue(repository.getFetchUrl());
gitPull.createArg().setValue("master");
return gitPull;
}
return cl;
}

/**
* The overriden {@link #executeCommand(ScmProviderRepository, ScmFileSet, CommandParameters)} in this class will
* The overridden {@link #executeCommand(ScmProviderRepository, ScmFileSet, CommandParameters)} in this class will
* not call this method!
* <p>
* {@inheritDoc}
*/
@Override
protected CheckOutScmResult executeCheckOutCommand(
ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, boolean recursive, boolean shallow)
throws ScmException {
Expand Down
Loading