Add args4j library for command line switch processing
[egit/graphgui.git] / org.spearce.jgit / src / org / spearce / jgit / pgm / Fetch.java
blob8a35025029087f40576010bd472527791df93575
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.ArrayList;
41 import java.util.List;
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 @Override
52 void execute(String[] args) throws Exception {
53 int argi = 0;
54 for (; argi < args.length; argi++) {
55 final String a = args[argi];
56 if ("--".equals(a)) {
57 argi++;
58 break;
59 } else
60 break;
62 if (args.length == 0)
63 args = new String[] { "origin" };
65 final Transport tn = Transport.open(db, args[argi++]);
66 final FetchResult r;
67 try {
68 final List<RefSpec> toget = new ArrayList<RefSpec>();
69 for (; argi < args.length; argi++)
70 toget.add(new RefSpec(args[argi]));
71 r = tn.fetch(new TextProgressMonitor(), toget);
72 if (r.getTrackingRefUpdates().isEmpty())
73 return;
74 } finally {
75 tn.close();
78 out.print("From ");
79 out.print(tn.getURI());
80 out.println();
81 for (final TrackingRefUpdate u : r.getTrackingRefUpdates()) {
82 final char type = shortTypeOf(u.getResult());
83 final String longType = longTypeOf(u);
84 final String src = abbreviateRef(u.getRemoteName(), false);
85 final String dst = abbreviateRef(u.getLocalName(), true);
87 out.format(" %c %-17s %-10s -> %s", type, longType, src, dst);
88 out.println();
92 private static String longTypeOf(final TrackingRefUpdate u) {
93 final RefUpdate.Result r = u.getResult();
94 if (r == RefUpdate.Result.LOCK_FAILURE)
95 return "[lock fail]";
97 if (r == RefUpdate.Result.IO_FAILURE)
98 return "[i/o error]";
100 if (r == RefUpdate.Result.NEW) {
101 if (u.getRemoteName().startsWith(REFS_HEADS))
102 return "[new branch]";
103 else if (u.getLocalName().startsWith(REFS_TAGS))
104 return "[new tag]";
105 return "[new]";
108 if (r == RefUpdate.Result.FORCED) {
109 final String aOld = abbreviateObject(u.getOldObjectId());
110 final String aNew = abbreviateObject(u.getNewObjectId());
111 return aOld + "..." + aNew;
114 if (r == RefUpdate.Result.FAST_FORWARD) {
115 final String aOld = abbreviateObject(u.getOldObjectId());
116 final String aNew = abbreviateObject(u.getNewObjectId());
117 return aOld + ".." + aNew;
120 if (r == RefUpdate.Result.REJECTED)
121 return "[rejected]";
122 if (r == RefUpdate.Result.NO_CHANGE)
123 return "[up to date]";
124 return "[" + r.name() + "]";
127 private static char shortTypeOf(final RefUpdate.Result r) {
128 if (r == RefUpdate.Result.LOCK_FAILURE)
129 return '!';
130 if (r == RefUpdate.Result.IO_FAILURE)
131 return '!';
132 if (r == RefUpdate.Result.NEW)
133 return '*';
134 if (r == RefUpdate.Result.FORCED)
135 return '+';
136 if (r == RefUpdate.Result.FAST_FORWARD)
137 return ' ';
138 if (r == RefUpdate.Result.REJECTED)
139 return '!';
140 if (r == RefUpdate.Result.NO_CHANGE)
141 return '=';
142 return ' ';