diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7d78bca --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +# Default ignored files +/shelf/ +/.idea/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Environment-dependent path to Maven home directory +/mavenHomeManager.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +/target +.idea/ +*.iml +# Compiled Maven output +/target/ +target/ + +# IntelliJ IDEA project files +*.iws +*.ipr + +# Compiled class files +*.class + +# Logs +*.log \ No newline at end of file diff --git a/README.md b/README.md index b03cf5a..dac48fe 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,144 @@ -# Advanced-git -This repo is a workshop assignment to practice conflict resolvation in git + +# 🚀 WS 11 - Advanced Git & Github + +--- + +## 📖 Overview +In this assignment, you will simulate multiple developers working on the same project using different Git branches. +You will create parallel development scenarios, encounter merge conflicts, and practice resolving them using Git. By the end, you should feel confident handling common conflict situations that occur in real-world software development. + +--- + +## Prerequisites +- Git installed +- A GitHub account +- Java SDK 25 +- Maven +--- +## 🎯 Objectives +By completing this assignment, you will: +- Become comfortable creating and switching between Git branches +- Learn how to resolve same-line content conflicts +- Practice resolving: +- Modify/modify conflicts +- Modify/delete conflicts +- Rename conflicts +- Understand how Git tracks file changes across branches + +--- +## Project Structure +- **Calculator.java:** Interface which defines calculator methods +- **BasicCalculator.java:** Implements Calculator with basic arithmetic +- **MathHelper.java:** Static utility methods (multiply, square, max) +- **ReportGenerator.java:** Uses Calculator and MathHelper to produce formatted output +--- + +## 📦 Task 1 – Refactoring Conflict + +### Scenario: +Two developers are working in parallel:
+- Developer A refactors the code by renaming a method. +- Developer B continues developing a new feature using the old method name. + +When their work is merged, a conflict will occur. + +1. First fork & clone the repository and create develop branch: + +``` +git clone https://github.com//conflict-demo.git +git checkout -b develop +``` + +2. Create two feature branches(feature/refactor-multiply & feature/change-multiply). we will assume they are 2 separate developers working on project. One of them will refactor project by renaming a method across whole project, while other developer is still working with old name. +``` +git checkout develop +git checkout -b feature/refactor-multiply +git checkout develop +git checkout -b feature/change-multiply +``` + +3. From feature/refactor-multiply branch, rename "multiply" method to "product" everywhere. +``` +git checkout feature/refactor-multiply +# TODO: rename method "multiply" to "product" in Calculator, BasicCalculator, MathHelper and ReportGenerator +git add . +git commit -m "refactor: renamed multiply to product" +git checkout develop +git merge feature/refactor-multiply +``` + +4. From feature/change-multiply branch, change the body of multiply method +``` +git checkout feature/change-multiply +# TODO: change the body of MathHelper.multiply (e.g., loop-based multiplication) +git add . +git commit -m "multiply method body changed" +git checkout develop +git merge feature/change-multiply # CONFLICT +``` + +5. Resolve the conflict +``` +# TODO: Open conflicted file(s), decide to keep the `product` name, and apply the new logic. +git add . +git commit -m "conflict resolved: keep rename, apply new multiply logic" +``` + +--- + +## 📦 Task 2 – Deletion Conflict + +### Scenario + +Two developers are working on the same interface: + +- Developer A decides that the `Calculator` interface is no longer needed and deletes it from the project. +- Developer B is still working on the interface and adds a new method to it. + +When the branches are merged, Git will detect a **modify/delete conflict** because one branch deleted the file while the other modified it. + +1. Create Two branches called feature/remove-calculator and feature/add-method-to-interface. We will delete interface file in project in first branch, while second one is working on it. Then we will solve conflict that happends in merging. +``` +git checkout develop +git checkout -b feature/remove-calculator +git checkout develop +git checkout -b feature/add-method-to-interface +``` + +2. From feature/remove-calculator branch, delete interface file completely: +``` +git checkout feature/remove-calculator +git rm src/main/java/com/ap/Calculator.java +git commit -m "remove Calculator interface" +git checkout develop +git merge feature/remove-calculator +``` + +3. From feature/add-method-to-interface add method to deleted interface +``` +git checkout feature/add-method-to-interface +# TODO: add "double calculateArea(double,double)" to Calculator, implement in BasicCalculator +git add . +git commit -m "add calculateArea() method" +git checkout develop +git merge feature/add-method-to-interface # CONFLICT +``` + +4. Resolve +- Keep the `Calculator` interface. +- Restore the deleted file. +- Add the new `calculateArea()` method to the interface. +``` +git add . +git commit -m "resolved: Restored deleted interface and added calculateArea " +``` + +--- + +## 📤 Deliverable +- Push your branches with all merge commits.
+- Show output of your git history: +``` +git log --oneline --graph --all --decorate +``` + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..d675918 --- /dev/null +++ b/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + com.ap + Advanced-git + 1.0-SNAPSHOT + + + 25 + 25 + UTF-8 + + + + org.junit.jupiter + junit-jupiter + 5.12.2 + test + + + + \ No newline at end of file diff --git a/src/main/java/com/ap/BasicCalculator.java b/src/main/java/com/ap/BasicCalculator.java new file mode 100644 index 0000000..4852598 --- /dev/null +++ b/src/main/java/com/ap/BasicCalculator.java @@ -0,0 +1,18 @@ +package com.ap; + +public class BasicCalculator implements Calculator { + @Override + public int add(int a, int b) { + return a + b; + } + + @Override + public int subtract(int a, int b) { + return a - b; + } + + @Override + public int multiply(int a, int b) { + return a * b; + } +} \ No newline at end of file diff --git a/src/main/java/com/ap/Calculator.java b/src/main/java/com/ap/Calculator.java new file mode 100644 index 0000000..0220dcb --- /dev/null +++ b/src/main/java/com/ap/Calculator.java @@ -0,0 +1,7 @@ +package com.ap; + +public interface Calculator { + int add(int a, int b); + int subtract(int a, int b); + int multiply(int a, int b); +} \ No newline at end of file diff --git a/src/main/java/com/ap/Main.java b/src/main/java/com/ap/Main.java new file mode 100644 index 0000000..76b8f00 --- /dev/null +++ b/src/main/java/com/ap/Main.java @@ -0,0 +1,8 @@ +package com.ap; + +public class Main { + public static void main(String[] args) { + ReportGenerator generator = new ReportGenerator(); + generator.generate(5, 3); + } +} \ No newline at end of file diff --git a/src/main/java/com/ap/MathHelper.java b/src/main/java/com/ap/MathHelper.java new file mode 100644 index 0000000..d624768 --- /dev/null +++ b/src/main/java/com/ap/MathHelper.java @@ -0,0 +1,15 @@ +package com.ap; + +public class MathHelper { + public static int multiply(int a, int b) { + return a * b; + } + + public static int square(int x) { + return x * x; + } + + public static int max(int a, int b) { + return a > b ? a : b; + } +} \ No newline at end of file diff --git a/src/main/java/com/ap/ReportGenerator.java b/src/main/java/com/ap/ReportGenerator.java new file mode 100644 index 0000000..2201769 --- /dev/null +++ b/src/main/java/com/ap/ReportGenerator.java @@ -0,0 +1,13 @@ +package com.ap; + +public class ReportGenerator { + public void generate(int x, int y) { + Calculator calc = new BasicCalculator(); + int sum = calc.add(x, y); + int diff = calc.subtract(x, y); + int product = calc.multiply(x, y); + int max = MathHelper.max(x, y); + System.out.println("Sum: " + sum + ", Diff: " + diff + + ", Product: " + product + ", Max: " + max); + } +} \ No newline at end of file diff --git a/src/test/java/CalculatorTest.java b/src/test/java/CalculatorTest.java new file mode 100644 index 0000000..93d2bbf --- /dev/null +++ b/src/test/java/CalculatorTest.java @@ -0,0 +1,26 @@ + +import com.ap.BasicCalculator; +import com.ap.Calculator; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + + +public class CalculatorTest { + @Test + void testAdd() { + Calculator calc = new BasicCalculator(); + assertEquals(5, calc.add(2, 3)); + } + + @Test + void testSubtract() { + Calculator calc = new BasicCalculator(); + assertEquals(2, calc.subtract(5, 3)); + } + + @Test + void testMultiply() { + Calculator calc = new BasicCalculator(); + assertEquals(6, calc.multiply(2, 3)); + } +} \ No newline at end of file