Skip to content

Fix 100725: Allow empty repositories to be synced #114163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

plainerman
Copy link
Contributor

This PR fixes #100725 and allows VSCode to properly sync git repositories that have been cloned before there were any commits.

Reproducing and Testing

To test it, I have attached a zip file with an empty git repository that now has commits (i.e. should be syncable).

  1. Download and unpack this repository empty.zip.
    If you feel uncomfortable downloading files from strangers, see below for git commands to simulate this state.
  2. Open the folder in VSCode.
  3. With the current version of VSCode (as seen in the gif in the issue Git - git sync issue on an empty repo #100725 (comment)), the git repository cannot be synced. Manually executing the command git pull works just fine.
    With the proposed fix, clicking the sync button works and syncs the repository as intended.

Proposed Changes

As described in the issue (#100725), the root cause is that the command used to fetch the upstream (git rev-parse) fails on an empty repository. This then causes this unexpected behavior.

I propose the following changes to resolve the issue:

extensions/git/src/git.ts method: getBranch(...)

  1. Wrap fetching the latest commit in a try-catch
    Although the commit cannot be fetched for an empty repository, without this catch the function is exited. The commit should be left undefined, while still properly fetching the upstream.
  2. Fetch the upstream via the git.config
    Once a branch has an upstream, it will be stored in the git config. Unfortunately, this command uses an older syntax of git, so we have to manually construct the full upstream path. See this stackoverflow post for more information about this section of the git config.

extensions/git/src/statusbar.ts method: command()
3. Do not require commit for syncing
Syncing a repository does not require a commit, whereas publishing does (git does not allow pushing branches without a history).
In combination with the proposed changes above, this makes the sync button clickable.


You might feel uncomfortable changing from git-rev to git-config in all cases. Although I strongly believe that this should not be a problem and did not cause any issues whatsoever in my tests: If you feel so, I am open for discussion. In the worst case, I can revert the last commit and we use git-config as a fallback. This can be seen in this commit: 4f3d47c
Personally, I would prefer the current changes.

Simulate a freshly cloned empty git repo

mkdir empty
cd empty
git init

# Add some origin, you can use any repo, but this is very small
git remote add origin https://github.com/plainerman/empty.git

# Now track master as described in here https://stackoverflow.com/a/625460/4417954
git config branch.master.remote origin
git config branch.master.merge refs/heads/master

@eamodio
Copy link
Contributor

eamodio commented Jan 14, 2021

I'm definitely hesitant to change the default behavior, but I think its probably a decent fallback. I've started a discussion for this here: git-for-windows/git#2981 to see what any Git maintainers might think.

@plainerman
Copy link
Contributor Author

@eamodio, with the answer from @derrickstolee in git-for-windows/git#2981 should I revert the last commit and add git-config as fallback?
Or would you like to wait for other responses / further evaluation?

@eamodio
Copy link
Contributor

eamodio commented Jan 14, 2021

Yeah I think we should add it as a fallback.

@plainerman
Copy link
Contributor Author

I have reverted the commit and git-config is the fallback behavior now.

Somehow, the windows tests are now failing. I believe that this is not related to my changes (other PRs are also failing). Maybe you could take a quick look at it.

Are there any other changes I should include? Or is this PR good to go (once the pipeline succeeds)?

@plainerman
Copy link
Contributor Author

I am looking forward to merging this PR. @eamodio, have you already had the time to look at my questions?
Thank you very much.

@plainerman
Copy link
Contributor Author

@eamodio I hope you are doing well.
I have just resolved the merge conflicts that came up. From my side, the PR is now ready again and I am looking froward to your review. Any updates?

@plainerman
Copy link
Contributor Author

@eamodio
I was wondering whether we could assign this to the February Milestone?
Thank you

@plainerman plainerman changed the base branch from master to main February 15, 2021 07:21
@plainerman
Copy link
Contributor Author

@joaomoreno, I haven't heard from @eamodio in a while (probably he is busy).
Since you were also involved in the issue, I was wondering whether we could discuss this implementation or if you know anything about @eamodio?

@plainerman
Copy link
Contributor Author

@joaomoreno, @eamodio I hope you are doing well.
I haven't heard back from you in quite some time now. I would love to get a code review and eventually merge this PR.
Is there anything I can do to help? Or can you give me an estimate on when you will have time?

Thank you very much

@joaomoreno
Copy link
Member

@plainerman I understand how frustrating it might be to just see your work pending here. But I have to ask for patience. We have no estimate right now. We're just busy with many other tasks right now. @eamodio will look into this when he carves out the time. Thanks!

@eamodio
Copy link
Contributor

eamodio commented Apr 14, 2021

@plainerman sorry for the delay. Can you please update this with the current main branch?

Also can you combine the extra git calls into a single call? Here is what I have in GitLens: https://github.com/eamodio/vscode-gitlens/blob/b60637f00e245fd23c84ceebf7a4aeb637c64d96/src/git/git.ts#L1255-L1274

@plainerman
Copy link
Contributor Author

Thank you for your responses!

I will incorporate the desired changes and will ping you @eamodio once I have changed this.

@plainerman plainerman marked this pull request as draft April 14, 2021 07:11
Executing git rev-parse on an empty repository does not work and sets no upstream (although there exists one)
Syncing a repository does not need a commit, whereas publishing a new branch does
@plainerman plainerman marked this pull request as ready for review April 26, 2021 18:41
@plainerman
Copy link
Contributor Author

Sorry @eamodio, I was busy the last days. The branch is now ready again.

I rebased the branch and combined the git config fetch into a single call. If there is anything I can do, just ping me.

To test it locally, you can use this empty repository. that has a commit but is not synced.

@lszomoru lszomoru assigned lszomoru and unassigned eamodio Oct 4, 2021
@plainerman
Copy link
Contributor Author

@lszomoru, I am glad to help if you need any assistance or have further questions.

@lszomoru lszomoru added this to the January 2022 milestone Jan 5, 2022
@lszomoru
Copy link
Member

lszomoru commented Jan 6, 2022

@plainerman, first of all I would like to apologise that it is taking so long for this change to be committed. I have taken a look at your code today, and I have manually ported the two changes to the latest codebase in main. Unfortunately I am still seeing errors when I do the following:

mkdir empty
cd empty
git init

# Add some origin, you can use any repo, but this is very small
git remote add origin https://github.com/plainerman/empty.git

# Now track master as described in here https://stackoverflow.com/a/625460/4417954
git config branch.master.remote origin
git config branch.master.merge refs/heads/master

When I open the repository, with your changes the Sync button is currently enabled in the status bar, but when I click on it I am getting the following error message in the Git output: fatal: couldn't find remote ref master. The same error happens if I push a commit and attempt to sync after the commit has been pushed.

I will further investigate the issue this week but please feel free to look at it yourself If you have time.

@plainerman
Copy link
Contributor Author

Thanks @lszomoru for the update. Maybe, I'll have time this weekend. Have you pushed the changes you made to my repository? I believe you have push permissions there, as you are a member here. Or should I rebase myself?

@lszomoru
Copy link
Member

lszomoru commented Jan 7, 2022

@plainerman, no I have not pushed any changes to your topic branch.
I have manually ported the two changes from this pull request onto main and tested things.

@lszomoru lszomoru modified the milestones: January 2022, February 2022 Jan 27, 2022
@lszomoru lszomoru modified the milestones: February 2022, March 2022 Feb 24, 2022
@lszomoru lszomoru modified the milestones: March 2022, Backlog Mar 21, 2022
@joaomoreno joaomoreno added the git GIT issues label May 2, 2022
@jrieken jrieken dismissed a stale review via eafa750 May 5, 2022 09:57
@vivodi
Copy link

vivodi commented Dec 29, 2024

Is this still relevant?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
git GIT issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Git - git sync issue on an empty repo
5 participants