View Javadoc
1   /*
2    * Copyright (C) 2011, Chris Aniszczyk <caniszczyk@gmail.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.pgm;
12  
13  import java.util.ArrayList;
14  import java.util.List;
15  
16  import org.eclipse.jgit.api.Git;
17  import org.eclipse.jgit.api.ResetCommand;
18  import org.eclipse.jgit.api.ResetCommand.ResetType;
19  import org.eclipse.jgit.api.errors.GitAPIException;
20  import org.eclipse.jgit.pgm.internal.CLIText;
21  import org.kohsuke.args4j.Argument;
22  import org.kohsuke.args4j.Option;
23  import org.kohsuke.args4j.spi.RestOfArgumentsHandler;
24  
25  @Command(common = true, usage = "usage_reset")
26  class Reset extends TextBuiltin {
27  
28  	@Option(name = "--soft", usage = "usage_resetSoft")
29  	private boolean soft = false;
30  
31  	@Option(name = "--mixed", usage = "usage_resetMixed")
32  	private boolean mixed = false;
33  
34  	@Option(name = "--hard", usage = "usage_resetHard")
35  	private boolean hard = false;
36  
37  	@Argument(required = false, index = 0, metaVar = "metaVar_commitish", usage = "usage_resetReference")
38  	private String commit;
39  
40  	@Argument(required = false, index = 1, metaVar = "metaVar_paths")
41  	@Option(name = "--", metaVar = "metaVar_paths", handler = RestOfArgumentsHandler.class)
42  	private List<String> paths = new ArrayList<>();
43  
44  	/** {@inheritDoc} */
45  	@Override
46  	protected void run() {
47  		try (Gitit.html#Git">Git git = new Git(db)) {
48  			ResetCommand command = git.reset();
49  			command.setRef(commit);
50  			if (!paths.isEmpty()) {
51  				for (String path : paths) {
52  					command.addPath(path);
53  				}
54  			} else {
55  				ResetType mode = null;
56  				if (soft) {
57  					mode = selectMode(mode, ResetType.SOFT);
58  				}
59  				if (mixed) {
60  					mode = selectMode(mode, ResetType.MIXED);
61  				}
62  				if (hard) {
63  					mode = selectMode(mode, ResetType.HARD);
64  				}
65  				if (mode == null) {
66  					throw die(CLIText.get().resetNoMode);
67  				}
68  				command.setMode(mode);
69  			}
70  			command.call();
71  		} catch (GitAPIException e) {
72  			throw die(e.getMessage(), e);
73  		}
74  	}
75  
76  	private static ResetType selectMode(ResetType mode, ResetType want) {
77  		if (mode != null)
78  			throw die("reset modes are mutually exclusive, select one"); //$NON-NLS-1$
79  		return want;
80  	}
81  }