Can I get the upstream remote branch by ruby-git - ruby

I'm using ruby-git to operate my Git repo. I can get the local branch that checkout from remote branch, how can I get it upstream remote branch? This's the code:
require 'Git'
repo = Git.open("xxxpath.git")
localbranch = repo.branches["localbranchnamexxx"]

The same way you would do it in normal git
remote_branch = repo.branches["origin/localbranchnamexxx"]

Counter-intuitively (to me, at least), the branch tracking information is stored in the git config, not in any branch or ref structures.
require 'git'
repo = Git.open("xxxpath.git")
localbranch = repo.current_branch
upstream_remote = repo.config["branch.#{localbranch}.remote"]
upstream_ref = repo.config["branch.#{localbranch}.merge"]
upstream_branch = upstream_ref.split('/').last
upstream = "#{upstream_remote}/#{upstream_branch}"

Related

Git Python does an branch exist

i need an explanation or example python code to check does an branch exist.
My repo is stored in an GitLab enviorement. I addded an GitLabVariable as an substitute
of the Branchname.
branches = git.Git().branch("--all").split()
if os.environ.get('GitLabVariable') in branches:

GitPython repo.git.checkout not checking out branch correctly

I am using following code to checkout or switch the branch within python code,
repo.git.checkout('branch_name')
But when the code later executes is still referring to 'master' branch code.
I am using GitPython version 2.1.11.
import git
repo = git.Repo("/home/user/.emacs.d")
To checkout a branch:
to see available branches
>>> repo.heads
[<git.Head "refs/heads/master">, <git.Head "refs/heads/straight">]
you can use the branch name like this:
>>> repo.heads.straight.checkout()
<git.Head "refs/heads/straight">
the branch changed to straight
If you want to use git directly
>>> repo.git.checkout("master")
"Your branch is up-to-date with 'origin/master'."
the branch changed to master

Generating release notes from git commits

I've created the following rake task below to generate our release notes for each sprint.
I'm pulling in all commits to master older than 2 weeks.
The problem is when a branch has been developed on for more than 2-week sprints, the older commits won't be included.
Can anyone suggest a way I can get these commits in?
task :new_release_note do
puts "Creating new release note"
puts "..."
git_log = `git log --since="two weeks ago" --no-merges --format=%B`
git_log.gsub!(/^$\n/, '')
git_log.gsub!(/^/, "* ")
current_time = DateTime.now
current_date = current_time.strftime "%Y-%m-%d"
current_date_UK = current_time.strftime "%d-%m-%Y"
template = "__Release Notes__
=======================
#{current_date_UK}
__New Features__
----------------
* -
__Improvements__
----------------
* -
__Fixes__
---------
* -
__Change Log__
----------------
Detailed release notes below, listing all commit messages for this release.
#{git_log}
"
out_file = File.new("./doc/release_notes/release-notes-#{current_date}.md", "w")
out_file.puts(template)
if File.exist?(out_file)
puts "New release note generated successfully at /doc/release-notes/release-notes-#{current_date}.md"
else
puts "Error - file not generated."
end
end
Can anyone suggest a way I can get these commits in?
Few options:
git tag
git notes
git whatchanged
git tag
Read this answer on what is git tag and how to use it: What is git tag, How to create tags & How to checkout git remote tag(s)
In short: git tag allows you to mark commit which can be later on to perform your merge. As you know
git pull = git fetch + git merge
So once you have marked your last merge with the tag you can pull out all the changes form the last merge
# "Merge" the last X commits based upon your previous tag
git cherry-pick <tag_name>..master
git notes
git notes allow us to add content to commit without updating the SHA-1 of the commit, meaning we can attach content to the commit while leaving the SHA-1 unmodified.
Now once you have your notes you can find out the last commit which you "merged" previously and grab the changes from this point on using the above cherry-pick.
You can search and find your notes with git log --grep
git whatchanged
Once you what is your referenced commit you can see the list of files which were updated during this time period with the git whatchanged command
# Print out a list of files which was updated/added between the 2 commits
git whatchanged <TAG_NAME>...HEAD
Consider using git tag and tag your releases with version numbers. What my team does is to create a release branch with a version number for each release i.e. release-2.5.8 and when the release is ready, it gets merged into master. Then we tag that merge commit with a version number i.e. v2.5.8 If you do this, along with squash merges then to see all the related commits it's as easy as doing:
git log v2.5.8...v2.5.9
Which will show you all the commits within those 2 releases.
The reason I recommend squash merging your feature branch is for exactly your use case. You want to know what was done during the dev of that feature, but how can you just by date? You really can't. So when your feature is ready to be merged into your release, if you squash merge, you can keep all the notes in a single commit for the merge of that feature. The idea here is you keep what is relevant and discard what is no longer needed during development.
You might also want to check out Gitflow

Rugged - fetch, pull rebase possible?

Using rugged how do you perform the following operations: fetch, pull and rebase?
I am using the development branch and after reviewing its documentation found here as a guide to the Remote class.
EDIT: Since git pull is just a shorthand for git fetch and git merge FETCH_HEAD the better question is how to perform git fetch, git merge and git rebase.
git fetch:
remote = Rugged::Remote.lookup(repo, "origin")
remote.connect(:fetch) do |r|
r.download
r.update_tips!
end
git merge:
merge_index = repo.merge_commits(
Rugged::Branches.lookup(repo, "master").tip,
Rugged::Branches.lookup(repo, "origin/master").tip
)
raise "Conflict detected!" if merge_index.conflicts?
merge_commit = Rugged::Commit.create(repo, {
parents: [
Rugged::Branches.lookup(repo, "master").tip,
Rugged::Branches.lookup(repo, "origin/master").tip
],
tree: merge_index.write_tree(repo),
message: 'Merged `origin/master` into `master`',
author: { name: "User", email: "example#test.com" },
committer: { name: "User", email: "example#test.com" },
update_ref: 'master'
})
git rebase:
Rebasing was not implemented yet in libgit2, and thus is not available in Rugged.
In general, your use case sounds very high level, while the rugged API is currently a bit more focused on low-level git repository access and modification. Eventually, we'll also have many higher-level helpers (like a more simple/correct pull) in the future, but we're not there yet.
The answer above seems to be outdated. The syntax has changed. I need to implement a pull action which i am trying to do by a fetch and then a merge and commit. For fetching i use the fetch method like this
repo.fetch('origin', [repo.head.name], credentials: credits)
And it seems to actually get something since the returned hash is full with information about what has been fetched. However, it is not written to disk. I would expect the branch to be behind several commits when i do git status in the command line but it is not. If i fetch a second time with the same command above then nothing is fetched. This is probably because it has already been fetched the first time but then i dont see where the fetch is.
Now if i go ahead and do the fetch manually in the command line and then try to merge the local copy of the remote branch and the local branch (local changes are already committed) using the following code
ref_name = repo.head.name # refs/heads/branchname
branch_name = ref_name.sub(/^refs\/heads\//, '') # branchname
remote_name = "#{remote}/#{branch_name}" # origin/branchname
remote_ref = "refs/heads/#{remote_name}" # refs/heads/origin/branchname
local_branch = repository.branches[branch_name]
remote_branch = repository.branches[remote_name]
index = repo.merge_commits(local_branch.target, remote_branch.target)
options = {
author: { time: Time.now }.merge(author),
committer: { time: Time.now }.merge(committer),
message: 'merged',
parents: [
local_branch.target,
remote_branch.target
],
tree: index.write_tree(repository),
update_ref: 'HEAD'
}
Rugged::Commit.create repo, options
It creates the commit as expected. The commit is also written to disk and is visible in the fistory. But for some reason the branch has now uncommitted changes. The local file contents have not changed. I would expect them to have the contents of the fetched commit.
Can anyone please provide a working example for a fetch, merge, commit? The version of rugged at time of writing this is 0.22.0b3
Update 1
This will bring my working tree to the wanted state
repo.checkout ref_name, strategy: :force
Update2
I found out how to fetch and save the state to disk
r = repo.remotes[remote]
r.fetch(credentials: git_credentials)
r.save

git, strange upstream entry which i cant get rid of

if i do
git remote -v
i get
origin https://..../foo.git (fetch)
origin https://..../foo.git (push)
upstream
this last upstream line there prevents me from adding a real upstream which i wanna merge from.
my config file looks like this:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
hideDotFiles = dotGitOnly
[remote "origin"]
url = https://..../foo.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
when i do
git fetch upstream
i get this error:
fatal: 'upstream' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
how can i get rid of that upstream?
Git has several scopes where config is set : system ($(prefix)/etc/gitconfig), global (~/.gitconfig) and local (.git/config of the current repository).
Since there is nothing about upstream in your local scope, you have to check the system and global scopes. It could simply be a write access permission to associated files.

Resources