Forking is a typical way of code reuse, which provides a simple way for developers to create a variant software (denoted as hard fork) by copying and modifying an existing codebase. Despite of the benefits, forking also leads to duplicate efforts in software maintenance. Developers need to port patches across the hard forks to address similar bugs or implement similar features. Due to the divergence between the source project and the hard fork, patch porting is complicated, which requires an adaption regarding different implementations of the same functionality. In this work, we take the first step to automate patch porting for hard forks under a zero-shot setting. We first conduct an empirical study of the patches ported from Vim to Neovim over the last ten years to investigate the necessities of patch porting and the potential flaws in the current practice. We then propose a large language model (LLM) based approach (namely PPatHF) to automatically port patches for hard forks on a function-wise basis. Specifically, PPatHF is composed of a reduction module and a porting module. Given the pre- and post-patch versions of a function from the reference project and the corresponding function from the target project, the reduction module first slims the input functions by removing code snippets less relevant to the patch. Then, the porting module leverages a LLM to apply the patch to the function from the target project. We evaluate PPatHF on 310 Neovim patches ported from Vim. The experimental results show that PPatHF outperforms the baselines significantly. Specifically, PPatHF can correctly port 131 (42.3%) patches and automate 57% of the manual edits required for the developer to port the patch.
翻译:分支是代码复用的典型方式,通过复制并修改现有代码库,为开发者创建变体软件(称为硬分叉)提供了简单途径。尽管分支具有优势,但也导致软件维护中的重复劳动。开发者需在硬分叉间移植补丁,以解决相似缺陷或实现相似功能。由于源项目与硬分叉之间存在差异,补丁移植过程复杂,需针对同一功能的不同实现进行适配。在本研究中,我们首次尝试在零样本设置下自动化硬分叉的补丁移植。首先,我们对过去十年间从Vim移植到Neovim的补丁进行了实证研究,以探究补丁移植的必要性及当前实践中的潜在缺陷。随后,我们提出一种基于大语言模型的方法(命名为PPatHF),用于在函数级别自动为硬分叉移植补丁。具体而言,PPatHF由缩减模块和移植模块组成。给定参考项目函数补丁前后的版本及目标项目对应函数,缩减模块首先通过移除与补丁不相关的代码片段来精简输入函数;接着,移植模块利用大语言模型将补丁应用于目标项目函数。我们基于从Vim移植的310个Neovim补丁评估了PPatHF。实验结果显示,PPatHF显著优于基线方法。具体而言,PPatHF能正确移植131个补丁(占比42.3%),并自动化了开发者手动移植补丁时所需编辑量的57%。