-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.sh
More file actions
executable file
·71 lines (53 loc) · 1.59 KB
/
create.sh
File metadata and controls
executable file
·71 lines (53 loc) · 1.59 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
#!/bin/bash
#
# Create a git branch and directory with empty files, taking a title and a URL.
# Example usage:
# ./create.sh "82. Remove Duplicates from Sorted List II" https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/
# Convert a string to snake case
to_snake_case() {
local s="$1"
# Convert to lowercase, replace non-alphanumeric characters (except underscore) with underscores
s=$(echo "$s" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/_/g')
# Replace multiple underscores with a single underscore
s=$(echo "$s" | sed 's/__*/_/g')
# Remove leading/trailing underscores
s=$(echo "$s" | sed 's/^_//;s/_$//')
echo "$s"
}
TITLE="$1"
URL="$2"
# Check if a directory name was provided
if [ -z "$TITLE" ]; then
echo "Usage: $0 <directory_name>"
exit 1
fi
# Convert the raw input to snake case
DIR_NAME=$(to_snake_case "$TITLE")
# Check if it's a Git repository
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "Error: Not a Git repository. Please run this script inside a Git repository."
exit 1
fi
# Create and checkout the Git branch (always branch from main)
git checkout main
git checkout -b "$DIR_NAME"
# Create the directory
mkdir -p "$DIR_NAME"
# Create empty files under the directory
touch "$DIR_NAME/step1.c"
touch "$DIR_NAME/step2.c"
touch "$DIR_NAME/step3.c"
touch "$DIR_NAME/memo.md"
# Populate memo.md using a heredoc
cat << EOF > "$DIR_NAME/memo.md"
# $TITLE
$URL
## Comments
### step1
*
### step2
*
### step3
*
EOF
echo "Directory '$DIR_NAME' created with template files: step1.c, step2.c, step3.c, memo.md"