1 /* 2 * Copyright (C) 2016, Christian Halstrick <christian.halstrick@sap.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 static org.eclipse.jgit.lib.Constants.OBJECTS; 13 14 import java.io.IOException; 15 import java.nio.file.Files; 16 import java.nio.file.Path; 17 18 import org.eclipse.jgit.lfs.lib.AnyLongObjectId; 19 import org.eclipse.jgit.lfs.lib.Constants; 20 import org.eclipse.jgit.lib.Repository; 21 22 /** 23 * Class which represents the lfs folder hierarchy inside a {@code .git} folder 24 * 25 * @since 4.6 26 */ 27 public class Lfs { 28 private Path root; 29 30 private Path objDir; 31 32 private Path tmpDir; 33 34 /** 35 * Constructor for Lfs. 36 * 37 * @param db 38 * the associated repo 39 * 40 * @since 4.11 41 */ Lfs(Repository db)42 public Lfs(Repository db) { 43 this.root = db.getDirectory().toPath().resolve(Constants.LFS); 44 } 45 46 /** 47 * Get the LFS root directory 48 * 49 * @return the path to the LFS directory 50 */ getLfsRoot()51 public Path getLfsRoot() { 52 return root; 53 } 54 55 /** 56 * Get the path to the temporary directory used by LFS. 57 * 58 * @return the path to the temporary directory used by LFS. Will be 59 * {@code <repo>/.git/lfs/tmp} 60 */ getLfsTmpDir()61 public Path getLfsTmpDir() { 62 if (tmpDir == null) { 63 tmpDir = root.resolve("tmp"); //$NON-NLS-1$ 64 } 65 return tmpDir; 66 } 67 68 /** 69 * Get the object directory used by LFS 70 * 71 * @return the path to the object directory used by LFS. Will be 72 * {@code <repo>/.git/lfs/objects} 73 */ getLfsObjDir()74 public Path getLfsObjDir() { 75 if (objDir == null) { 76 objDir = root.resolve(OBJECTS); 77 } 78 return objDir; 79 } 80 81 /** 82 * Get the media file which stores the original content 83 * 84 * @param id 85 * the id of the mediafile 86 * @return the file which stores the original content. Its path will look 87 * like 88 * {@code "<repo>/.git/lfs/objects/<firstTwoLettersOfID>/<remainingLettersOfID>"} 89 */ getMediaFile(AnyLongObjectId id)90 public Path getMediaFile(AnyLongObjectId id) { 91 String idStr = id.name(); 92 return getLfsObjDir().resolve(idStr.substring(0, 2)) 93 .resolve(idStr.substring(2, 4)).resolve(idStr); 94 } 95 96 /** 97 * Create a new temp file in the LFS directory 98 * 99 * @return a new temporary file in the LFS directory 100 * @throws java.io.IOException 101 * when the temp file could not be created 102 */ createTmpFile()103 public Path createTmpFile() throws IOException { 104 return Files.createTempFile(getLfsTmpDir(), null, null); 105 } 106 107 } 108