001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.scm.provider.git.gitexe.command.info; 020 021import org.apache.maven.scm.CommandParameter; 022import org.apache.maven.scm.CommandParameters; 023import org.apache.maven.scm.ScmException; 024import org.apache.maven.scm.ScmFileSet; 025import org.apache.maven.scm.ScmResult; 026import org.apache.maven.scm.command.AbstractCommand; 027import org.apache.maven.scm.command.info.InfoScmResult; 028import org.apache.maven.scm.provider.ScmProviderRepository; 029import org.apache.maven.scm.provider.git.command.GitCommand; 030import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils; 031import org.codehaus.plexus.util.cli.CommandLineUtils; 032import org.codehaus.plexus.util.cli.Commandline; 033 034/** 035 * @author Olivier Lamy 036 * @since 1.5 037 */ 038public class GitInfoCommand extends AbstractCommand implements GitCommand { 039 040 public static final int NO_REVISION_LENGTH = -1; 041 042 @Override 043 protected ScmResult executeCommand( 044 ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException { 045 046 GitInfoConsumer consumer = new GitInfoConsumer(fileSet); 047 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 048 049 Commandline cli = createCommandLine(repository, fileSet, parameters); 050 051 int exitCode = GitCommandLineUtils.execute(cli, consumer, stderr); 052 if (exitCode != 0) { 053 return new InfoScmResult(cli.toString(), "The git rev-parse command failed.", stderr.getOutput(), false); 054 } 055 return new InfoScmResult(cli.toString(), consumer.getInfoItems()); 056 } 057 058 public static Commandline createCommandLine( 059 ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException { 060 Commandline cli = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "rev-parse"); 061 cli.createArg().setValue("--verify"); 062 final int revLength = getRevisionLength(parameters); 063 if (revLength > NO_REVISION_LENGTH) // set the --short key only if revision length parameter is passed and 064 // different from -1 065 { 066 cli.createArg().setValue("--short=" + revLength); 067 } 068 cli.createArg().setValue("HEAD"); 069 070 return cli; 071 } 072 073 /** 074 * Get the revision length from the parameters 075 * 076 * @param parameters 077 * @return -1 if parameter {@link CommandParameter.SCM_SHORT_REVISION_LENGTH} is absent, <br> 078 * and otherwise - the length to be applied for the revision formatting 079 * @throws ScmException 080 * @since 1.7 081 */ 082 private static int getRevisionLength(final CommandParameters parameters) throws ScmException { 083 if (parameters == null) { 084 return NO_REVISION_LENGTH; 085 } else { 086 return parameters.getInt(CommandParameter.SCM_SHORT_REVISION_LENGTH, NO_REVISION_LENGTH); 087 } 088 } 089}