14909 - Red Cape Flying Cat's git project   

Description

Story

(The following story is irrelevant to the problem description.)

To save computer science students from the torture of teammates who don't know how to use git, Red Cape Flying Cat uses his knowledge from Introduction to Programming (II) to develop a git tool for everyone to use.

You, while developing alongside him, suddenly remember that you recently learned some string functions. Therefore, you decide to be responsible for some of the string processing parts.

Problem Statement

This is a partial judge problem.

In a single test case, the total number of function calls will not exceed 100000.

You need to implement the following five functions:

1. Determine Command Type

Goal

Your goal is to implement a function that determines the type of a command:

int get_command_type(const char *command);

If the command:

  • starts with git add, then return 0
  • starts with git commit, then return 1
  • starts with git log, then return 2

Constraint

  • The length of command is guaranteed not to exceed 50 characters.
  • command is a git command, and its format must be one of the following three:
    1. git add <file1> <file2> ...
    2. git commit -m "<message>"
    3. git log
  • File is composed of [1~10 uppercase/lowercase English letters].[1~10 uppercase/lowercase English letters].
  • At least have one file
  • Message length is 0~50, composed of uppercase/lowercase English letters, digits, spaces, and colons.

Example

code:

int t1 = get_command_type("git add a.txt b.txt");
if (t1 == 0) printf("Command type: add\n");

int t2 = get_command_type("git commit -m \"feat: add search bar\"");
if (t2 == 1) printf("Command type: commit\n");

int t3 = get_command_type("git log");
if (t3 == 2) printf("Command type: log\n");

output:

Command type: add
Command type: commit
Command type: log

Hint

You can use the following functions to implement the solution:

 

2. Validate Commit Message

Goal

Your goal is to implement a function that checks whether a commit message is valid:

int is_valid_commit_message(const char *message);

If the length of message is between 1 and 20 (inclusive), it is considered valid, and you should return 1; if it is an empty string or its length exceeds 20 characters, return 0.

Constraint

  • The length of message is 0~50 characters, composed of uppercase/lowercase English letters, digits, spaces, and colons.

Example

code:

if (is_valid_commit_message("") == 1) {
    printf("Commit message is valid\n");
} else {
    printf("Commit message is invalid\n");
}

if (is_valid_commit_message("feat: add search bar") == 1) {
    printf("Commit message is valid\n");
} else {
    printf("Commit message is invalid\n");
}

if (is_valid_commit_message("chore: I dont know what to write") == 1) {
    printf("Commit message is valid\n");
} else {
    printf("Commit message is invalid\n");
}

output:

Commit message is invalid
Commit message is valid
Commit message is invalid

Hint

You can use the following functions to implement the solution:

3. Parse git add File List

Goal

Your goal is to implement a function that parses the list of files following a git add command:

int parse_filenames(char *filenames, char *result[]);

You need to sequentially place each file from left to right into positions 0, 1, 2... of the result array, and return the total number of files parsed.

Constraint

  • filenames is a string with the format <file1> <file2> ... <fileN>, where each file is separated by exactly one space.
  • At least have one file.
  • file is composed of [1~10 uppercase/lowercase English letters].[1~10 uppercase/lowercase English letters].

Example

code:

char filenames[] = "a.txt b.txt c.txt";
char *result[55] = {NULL};
int k = parse_filenames(filenames, result);

printf("There have %d filenames: [%s, %s, %s]\n", k, result[0], result[1], result[2]);

output:

There have 3 filenames: [a.txt, b.txt, c.txt]

Hint

You can use the following functions to implement the solution:

4. Truncate Commit Hash ID

Goal

Your goal is to implement a function that generates a short Hash ID:

void get_short_commit_id(const char *hex, char *result);

You need to copy the "first 7 characters" of hex into the result array, and append the C string termination character \0 at the end.

Constraint

  • hex is a string with a length between 7 and 50, composed entirely of digits 0~9 and lowercase letters a~f.

Example

code:

char result[55] = {0};
get_short_commit_id("923fba7d39452eb573aa3834", result);
printf("Short commit ID: %s\n", result);

output:

Short commit ID: 923fba7

Hint

You can use the following functions to implement the solution:

5. Auto-generate Conventional Commit

Goal

Your goal is to implement a function that automatically applies the Commit message convention:

void generate_commit_message(const char *type, const char *message, char *result);

You need to combine them into <type>: <message> and place it into result, according to the following rules:

  1. If type is exactly equal to featfix, or docs, directly use that type as the classification prefix.
  2. If type is any string other than these three, forcibly change the classification to chore.

Constraint

  • The length of type is 1~10, composed of uppercase/lowercase English letters.
  • The length of message is 1~20, composed of uppercase/lowercase English letters and spaces.

Example

code:

char res1[100] = {0}, res2[100] = {0};
generate_commit_message("feat", "add search bar", res1);
generate_commit_message("random", "fix typo", res2);
printf("Generated commit message: %s\n", res1);
printf("Generated commit message: %s\n", res2);

output:

Generated commit message: feat: add search bar
Generated commit message: chore: fix typo

Hint

You can use the following functions to implement the solution:

Input

The first line contains a single integer Q (1 <= Q <= 100000), representing the total number of operations.

Each line of input is guaranteed to be no longer than 50 characters.

The above five operations are input in the following format:

1 <command>
2 "<message>"
3 <file1> <file2> ... <fileN>
4 <hex>
5 <type> "<message>"

Output

The output should have Q lines, corresponding to the output of each operation.

Command type: <result>
Commit message is valid
There have <k> filenames: [<file1>, <file2>, ...]
Short commit ID: <result>
Generated commit message: <result>

Sample Input  Download

Sample Output  Download

Partial Judge Code

14909.c

Partial Judge Header

14909.h

Tags




Discuss