Git is a popular distributed version control system designed to handle everything from small to large projects with speed and efficiency. By maintaining your RPG code in Git, you can improve collaboration, version control, and maintainability of your applications on the IBMi platform. In this tutorial, we’ll walk you through the process of setting up a Git repository for your RPG code and demonstrate how to manage your code effectively.
Prerequisites
Before you begin, make sure you have the following:
- Git installed on your local machine. You can download it from the official website: https://git-scm.com/
- Access to an IBMi system with your RPG code.
- An IDE that can handle RPG code, such as RDi or VS Code.
Step 1: Create a Git Repository
First, create a new directory on your local machine to store your RPG code:
mkdir my-rpg-project cd my-rpg-project
Next, initialize a new Git repository in the directory:
git init
Step 2: Add RPG Source Files to the Repository
Transfer your RPG source files from the IBMi system to your local machine using an FTP client or other file transfer methods. Once you have the files, copy them into the ‘my-rpg-project’ directory.
After copying the files, add them to the Git repository:
git add . git commit -m "Initial commit of RPG source files"
Step 3: Manage Changes and Collaborate
With your RPG code in Git, you can now track changes, collaborate with other developers, and manage your codebase more effectively. Here are some basic Git commands to help you get started:
git status– Check the status of your repository and see which files have been modified or staged for commit.git diff– View the differences between your working directory and the latest commit.git log– Review the commit history of your repository.git checkout– Switch between different branches or commits in your repository.git pull– Update your local repository with the latest changes from a remote repository.git push– Push your local changes to a remote repository.git branch– List, create, or delete branches in your repository.git merge– Merge changes from one branch into another.
Step 4: Set Up a Remote Repository
To collaborate with other developers and store your code securely off-site, set up a remote Git repository on a platform like GitHub, GitLab, or Bitbucket. After creating a new remote repository, follow the instructions provided by the platform to add the remote repository to your local Git configuration:
git remote add origin [REMOTE_REPOSITORY_URL]
Replace `[REMOTE_REPOSITORY_URL]` with the URL of your remote repository. Once the remote repository is set up, you can push your local changes to the remote repository:
git push -u origin main
From this point on, you and your team can collaborate on the same codebase, pushing and pulling changes as needed.
Step 5: Maintain RPG Code in Git
As you work on your RPG code, make sure to commit your changes regularly and follow best practices for version control:
- Write clear, concise commit messages that describe the changes you’ve made.
- Commit related changes together, and avoid mixing unrelated changes in a single commit.
- Use branches to work on new features or bug fixes, and merge them back into the main branch when they’re ready.
- Resolve conflicts and merge changes carefully to maintain a clean commit history.
- Keep your local repository in sync with the remote repository by pulling changes regularly.
Real World Example
Step 1: Design the Customer Management System
First, the team designs the customer management system, identifying the necessary components and their relationships. For instance, they decide to implement the following modules:
- Customer Master File (CMF) module for storing customer information.
- Order Processing module for handling customer orders.
- Invoice Generation module for creating invoices for completed orders.
- Reports module for generating sales and customer reports.
Step 2: Set Up the Git Repository
The team sets up a Git repository for the project, initializing it on their local machines and creating a remote repository on a platform like GitHub or GitLab for collaboration. They divide the tasks among themselves and create branches for each module:
git checkout -b customer_master_file git checkout -b order_processing git checkout -b invoice_generation git checkout -b reports
Step 3: Develop the Modules
Each developer works on their assigned module in their branch, committing changes regularly and pushing them to the remote repository. For example, the developer responsible for the Customer Master File module might create the following files:
- CMF_DCLF: Data description specifications for the Customer Master File.
- CMF_ADD: RPG code for adding a new customer.
- CMF_UPDATE: RPG code for updating existing customer information.
- CMF_DELETE: RPG code for deleting a customer.
- CMF_VIEW: RPG code for displaying customer details.
As they develop the module, they commit and push their changes:
git add . git commit -m "Add initial CMF module files" git push origin customer_master_file
Sample RPG Code for the Modules
Here are some sample RPG code snippets for the modules mentioned in the real-world example. Note that these examples are simplified and may not cover all aspects of a complete customer management system.
Customer Master File (CMF) Module
CMF_DCLF: Data description specifications for the Customer Master File:
**free dcl-f CMF_DCLF usage(*input:*output:*update:*delete) keyed; dcl-ds CMF_Record; CustomerId int(10); CustomerName varchar(50); PhoneNumber char(10); end-ds;
CMF_ADD: RPG code for adding a new customer:
**free
ctl-opt main(MainProcedure);
dcl-proc MainProcedure;
dcl-s newCustomerId int(10);
dcl-s newCustomerName varchar(50);
dcl-s newPhoneNumber char(10);
newCustomerId = 1001;
newCustomerName = 'John Doe';
newPhoneNumber = '5551234567';
AddCustomer(newCustomerId, newCustomerName, newPhoneNumber);
*INLR = *ON;
end-proc;
dcl-proc AddCustomer;
dcl-pi *N;
customerId int(10);
customerName varchar(50);
phoneNumber char(10);
end-pi;
CMF_Record.CustomerId = customerId;
CMF_Record.CustomerName = customerName;
CMF_Record.PhoneNumber = phoneNumber;
write CMF_DCLF CMF_Record;
end-proc;
Order Processing Module
Assuming that there is a separate file for orders (ORDERS_DCLF), a simplified example for adding a new order could look like this:
**free
ctl-opt main(MainProcedure);
dcl-f ORDERS_DCLF usage(*input:*output:*update:*delete) keyed;
dcl-ds Order_Record;
OrderId int(10);
CustomerId int(10);
ProductId int(10);
Quantity int(10);
OrderDate date;
end-ds;
dcl-proc MainProcedure;
dcl-s newOrderId int(10);
dcl-s customerId int(10);
dcl-s productId int(10);
dcl-s quantity int(10);
dcl-s orderDate date;
newOrderId = 1;
customerId = 1001;
productId = 2001;
quantity = 5;
orderDate = %date();
AddOrder(newOrderId, customerId, productId, quantity, orderDate);
*INLR = *ON;
end-proc;
dcl-proc AddOrder;
dcl-pi *N;
orderId int(10);
customerId int(10);
productId int(10);
quantity int(10);
orderDate date;
end-pi;
Order_Record.OrderId = orderId;
Order_Record.CustomerId = customerId;
Order_Record.ProductId = productId;
Order_Record.Quantity = quantity;
Order_Record.OrderDate = orderDate;
write ORDERS_DCLF Order_Record;
end-proc;
Step 4: Integrate the Modules
Once all developers have completed their modules, they merge their branches back into the main branch:
git checkout main git merge customer_master_file git merge order_processing git merge invoice_generation git merge reports
They resolve any conflicts and commit the merged code, ensuring that the application functions as expected.
Step 5: Ongoing Development and Maintenance
As the team continues to develop and maintain the customer management system, they use Git to track changes, collaborate, and manage the codebase effectively. They create new branches for bug fixes and feature enhancements, and they use pull requests to review and discuss proposed changes before merging them into the main branch.
Conclusion
In this tutorial, we’ve shown you how to maintain your RPG code in Git, allowing you to improve collaboration, version control, and maintainability of your applications on the IBMi platform. By following these steps and best practices, you can ensure that your RPG codebase remains organized and easy to manage as your application grows and evolves.