Repository: WeblateOrg/weblate

Issue:
Error when trying to add Mercurial repository with http access
I have a repository that runs Kallithea 0.2.1 with Mercurial support and access via http
So I add new translation component to the Weblate Project with the following parameters:
Version control system - Mercurial
Source code repository - http://username:mypassword@domain:portnumber/repository
Repository branch - testing

But after pressing continue I get an error under the "Source code repository" field - "Could not update repository: abort: unknown revision 'testing'! (255)"

Weblate's log looks as following
2019-09-12 18:38:03,510 DEBUG hg: exec hg init /data_directory/vcs/project/component [retcode=0]
2019-09-12 18:38:03,510 DEBUG hg: exec hg init /data_directory/vcs/project/component [retcode=0]
2019-09-12 18:38:03,522 INFO project/component: updating repository
2019-09-12 18:38:27,016 DEBUG hg: exec hg pull --branch . [retcode=0]
2019-09-12 18:38:27,016 DEBUG hg: exec hg pull --branch . [retcode=0]
2019-09-12 18:38:27,017 INFO project/component: update took 23.49 seconds
2019-09-12 18:38:27,017 DEBUG project/component: update: pulling from http://username:***@domain:portnumber/repository
2019-09-12 18:38:27,018 DEBUG project/component: update: adding changesets
2019-09-12 18:38:27,018 DEBUG project/component: update: adding manifests
2019-09-12 18:38:27,018 DEBUG project/component: update: adding file changes
2019-09-12 18:38:27,018 DEBUG project/component: update: added 1326 changesets with 4606 changes to 1300 files
2019-09-12 18:38:27,019 DEBUG project/component: update: (run 'hg update' to get a working copy)
2019-09-12 18:38:27,106 DEBUG hg: exec hg branches --template {branch}
 [retcode=0]
2019-09-12 18:38:27,106 DEBUG hg: exec hg branches --template {branch}
 [retcode=0]
2019-09-12 18:38:27,190 DEBUG hg: exec hg update testing [retcode=255]
2019-09-12 18:38:27,190 DEBUG hg: exec hg update testing [retcode=255]


If I then connect to the Weblate server via ssh and go to the /data_directory/vcs/project/component and print out command `hg branch` it states that the current branch is "default". Moreover the output of command `hg branches --template {branch}"\n"` doesn't list the branch "testing" in available branches.
"default" is a long depricated branch in this repository, but it seems that for some reason Weblate pulls it instead of the branch "testing" that I required on the "Add new component" screen.

I fixed this by removing all the files and pulling the needed branch to the data directory manually
`rm -fr ./* .*`
`hg pull http://username:mypassword@domain:portnumber/repository`
`hg update testing`

After this on Weblate component creation screen (which I dodn't close) I press the save button again and can continue with file selection. The only anomaly is that at Manage>Repository maintenance it says that "The remote repository needs to be merged!" and after a first commit "There are some new commits in the local repository." although everything seems to work fine and remote repository status is OK.

I was wondering if it's a bug in Weblate, a faulty repository or me doing something wrong?

And another note maybe related to this - merge style of the component was "rebase" by default and I couldn't make Weblate push changes back to repository. After changing it to "merge" everything works fine.
Documentation states that the default method is "merge" so maybe it's a bug that by default my component used "rebase" method.


Discussion:
This is probably caused by 1157140b3a3 (from #1308) which introduced fetching of current branch only and broke branches switching.

Resolving patch:
diff --git a/weblate/vcs/base.py b/weblate/vcs/base.py
--- a/weblate/vcs/base.py
+++ b/weblate/vcs/base.py
@@ -73,8 +73,6 @@ class Repository(object):
     _cmd = 'false'
     _cmd_last_revision = None
     _cmd_last_remote_revision = None
-    _cmd_update_remote = None
-    _cmd_push = None
     _cmd_status = ['status']
     _cmd_list_changed_files = None
 
@@ -231,8 +229,7 @@ def clone(cls, source, target, branch=None):
 
     def update_remote(self):
         """Update remote repository."""
-        self.execute(self._cmd_update_remote)
-        self.clean_revision_cache()
+        raise NotImplementedError()
 
     def status(self):
         """Return status of the repository."""
@@ -241,7 +238,7 @@ def status(self):
 
     def push(self):
         """Push given branch to remote repository."""
-        self.execute(self._cmd_push + [self.branch])
+        raise NotImplementedError()
 
     def reset(self):
         """Reset working copy to match remote branch."""
diff --git a/weblate/vcs/git.py b/weblate/vcs/git.py
--- a/weblate/vcs/git.py
+++ b/weblate/vcs/git.py
@@ -44,8 +44,6 @@ class GitRepository(Repository):
     _cmd_last_remote_revision = [
         'log', '-n', '1', '--format=format:%H', '@{upstream}'
     ]
-    _cmd_update_remote = ['fetch', 'origin']
-    _cmd_push = ['push', 'origin']
     _cmd_list_changed_files = ['diff', '--name-status']
 
     name = 'Git'
@@ -390,8 +388,14 @@ def list_remote_branches(self):
         ]
 
     def update_remote(self):
+        """Update remote repository."""
         self.execute(['remote', 'prune', 'origin'])
-        super(GitRepository, self).update_remote()
+        self.execute(['fetch', 'origin'])
+        self.clean_revision_cache()
+
+    def push(self):
+        """Push given branch to remote repository."""
+        self.execute(['push', 'origin', self.branch])
 
 
 class GitWithGerritRepository(GitRepository):
@@ -416,8 +420,6 @@ class SubversionRepository(GitRepository):
     name = 'Subversion'
     req_version = '1.6'
 
-    _cmd_update_remote = ['svn', 'fetch']
-    _cmd_push = ['svn', 'dcommit']
     _is_supported = None
     _version = None
 
@@ -484,10 +486,10 @@ def configure_remote(self, pull_url, push_url, branch):
     def update_remote(self):
         """Update remote repository."""
         if self._fetch_revision:
-            self.execute(self._cmd_update_remote + [self._fetch_revision])
+            self.execute(['svn', 'fetch', self._fetch_revision])
             self._fetch_revision = None
         else:
-            self.execute(self._cmd_update_remote + ['--parent'])
+            self.execute(['svn', 'fetch', '--parent'])
         self.clean_revision_cache()
 
     @classmethod
@@ -537,6 +539,10 @@ def get_remote_branch_name(self):
     def list_remote_branches(self):
         return []
 
+    def push(self):
+        """Push given branch to remote repository."""
+        self.execute(['svn', 'dcommit', self.branch])
+
 
 class GithubRepository(GitRepository):
 
diff --git a/weblate/vcs/mercurial.py b/weblate/vcs/mercurial.py
--- a/weblate/vcs/mercurial.py
+++ b/weblate/vcs/mercurial.py
@@ -41,7 +41,6 @@ class HgRepository(Repository):
     _cmd_last_remote_revision = [
         'log', '--limit', '1', '--template', '{node}', '--branch', '.'
     ]
-    _cmd_update_remote = ['pull', '--branch', '.']
     _cmd_list_changed_files = ['status', '--rev']
 
     name = 'Mercurial'
@@ -339,3 +338,8 @@ def cleanup(self):
         """Remove not tracked files from the repository."""
         self.set_config('extensions.purge', '')
         self.execute(['purge'])
+
+    def update_remote(self):
+        """Update remote repository."""
+        self.execute(['pull', '--branch', self.branch])
+        self.clean_revision_cache()


Test patch: