How to set the upstream branch for new local branch with JGit - eclipse

I use the following code to create a new branch from origin/master:
CreateBranchCommand branchCmd = git.branchCreate();
branchCmd.setForce(true);
branchCmd.setName("newFeatureBranch");
branchCmd.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);
branchCmd.setStartPoint("origin/master");
branchCmd.call();
After running this code I get a new local branch with the following upstream:
But that is not what I want. Its upstream should be set to refs/heads/newFeatureBranch:
How do I set the upstream in the new branch?

Related

Jenkins - tagging a build fails with NoHeadException

When I try to tag a specific build through Jenkins, I get the following error:
ERROR: Error tagging repo 'refs/remotes/origin/master' :
org.eclipse.jgit.api.errors.NoHeadException: Tag on repository without
HEAD currently not supported hudson.plugins.git.GitException:
org.eclipse.jgit.api.errors.NoHeadException: Tag on repository without
HEAD currently not supported at
org.jenkinsci.plugins.gitclient.JGitAPIImpl.tag(JGitAPIImpl.java:509)
at
hudson.plugins.git.GitTagAction$TagWorkerThread.perform(GitTagAction.java:199)
at hudson.model.TaskThread.run(TaskThread.java:129) Caused by:
org.eclipse.jgit.api.errors.NoHeadException: Tag on repository without
HEAD currently not supported at
org.eclipse.jgit.api.TagCommand.call(TagCommand.java:137) at
org.jenkinsci.plugins.gitclient.JGitAPIImpl.tag(JGitAPIImpl.java:507)
... 2 more Trying next branch Completed
When trying to tag in the workspace it works fine, HEAD is in fact attached, git refs look fine, could this be an issue that when Jenkins is trying to tag it is looking in the wrong working directory?
Is there a way to pull more verbose logs with how it's trying to tag?
FYI - using the Jenkins 2.81, and swarm Linux agents, latest Git plugin.
Consider the actual code throwing the exception:
try (RevWalk revWalk = new RevWalk(repo)) {
// if no id is set, we should attempt to use HEAD
if (id == null) {
ObjectId objectId = repo.resolve(Constants.HEAD + "^{commit}"); //$NON-NLS-1$
if (objectId == null)
throw new NoHeadException(
JGitText.get().tagOnRepoWithoutHEADCurrentlyNotSupported);
Double-check your configuration: see "Jenkins Git plugin detached HEAD". You need to make sure Jenkins actually does checkout a branch.
Try for instance to add a simple test step with a git status in it, to validate that.

LIbGit2Sharp: list all the pull request's authors

Using the LibGit2Sharp library, I'm trying to list all authors of pull requests on the master branch of a repo. I don't see anything in the docs, intellisense, or through search that has an example of this. Any pointers??
I have constructed the code below, but I'm getting the following error message An unhandled exception of type 'LibGit2Sharp.RepositoryNotFoundException' occurred in LibGit2Sharp.dll. Browsing around, it seems that the references I find are to local cloned repos and not remote repositories.
static void Main(string[] args)
{
var co = new CloneOptions();
co.CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials
{
Username = "username",
Password = "password"
};
var clonedRepoPath = Repository.Clone(url, "path/to/clone", co);
using (var repo = new Repository(clonedRepoPath))
{
foreach (Commit commit in repo.Commits)
{
Console.WriteLine(commit.Author.Name);
}
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}
}
}
}
...list all the pull requests ......
First of all, “pull requests” are a DVCS workflow method, and are not a feature of git. Most people inherently, and incorrectly, think it is a part of git. Github.com (and others) have a pull request workflow system that includes items such as a git merge, topic discussion, continuous integration (CI) hooks, issue referencing, user permissions, etc.. with ONLY the git merge being actually from the git DVCS.
That said, within a git repository, Github-style pull requests are merges between two commit-ishs (usually merging from topic branch to a master branch, but this is not a requirement) and thus the 'pull request' commit have two parents.
FYI: For merges that have three(+) parents, see this answer
So back to your question:
list the authors of all the pull requests on the master branch of a repo
That statement becomes the following git cmd:
git log master --merges --pretty=format:"%an %s" becomes:
In translating that to libgit2sharp:
// find the master branch in the repo
var masterBranch = repo.Branches.Single (branch => branch.FriendlyName == "master");
// Filter the branch's commits to ones that are merges
var mergeList = masterBranch.Commits.Where (p => p.Parents.Count () >= 2);
// Display the merge commits (pull requests)
foreach (Commit commit in mergeList)
{
Console.WriteLine("{0}\t{1}", commit.Author.Name, commit.MessageShort);
}
Example output of a github repo that uses pull requests:
João Matos Merge pull request #1966 from angeloc/master
Zoltan Varga Merge pull request #1965 from akoeplinger/fix-flaky-test
João Matos Merge pull request #1963 from angeloc/patch-1
Rodrigo Kumpera Merge pull request #1912 from ludovic-henry/threadpool-managed-asyncresult
Zoltan Varga Merge pull request #1959 from alexrp/master
Zoltan Varga Merge pull request #1958 from rolfbjarne/aot-error-reporting
Marek Safar Merge pull request #1955 from LogosBible/servicepoint_nre
...
Update:
Based upon the comment, libgit2sharp is not going to give the user what they want, you need to use the Github api.
Using Github Api via the Octokit library (you can directly make the Github REST calls or use another lib.), you can request all the open pull requests fairly easily:
public static async Task getPullRequests ()
{
var client = new GitHubClient (new ProductHeaderValue ("PlayScript"));
// Login Credentials if you need them for an enterprise acct/repo
// client.Credentials = GithubHelper.Credentials;
var connection = new Connection (new ProductHeaderValue ("PlayScript"));
var api = new ApiConnection (connection);
var pullrequests = new PullRequestsClient (api);
pulls = await pullrequests.GetAllForRepository ("PlayScriptRedux", "playscript");
}
....
Task.WaitAll(getPullRequests());
foreach (var pullrequest in pulls) {
Console.WriteLine (pullrequest.IssueUrl);
}
That would list one open pull request for my playscript repo under the PlayScriptRedux organization, i.e. console output:
https://api.github.com/repos/PlayScriptRedux/playscript/issues/89
Look at the Octokit pull request test fixture for more info
Also review the Github pull requests api info

EGit: cannot push, cannot pull

I'am using EGit and I commited a change to my local git repository.
I'd like to push this change to a remote.
When doing that, a dialog screen pops up which shows "rejected-master-master-non-fast-forward". The answer of this linked question states that I have to pull first.
When doing the pull, an EGit exception is thrown:
org.eclipse.jgit.api.errors.TransportException: Nothing to fetch.
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:139)
at org.eclipse.jgit.api.PullCommand.call(PullCommand.java:253)
at org.eclipse.egit.core.op.PullOperation$1.run(PullOperation.java:97)
at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2344)
at org.eclipse.egit.core.op.PullOperation.execute(PullOperation.java:128)
at org.eclipse.egit.ui.internal.pull.PullOperationUI.execute(PullOperationUI.java:139)
at org.eclipse.egit.ui.internal.pull.PullOperationUI$1.runInWorkspace(PullOperationUI.java:114)
at org.eclipse.core.internal.resources.InternalWorkspaceJob.run(InternalWorkspaceJob.java:38)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
Caused by: org.eclipse.jgit.errors.TransportException: Nothing to fetch.
at org.eclipse.jgit.transport.Transport.fetch(Transport.java:1087)
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:130)
... 8 more
It seems that I'm stuck. Who can help me out?
UPDATE
The .git/config file in my repository contains (remote URL hid):
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
[branch "master"]
remote = origin
merge = refs/heads/master
[remote "origin"]
url = <URL_HIDDEN>
I'm using Eclipse Git Team Provider 3.4.1.201406201815
The configuration of the repository seems to miss this line in the [remote "origin"] section:
fetch = +refs/heads/*:refs/remotes/origin/*
It tells git to fetch all refs starting with refs/heads/ (all branches) and store them under refs/remotes/origin/ locally. E.g. the refs/heads/master in the remote repository will become refs/remotes/origin/master locally (or origin/master in short).
Can you add that and try if it makes pull work?
Also, it would be very interesting to know in which way you first created/cloned this repository.
I also faced the same problem when using egit and could not pull the changes. But previous answer helped a lot. Now, there can be two ways to update the config file.
Direct Update in the file : Goto local_repo_location/.git/ and open config file and add :
[remote "origin"]
url = YOUR_REPO_URL
push = ALREADY_FILLED_VALUE
fetch = +refs/heads/:refs/remotes/origin/
Updating config using eclipse : Goto Windows -> Preferences -> Team -> Git -> Configuration and now under Repository Settings tab look under -> remote -> origin.
Problem is there is no key for fetch.
Now click : Add Entry and provide the values as follows :
Key : remote.origin.fetch
Value : +refs/heads/:refs/remotes/origin/
Now we are done.
i had the same problem pulling from remote repo was giving error (org.eclipse.jgit.api.errors.transportexception)
here's what i did
changed the origin to my fork account and pulled.
changed the origin back to the actual value and pulled.
donot really know about the problem of egit but this two simple steps solved my problem, hope it helps

FATAL: Invalid id: Process leaked file descriptors - Jenkins (github)

I am getting following error in jenkins with github.
Using strategy: Default
Last Built Revision: Revision 8d7d3f0898bc4583a80848033d6e0d27cc3e2096 (origin/master, origin/HEAD)
Fetching changes from 1 remote Git repository
Fetching upstream changes from origin
Seen branch in repository origin/HEAD
Seen branch in repository origin/master
Seen branch in repository origin/svn
Seen 3 remote branches
Commencing build of Revision 8d7d3f0898bc4583a80848033d6e0d27cc3e2096 (origin/master, origin/HEAD)
Checking out Revision 8d7d3f0898bc4583a80848033d6e0d27cc3e2096 (origin/master, origin/HEAD)
FATAL: Invalid id: Process leaked file descriptors. See http://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build for more information
java.lang.IllegalArgumentException: Invalid id: Process leaked file descriptors. See http://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build for more information
at org.eclipse.jgit.lib.ObjectId.fromString(ObjectId.java:232)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.doRevList(CliGitAPIImpl.java:959)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.revList(CliGitAPIImpl.java:945)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.isCommitInRepo(CliGitAPIImpl.java:970)
at hudson.plugins.git.GitAPI.isCommitInRepo(GitAPI.java:181)
at hudson.plugins.git.GitSCM.computeChangeLog(GitSCM.java:1292)
at hudson.plugins.git.GitSCM.access$1300(GitSCM.java:58)
at hudson.plugins.git.GitSCM$4.invoke(GitSCM.java:1257)
at hudson.plugins.git.GitSCM$4.invoke(GitSCM.java:1211)
at hudson.FilePath.act(FilePath.java:909)
at hudson.FilePath.act(FilePath.java:882)
at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1211)
at hudson.model.AbstractProject.checkout(AbstractProject.java:1408)
at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:676)
at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:88)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:581)
at hudson.model.Run.execute(Run.java:1603)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
at hudson.model.ResourceController.execute(ResourceController.java:88)
at hudson.model.Executor.run(Executor.java:247)
Normally it works fine during the day and when I start my system again on next day, it gives this error and can only be resolved by re-installing the github in Jenkins. The link in the error message says about Spawning process from build but I don't understand what does that mean (I run 1-2 builds only; on windows).
Thanks.

Jenkins pulling source code from GitHub private repo

I'm trying to set Jenkins up for my project in my server.
I uploaded my build script in GitHub private repo. I've established a SSH key, and deployed it through GitHub.
But when I try to build, Jenkins fails to pull successfully from my private repo
here is the console output:
Started by user anonymous
Building in workspace /Users/Shared/Jenkins/Home/jobs/moai-test/workspace
Checkout:workspace / /Users/Shared/Jenkins/Home/jobs/moai-test/workspace - hudson.remoting.LocalChannel#15e6e48b
Using strategy: Default
Fetching changes from 1 remote Git repository
Fetching upstream changes from https://github.com/****/****.git
ERROR: Problem fetching from origin / origin - could be unavailable. Continuing anyway
hudson.plugins.git.GitException: Error performing command: git fetch -t https://github.com/****/****.git +refs/heads/*:refs/remotes/origin/*
at hudson.plugins.git.GitAPI.launchCommandIn(GitAPI.java:862)
at hudson.plugins.git.GitAPI.launchCommand(GitAPI.java:817)
at hudson.plugins.git.GitAPI.fetch(GitAPI.java:197)
at hudson.plugins.git.GitAPI.fetch(GitAPI.java:1063)
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:812)
at hudson.plugins.git.GitSCM.access$100(GitSCM.java:90)
at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:1096)
at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:1064)
at hudson.FilePath.act(FilePath.java:842)
at hudson.FilePath.act(FilePath.java:824)
at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1064)
at hudson.model.AbstractProject.checkout(AbstractProject.java:1256)
at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:589)
at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:88)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:494)
at hudson.model.Run.execute(Run.java:1502)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
at hudson.model.ResourceController.execute(ResourceController.java:88)
at hudson.model.Executor.run(Executor.java:236)
Caused by: java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at java.lang.UNIXProcess.waitFor(UNIXProcess.java:115)
at hudson.Proc$LocalProc.join(Proc.java:319)
at hudson.Launcher$ProcStarter.join(Launcher.java:345)
at hudson.plugins.git.GitAPI.launchCommandIn(GitAPI.java:843)
... 18 more
ERROR: Could not fetch from any repository
FATAL: Could not fetch from any repository
hudson.plugins.git.GitException: Could not fetch from any repository
at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:1103)
at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:1064)
at hudson.FilePath.act(FilePath.java:842)
at hudson.FilePath.act(FilePath.java:824)
at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1064)
at hudson.model.AbstractProject.checkout(AbstractProject.java:1256)
at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:589)
at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:88)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:494)
at hudson.model.Run.execute(Run.java:1502)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
at hudson.model.ResourceController.execute(ResourceController.java:88)
at hudson.model.Executor.run(Executor.java:236)
Looking at your build log
Using strategy: Default
Fetching changes from 1 remote Git repository
Fetching upstream changes from https://github.com/****/****.git
ERROR: Problem fetching from origin / origin - could be unavailable. Continuing anyway
You are using the HTTPS URI for the github repo.
The HTTPS URI uses username/password to authenticate and not the SSH key.
To use the SSH key you need to checkout the SSH URI instead, e.g.
git#github.com:****/****.git
And that should fix your issue.
Alternatively you could give Jenkins the username/password (though that is a bigger security risk)