Stvad / CrowdAnki

Plugin for Anki SRS designed to facilitate cooperation on creation of notes and decks.
MIT License
520 stars 44 forks source link

Crashes when importing from git repository #161

Closed AlexFicachi closed 2 years ago

AlexFicachi commented 2 years ago

I'm unable to import the following git repo in more than one of my profiles: https://github.com/devaccelerator/knowledge.

I made a new profile and I was able to import it in that new profile without it crashing though.

Here is the error output from Anki:

Error
An error occurred. Please start Anki while holding down the shift key, which will temporarily disable the add-ons you have installed.
If the issue only occurs when add-ons are enabled, please use the Tools > Add-ons menu item to disable some add-ons and restart Anki, repeating until you discover the add-on that is causing the problem.
When you've discovered the add-on that is causing the problem, please report the issue on the add-on support site.
Debug info:
Anki 2.1.49 (dc80804a) Python 3.8.6 Qt 5.14.2 PyQt 5.14.2
Platform: Mac 10.16
Flags: frz=True ao=True sv=2
Add-ons, last update check: 2022-01-01 06:22:19
Add-ons possibly involved: ⁨CrowdAnki JSON exportimport Edit history Collaborate on deck creation⁩

Caught exception:
Traceback (most recent call last):
  File "/Users/ficachi/Library/Application Support/Anki2/addons21/1788670778/anki/ui/action_vendor.py", line 35, in <lambda>
    lambda: GitImporter.on_git_import_action(self.window.col))
  File "/Users/ficachi/Library/Application Support/Anki2/addons21/1788670778/github/github_importer.py", line 28, in on_git_import_action
    GitImporter(collection).import_from_git()
  File "/Users/ficachi/Library/Application Support/Anki2/addons21/1788670778/github/github_importer.py", line 33, in import_from_git
    self.clone_repository_and_import(repo_url)
  File "/Users/ficachi/Library/Application Support/Anki2/addons21/1788670778/github/github_importer.py", line 39, in clone_repository_and_import
    porcelain.pull(str(repo_local_path), repo_url)
  File "/Users/ficachi/Library/Application Support/Anki2/addons21/1788670778/dist/dulwich/porcelain.py", line 1182, in pull
    check_diverged(r, r.refs[rh], fetch_result.refs[lh])
  File "/Users/ficachi/Library/Application Support/Anki2/addons21/1788670778/dist/dulwich/porcelain.py", line 286, in check_diverged
    raise DivergedBranches(current_sha, new_sha)
dulwich.porcelain.DivergedBranches: b'c981f850a229b1ea5f3ee682eb8a3fc875f175f6

Let me know if you need any more details or help from my end? Thanks! :)

aplaice commented 2 years ago

Thanks very much for the bug report and sorry for the issue!

tldr; This is a simple git issue that CrowdAnki can't deal with atm, but that can be dealt with command-line git (or, if you prefer, any decent git GUI).


Diagnosis

As (probably unnecessary) background: CrowdAnki (logically) uses* the same local git repo for import and for snapshotting (it's placed at the "Snapshot Path" (see Tools > Addons > CrowdAnki > Config > Snapshot Path)) — i.e. when you do a git import, CrowdAnki pulls to that local git repo and when you do a snapshot, CrowdAnki commits to that local git repo.

* depending on configuration — I assume you have knowledge as one of your snapshot root decks?

The issue (AFAICT) is that this local git repo now contains commits (on its main branch) that aren't in the remote. Embarrassingly, it seems that CrowdAnki doesn't even deal with a situation where the local git repo is simply ahead of the remote (i.e. remote/main is an ancestor of local/main), where it shouldn't do anything. (It also doesn't cope with a situation where remote/main and local/main are actually diverged — i.e. neither is an ancestor of the other — and the backtrace won't distinguish between the two situations.)

Workaround

Navigate to the snapshot path (SNAPSHOT_PATH/PROFILE_NAME/knowledge (where the SNAPSHOT_PATH is as described above) in a terminal and check what precisely is the issue (whether local/main is just ahead of remote/main or whether they've diverged):

git log --graph main origin/main 

If main is simply ahead of origin/main then just push the local changes to the remote (https://github.com/devaccelerator/knowledge).

If the two branches have diverged, then merge them (or rebase origin/main on top of the local main — depending on your personal git preferences), push that to the remote and then do a CrowdAnki git import.

(You could also use a git GUI.)

I hope that this works and helps! If it doesn't or something is unclear, please ask!

aplaice commented 2 years ago

CrowdAnki fix

As mentioned above, there are two possible (related issues):

A. The local and remote branches have genuinely diverged (neither is an ancestor of the other). B. The local branch is ahead of the remote branch (remote is a direct ancestor of local).

Dealing with A properly will require turning CrowdAnki into a proper(-ish) git porcelain/front-end. However, dealing with B should be straightforward (do nothing — possibly just inform the user).

In other words, when CrowdAnki pulls from a git remote into an existing local/snapshot git repo, there are three possibilities:

  1. remote is ahead of local.

    In this case, CrowdAnki correctly updates local to remote.

  2. local is ahead of remote

    Currently, CrowdAnki crashes.

    IMO the "right thing" would be to leave local alone (do nothing), and inform the user that local is ahead of remote.

    For comparison git pull in this situation just says "Already up-to-date". IMO we, unlike git, should inform the user, in greater detail, since CrowdAnki does commits on the user's behalf when snapshotting, so they might not be aware what's happening.

    Would a message like:

    "Note that your local deck {deck} contains changes that aren't present in the remote git repository. Consider contributing them upstream (see https://github.com/Stvad/CrowdAnki/blob/master/README.md#how-to-collaborate-via-github for suggestions how)."

    be sufficiently end-user friendly (for users who might not be familiar with git)?

  3. local and remote have diverged

    Currently, CrowdAnki crashes.

    Ideally, we'd like to carry out some sort of automated merging, but doing this correctly will be tricky, so dealing with this is more of a long-term goal.

    However, in the short term we could at least avoid crashes and provide a pointer to somewhere (a wiki page?) that would describe how the user could manually resolve the issue.

AlexFicachi commented 2 years ago

Thanks for the info! To provide more details about my specific situation, Anki was crashing on an Anki profile that did not have access to the remote repo I was importing from. So the git log --graph main origin/main command didn't work for my situation. I ended up deleting the folder that contained the git branch for the deck I was trying to update and then I imported from the git repository again. It seemed to work after that since there was no branch it had to merge with, it just created a new one.