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
.spearce
.jgit
.lib
.RefUpdate
;
44 import org
.spearce
.jgit
.lib
.TextProgressMonitor
;
45 import org
.spearce
.jgit
.transport
.FetchResult
;
46 import org
.spearce
.jgit
.transport
.RefSpec
;
47 import org
.spearce
.jgit
.transport
.TrackingRefUpdate
;
48 import org
.spearce
.jgit
.transport
.Transport
;
50 class Fetch
extends TextBuiltin
{
51 @Argument(index
= 0, metaVar
= "uri-ish")
52 private String remote
= "origin";
54 @Argument(index
= 1, metaVar
= "refspec")
55 private List
<RefSpec
> toget
;
58 protected void run() throws Exception
{
59 final Transport tn
= Transport
.open(db
, remote
);
62 r
= tn
.fetch(new TextProgressMonitor(), toget
);
63 if (r
.getTrackingRefUpdates().isEmpty())
70 out
.print(tn
.getURI());
72 for (final TrackingRefUpdate u
: r
.getTrackingRefUpdates()) {
73 final char type
= shortTypeOf(u
.getResult());
74 final String longType
= longTypeOf(u
);
75 final String src
= abbreviateRef(u
.getRemoteName(), false);
76 final String dst
= abbreviateRef(u
.getLocalName(), true);
78 out
.format(" %c %-17s %-10s -> %s", type
, longType
, src
, dst
);
83 private static String
longTypeOf(final TrackingRefUpdate u
) {
84 final RefUpdate
.Result r
= u
.getResult();
85 if (r
== RefUpdate
.Result
.LOCK_FAILURE
)
88 if (r
== RefUpdate
.Result
.IO_FAILURE
)
91 if (r
== RefUpdate
.Result
.NEW
) {
92 if (u
.getRemoteName().startsWith(REFS_HEADS
))
93 return "[new branch]";
94 else if (u
.getLocalName().startsWith(REFS_TAGS
))
99 if (r
== RefUpdate
.Result
.FORCED
) {
100 final String aOld
= abbreviateObject(u
.getOldObjectId());
101 final String aNew
= abbreviateObject(u
.getNewObjectId());
102 return aOld
+ "..." + aNew
;
105 if (r
== RefUpdate
.Result
.FAST_FORWARD
) {
106 final String aOld
= abbreviateObject(u
.getOldObjectId());
107 final String aNew
= abbreviateObject(u
.getNewObjectId());
108 return aOld
+ ".." + aNew
;
111 if (r
== RefUpdate
.Result
.REJECTED
)
113 if (r
== RefUpdate
.Result
.NO_CHANGE
)
114 return "[up to date]";
115 return "[" + r
.name() + "]";
118 private static char shortTypeOf(final RefUpdate
.Result r
) {
119 if (r
== RefUpdate
.Result
.LOCK_FAILURE
)
121 if (r
== RefUpdate
.Result
.IO_FAILURE
)
123 if (r
== RefUpdate
.Result
.NEW
)
125 if (r
== RefUpdate
.Result
.FORCED
)
127 if (r
== RefUpdate
.Result
.FAST_FORWARD
)
129 if (r
== RefUpdate
.Result
.REJECTED
)
131 if (r
== RefUpdate
.Result
.NO_CHANGE
)