Ignore .gitignore files on git.overleaf.com repos (fix #1281)

This commit is contained in:
Winston Li
2016-10-08 16:12:20 +01:00
parent bdc699991f
commit d3eb737120
36 changed files with 786 additions and 13 deletions

View File

@@ -6,6 +6,7 @@ import uk.ac.ic.wlgitbridge.bridge.db.DBStore;
import uk.ac.ic.wlgitbridge.bridge.db.ProjectState;
import uk.ac.ic.wlgitbridge.bridge.lock.LockGuard;
import uk.ac.ic.wlgitbridge.bridge.lock.ProjectLock;
import uk.ac.ic.wlgitbridge.bridge.repo.ProjectRepo;
import uk.ac.ic.wlgitbridge.bridge.repo.RepoStore;
import uk.ac.ic.wlgitbridge.bridge.resource.ResourceCache;
import uk.ac.ic.wlgitbridge.bridge.resource.UrlResourceCache;

View File

@@ -1,12 +1,12 @@
package uk.ac.ic.wlgitbridge.bridge;
package uk.ac.ic.wlgitbridge.bridge.repo;
import com.google.common.base.Preconditions;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import uk.ac.ic.wlgitbridge.bridge.repo.RepoStore;
import uk.ac.ic.wlgitbridge.data.filestore.GitDirectoryContents;
import uk.ac.ic.wlgitbridge.data.filestore.RawFile;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
@@ -80,10 +80,23 @@ public class GitProjectRepo implements ProjectRepo {
}
}
public void resetHard() throws IOException {
Git git = new Git(getJGitRepository());
try {
git.reset().setMode(ResetCommand.ResetType.HARD).call();
} catch (GitAPIException e) {
throw new IOException(e);
}
}
public Repository getJGitRepository() {
return repository.get();
}
public File getDirectory() {
return getJGitRepository().getWorkTree();
}
private void initRepositoryField(RepoStore repoStore) throws IOException {
Preconditions.checkNotNull(repoStore);
Preconditions.checkArgument(Project.isValidProjectName(projectName));
@@ -103,10 +116,11 @@ public class GitProjectRepo implements ProjectRepo {
GitDirectoryContents contents
) throws IOException, GitAPIException {
Preconditions.checkState(repository.isPresent());
Repository repo = getJGitRepository();
String name = getProjectName();
Log.info("[{}] Writing commit", name);
contents.write();
Git git = new Git(repository.get());
Git git = new Git(getJGitRepository());
Log.info("[{}] Getting missing files", name);
Set<String> missingFiles = git.status().call().getMissing();
for (String missing : missingFiles) {
@@ -114,7 +128,10 @@ public class GitProjectRepo implements ProjectRepo {
git.rm().setCached(true).addFilepattern(missing).call();
}
Log.info("[{}] Calling Git add", name);
git.add().addFilepattern(".").call();
git.add(
).setWorkingTreeIterator(
new NoGitignoreIterator(repo)
).addFilepattern(".").call();
Log.info("[{}] Calling Git commit", name);
git.commit(
).setAuthor(

View File

@@ -0,0 +1,82 @@
package uk.ac.ic.wlgitbridge.bridge.repo;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.treewalk.WorkingTreeIterator;
import org.eclipse.jgit.treewalk.WorkingTreeOptions;
import org.eclipse.jgit.util.FS;
import java.io.File;
import java.lang.reflect.Field;
/**
* Created by winston on 08/10/2016.
*/
public class NoGitignoreIterator extends FileTreeIterator {
private static final Field ignoreNodeField;
static {
try {
ignoreNodeField = WorkingTreeIterator.class.getDeclaredField(
"ignoreNode"
);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
ignoreNodeField.setAccessible(true);
}
public NoGitignoreIterator(Repository repo) {
super(repo);
}
public NoGitignoreIterator(
Repository repo,
FileModeStrategy fileModeStrategy
) {
super(repo, fileModeStrategy);
}
public NoGitignoreIterator(File root, FS fs, WorkingTreeOptions options) {
super(root, fs, options);
}
public NoGitignoreIterator(
File root,
FS fs,
WorkingTreeOptions options,
FileModeStrategy fileModeStrategy
) {
super(root, fs, options, fileModeStrategy);
}
@Deprecated
protected NoGitignoreIterator(WorkingTreeIterator p, File root, FS fs) {
super(p, root, fs);
}
protected NoGitignoreIterator(FileTreeIterator p, File root, FS fs) {
super(p, root, fs);
}
protected NoGitignoreIterator(
WorkingTreeIterator p,
File root,
FS fs,
FileModeStrategy fileModeStrategy
) {
super(p, root, fs, fileModeStrategy);
}
@Override
protected void init(Entry[] list) {
super.init(list);
try {
ignoreNodeField.set(this, null);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -1,4 +1,4 @@
package uk.ac.ic.wlgitbridge.bridge;
package uk.ac.ic.wlgitbridge.bridge.repo;
import uk.ac.ic.wlgitbridge.bridge.repo.RepoStore;
import uk.ac.ic.wlgitbridge.data.filestore.GitDirectoryContents;

View File

@@ -20,13 +20,38 @@ public class GitDirectoryContents {
private final String commitMessage;
private final Date when;
public GitDirectoryContents(List<RawFile> files, File rootGitDirectory, String projectName, Snapshot snapshot) {
public GitDirectoryContents(
List<RawFile> files,
File rootGitDirectory,
String projectName,
String userName,
String userEmail,
String commitMessage,
Date when
) {
this.files = files;
gitDirectory = new File(rootGitDirectory, projectName);
userName = snapshot.getUserName();
userEmail = snapshot.getUserEmail();
commitMessage = snapshot.getComment();
when = snapshot.getCreatedAt();
this.gitDirectory = new File(rootGitDirectory, projectName);
this.userName = userName;
this.userEmail = userEmail;
this.commitMessage = commitMessage;
this.when = when;
}
public GitDirectoryContents(
List<RawFile> files,
File rootGitDirectory,
String projectName,
Snapshot snapshot
) {
this(
files,
rootGitDirectory,
projectName,
snapshot.getUserName(),
snapshot.getUserEmail(),
snapshot.getComment(),
snapshot.getCreatedAt()
);
}
public void write() throws IOException {

View File

@@ -32,7 +32,8 @@ public abstract class RawFile {
return false;
}
RawFile that = (RawFile) obj;
return getPath().equals(that.getPath()) && Arrays.equals(getContents(), that.getContents());
return getPath().equals(that.getPath())
&& Arrays.equals(getContents(), that.getContents());
}
}

View File

@@ -8,7 +8,7 @@ import org.eclipse.jgit.transport.resolver.RepositoryResolver;
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import uk.ac.ic.wlgitbridge.bridge.Bridge;
import uk.ac.ic.wlgitbridge.bridge.GitProjectRepo;
import uk.ac.ic.wlgitbridge.bridge.repo.GitProjectRepo;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import uk.ac.ic.wlgitbridge.server.Oauth2Filter;
import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException;