gitpython not adding, pushing or committing, and no error message - gitpython

I'm trying to add, push and commit using gitpython, to push everything in "C:\users\Blah Blah Blah.git" to my bitbucket acct.
Below is my code:
from git import Repo,remote
PATH_OF_GIT_REPO = r'C:\\users\\Blah Blah Blah'
COMMIT_MESSAGE = 'comment from python script'
repo = Repo(PATH_OF_GIT_REPO)
repo.git.add(update=True)
repo.index.commit(COMMIT_MESSAGE)
origin = repo.remote(name='origin')
origin.push()
Nothing seems to be happening, and I don't get any error messages neither.
I'd appreciate any kind of help :)

Related

Can I get the upstream remote branch by ruby-git

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}"

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

Ruby/Rugged How can I save change after a git fetch?

So, I wanted to perform a git pull with rugged, so I make a fetch and merge like this:
require 'rugged'
certificat = Rugged::Credentials::SshKey.new({username: 'git', privatekey: 'path/to/privatekey', publickey: 'path/to/publickey' })
repo = Rugged::Repository.new("/tmp/git")
repo.checkout('master')
# git fetch
remote = repo.remotes['origin']
remote.fetch(credentials: certificat)
remote.save # save new data
# git merge
commit = repo.branches['origin/master'].target
repo.references.update(repo.head, commit.oid)
But I have this error with the save method:
undefined method `save' for #<Rugged::Remote:0x0000000135d6e8> (NoMethodError)
I don't understand why especially that the save method is in the Rugged doc (here)
Somebody know why ?
EDIT: Ok, so this documentation is outdated, the method save doesn't exist anymore. I think my merge is imcomplete, somebody know ?
EDIT2: I just add this line at the end of this code and it's work !
repo.checkout_head({strategy: :force})

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

Sendgrid/PHP/Heroku Not Working

I added the basic version of SendGrid to Heroku so we could send user-feedback emails from our website. The basic testing implementation I'm using is below:
<?php
/**** Takes posted content from 'contact.html' and sends us an email *****/
require 'sendgrid-php/SendGrid_loader.php';
$sendgrid = new SendGrid('username', 'pwd');
$mail = new SendGrid\Mail();
$mail->
addTo('matthewpolega#gmail.com')->
setFrom('matthewpolega#gmail.com')->
setSubject('another')->
setText('Hello World!')->
setHtml('<strong>Hello World!</strong>');
$sendgrid->
smtp->
send($mail);
header( 'Location: contact.html' );
?>
It works fine in localhost testing. However, it stalls when I test it online. Has anybody experienced a problem like this?
It sounds like you're having some issues with submodules on Heroku. There are two ways you can fix this:
1) Figure out what you did wrong by reading the heroku submodule docs. It's probably as simple as git submodule add path/to/sendgrid
2) Remove the .git directory in the SendGrid module and check it in to your repo:
$ cd ../path/to/sendgrid_lib
$ rm -rf .git/
$ cd ../root/project/dir
$ git add ../path/to/sendgrid_lib
$ git commit -m "Removed SendGrid submodule and added to repo"

Resources