xref: /JGit/org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/InstallBuiltinLfsCommand.java (revision 5c5f7c6b146b24f2bd4afae1902df85ad6e57ea3)
1 /*
2  * Copyright (C) 2018, Markus Duft <markus.duft@ssi-schaefer.com> and others
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Distribution License v. 1.0 which is available at
6  * https://www.eclipse.org/org/documents/edl-v10.php.
7  *
8  * SPDX-License-Identifier: BSD-3-Clause
9  */
10 package org.eclipse.jgit.lfs;
11 
12 import java.io.IOException;
13 
14 import org.eclipse.jgit.api.errors.InvalidConfigurationException;
15 import org.eclipse.jgit.errors.ConfigInvalidException;
16 import org.eclipse.jgit.lib.ConfigConstants;
17 import org.eclipse.jgit.lib.Repository;
18 import org.eclipse.jgit.lib.StoredConfig;
19 import org.eclipse.jgit.util.FS;
20 import org.eclipse.jgit.util.LfsFactory.LfsInstallCommand;
21 import org.eclipse.jgit.util.SystemReader;
22 
23 /**
24  * Installs all required LFS properties for the current user, analogous to 'git
25  * lfs install', but defaulting to using JGit builtin hooks.
26  *
27  * @since 4.11
28  */
29 public class InstallBuiltinLfsCommand implements LfsInstallCommand {
30 
31 	private static final String[] ARGS_USER = new String[] { "lfs", "install" }; //$NON-NLS-1$//$NON-NLS-2$
32 
33 	private static final String[] ARGS_LOCAL = new String[] { "lfs", "install", //$NON-NLS-1$//$NON-NLS-2$
34 			"--local" }; //$NON-NLS-1$
35 
36 	private Repository repository;
37 
38 	/**
39 	 * {@inheritDoc}
40 	 *
41 	 * @throws IOException
42 	 *             if an I/O error occurs while accessing a git config or
43 	 *             executing {@code git lfs install} in an external process
44 	 * @throws InvalidConfigurationException
45 	 *             if a git configuration is invalid
46 	 * @throws InterruptedException
47 	 *             if the current thread is interrupted while waiting for the
48 	 *             {@code git lfs install} executed in an external process
49 	 */
50 	@Override
call()51 	public Void call() throws IOException, InvalidConfigurationException,
52 			InterruptedException {
53 		StoredConfig cfg = null;
54 		if (repository == null) {
55 			try {
56 				cfg = SystemReader.getInstance().getUserConfig();
57 			} catch (ConfigInvalidException e) {
58 				throw new InvalidConfigurationException(e.getMessage(), e);
59 			}
60 		} else {
61 			cfg = repository.getConfig();
62 		}
63 
64 		cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
65 				ConfigConstants.CONFIG_SECTION_LFS,
66 				ConfigConstants.CONFIG_KEY_USEJGITBUILTIN, true);
67 		cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
68 				ConfigConstants.CONFIG_SECTION_LFS,
69 				ConfigConstants.CONFIG_KEY_REQUIRED, true);
70 
71 		cfg.save();
72 
73 		// try to run git lfs install, we really don't care if it is present
74 		// and/or works here (yet).
75 		ProcessBuilder builder = FS.DETECTED.runInShell("git", //$NON-NLS-1$
76 				repository == null ? ARGS_USER : ARGS_LOCAL);
77 		if (repository != null) {
78 			builder.directory(repository.isBare() ? repository.getDirectory()
79 					: repository.getWorkTree());
80 		}
81 		FS.DETECTED.runProcess(builder, null, null, (String) null);
82 
83 		return null;
84 	}
85 
86 	/**
87 	 * Set the repository to install LFS for
88 	 *
89 	 * @param repo
90 	 *            the repository to install LFS into locally instead of the user
91 	 *            configuration
92 	 * @return this command
93 	 */
94 	@Override
setRepository(Repository repo)95 	public LfsInstallCommand setRepository(Repository repo) {
96 		this.repository = repo;
97 		return this;
98 	}
99 
100 }
101