aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/MultiPackIndex.java
blob: 1844223cc9efaafbab1cd3efcd82f711f2782097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (C) 2025, Google LLC.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.pgm;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jgit.internal.storage.file.ObjectDirectory;
import org.eclipse.jgit.internal.storage.file.Pack;
import org.eclipse.jgit.internal.storage.file.PackFile;
import org.eclipse.jgit.internal.storage.file.PackIndex;
import org.eclipse.jgit.internal.storage.midx.MultiPackIndexPrettyPrinter;
import org.eclipse.jgit.internal.storage.midx.MultiPackIndexWriter;
import org.eclipse.jgit.internal.storage.pack.PackExt;
import org.eclipse.jgit.lib.NullProgressMonitor;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;

@Command(common = true, usage = "usage_MultiPackIndex")
@SuppressWarnings("nls")
class MultiPackIndex extends TextBuiltin {
	@Argument(index = 0, required = true, usage = "write, print")
	private String command;

	@Option(name = "--midx")
	private String midxPath;

	/** {@inheritDoc} */
	@Override
	protected void run() throws IOException {
		switch (command) {
		case "print":
			printMultiPackIndex();
			break;
		case "write":
			writeMultiPackIndex();
			break;
		default:
			outw.println("Unknown command " + command);
		}
	}

	private void printMultiPackIndex() {
		if (midxPath == null || midxPath.isEmpty()) {
			throw die("'print' requires the path of a multipack "
					+ "index file with --midx option.");
		}

		try (FileInputStream is = new FileInputStream(midxPath)) {
			PrintWriter pw = new PrintWriter(outw, true);
			MultiPackIndexPrettyPrinter.prettyPrint(is.readAllBytes(), pw);
		} catch (FileNotFoundException e) {
			throw die(true, e);
		} catch (IOException e) {
			throw die(true, e);
		}
	}

	private void writeMultiPackIndex() throws IOException {
		if (!(db.getObjectDatabase() instanceof ObjectDirectory)) {
			throw die("This repository object db doesn't have packs");
		}

		File midx;
		if (midxPath == null || midxPath.isEmpty()) {
			midx = new File(((ObjectDirectory) db.getObjectDatabase())
					.getPackDirectory(), "multi-pack-index");
		} else {
			midx = new File(midxPath);
		}

		errw.println("Writing " + midx.getAbsolutePath());

		ObjectDirectory odb = (ObjectDirectory) db.getObjectDatabase();

		Map<String, PackIndex> indexes = new HashMap<>();
		for (Pack pack : odb.getPacks()) {
			PackFile packFile = pack.getPackFile().create(PackExt.INDEX);
			try {
				indexes.put(packFile.getName(), pack.getIndex());
			} catch (IOException e) {
				throw die("Cannot open index in pack", e);
			}
		}

		MultiPackIndexWriter writer = new MultiPackIndexWriter();
		try (FileOutputStream out = new FileOutputStream(midxPath)) {
			writer.write(NullProgressMonitor.INSTANCE, out, indexes);
		} catch (IOException e) {
			throw die("Cannot write midx " + midxPath, e);
		}
	}
}