<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Prajul Sahu</title>
    <description>The latest articles on DEV Community by Prajul Sahu (@prajulaaditya007).</description>
    <link>https://dev.to/prajulaaditya007</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F758841%2F8bf96a04-e7cc-48df-a118-8a4df4aee28a.jpeg</url>
      <title>DEV Community: Prajul Sahu</title>
      <link>https://dev.to/prajulaaditya007</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prajulaaditya007"/>
    <language>en</language>
    <item>
      <title>Creating a Git Pre-Commit Hook to Check Package Version Changes</title>
      <dc:creator>Prajul Sahu</dc:creator>
      <pubDate>Tue, 25 Jul 2023 05:40:31 +0000</pubDate>
      <link>https://dev.to/prajulaaditya007/pre-commit-hook-version-update-in-packagejson-d5a</link>
      <guid>https://dev.to/prajulaaditya007/pre-commit-hook-version-update-in-packagejson-d5a</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;:&lt;br&gt;
When working on projects with multiple contributors, it's essential to maintain consistency and track version changes accurately. In this blog, we'll explore how to create a pre-commit hook using Python that checks if the version in the &lt;code&gt;package.json&lt;/code&gt; file has been modified before allowing a commit. This simple step can prevent accidental version changes and ensure a smooth development process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Git repository with a &lt;code&gt;package.json&lt;/code&gt; file containing a &lt;code&gt;"version"&lt;/code&gt; field.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 1: Creating the Python Script&lt;br&gt;
First, let's create a Python script that will perform the version check. Save the following code as &lt;code&gt;pre-commit.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;read_package_version&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"package.json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check_version_change&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;old_version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;read_package_version&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;new_version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;popen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"git show HEAD:package.json"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;new_version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_version&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;old_version&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;new_version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"No changes to package version. Commit aborted."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;check_version_change&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2: Making the Script Executable&lt;br&gt;
Before we use the script as a Git hook, we need to make it executable. Open your terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x pre-commit.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Setting the execution permission for the script using &lt;code&gt;chmod +x pre-commit.py&lt;/code&gt; is specific to Linux/macOS. However, on Windows, this step is not necessary as Windows doesn't use execute permissions like Unix-based systems.&lt;/p&gt;

&lt;p&gt;Step 3: Setting Up the Hook&lt;br&gt;
Now, let's create a symbolic link from the script to the Git's &lt;code&gt;pre-commit&lt;/code&gt; hook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ln&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; ../../pre-commit.py .git/hooks/pre-commit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creating Symbolic Link:&lt;br&gt;
The command to create a symbolic link to the pre-commit script using &lt;code&gt;ln -s ../../pre-commit.py .git/hooks/pre-commit&lt;/code&gt; is specific to Linux/macOS. On Windows, you can use the mklink command to achieve the same result.&lt;/p&gt;

&lt;p&gt;For Windows, you can create a symbolic link using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mklink .git\hooks\pre-commit ..\..\pre-commit.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 4: Explanation of the Script&lt;br&gt;
Now that we have the script set up as a pre-commit hook, let's understand how it works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;read_package_version()&lt;/code&gt; function reads the &lt;code&gt;"version"&lt;/code&gt; field from the &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;check_version_change()&lt;/code&gt; function performs the version comparison. It first fetches the current version from the staged version of &lt;code&gt;package.json&lt;/code&gt; using &lt;code&gt;git show HEAD:package.json&lt;/code&gt;. It then compares it with the previous version from the latest commit. If the versions differ, it returns &lt;code&gt;True&lt;/code&gt;, allowing the commit. Otherwise, it prints a message and returns &lt;code&gt;False&lt;/code&gt;, aborting the commit.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 5: Testing the Pre-Commit Hook&lt;br&gt;
Now, whenever you attempt to commit changes, the pre-commit hook will automatically execute. It will check if the version in &lt;code&gt;package.json&lt;/code&gt; has been changed. If the version has been updated, the commit will proceed as usual. If there are no changes to the version, the commit will be aborted with the message "No changes to package version. Commit aborted."&lt;/p&gt;

&lt;p&gt;Conclusion:&lt;br&gt;
By implementing this simple pre-commit hook, you can ensure that version changes in your &lt;code&gt;package.json&lt;/code&gt; file are tracked accurately. This prevents unintentional version changes and helps maintain a more organized development process within your team.&lt;/p&gt;

&lt;p&gt;Whenever you commit your code, if &lt;code&gt;package.json&lt;/code&gt; is not staged for commit or even it is staged but the &lt;code&gt;"version"&lt;/code&gt; is not changed. With this hook in place, you can confidently manage version changes in your project and improve collaboration among team members. Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>git</category>
      <category>react</category>
      <category>terminal</category>
    </item>
  </channel>
</rss>
