public static boolean isUserSetup (File root) { Repository repository = getRepository(root); boolean userExists = true; if (repository != null) { try { StoredConfig config = repository.getConfig(); String name = config.getString("user", null, "name"); //NOI18N String email = config.getString("user", null, "email"); //NOI18N if (name == null || name.isEmpty() || email == null || email.isEmpty()) { userExists = false; } } finally { repository.close(); } } return userExists; }
public static void persistUser (File root, GitUser author) throws GitException { Repository repository = getRepository(root); if (repository != null) { try { StoredConfig config = repository.getConfig(); config.setString("user", null, "name", author.getName()); //NOI18N config.setString("user", null, "email", author.getEmailAddress()); //NOI18N try { config.save(); FileUtil.refreshFor(new File(GitUtils.getGitFolderForRoot(root), "config")); } catch (IOException ex) { throw new GitException(ex); } } finally { repository.close(); } } }
@Override protected void run () throws GitException { Repository repository = getRepository(); StoredConfig config = repository.getConfig(); config.unsetSection(ConfigConstants.CONFIG_REMOTE_SECTION, remote); Set<String> subSections = config.getSubsections(ConfigConstants.CONFIG_BRANCH_SECTION); for (String subSection : subSections) { if (remote.equals(config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, subSection, ConfigConstants.CONFIG_KEY_REMOTE))) { config.unset(ConfigConstants.CONFIG_BRANCH_SECTION, subSection, ConfigConstants.CONFIG_KEY_REMOTE); config.unset(ConfigConstants.CONFIG_BRANCH_SECTION, subSection, ConfigConstants.CONFIG_KEY_MERGE); } } try { config.save(); } catch (IOException ex) { throw new GitException(ex); } }
public void test199443_GlobalIgnoreFile () throws Exception { File f = new File(new File(workDir, "nbproject"), "file"); f.getParentFile().mkdirs(); f.createNewFile(); File ignoreFile = new File(workDir.getParentFile(), "globalignore"); write(ignoreFile, ".DS_Store\n.svn\nnbproject\nnbproject/private\n"); Repository repo = getRepository(getLocalGitRepository()); StoredConfig cfg = repo.getConfig(); cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_EXCLUDESFILE, ignoreFile.getAbsolutePath()); cfg.save(); GitClient client = getClient(workDir); assertEquals(Status.STATUS_IGNORED, client.getStatus(new File[] { f }, NULL_PROGRESS_MONITOR).get(f).getStatusIndexWC()); // now since the file is already ignored, no ignore file should be modified assertEquals(0, client.ignore(new File[] { f }, NULL_PROGRESS_MONITOR).length); // on the other hand, if .git/info/exclude reverts the effect of global excludes file, ignored file should be modified File dotGitIgnoreFile = new File(new File(repo.getDirectory(), "info"), "exclude"); dotGitIgnoreFile.getParentFile().mkdirs(); write(dotGitIgnoreFile, "!/nbproject/"); assertEquals(Status.STATUS_ADDED, client.getStatus(new File[] { f }, NULL_PROGRESS_MONITOR).get(f).getStatusIndexWC()); assertEquals(dotGitIgnoreFile, client.ignore(new File[] { f }, NULL_PROGRESS_MONITOR)[0]); assertEquals(Status.STATUS_IGNORED, client.getStatus(new File[] { f }, NULL_PROGRESS_MONITOR).get(f).getStatusIndexWC()); }
public void test199443_GlobalIgnoreFileOverwrite () throws Exception { File f = new File(new File(workDir, "nbproject"), "file"); f.getParentFile().mkdirs(); f.createNewFile(); File ignoreFile = new File(workDir.getParentFile(), "globalignore"); Repository repo = getRepository(getLocalGitRepository()); StoredConfig cfg = repo.getConfig(); cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_EXCLUDESFILE, ignoreFile.getAbsolutePath()); cfg.save(); write(ignoreFile, "!nbproject"); GitClient client = getClient(workDir); assertEquals(new File(workDir, Constants.GITIGNORE_FILENAME), client.ignore(new File[] { f }, NULL_PROGRESS_MONITOR)[0]); assertEquals("/nbproject/file", read(new File(workDir, Constants.GITIGNORE_FILENAME))); }
public void testAddRemote () throws Exception { StoredConfig config = repository.getConfig(); assertEquals(0, config.getSubsections("remote").size()); GitClient client = getClient(workDir); GitRemoteConfig remoteConfig = new GitRemoteConfig("origin", Arrays.asList(new File(workDir.getParentFile(), "repo2").toURI().toString()), Arrays.asList(new File(workDir.getParentFile(), "repo2").toURI().toString()), Arrays.asList("+refs/heads/*:refs/remotes/origin/*"), Arrays.asList("refs/remotes/origin/*:+refs/heads/*")); client.setRemote(remoteConfig, NULL_PROGRESS_MONITOR); config.load(); RemoteConfig cfg = new RemoteConfig(config, "origin"); assertEquals(Arrays.asList(new URIish(new File(workDir.getParentFile(), "repo2").toURI().toString())), cfg.getURIs()); assertEquals(Arrays.asList(new URIish(new File(workDir.getParentFile(), "repo2").toURI().toString())), cfg.getPushURIs()); assertEquals(Arrays.asList(new RefSpec("+refs/heads/*:refs/remotes/origin/*")), cfg.getFetchRefSpecs()); assertEquals(Arrays.asList(new RefSpec("refs/remotes/origin/*:+refs/heads/*")), cfg.getPushRefSpecs()); }
public void testMergeBranchNoHeadYet_196837 () throws Exception { StoredConfig cfg = getRemoteRepository().getConfig(); cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_BARE, false); cfg.save(); File otherRepo = getRemoteRepository().getWorkTree(); File original = new File(otherRepo, "f"); GitClient clientOtherRepo = getClient(otherRepo); write(original, "initial content"); clientOtherRepo.add(new File[] { original }, NULL_PROGRESS_MONITOR); clientOtherRepo.commit(new File[] { original }, "initial commit", null, null, NULL_PROGRESS_MONITOR); GitClient client = getClient(workDir); Map<String, GitTransportUpdate> updates = client.fetch(otherRepo.toURI().toString(), Arrays.asList(new String[] { "+refs/heads/master:refs/remotes/origin/master" }), NULL_PROGRESS_MONITOR); GitMergeResult result = client.merge("origin/master", NULL_PROGRESS_MONITOR); assertEquals(MergeStatus.FAST_FORWARD, result.getMergeStatus()); assertEquals(Arrays.asList(new String[] { ObjectId.zeroId().getName(), updates.get("origin/master").getNewObjectId() }), Arrays.asList(result.getMergedCommits())); }
public void testAddIgnoreExecutable () throws Exception { if (isWindows()) { // no reason to test on windows return; } File f = new File(workDir, "f"); write(f, "hi, i am executable"); f.setExecutable(true); File[] roots = { f }; GitClient client = getClient(workDir); StoredConfig config = repository.getConfig(); config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE, false); config.save(); // add should not set executable bit in index add(roots); Map<File, GitStatus> statuses = client.getStatus(roots, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, f, true, Status.STATUS_ADDED, Status.STATUS_NORMAL, Status.STATUS_ADDED, false); // index should differ from wt config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE, true); config.save(); statuses = client.getStatus(roots, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, f, true, Status.STATUS_ADDED, Status.STATUS_MODIFIED, Status.STATUS_ADDED, false); }
public void test199443_GlobalIgnoreFile () throws Exception { File f = new File(new File(workDir, "nbproject"), "file"); f.getParentFile().mkdirs(); f.createNewFile(); File ignoreFile = new File(workDir.getParentFile(), "globalignore"); write(ignoreFile, "nbproject"); Repository repo = getRepository(getLocalGitRepository()); StoredConfig cfg = repo.getConfig(); cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_EXCLUDESFILE, ignoreFile.getAbsolutePath()); cfg.save(); GitClient client = getClient(workDir); assertEquals(Status.STATUS_IGNORED, client.getStatus(new File[] { f }, NULL_PROGRESS_MONITOR).get(f).getStatusIndexWC()); assertEquals(new File(workDir, Constants.GITIGNORE_FILENAME), client.unignore(new File[] { f }, NULL_PROGRESS_MONITOR)[0]); write(new File(workDir, Constants.GITIGNORE_FILENAME), "/nbproject/file"); assertEquals(new File(workDir, Constants.GITIGNORE_FILENAME), client.unignore(new File[] { f }, NULL_PROGRESS_MONITOR)[0]); assertEquals("!/nbproject/file", read(new File(workDir, Constants.GITIGNORE_FILENAME))); }
/** * Adds the remote as origin to the repository */ @Override protected void doOK() { super.doOK(); StoredConfig config; try { config = GitAccess.getInstance().getRepository().getConfig(); RemoteConfig remoteConfig = new RemoteConfig(config, "origin"); URIish uri = new URIish(remoteRepoTextField.getText()); remoteConfig.addURI(uri); RefSpec spec = new RefSpec("+refs/heads/*:refs/remotes/origin/*"); remoteConfig.addFetchRefSpec(spec); remoteConfig.update(config); config.save(); } catch (NoRepositorySelected | URISyntaxException | IOException e) { if (logger.isDebugEnabled()) { logger.debug(e, e); } } dispose(); }
@Test(expected = MissingObjectException.class) public void testRemoteRepositoryHasNoCommitst() throws URISyntaxException, IOException, InvalidRemoteException, TransportException, GitAPIException, NoRepositorySelected { gitAccess.setRepository(LOCAL_TEST_REPOSITPRY); final StoredConfig config = gitAccess.getRepository().getConfig(); RemoteConfig remoteConfig = new RemoteConfig(config, "origin"); URIish uri = new URIish(db2.getDirectory().toURI().toURL()); remoteConfig.addURI(uri); remoteConfig.update(config); config.save(); gitAccess.add(new FileStatus(GitChangeType.ADD, "test.txt")); gitAccess.commit("file test added"); // throws missingObjectException db2.resolve(gitAccess.getLastLocalCommit().getName() + "^{commit}"); }
@Test public void testPushOK() throws URISyntaxException, IOException, InvalidRemoteException, TransportException, GitAPIException, NoRepositorySelected { gitAccess.setRepository(LOCAL_TEST_REPOSITPRY); final StoredConfig config = gitAccess.getRepository().getConfig(); RemoteConfig remoteConfig = new RemoteConfig(config, "origin"); URIish uri = new URIish(db2.getDirectory().toURI().toURL()); remoteConfig.addURI(uri); remoteConfig.update(config); config.save(); gitAccess.add(new FileStatus(GitChangeType.ADD, "test.txt")); gitAccess.commit("file test added"); gitAccess.push("", ""); assertEquals(db1.resolve(gitAccess.getLastLocalCommit().getName() + "^{commit}"), db2.resolve(gitAccess.getLastLocalCommit().getName() + "^{commit}")); }
protected void configureBranch(Git git, String branch) { // lets update the merge config if (Strings.isNotBlank(branch)) { StoredConfig config = git.getRepository().getConfig(); if (Strings.isNullOrBlank(config.getString("branch", branch, "remote")) || Strings.isNullOrBlank(config.getString("branch", branch, "merge"))) { config.setString("branch", branch, "remote", getRemote()); config.setString("branch", branch, "merge", "refs/heads/" + branch); try { config.save(); } catch (IOException e) { LOG.error("Failed to save the git configuration to " + basedir + " with branch " + branch + " on remote repo: " + remoteRepository + " due: " + e.getMessage() + ". This exception is ignored.", e); } } } }
public static void clone(String url, File target) throws GitAPIException, IOException { System.out.println( "Starting clone of " + url + " to " + target ); Git result = Git.cloneRepository().setURI( url ).setDirectory( target ).call(); try { StoredConfig config = result.getRepository().getConfig(); config.setBoolean( "core", null, "autocrlf", autocrlf ); config.save(); System.out.println( "Cloned git repository " + url + " to " + target.getAbsolutePath() + ". Current HEAD: " + commitHash( result ) ); } finally { result.close(); } }
@Test public void ifThereIsNoScmInfoAndNoRemoteBranchThenAnErrorIsThrown() throws GitAPIException, IOException, InterruptedException { final TestProject testProject = TestProject.singleModuleProject(); final StoredConfig config = testProject.local.getRepository().getConfig(); config.unsetSection("remote", "origin"); config.save(); try { testProject.mvnRelease("1"); Assert.fail("Should have failed"); } catch (final MavenExecutionException e) { assertThat(e.output, oneOf(containsString("[ERROR] Remote tags could not be listed!"))); } }
@Override public List<Remote> remoteList(String remoteName, boolean verbose) throws GitException { StoredConfig config = repository.getConfig(); Set<String> remoteNames = new HashSet<>(config.getSubsections(ConfigConstants.CONFIG_KEY_REMOTE)); if (remoteName != null && remoteNames.contains(remoteName)) { remoteNames.clear(); remoteNames.add(remoteName); } List<Remote> result = new ArrayList<>(remoteNames.size()); for (String remote : remoteNames) { try { List<URIish> uris = new RemoteConfig(config, remote).getURIs(); result.add( newDto(Remote.class) .withName(remote) .withUrl(uris.isEmpty() ? null : uris.get(0).toString())); } catch (URISyntaxException exception) { throw new GitException(exception.getMessage(), exception); } } return result; }
@Test public void shouldCloseCloneCommand() throws Exception { // given File fileMock = mock(File.class); Git cloneCommand = mock(Git.class); jGitConnection.setOutputLineConsumerFactory(mock(LineConsumerFactory.class)); when(repository.getWorkTree()).thenReturn(fileMock); when(repository.getDirectory()).thenReturn(fileMock); when(repository.getConfig()).thenReturn(mock(StoredConfig.class)); doReturn(cloneCommand) .when(jGitConnection) .executeRemoteCommand( nullable(String.class), nullable(TransportCommand.class), nullable(String.class), nullable(String.class)); // when jGitConnection.clone(CloneParams.create("url").withWorkingDir("fakePath")); // then verify(cloneCommand).close(); }
public static void configureBranch(Git git, String branch, String origin, String remoteRepository) { // lets update the merge config if (!Strings.isNullOrBlank(branch)) { StoredConfig config = git.getRepository().getConfig(); config.setString("branch", branch, "remote", origin); config.setString("branch", branch, "merge", "refs/heads/" + branch); config.setString("remote", origin, "url", remoteRepository); config.setString("remote", origin, "fetch", "+refs/heads/*:refs/remotes/" + origin + "/*"); try { config.save(); } catch (IOException e) { LOG.error("Failed to save the git configuration to " + git.getRepository().getDirectory() + " with branch " + branch + " on " + origin + " remote repo: " + remoteRepository + " due: " + e.getMessage() + ". This exception is ignored.", e); } } }
public List<RefSpec> execute() throws IOException, URISyntaxException { final List<RefSpec> specs = new ArrayList<>(); if (refSpecs == null || refSpecs.isEmpty()) { specs.add(new RefSpec("+refs/heads/*:refs/remotes/" + remote.getK1() + "/*")); specs.add(new RefSpec("+refs/tags/*:refs/tags/*")); specs.add(new RefSpec("+refs/notes/*:refs/notes/*")); } else { specs.addAll(refSpecs); } final StoredConfig config = git.getRepository().getConfig(); final String url = config.getString("remote", remote.getK1(), "url"); if (url == null) { final RemoteConfig remoteConfig = new RemoteConfig(git.getRepository().getConfig(), remote.getK1()); remoteConfig.addURI(new URIish(remote.getK2())); specs.forEach(remoteConfig::addFetchRefSpec); remoteConfig.update(git.getRepository().getConfig()); git.getRepository().getConfig().save(); } return specs; }
@Test public void shouldPullForcepullNotClean() throws Exception { Git git = mock(Git.class); StatusCommand statusCommand = mock(StatusCommand.class); Status status = mock(Status.class); Repository repository = mock(Repository.class); StoredConfig storedConfig = mock(StoredConfig.class); when(git.status()).thenReturn(statusCommand); when(git.getRepository()).thenReturn(repository); when(repository.getConfig()).thenReturn(storedConfig); when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git"); when(statusCommand.call()).thenReturn(status); when(status.isClean()).thenReturn(false); JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment); repo.setForcePull(true); boolean shouldPull = repo.shouldPull(git); assertThat("shouldPull was false", shouldPull, is(true)); }
@Test public void shouldPullNotClean() throws Exception { Git git = mock(Git.class); StatusCommand statusCommand = mock(StatusCommand.class); Status status = mock(Status.class); Repository repository = mock(Repository.class); StoredConfig storedConfig = mock(StoredConfig.class); when(git.status()).thenReturn(statusCommand); when(git.getRepository()).thenReturn(repository); when(repository.getConfig()).thenReturn(storedConfig); when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git"); when(statusCommand.call()).thenReturn(status); when(status.isClean()).thenReturn(false); JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment); boolean shouldPull = repo.shouldPull(git); assertThat("shouldPull was true", shouldPull, is(false)); }
@Test public void shouldPullClean() throws Exception { Git git = mock(Git.class); StatusCommand statusCommand = mock(StatusCommand.class); Status status = mock(Status.class); Repository repository = mock(Repository.class); StoredConfig storedConfig = mock(StoredConfig.class); when(git.status()).thenReturn(statusCommand); when(git.getRepository()).thenReturn(repository); when(repository.getConfig()).thenReturn(storedConfig); when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git"); when(statusCommand.call()).thenReturn(status); when(status.isClean()).thenReturn(true); JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment); boolean shouldPull = repo.shouldPull(git); assertThat("shouldPull was false", shouldPull, is(true)); }
private static void updateCoreIgnoreCaseSetting(@Nullable Git git) { if (SystemInfo.isFileSystemCaseSensitive) { return; } if (git == null) { LOG.info("jgit.Git is null, the command should have failed. Not updating the settings."); return; } StoredConfig config = git.getRepository().getConfig(); config.setString(ConfigConstants.CONFIG_CORE_SECTION, null, IGNORECASE_SETTING, Boolean.TRUE.toString()); try { config.save(); } catch (IOException e) { LOG.info("Couldn't save config for " + git.getRepository().getDirectory().getPath(), e); } }
private String loadDescriptionText(Repository repo) throws IOException { String desc = null; StoredConfig config = repo.getConfig(); IOException configError = null; try { config.load(); desc = config.getString("gitweb", null, "description"); } catch (ConfigInvalidException e) { configError = new IOException(e); } if (desc == null) { File descFile = new File(repo.getDirectory(), "description"); if (descFile.exists()) { desc = new String(IO.readFully(descFile)); if (DEFAULT_DESCRIPTION.equals(CharMatcher.WHITESPACE.trimFrom(desc))) { desc = null; } } else if (configError != null) { throw configError; } } return desc; }
@Override public boolean deleteRegistrations(List<GitblitRegistration> list) { boolean success = false; try { StoredConfig config = getConfig(); for (GitblitRegistration reg : list) { config.unsetSection(SERVER, reg.name); registrations.remove(reg.name); } config.save(); success = true; } catch (Throwable t) { Utils.showException(GitblitManager.this, t); } return success; }
private Set<String> listRemoteBranches(String remote) throws NotSupportedException, TransportException, URISyntaxException { Set<String> branches = new HashSet<>(); try (final Repository repo = getRepository()) { StoredConfig config = repo.getConfig(); try (final Transport tn = Transport.open(repo, new URIish(config.getString("remote",remote,"url")))) { tn.setCredentialsProvider(getProvider()); try (final FetchConnection c = tn.openFetch()) { for (final Ref r : c.getRefs()) { if (r.getName().startsWith(R_HEADS)) branches.add("refs/remotes/"+remote+"/"+r.getName().substring(R_HEADS.length())); } } } } return branches; }
private void execute() throws InvalidRemoteException, TransportException, GitAPIException, IOException { setProxy(); CloneCommand cmd = Git.cloneRepository() .setURI(config.getRemoteUrl()); if (config.getLocalPath() != "") { cmd.setDirectory(new File(config.getLocalPath())); } Git git = cmd.call(); // Set proxy setting to repository config. StoredConfig gitConfig = git.getRepository().getConfig(); gitConfig.setString("remote", "origin", "proxy", config.getProxyAddress()); gitConfig.save(); git.getRepository().close(); }
public String getRemoteOriginURL() { try { StoredConfig config = getStoredConfig(); String origin = config.getString("remote", "origin", "url"); if (origin != null && !origin.isEmpty()) return origin; Set<String> remoteNames = config.getSubsections("remote"); if (remoteNames.size() == 0) return ""; String url = config.getString("remote", remoteNames.iterator() .next(), "url"); return url; } catch (StopTaskException e) { } return ""; }
public void setRemote(String remote, String url) throws IOException { try { StoredConfig config = getStoredConfig(); Set<String> remoteNames = config.getSubsections("remote"); if (remoteNames.contains(remote)) { throw new IOException(String.format( "Remote %s already exists.", remote)); } config.setString("remote", remote, "url", url); String fetch = String.format("+refs/heads/*:refs/remotes/%s/*", remote); config.setString("remote", remote, "fetch", fetch); config.save(); mRemotes.add(remote); } catch (StopTaskException e) { } }
/** 저장소 생성함. * @throws Exception */ public boolean createRepository(){ try { git.init().setBare(true).setDirectory(new File(this.path)).call(); StoredConfig config = localRepo.getConfig(); config.setString("http", null, "receivepack", "true"); config.save(); }catch(Exception e) { System.err.println(e.getMessage()); return false; } return true; }
private void setupRebaseFlag (Repository repository) throws IOException { Ref baseRef = repository.getRef(revision); if (baseRef != null && baseRef.getName().startsWith(Constants.R_REMOTES)) { StoredConfig config = repository.getConfig(); String autosetupRebase = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE); boolean rebase = ConfigConstants.CONFIG_KEY_ALWAYS.equals(autosetupRebase) || ConfigConstants.CONFIG_KEY_REMOTE.equals(autosetupRebase); if (rebase && !config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION, branchName).isEmpty()) { config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, branchName, ConfigConstants.CONFIG_KEY_REBASE, rebase); config.save(); } } }
@Override protected void run () throws GitException { Repository repository = getRepository(); try { Ref ref = repository.getRef(trackedBranchName); if (ref == null) { throw new GitException(MessageFormat.format(Utils.getBundle(SetUpstreamBranchCommand.class) .getString("MSG_Error_UpdateTracking_InvalidReference"), trackedBranchName)); //NOI18N) } String remote = null; String branchName = ref.getName(); StoredConfig config = repository.getConfig(); if (branchName.startsWith(Constants.R_REMOTES)) { String[] elements = branchName.split("/", 4); remote = elements[2]; if (config.getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION).contains(remote)) { branchName = Constants.R_HEADS + elements[3]; setupRebaseFlag(repository); } else { // remote not yet set remote = null; } } if (remote == null) { remote = "."; //NOI18N } config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName, ConfigConstants.CONFIG_KEY_REMOTE, remote); config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName, ConfigConstants.CONFIG_KEY_MERGE, branchName); config.save(); } catch (IOException ex) { throw new GitException(ex); } ListBranchCommand branchCmd = new ListBranchCommand(repository, getClassFactory(), false, new DelegatingGitProgressMonitor(monitor)); branchCmd.run(); Map<String, GitBranch> branches = branchCmd.getBranches(); branch = branches.get(localBranchName); }
private void setupRebaseFlag (Repository repository) throws IOException { StoredConfig config = repository.getConfig(); String autosetupRebase = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE); boolean rebase = ConfigConstants.CONFIG_KEY_ALWAYS.equals(autosetupRebase) || ConfigConstants.CONFIG_KEY_REMOTE.equals(autosetupRebase); if (rebase) { config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName, ConfigConstants.CONFIG_KEY_REBASE, rebase); } }
public void testCreateBranchWithRebase () throws Exception { final File otherWT = new File(workDir.getParentFile(), "repo2"); GitClient client = getClient(otherWT); client.init(NULL_PROGRESS_MONITOR); File f = new File(otherWT, "f"); write(f, "init"); client.add(new File[] { f }, NULL_PROGRESS_MONITOR); client.commit(new File[] { f }, "init commit", null, null, NULL_PROGRESS_MONITOR); client = getClient(workDir); client.setRemote(new GitRemoteConfig("origin", Arrays.asList(new String[] { otherWT.getAbsolutePath() }), Arrays.asList(new String[] { otherWT.getAbsolutePath() }), Arrays.asList(new String[] { "refs/heads/*:refs/remotes/origin/*" }), Arrays.asList(new String[] { "refs/remotes/origin/*:refs/heads/*" })), NULL_PROGRESS_MONITOR); client.fetch("origin", NULL_PROGRESS_MONITOR); StoredConfig config = repository.getConfig(); config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE, ConfigConstants.CONFIG_KEY_NEVER); config.save(); GitBranch b = client.createBranch(BRANCH_NAME, "origin/master", NULL_PROGRESS_MONITOR); assertFalse(repository.getConfig().getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, BRANCH_NAME, ConfigConstants.CONFIG_KEY_REBASE, false)); client.deleteBranch(BRANCH_NAME, true, NULL_PROGRESS_MONITOR); config = repository.getConfig(); config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE, ConfigConstants.CONFIG_KEY_REMOTE); config.save(); b = client.createBranch(BRANCH_NAME, "origin/master", NULL_PROGRESS_MONITOR); assertTrue(repository.getConfig().getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, BRANCH_NAME, ConfigConstants.CONFIG_KEY_REBASE, false)); }
public void testBranchTracking () throws Exception { final File otherWT = new File(workDir.getParentFile(), "repo2"); GitClient client = getClient(otherWT); client.init(NULL_PROGRESS_MONITOR); File f = new File(otherWT, "f"); write(f, "init"); client.add(new File[] { f }, NULL_PROGRESS_MONITOR); client.commit(new File[] { f }, "init commit", null, null, NULL_PROGRESS_MONITOR); client = getClient(workDir); client.setRemote(new GitRemoteConfig("origin", Arrays.asList(otherWT.getAbsolutePath()), Arrays.asList(otherWT.getAbsolutePath()), Arrays.asList("+refs/heads/*:refs/remotes/origin/*"), Collections.<String>emptyList()), NULL_PROGRESS_MONITOR); client.fetch("origin", NULL_PROGRESS_MONITOR); GitBranch b = client.createBranch(Constants.MASTER, "origin/master", NULL_PROGRESS_MONITOR); assertEquals("origin/master", b.getTrackedBranch().getName()); assertTrue(b.getTrackedBranch().isRemote()); client.checkoutRevision(Constants.MASTER, true, NULL_PROGRESS_MONITOR); b = client.createBranch("nova1", Constants.MASTER, NULL_PROGRESS_MONITOR); assertNull(b.getTrackedBranch()); StoredConfig cfg = repository.getConfig(); cfg.setString(ConfigConstants.CONFIG_BRANCH_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOSETUPMERGE, "always"); cfg.save(); b = client.createBranch("nova2", Constants.MASTER, NULL_PROGRESS_MONITOR); assertEquals("master", b.getTrackedBranch().getName()); assertFalse(b.getTrackedBranch().isRemote()); // list branches Map<String, GitBranch> branches = client.getBranches(true, NULL_PROGRESS_MONITOR); b = branches.get(Constants.MASTER); assertEquals("origin/master", b.getTrackedBranch().getName()); assertTrue(b.getTrackedBranch().isRemote()); b = branches.get("origin/master"); assertNull(b.getTrackedBranch()); }
public void testBlameMixedLineEndings () throws Exception { File f = new File(workDir, "f"); String content = ""; for (int i = 0; i < 10000; ++i) { content += i + "\r\n"; } write(f, content); // lets turn autocrlf on StoredConfig cfg = repository.getConfig(); cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOCRLF, "true"); cfg.save(); File[] files = new File[] { f }; GitClient client = getClient(workDir); client.add(files, NULL_PROGRESS_MONITOR); GitRevisionInfo info = client.commit(files, "commit", null, null, NULL_PROGRESS_MONITOR); content = content.replaceFirst("0", "01"); write(f, content); // it should be up to date again org.eclipse.jgit.api.BlameCommand cmd = new Git(repository).blame(); cmd.setFilePath("f"); BlameResult blameResult = cmd.call(); assertEquals(info.getRevision(), blameResult.getSourceCommit(1).getName()); GitBlameResult res = client.blame(f, null, NULL_PROGRESS_MONITOR); assertNull(res.getLineDetails(0)); assertLineDetails(f, 1, info.getRevision(), info.getAuthor(), info.getCommitter(), res.getLineDetails(1)); // without autocrlf it should all be modified cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOCRLF, "false"); cfg.save(); res = client.blame(f, null, NULL_PROGRESS_MONITOR); assertNull(res.getLineDetails(1)); }
public void testIgnoreExecutable () throws Exception { if (isWindows()) { // no reason to test on win return; } File f = new File(workDir, "f"); write(f, "hi, i am executable"); f.setExecutable(true); File[] roots = { f }; add(roots); commit(roots); GitClient client = getClient(workDir); Map<File, GitStatus> statuses = client.getStatus(roots, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, f, true, Status.STATUS_NORMAL, Status.STATUS_NORMAL, Status.STATUS_NORMAL, false); f.setExecutable(false); statuses = client.getStatus(roots, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, f, true, Status.STATUS_NORMAL, Status.STATUS_MODIFIED, Status.STATUS_MODIFIED, false); StoredConfig config = repository.getConfig(); config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE, false); config.save(); statuses = client.getStatus(roots, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, f, true, Status.STATUS_NORMAL, Status.STATUS_NORMAL, Status.STATUS_NORMAL, false); config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE, true); config.save(); add(roots); statuses = client.getStatus(roots, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, f, true, Status.STATUS_MODIFIED, Status.STATUS_NORMAL, Status.STATUS_MODIFIED, false); config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE, false); config.save(); add(roots); statuses = client.getStatus(roots, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, f, true, Status.STATUS_MODIFIED, Status.STATUS_NORMAL, Status.STATUS_NORMAL, false); }
public void testRemoveRemote () throws Exception { File otherWT = new File(workDir.getParentFile(), "repo2"); GitClient client = getClient(otherWT); client.init(NULL_PROGRESS_MONITOR); File f = new File(otherWT, "f"); write(f, "init"); client.add(new File[] { f }, NULL_PROGRESS_MONITOR); client.commit(new File[] { f }, "init commit", null, null, NULL_PROGRESS_MONITOR); RemoteConfig cfg = new RemoteConfig(repository.getConfig(), "origin"); cfg.addURI(new URIish(otherWT.toURI().toURL().toString())); cfg.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*")); cfg.update(repository.getConfig()); repository.getConfig().save(); client = getClient(workDir); client.fetch("origin", NULL_PROGRESS_MONITOR); client.createBranch("master", "origin/master", NULL_PROGRESS_MONITOR); client.createBranch("nova", "origin/master", NULL_PROGRESS_MONITOR); StoredConfig config = repository.getConfig(); assertEquals("+refs/heads/*:refs/remotes/origin/*", config.getString("remote", "origin", "fetch")); assertEquals("origin", config.getString("branch", "master", "remote")); assertEquals("refs/heads/master", config.getString("branch", "master", "merge")); assertEquals("origin", config.getString("branch", "nova", "remote")); assertEquals("refs/heads/master", config.getString("branch", "nova", "merge")); // now try to remove the remote client.removeRemote("origin", NULL_PROGRESS_MONITOR); config = repository.getConfig(); config.load(); // is everything deleted? assertEquals(0, config.getSubsections("remote").size()); assertNull(config.getString("branch", "master", "remote")); assertNull(config.getString("branch", "master", "merge")); assertNull(config.getString("branch", "nova", "remote")); assertNull(config.getString("branch", "nova", "merge")); }
public void testMergeFFOnly () throws Exception { File f1 = new File(workDir, "file1"); File f2 = new File(workDir, "file2"); write(f1, "init"); write(f2, "init"); add(f1, f2); commit(f1, f2); GitClient client = getClient(workDir); client.createBranch(BRANCH_NAME, Constants.MASTER, NULL_PROGRESS_MONITOR); client.checkoutRevision(BRANCH_NAME, true, NULL_PROGRESS_MONITOR); write(f1, BRANCH_NAME); add(f1); client.commit(new File[] { f1 }, "change on branch", null, null, NULL_PROGRESS_MONITOR); client.checkoutRevision(Constants.MASTER, true, NULL_PROGRESS_MONITOR); write(f2, "another change"); add(f2); client.commit(new File[] { f2 }, "change on master", null, null, NULL_PROGRESS_MONITOR); GitMergeResult result = client.merge(BRANCH_NAME, GitRepository.FastForwardOption.FAST_FORWARD_ONLY, NULL_PROGRESS_MONITOR); // no merge commits allowed => FAIL assertEquals(MergeStatus.ABORTED, result.getMergeStatus()); // test also config files assertEquals(GitRepository.FastForwardOption.FAST_FORWARD, GitRepository.getInstance(workDir).getDefaultFastForwardOption()); StoredConfig cfg = repo.getConfig(); cfg.setEnum(ConfigConstants.CONFIG_KEY_MERGE, null, ConfigConstants.CONFIG_KEY_FF, org.eclipse.jgit.api.MergeCommand.FastForwardMode.Merge.ONLY); cfg.save(); assertEquals(GitRepository.FastForwardOption.FAST_FORWARD_ONLY, GitRepository.getInstance(workDir).getDefaultFastForwardOption()); result = client.merge(BRANCH_NAME, NULL_PROGRESS_MONITOR); // no merge commits allowed => FAIL assertEquals(MergeStatus.ABORTED, result.getMergeStatus()); result = client.merge(BRANCH_NAME, GitRepository.FastForwardOption.FAST_FORWARD, NULL_PROGRESS_MONITOR); // merge commits allowed => OK assertEquals(MergeStatus.MERGED, result.getMergeStatus()); }