Implement "jgit upload-pack" to support fetching from jgit
[egit/qmx.git] / org.spearce.jgit / src / org / spearce / jgit / transport / SideBandProgressMonitor.java
blob5cda7c509a4df7d2066271921177a193d6b74611
1 /*
2 * Copyright (C) 2008, Google Inc.
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.transport;
40 import java.io.BufferedOutputStream;
41 import java.io.OutputStreamWriter;
42 import java.io.PrintWriter;
44 import org.spearce.jgit.lib.Constants;
45 import org.spearce.jgit.lib.ProgressMonitor;
47 /** Write progress messages out to the sideband channel. */
48 class SideBandProgressMonitor implements ProgressMonitor {
49 private PrintWriter out;
51 private boolean output;
53 private long taskBeganAt;
55 private long lastOutput;
57 private String msg;
59 private int lastWorked;
61 private int totalWork;
63 SideBandProgressMonitor(final PacketLineOut pckOut) {
64 final int bufsz = SideBandOutputStream.SMALL_BUF
65 - SideBandOutputStream.HDR_SIZE;
66 out = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(
67 new SideBandOutputStream(SideBandOutputStream.CH_PROGRESS,
68 pckOut), bufsz), Constants.CHARSET));
71 public void start(final int totalTasks) {
72 // Ignore the number of tasks.
73 taskBeganAt = System.currentTimeMillis();
74 lastOutput = taskBeganAt;
77 public void beginTask(final String title, final int total) {
78 endTask();
79 msg = title;
80 lastWorked = 0;
81 totalWork = total;
84 public void update(final int completed) {
85 if (msg == null)
86 return;
88 final int cmp = lastWorked + completed;
89 final long now = System.currentTimeMillis();
90 if (!output && now - taskBeganAt < 500)
91 return;
92 if (totalWork == UNKNOWN) {
93 if (now - lastOutput >= 500) {
94 display(cmp, null);
95 lastOutput = now;
97 } else {
98 if ((cmp * 100 / totalWork) != (lastWorked * 100) / totalWork
99 || now - lastOutput >= 500) {
100 display(cmp, null);
101 lastOutput = now;
104 lastWorked = cmp;
105 output = true;
108 private void display(final int cmp, final String eol) {
109 final StringBuilder m = new StringBuilder();
110 m.append(msg);
111 m.append(": ");
113 if (totalWork == UNKNOWN) {
114 m.append(cmp);
115 } else {
116 final int pcnt = (cmp * 100 / totalWork);
117 if (pcnt < 100)
118 m.append(' ');
119 if (pcnt < 10)
120 m.append(' ');
121 m.append(pcnt);
122 m.append("% (");
123 m.append(cmp);
124 m.append("/");
125 m.append(totalWork);
126 m.append(")");
128 if (eol != null)
129 m.append(eol);
130 else
131 m.append(" \r");
132 out.print(m);
133 out.flush();
136 public boolean isCancelled() {
137 return false;
140 public void endTask() {
141 if (output) {
142 if (totalWork == UNKNOWN)
143 display(lastWorked, ", done\n");
144 else
145 display(totalWork, "\n");
147 output = false;
148 msg = null;