Explaining a diff commit file
Questionβ
Can ChatGPT understands a diff commit file, and, in case of a fix commit, explains what was the bug and what does the fix?
Short answerβ
Yes.
Step 1: Selecting a commit diffβ
See Collect examples by extracting fix commits. Then choose one in the list:
import random
random_commit = random.choice(fix_commits_changes)
An example of a fix commit:
(Tech) try to fix profiling
===========================
# android/build.gradle
+ flatDir { dirs project( ':react-native-profiling' ).file( 'libs' ) } ===========================
# android/settings.gradle
- include ':react-native-profiling'
- project(':react-native-profiling').projectDir = new File(rootProject.projectDir, '../node_modules/@pass-culture/react-native-profiling/android')
===========================
# ios/Podfile.lock
- - react-native-profiling (from `../node_modules/@pass-culture/react-native-profiling`)
+ - react-native-profiling (from `../node_modules/react-native-profiling`)
- :path: "../node_modules/@pass-culture/react-native-profiling"
+ :path: "../node_modules/react-native-profiling"
===========================
# package.json
- "version": "1.139.3",
+ "version": "1.139.2",
- "build": 10139003,
+ "build": 10139002,
Step 2: Prepare OpenAI APIβ
Just load the API key
import openai
import os
from dotenv import load_dotenv
load_dotenv("../../.env.secret")
openai.api_key = os.getenv("OPENAI_API_KEY")
Step 3: Ask ChatGPTβ
We use the legacy api with temperature to 0.3 to ask it to be a little bit creative but not too much.
response = openai.Completion.create(
model="text-davinci-003",
prompt=f"Explain what was the bug which was fixed in this commit: {random_commit}",
temperature=0.3,
max_tokens=150,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
)
print(response.choices[0].text)
NB: the PartialCommit class as a __repr__ method to convert it into a
string:
@dataclass
class FileDiff:
#...
def __repr__(self):
return (
f"# {self.filename}\n\n"
f"{self.content}"
)
@dataclass
class PartialCommit:
#...
def __repr__(self):
msg_len = len(self.message.split("\n")[0])
return (
f"{self.message}\n" +
"=" * msg_len + "\n" +
("\n" + "=" * msg_len + "\n").join(map(str, self.files))
)
Step 4: Analyse the answerβ
For the previous example, here is the answer we get:
This commit fixed a bug related to the react-native-profiling package. The bug was caused by an incorrect path in the Podfile.lock file, which was pointing to the wrong version of the package. The package.json file was also updated to reflect the correct version of the package.
- ChatGPT