View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
3    * Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
4    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5    * and other copyright owners as documented in the project's IP log.
6    *
7    * This program and the accompanying materials are made available
8    * under the terms of the Eclipse Distribution License v1.0 which
9    * accompanies this distribution, is reproduced below, and is
10   * available at http://www.eclipse.org/org/documents/edl-v10.php
11   *
12   * All rights reserved.
13   *
14   * Redistribution and use in source and binary forms, with or
15   * without modification, are permitted provided that the following
16   * conditions are met:
17   *
18   * - Redistributions of source code must retain the above copyright
19   *   notice, this list of conditions and the following disclaimer.
20   *
21   * - Redistributions in binary form must reproduce the above
22   *   copyright notice, this list of conditions and the following
23   *   disclaimer in the documentation and/or other materials provided
24   *   with the distribution.
25   *
26   * - Neither the name of the Eclipse Foundation, Inc. nor the
27   *   names of its contributors may be used to endorse or promote
28   *   products derived from this software without specific prior
29   *   written permission.
30   *
31   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44   */
45  
46  package org.eclipse.jgit.pgm;
47  
48  import java.io.IOException;
49  import java.text.MessageFormat;
50  import java.util.List;
51  
52  import org.eclipse.jgit.api.FetchCommand;
53  import org.eclipse.jgit.api.Git;
54  import org.eclipse.jgit.lib.Constants;
55  import org.eclipse.jgit.lib.SubmoduleConfig.FetchRecurseSubmodulesMode;
56  import org.eclipse.jgit.lib.TextProgressMonitor;
57  import org.eclipse.jgit.pgm.internal.CLIText;
58  import org.eclipse.jgit.transport.FetchResult;
59  import org.eclipse.jgit.transport.RefSpec;
60  import org.eclipse.jgit.transport.TagOpt;
61  import org.kohsuke.args4j.Argument;
62  import org.kohsuke.args4j.Option;
63  
64  @Command(common = true, usage = "usage_updateRemoteRefsFromAnotherRepository")
65  class Fetch extends AbstractFetchCommand implements FetchCommand.Callback {
66  	@Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
67  	int timeout = -1;
68  
69  	@Option(name = "--fsck", usage = "usage_performFsckStyleChecksOnReceive")
70  	private Boolean fsck;
71  
72  	@Option(name = "--no-fsck")
73  	void nofsck(@SuppressWarnings("unused") final boolean ignored) {
74  		fsck = Boolean.FALSE;
75  	}
76  
77  	@Option(name = "--prune", usage = "usage_pruneStaleTrackingRefs")
78  	private Boolean prune;
79  
80  	@Option(name = "--dry-run")
81  	private boolean dryRun;
82  
83  	@Option(name = "--thin", usage = "usage_fetchThinPack")
84  	private Boolean thin;
85  
86  	@Option(name = "--no-thin")
87  	void nothin(@SuppressWarnings("unused") final boolean ignored) {
88  		thin = Boolean.FALSE;
89  	}
90  
91  	@Option(name = "--quiet", usage = "usage_quiet")
92  	private Boolean quiet;
93  
94  	@Option(name = "--tags", usage="usage_tags", aliases = { "-t" })
95  	private Boolean tags;
96  
97  	@Option(name = "--no-tags", usage = "usage_notags", aliases = { "-n" })
98  	void notags(@SuppressWarnings("unused")
99  	final boolean ignored) {
100 		tags = Boolean.FALSE;
101 	}
102 
103 	@Option(name = "--force", usage = "usage_forcedFetch", aliases = { "-f" })
104 	private Boolean force;
105 
106 	private FetchRecurseSubmodulesMode recurseSubmodules;
107 
108 	@Option(name = "--recurse-submodules", usage = "usage_recurseSubmodules")
109 	void recurseSubmodules(String mode) {
110 		if (mode == null || mode.isEmpty()) {
111 			recurseSubmodules = FetchRecurseSubmodulesMode.YES;
112 		} else {
113 			for (FetchRecurseSubmodulesMode m : FetchRecurseSubmodulesMode
114 					.values()) {
115 				if (m.matchConfigValue(mode)) {
116 					recurseSubmodules = m;
117 					return;
118 				}
119 			}
120 			throw die(MessageFormat
121 					.format(CLIText.get().invalidRecurseSubmodulesMode, mode));
122 		}
123 	}
124 
125 	@Option(name = "--no-recurse-submodules", usage = "usage_noRecurseSubmodules")
126 	void noRecurseSubmodules(@SuppressWarnings("unused")
127 	final boolean ignored) {
128 		recurseSubmodules = FetchRecurseSubmodulesMode.NO;
129 	}
130 
131 	@Argument(index = 0, metaVar = "metaVar_uriish")
132 	private String remote = Constants.DEFAULT_REMOTE_NAME;
133 
134 	@Argument(index = 1, metaVar = "metaVar_refspec")
135 	private List<RefSpec> toget;
136 
137 	/** {@inheritDoc} */
138 	@Override
139 	protected void run() throws Exception {
140 		try (Git git = new Git(db)) {
141 			FetchCommand fetch = git.fetch();
142 			if (fsck != null)
143 				fetch.setCheckFetchedObjects(fsck.booleanValue());
144 			if (prune != null)
145 				fetch.setRemoveDeletedRefs(prune.booleanValue());
146 			if (toget != null)
147 				fetch.setRefSpecs(toget);
148 			if (tags != null) {
149 				fetch.setTagOpt(tags.booleanValue() ? TagOpt.FETCH_TAGS
150 						: TagOpt.NO_TAGS);
151 			}
152 			if (0 <= timeout)
153 				fetch.setTimeout(timeout);
154 			fetch.setDryRun(dryRun);
155 			fetch.setRemote(remote);
156 			if (thin != null)
157 				fetch.setThin(thin.booleanValue());
158 			if (quiet == null || !quiet.booleanValue())
159 				fetch.setProgressMonitor(new TextProgressMonitor(errw));
160 			fetch.setRecurseSubmodules(recurseSubmodules).setCallback(this);
161 			if (force != null) {
162 				fetch.setForceUpdate(force.booleanValue());
163 			}
164 
165 			FetchResult result = fetch.call();
166 			if (result.getTrackingRefUpdates().isEmpty()
167 					&& result.submoduleResults().isEmpty())
168 				return;
169 
170 			showFetchResult(result);
171 		}
172 	}
173 
174 	/** {@inheritDoc} */
175 	@Override
176 	public void fetchingSubmodule(String name) {
177 		try {
178 			outw.println(MessageFormat.format(CLIText.get().fetchingSubmodule,
179 					name));
180 			outw.flush();
181 		} catch (IOException e) {
182 			// ignore
183 		}
184 	}
185 }