org.eclipse.jgit.lib.ConfigConstants Java Examples

The following examples show how to use org.eclipse.jgit.lib.ConfigConstants. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GitClientCloneTest.java    From git-client-plugin with MIT License 6 votes vote down vote up
@Test
public void test_clone_refspecs() throws Exception {
    List<RefSpec> refspecs = Arrays.asList(
            new RefSpec("+refs/heads/master:refs/remotes/origin/master"),
            new RefSpec("+refs/heads/1.4.x:refs/remotes/origin/1.4.x")
    );
    testGitClient.clone_().url(workspace.localMirror()).refspecs(refspecs).repositoryName("origin").execute();
    testGitClient.withRepository((Repository workRepo, VirtualChannel channel) -> {
        String[] fetchRefSpecs = workRepo.getConfig().getStringList(ConfigConstants.CONFIG_REMOTE_SECTION, Constants.DEFAULT_REMOTE_NAME, "fetch");
        assertThat(fetchRefSpecs.length, is(2));
        assertThat(fetchRefSpecs[0], is("+refs/heads/master:refs/remotes/origin/master"));
        assertThat(fetchRefSpecs[1], is("+refs/heads/1.4.x:refs/remotes/origin/1.4.x"));
        return null;
    });
    Set<Branch> remoteBranches = testGitClient.getRemoteBranches();
    assertBranchesExist(remoteBranches, "origin/master");
    assertBranchesExist(remoteBranches, "origin/1.4.x");
    assertThat(remoteBranches.size(), is(2));
}
 
Example #2
Source File: BranchTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testDeleteUntrackedLocalBranch () throws Exception {
    File f = new File(workDir, "f");
    File[] files = { f };
    write(f, "init");
    add(files);
    commit(files);
    GitClient client = getClient(workDir);
    GitBranch b = client.createBranch(BRANCH_NAME, "master", NULL_PROGRESS_MONITOR);
    Map<String, GitBranch> branches = client.getBranches(false, NULL_PROGRESS_MONITOR);
    assertEquals(2, branches.size());
    assertNotNull(branches.get(BRANCH_NAME));
    assertEquals(0, repository.getConfig().getSubsections(ConfigConstants.CONFIG_BRANCH_SECTION).size());
    
    // delete branch
    client.deleteBranch(BRANCH_NAME, false, NULL_PROGRESS_MONITOR);
    branches = client.getBranches(false, NULL_PROGRESS_MONITOR);
    assertEquals(1, branches.size());
    assertNull(branches.get(BRANCH_NAME));
}
 
Example #3
Source File: MergeTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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()));
}
 
Example #4
Source File: SetUpstreamBranchTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testRemoteTrackingNoRemoteSet () throws GitException {
    GitClient client = getClient(workDir);
    File f = new File(workDir, "f");
    add(f);
    client.commit(new File[] { f }, "init commit", null, null, NULL_PROGRESS_MONITOR);
    
    // push to remote
    String remoteUri = getRemoteRepository().getWorkTree().toURI().toString();
    client.push(remoteUri,
            Arrays.asList("refs/heads/master:refs/heads/master"),
            Arrays.asList("+refs/heads/*:refs/remotes/origin/*"),
            NULL_PROGRESS_MONITOR);
    Map<String, GitBranch> branches = client.getBranches(true, NULL_PROGRESS_MONITOR);
    assertTrue(branches.containsKey("origin/master"));
    assertNull(branches.get("master").getTrackedBranch());
    
    // set tracking
    GitBranch b = client.setUpstreamBranch("master", "origin/master", NULL_PROGRESS_MONITOR);
    assertEquals("origin/master", b.getTrackedBranch().getName());
    
    Config cfg = repository.getConfig();
    assertEquals(".", cfg.getString(ConfigConstants.CONFIG_BRANCH_SECTION, "master", ConfigConstants.CONFIG_KEY_REMOTE));
    assertEquals("refs/remotes/origin/master", cfg.getString(ConfigConstants.CONFIG_BRANCH_SECTION, "master", ConfigConstants.CONFIG_KEY_MERGE));
}
 
Example #5
Source File: IgnoreTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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)));
}
 
Example #6
Source File: IgnoreTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example #7
Source File: Utils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static GitBranch getTrackedBranch (Config config, String branchName, Map<String, GitBranch> allBranches) {
    String remoteName = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branchName, ConfigConstants.CONFIG_KEY_REMOTE);
    String trackedBranchName = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branchName, ConfigConstants.CONFIG_KEY_MERGE);
    if (trackedBranchName != null) {
        if (trackedBranchName.startsWith(Constants.R_HEADS)) {
            trackedBranchName = trackedBranchName.substring(Constants.R_HEADS.length());
        } else if (trackedBranchName.startsWith(Constants.R_REMOTES)) {
            trackedBranchName = trackedBranchName.substring(Constants.R_REMOTES.length());
        }
    }
    if (trackedBranchName == null) {
        return null;
    } else {
        if (remoteName != null && ".".equals(remoteName)) { //NOI18N
            remoteName = ""; //NOI18N
        } else {
            remoteName = remoteName + "/"; //NOI18N
        }
        return allBranches.get(remoteName + trackedBranchName);
    }
}
 
Example #8
Source File: AddTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #9
Source File: UnignoreTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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)));
}
 
Example #10
Source File: RemoveRemoteCommand.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #11
Source File: GitCloneHandlerV1.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
public static void doConfigureClone(Git git, String user, String gitUserName, String gitUserMail) throws IOException, CoreException, JSONException {
	StoredConfig config = git.getRepository().getConfig();
	if (gitUserName == null && gitUserMail == null) {
		JSONObject gitUserConfig = getUserGitConfig(user);
		if (gitUserConfig != null) {
			gitUserName = gitUserConfig.getString("GitName"); //$NON-NLS-1$
			gitUserMail = gitUserConfig.getString("GitMail"); //$NON-NLS-1$
		}
	}
	if (gitUserName != null)
		config.setString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_NAME, gitUserName);
	if (gitUserMail != null)
		config.setString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_EMAIL, gitUserMail);

	config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE, false);
	config.save();
}
 
Example #12
Source File: RepositoryInfo.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void removeDeletedBranches (Map<String, GitBranch> branches) {
    Set<String> localBranches = new HashSet<>();
    for (Map.Entry<String, GitBranch> b : branches.entrySet()) {
        if (!b.getValue().isRemote() && !b.getKey().equals(GitBranch.NO_BRANCH)) {
            localBranches.add(b.getKey());
        }
    }
    if (!localBranches.equals(cachedBranches)) {
        cachedBranches = localBranches;
        List<String> toRemove = new ArrayList<>(config.getSubsections(ConfigConstants.CONFIG_BRANCH_SECTION));
        boolean save = false;
        for (String branchName : toRemove) {
            if (!localBranches.contains(branchName)) {
                config.unsetSection(ConfigConstants.CONFIG_BRANCH_SECTION, branchName);
                save = true;
            }
        }
        if (save) {
            save();
        }
    }
}
 
Example #13
Source File: GitRemoteHandlerV1.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
private boolean handleDelete(HttpServletRequest request, HttpServletResponse response, String path) throws CoreException, IOException, URISyntaxException,
		JSONException, ServletException {
	Path p = new Path(path);
	if (p.segment(1).equals("file")) { //$NON-NLS-1$
		// expected path: /gitapi/remote/{remote}/file/{path}
		String remoteName = p.segment(0);

		File gitDir = GitUtils.getGitDir(p.removeFirstSegments(1));
		Repository db = null;
		try {
			db = FileRepositoryBuilder.create(gitDir);
			StoredConfig config = db.getConfig();
			config.unsetSection(ConfigConstants.CONFIG_REMOTE_SECTION, remoteName);
			config.save();
			// TODO: handle result
			return true;
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	return false;
}
 
Example #14
Source File: Branch.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@PropertyDescription(name = GitConstants.KEY_REMOTE)
private JSONArray getRemotes() throws URISyntaxException, JSONException, IOException, CoreException {
	String branchName = Repository.shortenRefName(ref.getName());
	String remoteName = getConfig().getString(ConfigConstants.CONFIG_BRANCH_SECTION, branchName, ConfigConstants.CONFIG_KEY_REMOTE);
	List<RemoteConfig> remoteConfigs = RemoteConfig.getAllRemoteConfigs(getConfig());
	ArrayList<JSONObject> remotes = new ArrayList<JSONObject>();
	for (RemoteConfig remoteConfig : remoteConfigs) {
		if (!remoteConfig.getFetchRefSpecs().isEmpty()) {
			Remote r = new Remote(cloneLocation, db, remoteConfig.getName());
			r.setNewBranch(branchName);
			// Ensure that the remote tracking branch is the first in the list.
			// Insert at the beginning of the list as well when the remote tracking branch is not set but the branch has been pushed to the remote
			if (remoteConfig.getName().equals(remoteName) || (remoteName == null && db.resolve(Constants.R_REMOTES + remoteConfig.getName() + "/" + branchName) != null)) {
				remotes.add(0, r.toJSON());
			} else {
				remotes.add(r.toJSON());
			}
		}
	}
	JSONArray result = new JSONArray();
	for (JSONObject remote : remotes) {
		result.put(remote);
	}
	return result;
}
 
Example #15
Source File: RemoteBranch.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
private Ref findRef() {
	try {
		Set<String> configNames = getConfig().getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION);
		for (String configName : configNames) {
			if (configName.equals(remote.getName())) {
				final String fullName = getName(true, false);
				Ref ref = db.getRefDatabase().getRef(fullName);
				if (ref != null && !ref.isSymbolic()) {
					return ref;
				}
			}
		}
	} catch (IOException e) {
		// ignore, return null
	}
	return null;
}
 
Example #16
Source File: GitCloneTest.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testGetCloneAndPull() throws Exception {
	// see bug 339254
	createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
	String workspaceId = getWorkspaceId(workspaceLocation);

	JSONObject project = createProjectOrLink(workspaceLocation, getMethodName().concat("Project"), null);
	String contentLocation = clone(workspaceId, project).getString(ProtocolConstants.KEY_CONTENT_LOCATION);

	JSONArray clonesArray = listClones(workspaceId, null);
	assertEquals(1, clonesArray.length());

	Repository r = getRepositoryForContentLocation(contentLocation);

	// overwrite user settings, do not rebase when pulling, see bug 372489
	StoredConfig cfg = r.getConfig();
	cfg.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER, ConfigConstants.CONFIG_KEY_REBASE, false);
	cfg.save();

	// TODO: replace with RESTful API when ready, see bug 339114
	Git git = Git.wrap(r);
	PullResult pullResult = git.pull().call();
	assertEquals(pullResult.getMergeResult().getMergeStatus(), MergeStatus.ALREADY_UP_TO_DATE);
	assertEquals(RepositoryState.SAFE, git.getRepository().getRepositoryState());
}
 
Example #17
Source File: GitUtils.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns whether the key gerrit.createchangeid is set to true in the git configuration
 * 
 * @param config
 *            the configuration of the git repository
 * @return true if the key gerrit.createchangeid is set to true
 */
public static boolean isGerrit(Config config, String remote) {
	String[] list = config.getStringList(ConfigConstants.CONFIG_REMOTE_SECTION, remote, GitConstants.KEY_IS_GERRIT.toLowerCase());
	for (int i = 0; i < list.length; i++) {
		if (list[i].equals("true")) {
			return true;
		}
	}
	return false;
}
 
Example #18
Source File: AddTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testUpdateIndexIgnoreExecutable () throws Exception {
    if (isWindows()) {
        // no reason to test on windows
        return;
    }
    File f = new File(workDir, "f");
    write(f, "hi, i am not executable");
    File[] roots = { f };
    add(roots);
    commit(roots);
    f.setExecutable(true);
    GitClient client = getClient(workDir);
    StoredConfig config = repository.getConfig();
    config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE, false);
    config.save();
    write(f, "hi, i am executable");
    // 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_MODIFIED, Status.STATUS_NORMAL, Status.STATUS_MODIFIED, 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_MODIFIED, Status.STATUS_MODIFIED, Status.STATUS_MODIFIED, false);
}
 
Example #19
Source File: AddTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testAddKeepExecutableInIndex () 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);
    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);
    
    StoredConfig config = repository.getConfig();
    config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE, false);
    config.save();
    // add should not overwrite executable bit in index
    f.setExecutable(false);
    add(roots);
    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);
}
 
Example #20
Source File: Remote.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public JSONObject toJSON(boolean includeChildren) throws JSONException, URISyntaxException, IOException, CoreException {
	Assert.isLegal(getConfig().getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION).contains(name), NLS.bind("Remote {0} not found.", name));
	if (includeChildren)
		return jsonSerializer.serialize(this, DEFAULT_RESOURCE_SHAPE);
	else
		return jsonSerializer.serialize(this, DEFAULT_RESOURCE_SHAPE_WITHOUT_CHILDREN);
}
 
Example #21
Source File: MergeTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #22
Source File: GitClientCloneTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void test_clone_refspec() throws Exception {
    testGitClient.clone_().url(workspace.localMirror()).repositoryName("origin").execute();

    WorkspaceWithRepo anotherWorkspace = new WorkspaceWithRepo(secondRepo.getRoot(), "git", listener);
    anotherWorkspace.launchCommand("git", "clone", anotherWorkspace.localMirror(), "./");
    anotherWorkspace.getGitClient().withRepository((final Repository realRepo, VirtualChannel channel) -> anotherWorkspace.getGitClient().withRepository((final Repository implRepo, VirtualChannel channel1) -> {
        final String realRefspec = realRepo.getConfig().getString(ConfigConstants.CONFIG_REMOTE_SECTION, Constants.DEFAULT_REMOTE_NAME, "fetch");
        final String implRefspec = implRepo.getConfig().getString(ConfigConstants.CONFIG_REMOTE_SECTION, Constants.DEFAULT_REMOTE_NAME, "fetch");
        assertThat("Refspecs vs. original clone", implRefspec, is(realRefspec));
        return null;
    }));
}
 
Example #23
Source File: StatusTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #24
Source File: InitTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void assertConfig (FileBasedConfig config) throws Exception {
    config.load();
    // filemode
    assertEquals(isWindows() ? "false" : "true", config.getString(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE));
    // bare
    assertEquals("false", config.getString(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_BARE));
    // autocrlf
    assertEquals(null, config.getString(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOCRLF));
}
 
Example #25
Source File: GitRepositoriesPreferencePage.java    From MergeProcessor with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String getText(Object element) {
	if (element instanceof Path) {
		try (final Git repo = Git.open(((Path) element).toFile())) {
			return repo.getRepository().getConfig().getString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin", //$NON-NLS-1$
					ConfigConstants.CONFIG_KEY_URL);
		} catch (IOException e) {
			Logger.getLogger(GitRepositoriesPreferencePage.class.getName()).log(Level.SEVERE, e.getMessage(),
					e);
		}
	}
	return super.getText(element);
}
 
Example #26
Source File: SetUpstreamBranchCommand.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void run () throws GitException {
    Repository repository = getRepository();
    
    try {
        Ref ref = repository.findRef(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);
}
 
Example #27
Source File: AbstractGitTest.java    From onedev with MIT License 5 votes vote down vote up
@Override
protected void setup() {
	gitDir = FileUtils.createTempDir();
	
	try {
		git = Git.init().setBare(false).setDirectory(gitDir).call();
	} catch (IllegalStateException | GitAPIException e) {
		throw new RuntimeException(e);
	}
	
	git.getRepository().getConfig().setEnum(ConfigConstants.CONFIG_DIFF_SECTION, null, 
			ConfigConstants.CONFIG_KEY_ALGORITHM, SupportedAlgorithm.HISTOGRAM);
	
	Mockito.when(AppLoader.getInstance(GitConfig.class)).thenReturn(new GitConfig() {

		private static final long serialVersionUID = 1L;

		@Override
		public String getExecutable() {
			return "git";
		}
		
	});
	
	String gitError = GitCommand.checkError("git");
    Assert.assertTrue(gitError, gitError == null);
}
 
Example #28
Source File: MergeConfig.java    From onedev with MIT License 5 votes vote down vote up
private static String[] getMergeOptions(String branch, Config config) {
	String mergeOptions = config.getString(
			ConfigConstants.CONFIG_BRANCH_SECTION, branch,
			ConfigConstants.CONFIG_KEY_MERGEOPTIONS);
	if (mergeOptions != null) {
		return mergeOptions.split("\\s"); //$NON-NLS-1$
	}
	return new String[0];
}
 
Example #29
Source File: RepositoryInfo.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void setAutoSyncBranch (String branch, boolean autoSync) {
    boolean oldVal = getAutoSyncBranch(branch);
    if (oldVal != autoSync) {
        if (autoSync) {
            config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, branch, CONFIG_AUTO_SYNC, autoSync);
        } else {
            config.unset(ConfigConstants.CONFIG_BRANCH_SECTION, branch, CONFIG_AUTO_SYNC);
            if (config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION, branch).isEmpty()) {
                config.unsetSection(ConfigConstants.CONFIG_BRANCH_SECTION, branch);
            }
        }
        save();
    }
}
 
Example #30
Source File: CreateBranchCommand.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void setupRebaseFlag (Repository repository) throws IOException {
    Ref baseRef = repository.findRef(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();
        }
    }
}