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.status; 020 021import java.net.URI; 022 023import org.apache.maven.scm.ScmException; 024import org.apache.maven.scm.ScmFileSet; 025import org.apache.maven.scm.command.status.AbstractStatusCommand; 026import org.apache.maven.scm.command.status.StatusScmResult; 027import org.apache.maven.scm.provider.ScmProviderRepository; 028import org.apache.maven.scm.provider.git.command.GitCommand; 029import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils; 030import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository; 031import org.codehaus.plexus.util.cli.CommandLineUtils; 032import org.codehaus.plexus.util.cli.Commandline; 033import org.slf4j.Logger; 034 035/** 036 * @author <a href="mailto:brett@apache.org">Brett Porter</a> 037 * 038 */ 039public class GitStatusCommand extends AbstractStatusCommand implements GitCommand { 040 /** {@inheritDoc} */ 041 protected StatusScmResult executeStatusCommand(ScmProviderRepository repo, ScmFileSet fileSet) throws ScmException { 042 int exitCode; 043 CommandLineUtils.StringStreamConsumer stderr; 044 045 URI relativeRepositoryPath = getRelativeCWD(logger, fileSet); 046 047 Commandline cl = createCommandLine((GitScmProviderRepository) repo, fileSet); 048 049 GitStatusConsumer consumer = new GitStatusConsumer(fileSet.getBasedir(), relativeRepositoryPath, fileSet); 050 051 stderr = new CommandLineUtils.StringStreamConsumer(); 052 053 exitCode = GitCommandLineUtils.execute(cl, consumer, stderr); 054 if (exitCode != 0) { 055 // git-status returns non-zero if nothing to do 056 if (logger.isInfoEnabled()) { 057 logger.info("nothing added to commit but untracked files present (use \"git add\" to track)"); 058 } 059 } 060 061 return new StatusScmResult(cl.toString(), consumer.getChangedFiles()); 062 } 063 064 // ---------------------------------------------------------------------- 065 // 066 // ---------------------------------------------------------------------- 067 068 /** 069 * Get the dir relative to the repository root. 070 * 071 * @param logger the caller command logger. 072 * @param fileSet in which subdir to execute. 073 * @return the relative URI. 074 * @throws ScmException if execute() fails. 075 */ 076 public static URI getRelativeCWD(Logger logger, ScmFileSet fileSet) throws ScmException { 077 Commandline clRevparse = createRevparseShowPrefix(fileSet); 078 079 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer(); 080 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 081 082 URI relativeRepositoryPath = null; 083 084 int exitCode = GitCommandLineUtils.execute(clRevparse, stdout, stderr); 085 if (exitCode != 0) { 086 // git-status returns non-zero if nothing to do 087 if (logger.isInfoEnabled()) { 088 logger.info("Could not resolve prefix"); 089 } 090 } else { 091 relativeRepositoryPath = 092 GitStatusConsumer.uriFromPath(stdout.getOutput().trim()); 093 } 094 return relativeRepositoryPath; 095 } 096 097 public static Commandline createCommandLine(GitScmProviderRepository repository, ScmFileSet fileSet) { 098 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "status"); 099 cl.addArguments(new String[] {"--porcelain", "."}); 100 return cl; 101 } 102 103 public static Commandline createRevparseShowPrefix(ScmFileSet fileSet) { 104 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "rev-parse"); 105 cl.addArguments(new String[] {"--show-prefix"}); 106 return cl; 107 } 108}