def commit_is_merged_diff(driver_repo, kernel_repo, commit): ok = True for f in commit['files']: if not is_alsa_file(f): continue src = git_repo(driver_repo) + '/' + to_alsa_file2(f) dst = git_repo(kernel_repo) + '/' + f lines = popen("diff -uN %s %s 2> /dev/null" % (src, dst)).readlines() if lines: ok = False break return ok
def commit_is_merged_hard(kernel_repo, commit, driver_repo, driver_commits): ref = commit['ref'] fp = git_popen(kernel_repo, "diff %s~1..%s" % (ref, ref)) rlines, addfiles, rmfiles = analyze_diff(fp) fp.close() files = addfiles + rmfiles for dc in driver_commits: ok = True for f in files: if not f in dc['files']: ok = False break if ok: ref1 = dc['ref'] curdir = getcwd() chdir(git_repo(driver_repo)) fp = git_popen(driver_repo, "diff %s~1..%s %s" % (ref1, ref1, ' '.join(files))) chdir(curdir) lines = fp.readlines() fp.close() #print 'Candidate', dc['ref'][:7], dc['comment'].splitlines()[0] #if ref1[:7] == 'd26326d': # open("/dev/shm/aaa.1", "w+").write(''.join(lines)) # open("/dev/shm/aaa.2", "w+").write(''.join(rlines)) if diff_compare2(lines, rlines): return dc return None
def extract_git_release(): script_folder = os.path.dirname(__file__) dev_git = utils.git_repo(script_folder) return(dev_git.release_tag())
def try_to_merge_hard(driver_repo, driver_branch, kernel_repo, kernel_branch, commit): ref = commit['ref'] fp = git_popen(kernel_repo, "diff %s~1..%s" % (ref, ref)) rlines, addfiles, rmfiles = analyze_diff(fp) fp.close() patchfile = tmpfile('alsa-kmirror-patch') fp = open(patchfile, 'w+') fp.write(''.join(rlines)) fp.close() curdir = getcwd() chdir(git_repo(driver_repo)) lines = git_popen(driver_repo, "apply -v --check %s 2>&1" % patchfile).readlines() patch = plines = None for line in lines: if line.startswith('Checking patch '): patch = line[15:-4] plines = None elif line == 'error: while searching for:\n': plines = [] elif line.startswith('error: '): if patch and plines is not None: break patch = plines = None else: if plines is not None: plines.append(line) if not patch and not plines: return False chdir(git_repo(kernel_repo)) lines = git_popen(kernel_repo, "annotate %s %s" % (to_kernel_file(patch), ref)).readlines() start = end = None idx = -1 missing = plines[:] missingrefs = [] for line in lines: idx += 1 pos = line.find('\t') if pos < 0: continue hash = line[:pos] pos = line.find(')') if pos < 0: continue code = line[pos + 1:] if code in missing: missing.remove(code) if start is None: start = idx end = idx elif start is not None: missing.append(code) if start is None: return False for idx in range(max(0, start - 1), min(len(lines), end + 2)): line = lines[idx] pos = line.find('\t') if pos < 0: continue hash = line[:pos] if hash == ref[:8]: continue pos = line.find(')') if pos < 0: continue code = line[pos + 1:] for m in missing: if code == m: if not hash in missingrefs: missingrefs.append(hash) chdir(curdir) ok = False for mref in missingrefs: commits = git_read_commits(kernel_repo, mref + "~1", mref) if commits and try_to_merge(driver_repo, driver_branch, kernel_repo, commits[0]): ok = True return ok
def try_to_merge(driver_repo, driver_branch, src_repo, commit, filecheck=is_alsa_file, fileconv=to_alsa_file2, do_checkout=True): comment = commit['comment'].splitlines() ref = commit['ref'] print 'Merging %s %s' % (ref[:7], comment[0]) #fp = git_popen(src_repo, "diff --binary %s~1..%s" % (ref, ref)) root = '' if 'root_flag' in commit and commit['root_flag']: root = '--root ' fp = git_popen(src_repo, "diff-tree -p --binary %s%s" % (root, ref)) rlines, addfiles, rmfiles = analyze_diff(fp, filecheck=filecheck, fileconv=fileconv) fp.close() patchfile = tmpfile('alsa-merge-patch') fp = open(patchfile, 'w+') fp.write(''.join(rlines)) fp.close() commentfile = tmpfile('alsa-merge-comment') fp = open(commentfile, 'w+') fp.write(''.join(commit['comment'])) fp.close() elems = re.compile('(.*?)\s+<(.*)>').match(commit['Author']) exports = 'export GIT_AUTHOR_NAME="%s" ; ' % elems.group(1) exports += 'export GIT_AUTHOR_EMAIL="%s" ; ' % elems.group(2) exports += 'export GIT_AUTHOR_DATE="%s" ; ' % commit['AuthorDate'] elems = re.compile('(.*?)\s+<(.*)>').match(commit['Commit']) exports += 'export GIT_COMMITER_NAME="%s" ; ' % elems.group(1) exports += 'export GIT_COMMITER_EMAIL="%s" ; ' % elems.group(2) exports += 'export GIT_COMMITER_DATE="%s" ; ' % commit['CommitDate'] exports += 'export GIT_COMMITTER_NAME="%s" ; ' % elems.group(1) exports += 'export GIT_COMMITTER_EMAIL="%s" ; ' % elems.group(2) exports += 'export GIT_COMMITTER_DATE="%s"' % commit['CommitDate'] curdir = getcwd() if do_checkout and git_system(driver_repo, "checkout -q %s" % driver_branch): raise ValueError, 'git checkout' chdir(git_repo(driver_repo)) lines = popen("LANG=C patch -f -p 1 --dry-run --reject-file=%s < %s" % (tmpfile("rejects"), patchfile)).readlines() print ''.join(lines) failed = fuzz = succeed = 0 for line in lines: if line.find('FAILED') >= 0: failed += 1 if line.find('succeed') >= 0: if line.find('fuzz') >= 0: fuzz += 1 else: succeed += 1 if failed: print 'Merge skipped %s %s (%s failed)' % (ref[:7], comment[0], failed) chdir(curdir) if do_checkout and \ ref[:7] in ['bdb527e', 'fc5b15f', '82b1d73', \ '02a237b', 'ef8d60f', 'c0fa6c8', \ '1605282', '3946860', 'd70f363', \ '6539799', '152a3a7', '79980d9']: print ' It is probably OK...' return False raise ValueError return False if git_system( driver_repo, "apply --check --binary --allow-binary-replacement %s" % patchfile): print 'Merge skipped %s %s (apply check)' % (ref[:7], comment[0]) chdir(curdir) if not do_checkout: raise ValueError return False if git_system(driver_repo, "apply --binary --allow-binary-replacement %s" % patchfile): chdir(curdir) raise ValueError, 'git apply' if addfiles and git_system(driver_repo, "add %s" % ' '.join(addfiles)): chdir(curdir) raise ValueError, 'git add' if rmfiles and git_system(driver_repo, "rm %s" % ' '.join(rmfiles)): chdir(curdir) raise ValueError, 'git rm' if git_system(driver_repo, "commit -F %s" % commentfile, exports=exports): chdir(curdir) raise ValueError, 'git commit' chdir(curdir) print 'Merge complete %s %s (%s fuzz)' % (ref[:7], comment[0], fuzz) return True
def deploy(self, git_dest='src', integration=False, origin_update=False, upstream_update=False, rebase_update=False, branches=['clean/master'], prlist=[], origin='', origin_master='', pull_flags=['ff-only'], upstream=''): if git_dest[0] != '/': dest = os.path.join(self.base_path, git_dest) else: dest = git_dest # print("@@@@@@@@@@@@@@@@@@@@", dest, self.dry_run) dev_git = utils.git_repo(dest, logger=self.logger, dry_run=self.dry_run) origin_branches = utils.get_branches(origin, branch_selection=branches) upstream_branches = utils.get_branches( upstream, branch_pattern='.*?\s+refs/pull/([0-9]*?)/head\s+', # branch_exclude_pattern='.*?\s+refs/pull/({branch})/merge\s+', branch_format_string='pull/{branch}/head', branch_selection=prlist) local_pr = utils.trasf_match(upstream_branches, in_match='.*/([0-9]*)/(.*)', out_format='pull/{name}/clean') self.logger.info("found %s searching for upstream_branches %s->" % (str(local_pr), str(upstream_branches))) if not os.path.exists(dest): self.logger.info("MISSING destination_dir-->" + dest + "<-- ") os.makedirs(dest) self.logger.info("init git repo into dir: " + dest) dev_git.init() dev_git.get_remotes() self.logger.info("Adding remote origin %s fetching branches %s" % (origin, str(origin_branches))) dev_git.add_remote(origin, name='origin', fetch_branches=origin_branches) self.logger.info("Adding remote upstream %s " % (upstream)) dev_git.add_remote(upstream, name='upstream') dev_git.fetch(name='origin', branches=origin_branches) if integration: if len(origin_branches) > 0: upstream_clean = origin_branches[0] # print("--------------------------------------" + upstream_clean + "-----------------------") dev_git.checkout(upstream_clean) if upstream_update: dev_git.sync_upstream() dev_git.checkout(upstream_clean, newbranch=origin_master) if upstream_update: dev_git.sync_upstream() for b in origin_branches[1:]: if upstream_update: merge_branch = b + '_merge' self.logger.info( "merge updated upstream branch: " + b + " into " + merge_branch) dev_git.checkout(b, newbranch=merge_branch) dev_git.merge(upstream_clean) if rebase_update: rebase_branch = b + '_rebase' self.logger.info( "merge upstream, rebase with -Xtheirs option and merge againnormal merge with :-Xtheirs branch: " + b + " into " + rebase_branch) dev_git.checkout(b, newbranch=rebase_branch) dev_git.merge(upstream_clean) dev_git.rebase(branch=upstream_clean, options=['-Xtheirs']) dev_git.merge(merge_branch, options=['-Xtheirs']) self.logger.info( "direct in_place rebase updated upstream of branch: " + b) dev_git.checkout(b) dev_git.rebase(branch=upstream_clean, options=['-Xtheirs']) self.logger.info( "comparing direct in_place rebased branch: " + b + " with merged,rebased and remerged branch " + rebase_branch) out_diff = dev_git.compare_branches( b, rebase_branch) if out_diff == '': dev_git.delete(rebase_branch) else: self.logger.warning("DIFF " + b + " " + rebase_branch + "\n" + out_diff) #dev_git.merge(merge_branch, options=['-Xtheirs']) self.logger.info( "comparing direct in_place rebased %s with simply merged %s " % (b, merge_branch)) out_diff = dev_git.compare_branches( b, merge_branch) if out_diff == '': self.logger.info("removing " + merge_branch) dev_git.delete(merge_branch) else: self.logger.warning("DIFF " + b + " " + merge_branch + "\n" + out_diff) # not needed? dev_git.checkout(b) # dev_git.delete(merge_branch) dev_git.checkout(b) dev_git.fetch(name='upstream', branches=local_pr) for n, branch in local_pr.items(): self.logger.info( "itegrating pr %s (%s) into %s by updating into %s" % ( n, branch, origin_master, branch + '_update', )) dev_git.checkout(branch, newbranch=branch + '_update') dev_git.merge(upstream_clean, comment='sync with upstream develop ') dev_git.checkout(origin_master) dev_git.merge(branch + '_update', comment='merged ' + branch) dev_git.checkout(origin_master) for branch in origin_branches[1:]: self.logger.info("merging %s into %s" % (branch, origin_master)) dev_git.merge(branch, comment='merged ' + branch) else: dev_git.fetch(name='origin', branches=[origin_master]) dev_git.checkout(origin_master) else: self.logger.warning("Folder ->" + dest + "<- already existing") if origin_update: self.logger.info("Updating Folder ->" + dest + "<-") pull_options = [] for flag in pull_flags: pull_options.append('--' + flag) local_branches = dev_git.get_local_branches() for branch in origin_branches[1:]: if branch in local_branches: dev_git.checkout(branch) dev_git.sync_upstream(upstream='origin', master=branch, options=pull_options) else: dev_git.fetch(name='origin', branches=[branch]) dev_git.checkout(branch) if upstream_update: merge_options = [] if rebase_update: merge_options.append('--rebase') dev_git.sync_upstream(options=merge_options)