2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
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
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")
60 @Option(name
= "--no-fsck")
61 void nofsck(final boolean ignored
) {
65 @Option(name
= "--thin", usage
= "fetch thin pack")
68 @Option(name
= "--no-thin")
69 void nothin(final boolean ignored
) {
73 @Argument(index
= 0, metaVar
= "uri-ish")
74 private String remote
= "origin";
76 @Argument(index
= 1, metaVar
= "refspec")
77 private List
<RefSpec
> toget
;
80 protected void run() throws Exception
{
81 final Transport tn
= Transport
.open(db
, remote
);
83 tn
.setCheckFetchedObjects(fsck
.booleanValue());
85 tn
.setFetchThin(thin
.booleanValue());
88 r
= tn
.fetch(new TextProgressMonitor(), toget
);
89 if (r
.getTrackingRefUpdates().isEmpty())
95 boolean shownURI
= false;
96 for (final TrackingRefUpdate u
: r
.getTrackingRefUpdates()) {
97 if (!verbose
&& u
.getResult() == RefUpdate
.Result
.NO_CHANGE
)
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);
107 out
.print(tn
.getURI());
112 out
.format(" %c %-17s %-10s -> %s", type
, longType
, src
, dst
);
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
))
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
)
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
)
155 if (r
== RefUpdate
.Result
.IO_FAILURE
)
157 if (r
== RefUpdate
.Result
.NEW
)
159 if (r
== RefUpdate
.Result
.FORCED
)
161 if (r
== RefUpdate
.Result
.FAST_FORWARD
)
163 if (r
== RefUpdate
.Result
.REJECTED
)
165 if (r
== RefUpdate
.Result
.NO_CHANGE
)