Add --[no-]thin and --[no-]fsck optiosn to fetch command line tool
[egit.git] / org.spearce.jgit.pgm / src / org / spearce / jgit / pgm / Fetch.java
blobad7e08f68b9bafa91b7f02541be54b73e1dc73f9
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.pgm;
40 import java.util.List;
42 import org.kohsuke.args4j.Argument;
43 import org.kohsuke.args4j.Option;
44 import org.spearce.jgit.lib.Constants;
45 import org.spearce.jgit.lib.RefUpdate;
46 import org.spearce.jgit.lib.TextProgressMonitor;
47 import org.spearce.jgit.transport.FetchResult;
48 import org.spearce.jgit.transport.RefSpec;
49 import org.spearce.jgit.transport.TrackingRefUpdate;
50 import org.spearce.jgit.transport.Transport;
52 @Command(common = true, usage = "Update remote refs from another repository")
53 class Fetch extends TextBuiltin {
54 @Option(name = "--verbose", aliases = { "-v" }, usage = "be more verbose")
55 private boolean verbose;
57 @Option(name = "--fsck", usage = "perform fsck style checks on receive")
58 private Boolean fsck;
60 @Option(name = "--no-fsck")
61 void nofsck(final boolean ignored) {
62 fsck = Boolean.FALSE;
65 @Option(name = "--thin", usage = "fetch thin pack")
66 private Boolean thin;
68 @Option(name = "--no-thin")
69 void nothin(final boolean ignored) {
70 thin = Boolean.FALSE;
73 @Argument(index = 0, metaVar = "uri-ish")
74 private String remote = "origin";
76 @Argument(index = 1, metaVar = "refspec")
77 private List<RefSpec> toget;
79 @Override
80 protected void run() throws Exception {
81 final Transport tn = Transport.open(db, remote);
82 if (fsck != null)
83 tn.setCheckFetchedObjects(fsck.booleanValue());
84 if (thin != null)
85 tn.setFetchThin(thin.booleanValue());
86 final FetchResult r;
87 try {
88 r = tn.fetch(new TextProgressMonitor(), toget);
89 if (r.getTrackingRefUpdates().isEmpty())
90 return;
91 } finally {
92 tn.close();
95 boolean shownURI = false;
96 for (final TrackingRefUpdate u : r.getTrackingRefUpdates()) {
97 if (!verbose && u.getResult() == RefUpdate.Result.NO_CHANGE)
98 continue;
100 final char type = shortTypeOf(u.getResult());
101 final String longType = longTypeOf(u);
102 final String src = abbreviateRef(u.getRemoteName(), false);
103 final String dst = abbreviateRef(u.getLocalName(), true);
105 if (!shownURI) {
106 out.print("From ");
107 out.print(tn.getURI());
108 out.println();
109 shownURI = true;
112 out.format(" %c %-17s %-10s -> %s", type, longType, src, dst);
113 out.println();
117 private String longTypeOf(final TrackingRefUpdate u) {
118 final RefUpdate.Result r = u.getResult();
119 if (r == RefUpdate.Result.LOCK_FAILURE)
120 return "[lock fail]";
122 if (r == RefUpdate.Result.IO_FAILURE)
123 return "[i/o error]";
125 if (r == RefUpdate.Result.NEW) {
126 if (u.getRemoteName().startsWith(Constants.R_HEADS))
127 return "[new branch]";
128 else if (u.getLocalName().startsWith(Constants.R_TAGS))
129 return "[new tag]";
130 return "[new]";
133 if (r == RefUpdate.Result.FORCED) {
134 final String aOld = u.getOldObjectId().abbreviate(db);
135 final String aNew = u.getNewObjectId().abbreviate(db);
136 return aOld + "..." + aNew;
139 if (r == RefUpdate.Result.FAST_FORWARD) {
140 final String aOld = u.getOldObjectId().abbreviate(db);
141 final String aNew = u.getNewObjectId().abbreviate(db);
142 return aOld + ".." + aNew;
145 if (r == RefUpdate.Result.REJECTED)
146 return "[rejected]";
147 if (r == RefUpdate.Result.NO_CHANGE)
148 return "[up to date]";
149 return "[" + r.name() + "]";
152 private static char shortTypeOf(final RefUpdate.Result r) {
153 if (r == RefUpdate.Result.LOCK_FAILURE)
154 return '!';
155 if (r == RefUpdate.Result.IO_FAILURE)
156 return '!';
157 if (r == RefUpdate.Result.NEW)
158 return '*';
159 if (r == RefUpdate.Result.FORCED)
160 return '+';
161 if (r == RefUpdate.Result.FAST_FORWARD)
162 return ' ';
163 if (r == RefUpdate.Result.REJECTED)
164 return '!';
165 if (r == RefUpdate.Result.NO_CHANGE)
166 return '=';
167 return ' ';