View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.pgm.opt;
13  
14  import java.io.File;
15  import java.io.IOException;
16  import java.text.MessageFormat;
17  
18  import org.eclipse.jgit.dircache.DirCache;
19  import org.eclipse.jgit.dircache.DirCacheIterator;
20  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
21  import org.eclipse.jgit.errors.MissingObjectException;
22  import org.eclipse.jgit.lib.ObjectId;
23  import org.eclipse.jgit.lib.ObjectReader;
24  import org.eclipse.jgit.pgm.internal.CLIText;
25  import org.eclipse.jgit.treewalk.AbstractTreeIterator;
26  import org.eclipse.jgit.treewalk.CanonicalTreeParser;
27  import org.eclipse.jgit.treewalk.FileTreeIterator;
28  import org.eclipse.jgit.treewalk.WorkingTreeOptions;
29  import org.eclipse.jgit.util.FS;
30  import org.kohsuke.args4j.CmdLineException;
31  import org.kohsuke.args4j.CmdLineParser;
32  import org.kohsuke.args4j.OptionDef;
33  import org.kohsuke.args4j.spi.OptionHandler;
34  import org.kohsuke.args4j.spi.Parameters;
35  import org.kohsuke.args4j.spi.Setter;
36  
37  /**
38   * Custom argument handler
39   * {@link org.eclipse.jgit.treewalk.AbstractTreeIterator} from string values.
40   * <p>
41   * Assumes the parser has been initialized with a Repository.
42   */
43  public class AbstractTreeIteratorHandler extends
44  		OptionHandler<AbstractTreeIterator> {
45  	private final org.eclipse.jgit.pgm.opt.CmdLineParser clp;
46  
47  	/**
48  	 * Create a new handler for the command name.
49  	 * <p>
50  	 * This constructor is used only by args4j.
51  	 *
52  	 * @param parser
53  	 *            a {@link org.kohsuke.args4j.CmdLineParser} object.
54  	 * @param option
55  	 *            a {@link org.kohsuke.args4j.OptionDef} object.
56  	 * @param setter
57  	 *            a {@link org.kohsuke.args4j.spi.Setter} object.
58  	 */
59  	public AbstractTreeIteratorHandler(final CmdLineParser parser,
60  			final OptionDef option,
61  			final Setter<? super AbstractTreeIterator> setter) {
62  		super(parser, option, setter);
63  		clp = (org.eclipse.jgit.pgm.opt.CmdLineParser) parser;
64  	}
65  
66  	/** {@inheritDoc} */
67  	@Override
68  	public int parseArguments(Parameters params) throws CmdLineException {
69  		final String name = params.getParameter(0);
70  
71  		if (new File(name).isDirectory()) {
72  			setter.addValue(new FileTreeIterator(
73  				new File(name),
74  				FS.DETECTED,
75  				clp.getRepository().getConfig().get(WorkingTreeOptions.KEY)));
76  			return 1;
77  		}
78  
79  		if (new File(name).isFile()) {
80  			final DirCache dirc;
81  			try {
82  				dirc = DirCache.read(new File(name), FS.DETECTED);
83  			} catch (IOException e) {
84  				throw new CmdLineException(clp, MessageFormat.format(CLIText.get().notAnIndexFile, name), e);
85  			}
86  			setter.addValue(new DirCacheIterator(dirc));
87  			return 1;
88  		}
89  
90  		final ObjectId id;
91  		try {
92  			id = clp.getRepository().resolve(name);
93  		} catch (IOException e) {
94  			throw new CmdLineException(clp, CLIText.format(e.getMessage()));
95  		}
96  		if (id == null)
97  			throw new CmdLineException(clp,
98  					CLIText.format(CLIText.get().notATree), name);
99  
100 		final CanonicalTreeParser p = new CanonicalTreeParser();
101 		try (ObjectReader curs = clp.getRepository().newObjectReader()) {
102 			p.reset(curs, clp.getRevWalk().parseTree(id));
103 		} catch (MissingObjectException | IncorrectObjectTypeException e) {
104 			CmdLineException cle = new CmdLineException(clp,
105 					CLIText.format(CLIText.get().notATree), name);
106 			cle.initCause(e);
107 			throw cle;
108 		} catch (IOException e) {
109 			throw new CmdLineException(clp,
110 					CLIText.format(CLIText.get().cannotReadBecause), name,
111 					e.getMessage());
112 		}
113 
114 		setter.addValue(p);
115 		return 1;
116 	}
117 
118 	/** {@inheritDoc} */
119 	@Override
120 	public String getDefaultMetaVariable() {
121 		return CLIText.get().metaVar_treeish;
122 	}
123 }