GitPython icon indicating copy to clipboard operation
GitPython copied to clipboard

origin.refs always '[]'

Open callsamleung opened this issue 7 years ago • 12 comments

set this doc: http://gitpython.readthedocs.io/en/stable/tutorial.html#handling-remotes

       empty_repo = git.Repo.init(osp.join(rw_dir, 'empty'))
        origin = empty_repo.create_remote('origin', repo.remotes.origin.url)
        assert origin.exists()
        assert origin == empty_repo.remotes.origin == empty_repo.remotes['origin']
        origin.fetch()                  # assure we actually have data. fetch() returns useful information
        # Setup a local tracking branch of a remote branch
        empty_repo.create_head('master', origin.refs.master)  # create local branch "master" from remote "master"
        empty_repo.heads.master.set_tracking_branch(origin.refs.master)  # set local "master" to track remote "master
        empty_repo.heads.master.checkout()  # checkout local "master" to working tree

origin.refs equals [] so origin.refs.master this will raise error: AttributeError: 'IterableList' object has no attribute 'origin/master'

callsamleung avatar Aug 02 '17 08:08 callsamleung

I cannot reproduce this issue. Can you provide a self-sustaining script that can?

Byron avatar Sep 28 '17 14:09 Byron

thanks reply, It's work now, version GitPython==2.1.7, close this issue plz, Thank you again.

snowleung avatar Sep 29 '17 03:09 snowleung

Hi.. I'm having the same issue here:

When cloning:

repo = git.Repo("my-awesome-project-folder")
o = repo.remotes.origin

It breaks into this error:

2020-04-07 11:53:12,844 - ERROR - 'IterableList' object has no attribute 'origin'

And when I tried to do something with the origin url, it fails also:

try
  repo = git.Repo("my-awesome-project-folder")
  origin = repo.remote('origin')
except Exception as e:
  logger.error(e)

And breaks into Exception:

2020-04-07 11:53:12,892 - ERROR - Remote named 'origin' didn't exist

The proyect has a remote and is called origin. Actually, a tried to create it but I doesnt let me to do it, because origin already exists.

$ git --version
git version 1.8.3.1
$ pip freeze
GitPython==3.0.0

I've been working with my script tons of times before. I manage the repo for my organization, around 700 differents repos. I have this problem with only one project.

Really strange behavior. I hope this comment help to find the bug.

panterozo avatar Apr 07 '20 17:04 panterozo

Thanks for the detailed information, @panterozo . Maybe you will be able to use the debugging tips to get down to the bottom of this. Thanks so much for your consideration.

Byron avatar Apr 11 '20 07:04 Byron

Hi again.. I have being monitoring this behavior, and some times works and some times doesnt. About a previous ask https://github.com/gitpython-developers/GitPython/issues/910#issuecomment-612354380, the anwser is this:

$ cat  .git/packed-refs
# pack-refs with: peeled fully-peeled
0a5bb0272d30f4c8582d1f45b22b9987f977fb1a refs/remotes/origin/Desarrollo-CI

Its seems exactly as you described before.

Another think... I enabled debug mode and I could get some differences, bewteen one project that works and one that doesnt.

This Works

2020-07-09 21:53:37,120 - DEBUG - Popen(['git', 'clone', '-v', 'https://gitlab-ci-token:[email protected]/BECH/microservices/myproject001-ok.git', './projectsGit/myproject001-ok'], cwd=/home/idm/ialvare5/gitlab-python, universal_newlines=True, shell=None, istream=None)
2020-07-09 21:53:38,464 - DEBUG - Cmd(['git', 'clone', '-v', 'https://gitlab-ci-token:[email protected]/BECH/microservices/myproject001-ok.git', './projectsGit/myproject001-ok'])'s unused stdout: Cloning into './projectsGit/myproject001-ok'...
2020-07-09 21:53:38,466 - DEBUG - Popen(['git', 'rev-parse', '--git-path', 'config'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject001-ok, universal_newlines=False, shell=None, istream=None)
2020-07-09 21:53:38,472 - DEBUG - Popen(['git', 'rev-parse', '--git-path', 'config'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject001-ok, universal_newlines=False, shell=None, istream=None)
2020-07-09 21:53:38,476 - INFO - The project 'myproject001-ok' has been cloned
2020-07-09 21:53:38,477 - DEBUG - Popen(['git', 'rev-parse', '--git-path', 'config'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject001-ok, universal_newlines=False, shell=None, istream=None)
2020-07-09 21:53:38,482 - DEBUG - Popen(['git', 'checkout', 'Desarrollo-CI'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject001-ok, universal_newlines=False, shell=None, istream=None)
2020-07-09 21:53:38,518 - DEBUG - Popen(['git', 'cat-file', '--batch-check'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject001-ok, universal_newlines=False, shell=None, istream=<valid stream>)
2020-07-09 21:53:38,523 - DEBUG - Popen(['git', 'rev-parse', '--git-path', 'config'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject001-ok, universal_newlines=False, shell=None, istream=None)
2020-07-09 21:53:38,537 - DEBUG - Popen(['git', 'cat-file', '--batch'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject001-ok, universal_newlines=False, shell=None, istream=<valid stream>)
2020-07-09 21:53:38,544 - DEBUG - Popen(['git', 'rev-parse', '--git-path', 'config'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject001-ok, universal_newlines=False, shell=None, istream=None)
2020-07-09 21:53:38,549 - DEBUG - Popen(['git', 'push', '--porcelain', 'origin'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject001-ok, universal_newlines=True, shell=None, istream=None)
2020-07-09 21:53:42,222 - INFO - The pipeline for 'myproject001-ok' project was successfully updated

This Doesnt Work

2020-07-09 22:00:42,006 - DEBUG - Popen(['git', 'rev-parse', '--git-path', 'config'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject002-nok, universal_newlines=False, shell=None, istream=None)
2020-07-09 22:00:42,011 - DEBUG - Popen(['git', 'rev-parse', '--git-path', 'config'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject002-nok, universal_newlines=False, shell=None, istream=None)
2020-07-09 22:00:42,016 - INFO - The project 'myproject002-nok' has been cloned
2020-07-09 22:00:42,017 - DEBUG - Popen(['git', 'rev-parse', '--git-path', 'config'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject002-nok, universal_newlines=False, shell=None, istream=None)
2020-07-09 22:00:42,022 - DEBUG - Popen(['git', 'checkout', 'Desarrollo-CI'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject002-nok, universal_newlines=False, shell=None, istream=None)
2020-07-09 22:00:42,045 - DEBUG - Popen(['git', 'cat-file', '--batch-check'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject002-nok, universal_newlines=False, shell=None, istream=<valid stream>)
2020-07-09 22:00:42,050 - DEBUG - Popen(['git', 'rev-parse', '--git-path', 'config'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject002-nok, universal_newlines=False, shell=None, istream=None)
2020-07-09 22:00:42,063 - DEBUG - Popen(['git', 'cat-file', '--batch'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject002-nok, universal_newlines=False, shell=None, istream=<valid stream>)
2020-07-09 22:00:42,069 - DEBUG - Popen(['git', 'rev-parse', '--git-path', 'config'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject002-nok, universal_newlines=False, shell=None, istream=None)
2020-07-09 22:00:42,074 - ERROR - Remote named 'origin' didn't exist
2020-07-09 21:53:42,222 - INFO - The project 'myproject001-ok' failed

The only different lines is 2020-07-09 22:00:42,069 - DEBUG - Popen(['git', 'rev-parse', '--git-path', 'config'], cwd=/home/idm/ialvare5/gitlab-python/projectsGit/myproject002-nok, universal_newlines=False, shell=None, istream=None).. for some reason, the first one said git push, but the second one rev-parse.

The complete secuence of my code is as follow:

from git import Repo
import git


def cargarPipeline(self):
        error=""
        try:
                repo2 = self.cloneRepo(self.getProjectObject().http_url_to_repo,self.getNombre())
                if isinstance(repo2, str):
                        if repo2 == "":
                                raise ValueError("An error has occurs by cloning the project '%s' " % self.getNombre())
                        elif repo2.startswith('Error:'):
                                raise ValueError(repo2)

                # Change the controller from Repo to GitPython
                repo = git.Repo("./projectsGit/"+self.getNombre())
                try:
                        repo.git.checkout('Desarrollo-CI')
                except Exception as e:
                        logger.error(e)
                        raise ValueError("Error changing branch 'Desarrollo-CI' for loading pipeline file to project '%s' " % self.getNombre())

                commit_message = 'pipeline: the pipeline was updated'
                if self.getTipoProyecto() == "apps":
                        shutil.copy2('./projectsGit/pipelines/.gitlab-ci-Apps.yml', "./projectsGit/"+self.getNombre()+'/.gitlab-ci.yml')
                        commit_message += " apps"
                else:
                        shutil.copy2('./projectsGit/pipelines/.gitlab-ci-Microservicios.yml', "./projectsGit/"+self.getNombre()+'/.gitlab-ci.yml')
                        commit_message += " ms"
                file_list = [
                        ".gitlab-ci.yml"
                ]
                repo.index.add(file_list)
                repo.index.commit(commit_message)
                origin = repo.remote('origin')
                origin.push()
        except Exception as e:
                logger.error(e)
                error="Error: 1" + str(e)
        else:
                logger.info("The pipeline for '%s' project was successfully updated" % self.getNombre())
                pass
        finally:
                return error

This program is use for uploading a new version of pipeline .gitlab-ci.yml. Maybe, when I change the controller could breaks something?.

The clone function is as follow

def cloneRepo(self, cloneURL, projectName):
        repo=""
        try:
                # Se asocia token
                url = cloneURL.replace("https://","https://gitlab-ci-token:<token>")
                if os.path.exists("./projectsGit/"+projectName):
                        repo = git.Repo("./projectsGit/"+projectName)
                        o = repo.remotes.origin
                        o.pull()
                else:
                        repo = Repo.clone_from(url, "./projectsGit/"+projectName)
        except Exception as e:
                logger.error(e)
                error="Error: " + str(e)
        else:
                logger.info("The project '%s' has been cloned" % projectName)
                pass
        finally:
                return repo

Im looking forward,

Thanks

panterozo avatar Jul 10 '20 02:07 panterozo

@panterozo Thanks for posting, I reopened the issue in the hopes for someone to look into it! On another note, it appears that a gitlab CI token starting with AYxDdSp… leaked - I redacted it and hope you will be able to avert any danger stemming from it being posted here.

Byron avatar Jul 12 '20 02:07 Byron

Hi @Byron.. Thanks by your consern. That token wasn't real. It was put there only to understand what was happening.

panterozo avatar Jul 13 '20 15:07 panterozo

I've had the "Remote named 'origin' didn't exist" error an it was caused by the directory path containing some characters that GitPython didn't like.

repo = git.Repo("my-awesome-project-folder") my-awesome-project-folder might contain characters that GitPython (and other libraries) don't like.

I don't understand much about your particular issue, but thought this could help. Re-cloning my git repo in an other folder fixed the issue for me.

charles-maheu avatar Aug 08 '22 20:08 charles-maheu

Indeed, GitPython has great issues handling paths due to the way Python handles encodings. Maybe there is a working way of doing that these days, but if so GitPython doesn't know about it yet. Or in other words: there are countless issues due to path encoding woes and currently it seems path handling is broken beyond repair.

Byron avatar Aug 09 '22 01:08 Byron

Hi, when I call repo.remotes.origin.refs, I usually get a list of branches e.g. [<git.RemoteReference "refs/remotes/origin/main">], but for another repo, it returns the commit instead [<git.RemoteReference "refs/remotes/origin/b1bd1cxxxxx">]. Do you have any suggestions?

fuhuifang avatar Jun 12 '23 20:06 fuhuifang

I'm seeing the same behavior. refs is empty for one repo, but not another. The output of git remote show origin is very similar for both.

orodbhen avatar Oct 16 '23 19:10 orodbhen

The cause for me ended up being that repo had been cloned with mirror=True. Though git remote show still shows the refs, so it seems like that ought to be listable via the python bindings as well.

orodbhen avatar Oct 17 '23 14:10 orodbhen