@Override @Before public void setUp() throws Exception { super.setUp(); dbTarget = createWorkRepository(); source = new Git(db); target = new Git(dbTarget); // put some file in the source repo sourceFile = new File(db.getWorkTree(), "SomeFile.txt"); writeToFile(sourceFile, "Hello world"); // and commit it source.add().addFilepattern("SomeFile.txt").call(); source.commit().setMessage("Initial commit for source").call(); // configure the target repo to connect to the source via "origin" StoredConfig targetConfig = dbTarget.getConfig(); targetConfig.setString("branch", "master", "remote", "origin"); targetConfig.setString("branch", "master", "merge", "refs/heads/master"); RemoteConfig config = new RemoteConfig(targetConfig, "origin"); config.addURI(new URIish(source.getRepository().getWorkTree().getPath())); config.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*")); config.update(targetConfig); targetConfig.save(); targetFile = new File(dbTarget.getWorkTree(), "SomeFile.txt"); // make sure we have the same content target.pull().call(); assertFileContentsEqual(targetFile, "Hello world"); }
@Before public void prepare() throws Exception { Repository childRepository = lookupRepository(childRepositoryFile); Repository repository = lookupRepository(repositoryFile); StoredConfig config = repository.getConfig(); RemoteConfig remoteConfig = new RemoteConfig(config, "origin"); remoteConfig.addURI(new URIish(childRepository.getDirectory().getParentFile().toURI().toURL())); remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*")); remoteConfig.update(config); config.setString( ConfigConstants.CONFIG_BRANCH_SECTION, "master", ConfigConstants.CONFIG_KEY_REMOTE, "origin"); config.setString( ConfigConstants.CONFIG_BRANCH_SECTION, "master", ConfigConstants.CONFIG_KEY_MERGE, "refs/heads/master"); config.save(); FetchOperation fetchOperation = new FetchOperation(repository, remoteConfig, 60, false); fetchOperation.run(null); }
private void saveRemote(final URIish uri) throws URISyntaxException, IOException { final StoredConfig dstcfg = dst.getConfig(); final RemoteConfig rc = new RemoteConfig(dstcfg, remoteName); rc.addURI(uri); rc.addFetchRefSpec( new RefSpec() .setForceUpdate(true) .setSourceDestination( Constants.R_HEADS + "*", // $NON-NLS-1$ Constants.R_REMOTES + remoteName + "/*")); // $NON-NLS-1$ rc.update(dstcfg); dstcfg.save(); }
// add new remote private boolean addRemote(HttpServletRequest request, HttpServletResponse response, String path) throws IOException, JSONException, ServletException, CoreException, URISyntaxException { // expected path: /git/remote/file/{path} Path p = new Path(path); JSONObject toPut = OrionServlet.readJSONRequest(request); String remoteName = toPut.optString(GitConstants.KEY_REMOTE_NAME, null); // remoteName is required if (remoteName == null || remoteName.isEmpty()) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Remote name must be provided", null)); } String remoteURI = toPut.optString(GitConstants.KEY_REMOTE_URI, null); // remoteURI is required if (remoteURI == null || remoteURI.isEmpty()) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Remote URI must be provided", null)); } String fetchRefSpec = toPut.optString(GitConstants.KEY_REMOTE_FETCH_REF, null); String remotePushURI = toPut.optString(GitConstants.KEY_REMOTE_PUSH_URI, null); String pushRefSpec = toPut.optString(GitConstants.KEY_REMOTE_PUSH_REF, null); File gitDir = GitUtils.getGitDir(p); Repository db = new FileRepository(gitDir); StoredConfig config = db.getConfig(); RemoteConfig rc = new RemoteConfig(config, remoteName); rc.addURI(new URIish(remoteURI)); // FetchRefSpec is required, but default version can be generated // if it isn't provided if (fetchRefSpec == null || fetchRefSpec.isEmpty()) { fetchRefSpec = String.format("+refs/heads/*:refs/remotes/%s/*", remoteName); // $NON-NLS-1$ } rc.addFetchRefSpec(new RefSpec(fetchRefSpec)); // pushURI is optional if (remotePushURI != null && !remotePushURI.isEmpty()) rc.addPushURI(new URIish(remotePushURI)); // PushRefSpec is optional if (pushRefSpec != null && !pushRefSpec.isEmpty()) rc.addPushRefSpec(new RefSpec(pushRefSpec)); rc.update(config); config.save(); URI cloneLocation = BaseToCloneConverter.getCloneLocation(getURI(request), BaseToCloneConverter.REMOTE_LIST); Remote remote = new Remote(cloneLocation, db, remoteName); JSONObject result = new JSONObject(); result.put(ProtocolConstants.KEY_LOCATION, remote.getLocation()); OrionServlet.writeJSONResponse(request, response, result); response.setHeader( ProtocolConstants.HEADER_LOCATION, result.getString(ProtocolConstants.KEY_LOCATION)); response.setStatus(HttpServletResponse.SC_CREATED); return true; }