-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic-comparison.sh
More file actions
184 lines (150 loc) · 5.56 KB
/
basic-comparison.sh
File metadata and controls
184 lines (150 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
# Example: Basic File Comparison
# Purpose: Demonstrates basic file comparison functionality with different output formats
# Usage: ./basic-comparison.sh
set -e
echo "Smart Code Diff Example: Basic File Comparison"
echo "=============================================="
# Check if smart-diff-cli is available
if ! command -v smart-diff-cli &> /dev/null; then
echo "Error: smart-diff-cli not found in PATH"
echo "Please build the project and add target/release to your PATH"
exit 1
fi
# Create sample directory
SAMPLE_DIR="$(dirname "$0")/../sample-code/java"
mkdir -p "$SAMPLE_DIR"
# Create sample Java files for comparison
cat > "$SAMPLE_DIR/Calculator.java" << 'EOF'
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int multiply(int a, int b) {
return a * b;
}
public boolean isEven(int number) {
return number % 2 == 0;
}
public double divide(double a, double b) {
if (b == 0) {
throw new IllegalArgumentException("Division by zero");
}
return a / b;
}
}
EOF
cat > "$SAMPLE_DIR/Calculator_refactored.java" << 'EOF'
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int multiply(int a, int b) {
return a * b;
}
// Renamed method with extracted logic
public boolean isNumberEven(int number) {
return checkEvenness(number);
}
// Extracted method
private boolean checkEvenness(int number) {
return number % 2 == 0;
}
public double divide(double a, double b) {
if (b == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
return a / b;
}
// New method
public int subtract(int a, int b) {
return a - b;
}
}
EOF
echo "Created sample Java files:"
echo " - $SAMPLE_DIR/Calculator.java"
echo " - $SAMPLE_DIR/Calculator_refactored.java"
echo
# Example 1: Basic comparison with default settings
echo "Example 1: Basic Comparison (Text Output)"
echo "----------------------------------------"
smart-diff-cli compare "$SAMPLE_DIR/Calculator.java" "$SAMPLE_DIR/Calculator_refactored.java"
echo
# Example 2: JSON output for programmatic processing
echo "Example 2: JSON Output"
echo "---------------------"
smart-diff-cli compare --output json "$SAMPLE_DIR/Calculator.java" "$SAMPLE_DIR/Calculator_refactored.java" | jq '.similarity'
echo
# Example 3: Custom similarity threshold
echo "Example 3: Custom Similarity Threshold (0.8)"
echo "--------------------------------------------"
smart-diff-cli compare --threshold 0.8 "$SAMPLE_DIR/Calculator.java" "$SAMPLE_DIR/Calculator_refactored.java"
echo
# Example 4: Ignore whitespace changes
echo "Example 4: Ignoring Whitespace Changes"
echo "-------------------------------------"
# Create a version with different formatting
cat > "$SAMPLE_DIR/Calculator_formatted.java" << 'EOF'
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int multiply(int a, int b) {
return a * b;
}
public boolean isEven(int number) {
return number % 2 == 0;
}
public double divide(double a, double b) {
if (b == 0) {
throw new IllegalArgumentException("Division by zero");
}
return a / b;
}
}
EOF
echo "Without --ignore-whitespace:"
smart-diff-cli compare "$SAMPLE_DIR/Calculator.java" "$SAMPLE_DIR/Calculator_formatted.java" | head -5
echo
echo "With --ignore-whitespace:"
smart-diff-cli compare --ignore-whitespace "$SAMPLE_DIR/Calculator.java" "$SAMPLE_DIR/Calculator_formatted.java" | head -5
echo
# Example 5: HTML output for visual inspection
echo "Example 5: HTML Output Generation"
echo "--------------------------------"
OUTPUT_DIR="$(dirname "$0")/output"
mkdir -p "$OUTPUT_DIR"
smart-diff-cli compare --output html "$SAMPLE_DIR/Calculator.java" "$SAMPLE_DIR/Calculator_refactored.java" > "$OUTPUT_DIR/comparison.html"
echo "HTML report generated: $OUTPUT_DIR/comparison.html"
echo "Open this file in a web browser to view the interactive comparison"
echo
# Example 6: Force language detection
echo "Example 6: Force Language Detection"
echo "----------------------------------"
# Create a file with wrong extension
cp "$SAMPLE_DIR/Calculator.java" "$SAMPLE_DIR/Calculator.txt"
echo "Comparing .txt file with forced Java language detection:"
smart-diff-cli compare --language java "$SAMPLE_DIR/Calculator.txt" "$SAMPLE_DIR/Calculator_refactored.java" | head -5
echo
# Example 7: Verbose output with timing
echo "Example 7: Verbose Output with Timing"
echo "------------------------------------"
time smart-diff-cli compare --output json "$SAMPLE_DIR/Calculator.java" "$SAMPLE_DIR/Calculator_refactored.java" | jq '.execution_time_ms'
echo
# Cleanup
echo "Cleaning up temporary files..."
rm -f "$SAMPLE_DIR/Calculator_formatted.java" "$SAMPLE_DIR/Calculator.txt"
echo "Basic comparison examples completed successfully!"
echo
echo "Key takeaways:"
echo "1. Smart Code Diff detects structural changes, not just text differences"
echo "2. Function renaming and extraction are identified as refactoring patterns"
echo "3. Different output formats serve different use cases"
echo "4. Similarity thresholds can be adjusted based on requirements"
echo "5. Language detection can be forced when file extensions are misleading"
echo
echo "Next steps:"
echo "- Try the directory comparison example: ./directory-analysis.sh"
echo "- Explore the web interface at http://localhost:3000 (after starting smart-diff-server)"
echo "- Check the API examples in ../api/ directory"