Display a progress meter during merge-recursive.
Because large merges on slow systems can take up to a minute to execute we should try to keep the user entertained with a progress meter to let them know how far we have progressed through the current merge. The progress meter considers each entry in the in-memory index to be a unit, which means a single recursive merge will double the number of units in the progress meter. Files which are unmerged after the 3-way tree merge are also considered a unit within the progress meter. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Shawn O. Pearce committed
Jan 14, 2007 at 00:28 UTC
3f6ee2d15ab4be8690c17c0af0338b8495f6f706
1 file changed
+66
-7
merge-recursive.c
+66
-7
@@ -79,6 +79,11 @@ static struct path_list current_directory_set = {NULL, 0, 0, 1};
79
static int call_depth = 0;
80
static int verbosity = 2;
81
static int buffer_output = 1;
82
+static int do_progress = 1;
83
+static unsigned last_percent;
84
+static unsigned merged_cnt;
85
+static unsigned total_cnt;
86
+static volatile sig_atomic_t progress_update;
87
static struct output_buffer *output_list, *output_end;
88
89
static int show (int v)
@@ -153,6 +158,39 @@ static void output_commit_title(struct commit *commit)
158
}
159
}
160
161
+static void progress_interval(int signum)
162
+{
163
+ progress_update = 1;
164
+}
165
+
166
+static void setup_progress_signal(void)
167
+{
168
+ struct sigaction sa;
169
+ struct itimerval v;
170
+
171
+ memset(&sa, 0, sizeof(sa));
172
+ sa.sa_handler = progress_interval;
173
+ sigemptyset(&sa.sa_mask);
174
+ sa.sa_flags = SA_RESTART;
175
+ sigaction(SIGALRM, &sa, NULL);
176
+
177
+ v.it_interval.tv_sec = 1;
178
+ v.it_interval.tv_usec = 0;
179
+ v.it_value = v.it_interval;
180
+ setitimer(ITIMER_REAL, &v, NULL);
181
+}
182
+
183
+static void display_progress()
184
+{
185
+ unsigned percent = total_cnt ? merged_cnt * 100 / total_cnt : 0;
186
+ if (progress_update || percent != last_percent) {
187
+ fprintf(stderr, "%4u%% (%u/%u) done\r",
188
+ percent, merged_cnt, total_cnt);
189
+ progress_update = 0;
190
+ last_percent = percent;
191
+ }
192
+}
193
+
194
static struct cache_entry *make_cache_entry(unsigned int mode,
195
const unsigned char *sha1, const char *path, int stage, int refresh)
196
{
@@ -315,11 +353,14 @@ static struct path_list *get_unmerged(void)
353
int i;
354
355
unmerged->strdup_paths = 1;
356
+ total_cnt += active_nr;
357
319
- for (i = 0; i < active_nr; i++) {
358
+ for (i = 0; i < active_nr; i++, merged_cnt++) {
359
struct path_list_item *item;
360
struct stage_data *e;
361
struct cache_entry *ce = active_cache[i];
362
+ if (do_progress)
363
+ display_progress();
364
if (!ce_stage(ce))
365
continue;
366
@@ -1096,13 +1137,15 @@ static int merge_trees(struct tree *head,
1137
re_merge = get_renames(merge, common, head, merge, entries);
1138
clean = process_renames(re_head, re_merge,
1139
branch1, branch2);
1099
- for (i = 0; i < entries->nr; i++) {
1140
+ total_cnt += entries->nr;
1141
+ for (i = 0; i < entries->nr; i++, merged_cnt++) {
1142
const char *path = entries->items[i].path;
1143
struct stage_data *e = entries->items[i].util;
1102
- if (e->processed)
1103
- continue;
1104
- if (!process_entry(path, e, branch1, branch2))
1144
+ if (!e->processed
1145
+ && !process_entry(path, e, branch1, branch2))
1146
clean = 0;
1147
+ if (do_progress)
1148
+ display_progress();
1149
}
1150
1151
path_list_clear(re_merge, 0);
@@ -1210,6 +1253,15 @@ static int merge(struct commit *h1,
1253
commit_list_insert(h1, &(*result)->parents);
1254
commit_list_insert(h2, &(*result)->parents->next);
1255
}
1256
+ if (!call_depth && do_progress) {
1257
+ /* Make sure we end at 100% */
1258
+ if (!total_cnt)
1259
+ total_cnt = 1;
1260
+ merged_cnt = total_cnt;
1261
+ progress_update = 1;
1262
+ display_progress();
1263
+ fputc('\n', stderr);
1264
+ }
1265
flush_output();
1266
return clean;
1267
}
@@ -1279,6 +1331,12 @@ int main(int argc, char *argv[])
1331
}
1332
if (argc - i != 3) /* "--" "<head>" "<remote>" */
1333
die("Not handling anything other than two heads merge.");
1334
+ if (verbosity >= 5) {
1335
+ buffer_output = 0;
1336
+ do_progress = 0;
1337
+ }
1338
+ else
1339
+ do_progress = isatty(1);
1340
1341
branch1 = argv[++i];
1342
branch2 = argv[++i];
@@ -1288,8 +1346,9 @@ int main(int argc, char *argv[])
1346
1347
branch1 = better_branch_name(branch1);
1348
branch2 = better_branch_name(branch2);
1291
- if (verbosity >= 5)
1292
- buffer_output = 0;
1349
+
1350
+ if (do_progress)
1351
+ setup_progress_signal();
1352
if (show(3))
1353
printf("Merging %s with %s\n", branch1, branch2);
1354