How to fetch branches and commits of Git Repo using LibGit2Sharp from network path without a local repository? - libgit2sharp

Using LibGit2Sharp, how can I fetch the list of all Remote Branches given the repo url ?
I do not have a clone of the repo on the local disk. What's the simplest way to get the list of Branches and once I get the list of branches, I'd like to get the list of Commit Id's in a branch of my choice.
I do see a way to do all this if I have a Git Repo cloned on my local disk but, I don't want to clone it for obtaining this info.

Using LibGit2Sharp, how can I fetch the list of all Remote Branches given the repo url ?
I do not have a clone of the repo on the local disk. What's the simplest way to get the list of Branches
Through LibGit2Sharp, one can read information about the references (mostly branches and tags) of a remote repository, without cloning it. This will retrieve the name of those references and the sha of the commit each one points to.
This can be done through the IEnumerable<Reference> ListRemoteReferences(string url) static method. This is the equivalent of the git ls-remote CLI command.
once I get the list of branches, I'd like to get the list of Commit Id's in a branch of my choice.
Unfortunately, ListRemoteReferences() will only return the tip of each branch. Would you need to enumerate the other commits referenced by this branch, only using LibGit2Sharp, you'll have to locally clone the remote repository.
As an alternative, rather than cloning, once you've got the SHAs of the tips, you could leverage the Commit GitHub API and for each commit, retrieve its parent(s), reapplying recursively the API invocation for each of them. Note that this last option will be far less performant (network I/O, API rate limitting, ...).

Related

Jenkins multi branch pipeline to create a pr in one repo upon a merge to master in another

I have created a job with access to two repos, read access for A and write access for B. Upon a successful merge to master of a pr in repo A, I want a job to be triggered that adds the hash of the successful PR from repo A to repo B/file.yaml and creates a PR in repo B for this addition.
I understand the basic of getting the job to trigger based on a successful merge. I am stuck on how to get the job to change a yaml file and create a pr based on this change.
Once your second build triggers, you have to do is checkout repo B. You probably also want to create a new branch (you can use git commands for all this) and append your hash to the file. The easiest way to do that is to echo the line:
sh "echo $COMMIT_HASH >> file.yaml"
Then push the code to your branch. Once the code is in the branch, you can create the PR, but how you do this depends on your Git host. Most popular hosts (Bitbucket, GitLab, GitHub) have API endpoints you can send POST requests to with the branch name. When the API gets the request, a PR is created. For example, this is how you do it with Bitbucket: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-post.
If you are using a somewhat obscure Git host that doesn't have an API then you might be in a bit of trouble.

How to get Bitbucket/Git URL for a specific branch?

I use bitbucket but I need to pull a specific branch using the git url.
My git url look like:
https://bitbucket.com/scm/<project_name>/<repo_name>.git
I need to pull specific branch though so I tried:
https://bitbucket.com/<username>/scm/<project_name>/<repo_name>/branch/<branch_name>
https://bitbucket.com/<project_name>/<repo_name>/branch/<branch_name>
https://bitbucket.com/scm/<project_name>/<repo_name>/branch/<branch_name>
https://bitbucket.com/scm/<project_name>/<repo_name>.git#<branch_name>
I keep getting errors like 501 or not found
The goal is to connect my repository to Informatica https://docs.informatica.com/data-integration/powercenter/10-4-0/application-service-guide/model-repository-service/version-control-for-the-model-repository-service/configure-and-synchronize-a-model-repository-after-changing-vers.html
I'm not 100% sure I understand what you are trying to do.
If you have already cloned your repo, you can pull a specific branch using the url instead of the name of your remote.
# You normally pull with this command:
git pull origin my_branch
# You can pull using the URL instead:
git pull bitbucket.com/scm/<project_name>/<repo_name>.git my_branch
If you have not yet cloned your repo, you can clone a specific branch:
git clone --branch my_branch bitbucket.com/scm/<project_name>/<repo_name>.git
I need to pull specific branch though so I tried:
Those all seem like reasonable ways to do it for http urls, Git uses a way that doesn't depend on any specific server's url format
git remote add somename u://r/l -t branch
and you can repeat the -t branch option as many times as there are branches you'd like to track, or just let it default to all branches.
If you don't want tags, add --no-tags. I usually don't for sideband repos like this, I count it as a minor wart, probably too late or anyway not worth it to fix, that git remote add doesn't default to this for any remote not named origin.
Then
git fetch somename
brings the tracking refs up to date.

Difference between lightweight checkout and shallow clone in Jenkins

In the pipeline SCM configuration of Jenkins job builder, we have two options- lightweight checkout and shallow clone. What is the difference between these options and when do we use each option?
From the documentation:
Shallow clone.
Perform a shallow clone, so that git will not download the history of the project, saving time and disk space when you just want to access the latest version of a repository.
Lightweight checkout.
If selected, try to obtain the Pipeline script contents directly from the SCM without performing a full checkout. The advantage of this mode is its efficiency; however, you will not get any changelogs or polling based on the SCM. (If you use checkout scm during the build, this will populate the changelog and initialize polling.) Also build parameters will not be substituted into SCM configuration in this mode. Only selected SCM plugins support this mode.
To sum up:
Shallow Clone is the Git feature that lets you pull down just the latest commits, not the entire repo history. So if your project has years of history, or history from thousands of commits, you can select a particular depth to pull.
Lightweight checkout is a Jenkins capability that enables to pull a specific file from the repo, as opposed to the entire repo. So it is useful for example when fetching the Jenkinsfile from a repo because you you need only the specific file and Don't care about other SCM information.

Using git pull properly

I am new to git so please bear with me. I have a rails application on my local machine that I am experimenting with and pushing to the master branch periodically. It works at the moment, but I have fallen behind, and now I am many commits behind the master.
$ git branch
* master
$ git status
On branch master
Your branch is behind 'origin/master' by 27 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean
However, when I use git pull and then start rails, my application breaks with a precompiler error. So I am forced to use git --reset to go back to the local commit before I used git pull.
What is the right way to get around this issue and merge with the latest changes on the master branch? Would one use git --rebase in this case?
Try
git stash
git pull origin master
And once it updates, git stash apply to reapply your local changes
Since nobody has stated this clearly yet: You ask
What is the right way to get around this issue and merge with the latest changes on the master branch?
When you do git pull that does merge the remote changes into your current branch. Whether you would choose to do a rebase instead of a merge (per your other question) is a separate issue, but the default behavior is to combine the two sets of changes (local and remote).
More precisely, by default git pull does a fetch followed by a merge. The exact merge operation depends on configuration and on command-line options, but in a typical configuration where origin/master is upstream of master, saying
git pull
will merge origin/master into master.
So why the errors?
One possibility is that there were merge conflicts. If that happens, git will tell you. If you say git status in this condition, it will tell you that there's a merge in progress and it will indicate which paths (files) need conflict resolution.
Another possibility is that the changes don't conflict (in that they don't affect the same region of the same file) but still don't work properly together. That you would simply have to debug.

Git-Tfs: Pull a TFS changeset into existing git repository

I've got a git repository that was migrated from a TFS repository. There is currently no link between the repositories.
If I use git tfs list-remote-branches {http://...TFSRepo}, I can see the branches in TFS.
What I want to do is be able to pull a TFS changeset into my current git repository -- preferably as a commit, but I'd take just pulling the changes into the current branch.
quick-clone doesn't do what I need as it creates a new git repository (buried in my existing git repository...)
Is is possible to perform a pull of a single TFS changeset using the git-tfs extensions?
There is no way to pull only one changeset in an existing git repository that is not a git-tfs repository.
But you've got 2 possibilities to achieve your goal...
The first is to do a quick-clone in a new repository and then add this repository has a remote and fetch the commit you want.
The second is more tricky, less sure to work because you must understand how git-tfs works.
Amend the last commit to add at the end of the commit message something like that:
git-tfs-id: [https://tfs.codeplex.com:443/tfs/TFS16]$/valtechgittfs/BugMerge;C31467
with :
git-tfs-id: [https://url.tfs.server:/tfs/TeamCollection]$/ProjectName/BranchName;CchangesetId
where you specify as the changeset id, the one of the last changeset before the one you want to fetch. Then, if you have at least the v0.20 of git-tfs, then, fetch changesets...

Resources