Patchwork D7959: lfs: move the initialization of the upload request into the try block

login
register
mail settings
Submitter phabricator
Date Jan. 21, 2020, 6:37 p.m.
Message ID <differential-rev-PHID-DREV-c3pjsn666gsewzocph4m-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/44556/
State Superseded
Headers show

Comments

phabricator - Jan. 21, 2020, 6:37 p.m.
mharbison72 created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This (almost) guarantees that the file is closed in the case of an exception.
  The one hole is if the `seek(SEEK_END)`/`tell()`/`seek(0)` sequence fails.  But
  that's going to go away when subclassing `httpconnection.httpsendfile` to fix
  the worker problem, so I'm not going to worry too much.  (And that class appears
  to have the same problem.)

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D7959

AFFECTED FILES
  hgext/lfs/blobstore.py

CHANGE DETAILS




To: mharbison72, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/hgext/lfs/blobstore.py b/hgext/lfs/blobstore.py
--- a/hgext/lfs/blobstore.py
+++ b/hgext/lfs/blobstore.py
@@ -490,15 +490,17 @@ 
                     _(b'detected corrupt lfs object: %s') % oid,
                     hint=_(b'run hg verify'),
                 )
-            request.data = lfsuploadfile(localstore.open(oid))
-            request.get_method = lambda: 'PUT'
-            request.add_header('Content-Type', 'application/octet-stream')
-            request.add_header('Content-Length', len(request.data))
 
         for k, v in headers:
             request.add_header(pycompat.strurl(k), pycompat.strurl(v))
 
         try:
+            if action == b'upload':
+                request.data = lfsuploadfile(localstore.open(oid))
+                request.get_method = lambda: 'PUT'
+                request.add_header('Content-Type', 'application/octet-stream')
+                request.add_header('Content-Length', len(request.data))
+
             with contextlib.closing(self.urlopener.open(request)) as res:
                 contentlength = res.info().get(b"content-length")
                 ui = self.ui  # Shorten debug lines