trilbymedia / grav-plugin-git-sync

Collaboratively Synchronize your Grav `user` folder hosted on GitHub, BitBucket or GitLab
Apache License 2.0
240 stars 58 forks source link

sync error using gitlab repo with default branch 'main' #183

Closed breisfeld closed 3 years ago

breisfeld commented 3 years ago

I have recently been converting my naming convention of my repos from 'master' to 'main'.

Though I have configured the 'Advanced Git Settings' in the plugin to use 'main' as the local and remote branch names, I get an error when trying to sync:

error: src refspec main does not match any. error: failed to push some refs to 'https://user:{password}@gitlab.com/user/habsbot-grav.git'

w00fz commented 3 years ago

Hi @breisfeld , I think your user is still set to master branch, you might have to manually switch the branch to main via CLI.

git branch main -u origin/main

breisfeld commented 3 years ago

Hello @w00fz, When I set up the repo there was no master branch at all, so I don't think this is the issue.

w00fz commented 3 years ago

The error suggests that the main branch does not match any branch in the configuration. So i believe you are not tracking it. The command above should fix that.

You can verify this if you look into .git/config

breisfeld commented 3 years ago

Weeks ago, I set my preferences in Gitlab so that default branches were called 'main'. I just started using grav, so my repo had only a 'main' branch and no 'master' at all.

My config contains

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = git@gitlab.com:user/habsbot-grav.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
    remote = origin
    merge = refs/heads/main
w00fz commented 3 years ago

Yeah that config looks correct. Are you able to do a git fetch from the user folder?

w00fz commented 3 years ago

Also can you check user/config/plugins/git-sync.yaml ?

breisfeld commented 3 years ago

git fetch from my local repo seems to work fine.

git-sync.yaml contains

...
branch: main
remote:
  name: origin
  branch: main
git:
  author: gituser
  message: '(Grav GitSync) Automatic Commit'
  name: GitSync
  email: git-sync@trilby.media
  bin: git
logging: false
breisfeld commented 3 years ago

I quickly created a local and remote 'master' branch and checked it out. I specified 'master' as the default in Gitlab and went so far as to remove the 'main' branch. I then edited the grav plugin advanced settings to reflect these changes. Syncing via the plugin seemed to work perfectly. Does 'master' have some special standing?

breisfeld commented 3 years ago

By the way, thank you for your hard work in creating and supporting this plugin! I am sure that once things are working as designed (due to my error or some minor issue with the plugin), it will be incredibly useful and save me loads of time.

w00fz commented 3 years ago

Well this is odd, master is not hardcoded anywhere that I can see, other than in the blueprint.yaml. In fact, this was an issue a long time ago where you couldn't really use anything other than the default master branch, but that was sorted. See https://github.com/trilbymedia/grav-plugin-git-sync/issues/56, you can see the error the author of the issue was getting is actually like the one you are seeing.

If you switch back to main as branches and enable the gitsync logging, can you see what kind of commands are happening? That might help identifying the problem.

breisfeld commented 3 years ago

Where do the logs get written?

w00fz commented 3 years ago

That would be in logs/grav.log

breisfeld commented 3 years ago

The latest entry in the log is

[2020-10-15 22:32:19] grav.NOTICE: gitsync[command]: LC_ALL=C git -C '/var/www/html/grav/user/' push origin main:main 2>&1 [] []
[2020-10-15 22:32:20] grav.NOTICE: gitsync[output]: error: src refspec main does not match any. error: failed to push some refs to 'https://user:{password}@gitlab.com/user/habsbot-grav.git' [] []
w00fz commented 3 years ago

It does look like the command is properly trying to push to main, in the command line says push origin main:main, yet git is failing to find the main reference in your repo. I am confused as to why it's doing that since you say it has the main branch both local and remote.

breisfeld commented 3 years ago

Any other diagnoses I can run? Otherwise, I may just deploy a 'master' branch, which seems to work.

w00fz commented 3 years ago

I will properly test this and get to the bottom of it but in the meantime other than manually trying the git push origin main:main command from the user folder, and ensure that works, I am afraid I don't have any other suggestion.

Maybach91 commented 3 years ago

I got it work by manually changed the mastermain in these files:

.git/HEAD
.git/FETCH_HEAD

and remove the files master from the refs/heads/ and refs/remotes/origin/

Maybe while initialize / config the plugin on install it sets the wrong default values (master) and thats why the .git/ folder is wrong configured? But dunno... :)

@breisfeld maybe try this to get it work ↑

breisfeld commented 3 years ago

Thanks for the suggestion, but it didn't work for me. I've gone back to 'master' as the default branch for this repo.

fm commented 3 years ago

I got it work by manually changed the mastermain in these files:

.git/HEAD
.git/FETCH_HEAD

and remove the files master from the refs/heads/ and refs/remotes/origin/

Maybe while initialize / config the plugin on install it sets the wrong default values (master) and thats why the .git/ folder is wrong configured?

Ran into the same issue, and I got it to work by doing @Maybach91's trick...

also running git branch on the user folder said master not main before the change

[faisal@atlas .git]$ git branch
* master

and main after the change

[faisal@atlas user]$ git branch
* main
Louson commented 3 years ago

Master branch is set at the initialization of the repo (when you do git init). By default it is master. According to the manual (git help init) :

If not specified, fall back to the default name
           (currently master, but this is subject to change in the future; the
           name can be customized via the init.defaultBranch configuration
           variable).

Then it's probably possible to avoid the bug by setting this before configuring the plugin in the system git configuration (/etc/gitconfig for example) :

[init]
    defaultBranch = main

Or with the command

# git config --system init.defaultBranch main

And I guess if you do this, you won't be able to use master branch

Anyway, I see two things :

If I change the local branch, it is never created locally

git branch -a
* master
  remotes/origin/main
  remotes/origin/master

Then pushing main to main can't be possible.

You might need a -b <branch> when init is done (probably in function initializeRepository), or you might just need to add a -b in the checkout.

If I keep master as local branch but change remote

Here is the log:

[2021-03-15 14:56:30] grav.NOTICE: gitsync[command]: LC_ALL=C git -C '/var/www/grav/user/' push origin main:main 2>&1 [] []

while it should be master:main

Then I guess you get the wrong name when you do: $local_branch = $this->getConfig('branch', $branch); You probably catch the remote name instead of the local one.

I hope this can be helpful

0x01af commented 3 years ago

I got it work by manually changed the mastermain in these files:

.git/HEAD
.git/FETCH_HEAD

and remove the files master from the refs/heads/ and refs/remotes/origin/ Maybe while initialize / config the plugin on install it sets the wrong default values (master) and thats why the .git/ folder is wrong configured?

Ran into the same issue, and I got it to work by doing @Maybach91's trick...

also running git branch on the user folder said master not main before the change

[faisal@atlas .git]$ git branch
* master

and main after the change

[faisal@atlas user]$ git branch
* main

I had the same issue with a fresh installation of GRAV and GRAV Plugin Git Sync. After configuring by wizard, the plugin used 'master'. I changed the plugin configuration to 'main' (parameter 'local branch' and 'remote branch'), but plugin showed the known error message 'error: src refspec main does not match any ...'.

In my installation, I had only to correct the file '.git/HEAD' and remove file 'master' at the folder 'refs/heads/'. The other file '.git/FETCH_HEAD' used already 'main' (no wrong values) and in folder 'refs/remotes/origin' existed only a file 'main'.

Afterwards, I saved once again the plugin's configuration.

w00fz commented 3 years ago

Thanks for your patience everyone. This is now fixed and I'll release it soon. The new version will hook better to any custom remote branch and I have a mapping of what are the new default branches (ie, GitHub is now main, GitLab has announced later this month will do the same transition, BitBucket is still master but on the same way). All of this is going to be automatically handled by GitSync but you can also specify your own branch AND testing the connection from the Wizard will also make sure the branch exists.

Better support for branches other than master. This includes the transition to main from GitHub and the groundwork to support other big providers making the change as announced soon. GitSync is now capable of preset the branch based on the provider selected. You are now also able to specify any custom branch and when testing the repository connection it will also ensure the branch exists and provide feedback if not.

w00fz-2021-04-16_@_16 19 27@2x