001package org.apache.maven.scm.provider.git.gitexe.command.status; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.net.URI; 023 024import org.apache.maven.scm.ScmException; 025import org.apache.maven.scm.ScmFileSet; 026import org.apache.maven.scm.command.AbstractCommand; 027import org.apache.maven.scm.command.status.AbstractStatusCommand; 028import org.apache.maven.scm.command.status.StatusScmResult; 029import org.apache.maven.scm.provider.ScmProviderRepository; 030import org.apache.maven.scm.provider.git.command.GitCommand; 031import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils; 032import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository; 033import org.codehaus.plexus.util.cli.CommandLineUtils; 034import org.codehaus.plexus.util.cli.Commandline; 035 036/** 037 * @author <a href="mailto:brett@apache.org">Brett Porter</a> 038 * 039 */ 040public class GitStatusCommand 041 extends AbstractStatusCommand 042 implements GitCommand 043{ 044 /** {@inheritDoc} */ 045 protected StatusScmResult executeStatusCommand( ScmProviderRepository repo, ScmFileSet fileSet ) 046 throws ScmException 047 { 048 int exitCode; 049 CommandLineUtils.StringStreamConsumer stderr; 050 051 URI relativeRepositoryPath = getRelativeCWD( this, fileSet ); 052 053 Commandline cl = createCommandLine( (GitScmProviderRepository) repo, fileSet ); 054 055 GitStatusConsumer consumer = new GitStatusConsumer( getLogger(), fileSet.getBasedir(), relativeRepositoryPath ); 056 057 stderr = new CommandLineUtils.StringStreamConsumer(); 058 059 exitCode = GitCommandLineUtils.execute( cl, consumer, stderr, getLogger() ); 060 if ( exitCode != 0 ) 061 { 062 // git-status returns non-zero if nothing to do 063 if ( getLogger().isInfoEnabled() ) 064 { 065 getLogger().info( "nothing added to commit but untracked files present (use \"git add\" to track)" ); 066 } 067 } 068 069 return new StatusScmResult( cl.toString(), consumer.getChangedFiles() ); 070 } 071 072 // ---------------------------------------------------------------------- 073 // 074 // ---------------------------------------------------------------------- 075 076 /** 077 * Get the dir relative to the repository root. 078 * 079 * @param caller the caller command. 080 * @param fileSet in which subdir to execute. 081 * @return the relative URI. 082 * @throws ScmException if execute() fails. 083 */ 084 public static URI getRelativeCWD( AbstractCommand caller, ScmFileSet fileSet ) 085 throws ScmException 086 { 087 Commandline clRevparse = createRevparseShowPrefix( fileSet ); 088 089 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer(); 090 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 091 092 URI relativeRepositoryPath = null; 093 094 int exitCode = GitCommandLineUtils.execute( clRevparse, stdout, stderr, caller.getLogger() ); 095 if ( exitCode != 0 ) 096 { 097 // git-status returns non-zero if nothing to do 098 if ( caller.getLogger().isInfoEnabled() ) 099 { 100 caller.getLogger().info( "Could not resolve prefix" ); 101 } 102 } 103 else 104 { 105 relativeRepositoryPath = GitStatusConsumer.uriFromPath( stdout.getOutput().trim() ); 106 } 107 return relativeRepositoryPath; 108 } 109 110 public static Commandline createCommandLine( GitScmProviderRepository repository, ScmFileSet fileSet ) 111 { 112 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "status" ); 113 cl.addArguments( new String[] { "--porcelain", "." } ); 114 return cl; 115 } 116 117 public static Commandline createRevparseShowPrefix( ScmFileSet fileSet ) 118 { 119 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "rev-parse" ); 120 cl.addArguments( new String[] { "--show-prefix" } ); 121 return cl; 122 } 123}