/* Copyright 2012 the original author or authors.
 *
 * 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.ajoberstar.gradle.git.tasks;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.ajoberstar.gradle.util.ObjectUtil;
import org.eclipse.jgit.api.CleanCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.gradle.api.GradleException;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;

/**
 * Task to clean the working tree of a local Git repository.
 * 
 * @since 0.2.3
 * @author Alex Lixandru
 */
public class GitClean extends GitBase {
	private Set<Object> paths = null;
	
	/**
	 * Reset the changes as configured.
	 */
	@TaskAction
	public void reset() {
		final CleanCommand cmd = getGit().clean();
		
		cmd.setPaths(getPaths());
		
		try {
			cmd.call();
		} catch (GitAPIException e) {
			throw new GradleException("Problem with clean.", e);
		}		
		//TODO add progress monitor to log progress to Gradle status bar
	}

	/**
	 * Gets the paths to clean.
	 * @return the paths to clean
	 */
	@Input
	public Set<String> getPaths() {
		if (paths == null) {
			return Collections.emptySet();
		}
		Set<String> cleanPaths = new HashSet<String>();
		for (Object path : paths) {
			cleanPaths.add(ObjectUtil.unpackString(path));
		}
		return cleanPaths;
	}
	
	/**
	 * Adds paths to be cleaned. 
	 * If paths are set, only these paths are affected by the cleaning.
	 * 
	 * @param cleanPaths the paths to be cleaned
	 */
	public void paths(Object... cleanPaths) {
		if (paths == null) {
			this.paths = new HashSet<Object>();
		}
		Collections.addAll(paths, cleanPaths);
	}
	
	/**
	 * Sets the paths for the clean command.
	 * If paths are set, only these paths are affected by the cleaning.
	 * 
	 * @param cleanPaths the paths to be cleaned
	 */
	@SuppressWarnings("unchecked")
	public void setPaths(Set<? extends Object> cleanPaths) {
		this.paths = (Set<Object>) cleanPaths;
	}
	
}

@@ -1253,16 +1253,128 @@
 gured.%0D%0A
+%09 * If %7B@code paths%7D is set, only the specified%0D%0A%09 * paths will be cleaned.  Otherwise all paths%0D%0A%09 * will be.%0D%0A
 %09 */%0D%0A%09@
@@ -2078,84 +2078,8 @@
 . %0D%0A
-%09 * If paths are set, only these paths are affected by the cleaning.%0D%0A%09 * %0D%0A
 %09 * 
@@ -2282,25 +2282,24 @@
 aths);%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09/**%0D%0A%09 * 
@@ -2298,19 +2298,63 @@
 **%0D%0A%09 * 
-Set
+Adds paths to be cleaned.%0D%0A%09 * @param cleanPath
 s the pa
@@ -2361,106 +2361,280 @@
 ths 
-for th
+to b
 e clean
- command.%0D%0A%09 * If paths are set, only these paths are affected by the cleaning.%0D%0A%09 * 
+ed%0D%0A%09 */%0D%0A%09public void paths(Iterable%3C? extends Object%3E cleanPaths) %7B%0D%0A%09%09if (paths == null) %7B%0D%0A%09%09%09this.paths = new HashSet%3CObject%3E();%0D%0A%09%09%7D%0D%0A%09%09for (Object path : cleanPaths) %7B%0D%0A%09%09%09this.paths.add(path);%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A%09/**%0D%0A%09 * Sets the paths for the clean command.
 %0D%0A%09 
@@ -2688,41 +2688,8 @@
 */%0D%0A
-%09@SuppressWarnings(%22unchecked%22)%0D%0A
 %09pub
@@ -2710,26 +2710,16 @@
 ths(Set%3C
-? extends 
 Object%3E 
@@ -2751,22 +2751,8 @@
 hs =
- (Set%3CObject%3E)
  cle
@@ -2765,14 +2765,11 @@
 s;%0D%0A%09%7D%0D%0A
-%09%0D%0A
 %7D%0D%0A

Add paths(Iterable<? extends Object>) to GitClean.