Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

"Exception Cmd('git') failed due to:" - not trigegring try:except #1584

Answered by Byron
rusnackor asked this question in Q&A
Discussion options

Hello guys.

I am preparing scrip that is meant to pull/checkout more repositories in different directories.
However, soemtimes can happen this:

Exception Cmd('git') failed due to: exit code(1)
 cmdline: git pull -v origin
 stderr: 'error: Your local changes to the following files would be overwritten by merge:
 common_logic.py
 git_jobs.py
 svn_jobs.py
 test_updating.py
 updater.py
 updater_core.py
 updating_conf.py
 updating_consts.py
Please commit your changes or stash them before you merge.
Aborting'

But when I try to put my code into try:except block, like this:

import git
try:
 repo = git.Repo("/path/to/my/repo")
 o = repo.remotes.origin
 result = o.pull()
 for item in result:
 print("Item : {}".format(item))
except Exception as e:
 print("Exception {}".format(e))

Nothing changes, the script still throws exception and fails. Is it possible to cover this case, and make it not to crash?

PS: Is it possible to make it, that it does not ask me for credentials every time? (like put them inside of my code)

You must be logged in to vote

Nothing changes, the script still throws exception and fails. Is it possible to cover this case, and make it not to crash?

This would prevent the exception from stopping the program there, maybe there are other exceptions happening later due to the pull not being successful after all? Manually checking for this case would certainly be required to avoid exceptions.

Is it possible to make it, that it does not ask me for credentials every time? (like put them inside of my code)

There is some information about this in the Handling Remotes section of the docs.

Replies: 1 comment 2 replies

Comment options

Nothing changes, the script still throws exception and fails. Is it possible to cover this case, and make it not to crash?

This would prevent the exception from stopping the program there, maybe there are other exceptions happening later due to the pull not being successful after all? Manually checking for this case would certainly be required to avoid exceptions.

Is it possible to make it, that it does not ask me for credentials every time? (like put them inside of my code)

There is some information about this in the Handling Remotes section of the docs.

You must be logged in to vote
2 replies
Comment options

No, there are no other exceptions, or at least, those are not visible:

my_user@my_pc: ~/my_dir/my_subdir $ python3 test_pull.py
1
2
3
4
Username for 'https://git.my_server.eu': myusername
Password for 'https://myusername@git.my_server.eu':
Exception Cmd('git') failed due to: exit code(1)
 cmdline: git pull -v origin
 stderr: 'error: Your local changes to the following files would be overwritten by merge:
 common_logic.py
 git_jobs.py
 svn_jobs.py
 test_updating.py
 updater.py
 updater_core.py
 updating_conf.py
 updating_consts.py
Please commit your changes or stash them before you merge.
Aborting'
my_user@my_pc: ~/my_dir/my_subdir $

I added debug prints after each line. The exception is exactly on pull.

 print(2)
 o = repo.remotes.origin
 print(3)
 #print(repo.untracked_files)
 print(4)
 result = o.pull()
 print(5)
 for item in result:
Comment options

Interesting, I have no clue how a caught exception can still halt a python program.

Answer selected by Byron
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1583 on May 10, 2023 18:03.

AltStyle によって変換されたページ (->オリジナル) /