aboutsummaryrefslogtreecommitdiff
path: root/b4/diff.py
diff options
context:
space:
mode:
authorKonstantin Ryabitsev <konstantin@linuxfoundation.org>2020-05-25 15:22:30 -0400
committerKonstantin Ryabitsev <konstantin@linuxfoundation.org>2020-05-25 15:22:30 -0400
commit59be08453137a3b9c6a25dc6787b5066a88a84cd (patch)
treedfc5acde52c5cd72895dcd9af7c46260f2a11e1a /b4/diff.py
parent34bcb457865d0b58e08486b2ceb80e9067717e60 (diff)
downloadb4-59be08453137a3b9c6a25dc6787b5066a88a84cd.tar.gz
Add -3 to "b4 am" to prep for a 3way merge
The original code used for b4 diff was to prepare for a 3-way merge by making sure that all blob indexes exist in the local repo. Add this functionality to "b4 am" and document all the features added in the 0.5.0 branch. Signed-off-by: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
Diffstat (limited to 'b4/diff.py')
-rw-r--r--b4/diff.py112
1 files changed, 2 insertions, 110 deletions
diff --git a/b4/diff.py b/b4/diff.py
index 7ff4e47..ab23b0c 100644
--- a/b4/diff.py
+++ b/b4/diff.py
@@ -19,114 +19,6 @@ from tempfile import mkstemp
logger = b4.logger
-def make_fake_commit_range(gitdir, lser):
- start_commit = end_commit = None
- # Do we have it in cache already?
- cachedir = b4.get_cache_dir()
- # Use the msgid of the first non-None patch in the series
- msgid = None
- for lmsg in lser.patches:
- if lmsg is not None:
- msgid = lmsg.msgid
- break
- if msgid is None:
- logger.critical('Cannot operate on an empty series')
- return None, None
- cachefile = os.path.join(cachedir, '%s.fakeam' % urllib.parse.quote_plus(msgid))
- if os.path.exists(cachefile):
- stalecache = False
- with open(cachefile, 'r') as fh:
- cachedata = fh.read()
- chunks = cachedata.strip().split()
- if len(chunks) == 2:
- start_commit, end_commit = chunks
- else:
- stalecache = True
- if start_commit is not None and end_commit is not None:
- # Make sure they are still there
- ecode, out = b4.git_run_command(gitdir, ['cat-file', '-e', start_commit])
- if ecode > 0:
- stalecache = True
- else:
- ecode, out = b4.git_run_command(gitdir, ['cat-file', '-e', end_commit])
- if ecode > 0:
- stalecache = True
- else:
- logger.debug('Using previously generated range')
- return start_commit, end_commit
-
- if stalecache:
- logger.debug('Stale cache for [v%s] %s', lser.revision, lser.subject)
- os.unlink(cachefile)
-
- logger.info('Preparing fake-am for v%s: %s', lser.revision, lser.subject)
- with b4.git_temp_worktree(gitdir):
- # We are in a temporary chdir at this time, so writing to a known file should be safe
- mbxf = '.__git-am__'
- mbx = mailbox.mbox(mbxf)
- # Logic largely borrowed from gj_tools
- seenfiles = set()
- for lmsg in lser.patches[1:]:
- logger.debug('Looking at %s', lmsg.full_subject)
- lmsg.load_hashes()
- if not len(lmsg.blob_indexes):
- logger.critical('ERROR: some patches do not have indexes')
- logger.critical(' automatic range-diff would be misleading')
- return None, None
- for fn, fi in lmsg.blob_indexes:
- if fn in seenfiles:
- # We already processed this file, so this blob won't match
- continue
- seenfiles.add(fn)
- if set(fi) == {'0'}:
- # New file creation, nothing to do here
- logger.debug(' New file: %s', fn)
- continue
- # Try to grab full ref_id of this hash
- ecode, out = b4.git_run_command(gitdir, ['rev-parse', fi])
- if ecode > 0:
- logger.critical(' ERROR: Could not find matching blob for %s (%s)', fn, fi)
- # TODO: better handling
- return None, None
- logger.debug(' Found matching blob for: %s', fn)
- fullref = out.strip()
- gitargs = ['update-index', '--add', '--cacheinfo', f'0644,{fullref},{fn}']
- ecode, out = b4.git_run_command(None, gitargs)
- if ecode > 0:
- logger.critical(' ERROR: Could not run update-index for %s (%s)', fn, fullref)
- return None, None
- mbx.add(lmsg.msg.as_string(policy=b4.emlpolicy).encode('utf-8'))
-
- mbx.close()
- ecode, out = b4.git_run_command(None, ['write-tree'])
- if ecode > 0:
- logger.critical('ERROR: Could not write fake-am tree')
- return None, None
- treeid = out.strip()
- # At this point we have a worktree with files that should cleanly receive a git am
- gitargs = ['commit-tree', treeid + '^{tree}', '-F', '-']
- ecode, out = b4.git_run_command(None, gitargs, stdin='Initial fake commit'.encode('utf-8'))
- if ecode > 0:
- logger.critical('ERROR: Could not commit-tree')
- return None, None
- start_commit = out.strip()
- b4.git_run_command(None, ['reset', '--hard', start_commit])
- ecode, out = b4.git_run_command(None, ['am', mbxf])
- if ecode > 0:
- logger.critical('ERROR: Could not fake-am version %s', lser.revision)
- return None, None
- ecode, out = b4.git_run_command(None, ['rev-parse', 'HEAD'])
- end_commit = out.strip()
- logger.info(' range: %.12s..%.12s', start_commit, end_commit)
-
- with open(cachefile, 'w') as fh:
- logger.debug('Saving into cache: %s', cachefile)
- logger.debug(' %s..%s', start_commit, end_commit)
- fh.write(f'{start_commit} {end_commit}\n')
-
- return start_commit, end_commit
-
-
def diff_same_thread_series(cmdargs):
msgid = b4.get_msgid(cmdargs)
wantvers = cmdargs.wantvers
@@ -221,13 +113,13 @@ def main(cmdargs):
sys.exit(1)
# Prepare the lower fake-am range
- lsc, lec = make_fake_commit_range(cmdargs.gitdir, lser)
+ lsc, lec = lser.make_fake_am_range(gitdir=cmdargs.gitdir)
if lsc is None or lec is None:
logger.critical('---')
logger.critical('Could not create fake-am range for lower series v%s', lser.revision)
sys.exit(1)
# Prepare the upper fake-am range
- usc, uec = make_fake_commit_range(cmdargs.gitdir, user)
+ usc, uec = user.make_fake_am_range(gitdir=cmdargs.gitdir)
if usc is None or uec is None:
logger.critical('---')
logger.critical('Could not create fake-am range for upper series v%s', user.revision)