diff --git a/hgext/shelve.py b/hgext/shelve.py
index 7cbb3a01fa38190d986741117a867a2e8586c668_aGdleHQvc2hlbHZlLnB5..1289a122cf3f55ed8c37be11157fe8acf06abfe5_aGdleHQvc2hlbHZlLnB5 100644
--- a/hgext/shelve.py
+++ b/hgext/shelve.py
@@ -112,8 +112,9 @@
         return bundlerepo.bundlerepository(self.repo.baseui, self.repo.root,
                                            self.vfs.join(self.fname))
     def writebundle(self, bases, node):
-        btype = 'HG10BZ'
-        cgversion = '01'
-        compression = None
-        if 'generaldelta' in self.repo.requirements:
+        cgversion = changegroup.safeversion(self.repo)
+        if cgversion == '01':
+            btype = 'HG10BZ'
+            compression = None
+        else:
             btype = 'HG20'
@@ -119,5 +120,4 @@
             btype = 'HG20'
-            cgversion = '02'
             compression = 'BZ'
 
         cg = changegroup.changegroupsubset(self.repo, bases, [node], 'shelve',
diff --git a/mercurial/changegroup.py b/mercurial/changegroup.py
index 7cbb3a01fa38190d986741117a867a2e8586c668_bWVyY3VyaWFsL2NoYW5nZWdyb3VwLnB5..1289a122cf3f55ed8c37be11157fe8acf06abfe5_bWVyY3VyaWFsL2NoYW5nZWdyb3VwLnB5 100644
--- a/mercurial/changegroup.py
+++ b/mercurial/changegroup.py
@@ -963,7 +963,8 @@
 
 def safeversion(repo):
     # Finds the smallest version that it's safe to assume clients of the repo
-    # will support.
+    # will support. For example, all hg versions that support generaldelta also
+    # support changegroup 02.
     versions = supportedversions(repo)
     if 'generaldelta' in repo.requirements:
         versions.discard('01')
diff --git a/tests/test-treemanifest.t b/tests/test-treemanifest.t
index 7cbb3a01fa38190d986741117a867a2e8586c668_dGVzdHMvdGVzdC10cmVlbWFuaWZlc3QudA==..1289a122cf3f55ed8c37be11157fe8acf06abfe5_dGVzdHMvdGVzdC10cmVlbWFuaWZlc3QudA== 100644
--- a/tests/test-treemanifest.t
+++ b/tests/test-treemanifest.t
@@ -296,6 +296,22 @@
   $ hg st --change tip
   M dir1/a
 
+Shelving and unshelving should work
+
+  $ echo foo >> dir1/a
+  $ hg --config extensions.shelve= shelve
+  shelved as default
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ hg --config extensions.shelve= unshelve
+  unshelving change 'default'
+  $ hg diff --nodates
+  diff -r 708a273da119 dir1/a
+  --- a/dir1/a
+  +++ b/dir1/a
+  @@ -1,1 +1,2 @@
+   1
+  +foo
+
 Create deeper repo with tree manifests.
 
   $ cd ..