diff options
author | avahe-kellenberger | 2019-08-05 23:36:40 -0400 |
---|---|---|
committer | avahe-kellenberger | 2019-08-05 23:36:40 -0400 |
commit | 1f004fa1821ee01aada03193cbd1002f8f2745b3 (patch) | |
tree | 1fa828d1744289e3e1fb758763aec4b2a14f246a | |
parent | 88c9335d917a2596280f1013d8bbe52920dfdfec (diff) | |
download | aur-1f004fa1821ee01aada03193cbd1002f8f2745b3.tar.gz |
Updated to new version.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | PKGBUILD | 4 | ||||
-rw-r--r-- | README.md | 39 | ||||
-rw-r--r-- | pair.c | 302 |
4 files changed, 2 insertions, 344 deletions
diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 2518e23f6cb9..000000000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.vscode/**
\ No newline at end of file @@ -1,6 +1,6 @@ # Maintainer: Avahe Kellenberger <avahe@protonmail.ch> pkgname='git-pair' -pkgver='1.0.3' +pkgver='1.0.4' pkgrel='2' pkgdesc="Pair programming tool to define co-authors in git commits." arch=('x86_64') @@ -9,7 +9,7 @@ license=('GPL2') depends=('git') makedepends=('gcc') source=("$url/archive/v$pkgver.tar.gz") -md5sums=('10b852babbc8c8140b2ba990bf38a467') +md5sums=('ee9caf6463728547ccb89e5ae1d91d88') build() { cd "$pkgname-$pkgver" diff --git a/README.md b/README.md deleted file mode 100644 index 0267fc4c5d8e..000000000000 --- a/README.md +++ /dev/null @@ -1,39 +0,0 @@ -# git-pair -Pair Programming git Management Tool - -# Installation - -## Arch Linux based - -[Install from the AUR](https://aur.archlinux.org/packages/git-pair/) - -## Build from source - -```sh -$ gcc pair.c -o git-pair -``` - -# Usage - -Run `./git-pair help` to see available commands. - -<hr> - -## Initialize your workspace by adding and selecting git authors - -```sh -$ ./git-pair init -``` - -## To select or change the current author/co-author, execute without parameters - -```sh -$ ./git-pair -``` - -## Add new authors - -```sh -$ ./git-pair add -``` - diff --git a/pair.c b/pair.c deleted file mode 100644 index b2382eaeec62..000000000000 --- a/pair.c +++ /dev/null @@ -1,302 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#define NO_FORMAT "\033[0m" -#define BOLD "\033[1m" -#define GREEN "\033[38;5;10m" -#define RED "\033[38;5;203m" -#define YELLOW "\033[38;5;226m" - -const char *authors_file_name = ".gitauthors"; -const char *commit_template_path = ".git/commit-template"; - -const char *title = -" _ _ _\n" -" __ _(_) |_ _ __ __ _(_)_ __\n" -" / _` | | __| | '_ \\ / _` | | '__|\n" -" | (_| | | |_ | |_) | (_| | | |\n" -" \\__, |_|\\__| | .__/ \\__,_|_|_|\n" -" |___/ |_|\n" -" -------------------------------------\n\n" -; - -int init(void); -int prompt_add_author(void); -int add_author(void); -int select_authors(void); -int select_author_index(int author_count, char *prompt); -void display_available_authors(char **authors, int author_count); -void free_authors(char **authors, int author_count); -char **read_authors(int *length); - -int set_author(char *name, char *email); -int set_author_name(char *name); -int set_author_email(char *email); -int set_co_author(char *name, char *email); -int set_commit_template(void); - -void print_help(void); -void print_title(void); - -int main(int argc, char *argv[]) { - if (argc < 2) { - select_authors(); - } else { - const char *param = argv[1]; - if (strcmp("add", param) == 0) { - int added = prompt_add_author(); - printf("%sAuthors added: %d%s\n", GREEN, added, NO_FORMAT); - } else if (strcmp("init", param) == 0) { - init(); - } else if (strcmp("help", param) == 0) { - print_help(); - } else { - printf("%sInvalid input ", RED); - printf("- run the `help` command to see parameter options.%s\n", NO_FORMAT); - } - } - return 0; -} - -/** - * - */ -int init(void) { - print_title(); - - // Add authors. - if (prompt_add_author() > 0) { - return select_authors(); - } - return -1; -} - -/** - * Asks the user if they want to add an author, until they explicitly exit. - * @return The number of authors added. - */ -int prompt_add_author(void) { - int count = 1; - while (add_author() == 0) { - printf("%s\nPress enter to add an author, or q to exit:%s ", RED, NO_FORMAT); - // Exit if the user only enters q. - if (getchar() == 'q') { - break; - } - printf("\n"); - count++; - } - return count; -} - -/** - * Adds an author entry to the authors file. - * @return 0 if an author was added successfully. - */ -int add_author(void) { - // Prompt for author name. - char author_name[BUFSIZ]; - printf("%sEnter author's full name:%s ", GREEN, NO_FORMAT); - fgets(author_name, BUFSIZ, stdin); - author_name[strcspn(author_name, "\n")] = '\0'; - - // Prompt for author email. - char author_email[BUFSIZ]; - printf("%sEnter author's email:%s ", GREEN, NO_FORMAT); - fgets(author_email, BUFSIZ, stdin); - author_email[strcspn(author_email, "\n")] = '\0'; - - if (author_name == NULL || author_email == NULL) { - printf("%sNo author added - exiting.%s", RED, NO_FORMAT); - return -1; - } - - // Open authors file for appending. - FILE *authors_file = fopen(authors_file_name, "a+"); - - // Create entry format. - char entry[BUFSIZ]; - snprintf(entry, BUFSIZ, "%s:<%s>\n", author_name, author_email); - - // Write entry to the file. - fputs(entry, authors_file); - - // Close file on exit. - return fclose(authors_file); -} - -/** - * Selects the author and (options) co-author of future commits. - * @return 0 if an author was selected successfully. - */ -int select_authors(void) { - int author_count = 0; - char **authors = read_authors(&author_count); - - // Show available authors. - display_available_authors(authors, author_count); - - // Set the author. - - int index = select_author_index(author_count, "\n%sSelect the author:%s "); - if (index < -1 || index > author_count - 1) { - printf("%sIndex out of bounds - exiting.%s\n", RED, NO_FORMAT); - exit(1); - } - - char *entry, *name, *email; - if (index == -1) { - set_author("", ""); - printf("%sRemoved author.%s\n", RED, NO_FORMAT); - } else { - entry = index == -1 ? "" : strdup(authors[index]); - name = index == -1 ? "" : strsep(&entry, ":"); - email = entry; - if (set_author(name, email) != 0) { - return -1; - } - printf("%sSet git user and email as %s %s%s\n\n", GREEN, name, email, NO_FORMAT); - } - - - // Set the co-author. - index = select_author_index(author_count, "%sSelect the co-author:%s "); - if (index < -1 || index > author_count - 1) { - printf("%sIndex out of bounds - exiting.%s\n", RED, NO_FORMAT); - exit(1); - } - - if (index == -1) { - set_co_author("", ""); - printf("%sRemoved co-author.%s\n", RED, NO_FORMAT); - } else { - entry = strdup(authors[index]); - name = strsep(&entry, ":"); - email = entry; - if (set_co_author(name, email) != 0) { - return -1; - } - - if (set_commit_template() != 0) { - return -1; - } - printf("%sSet co-author as: %s %s%s\n", GREEN, name, email, NO_FORMAT); - } - - free_authors(authors, author_count); - return 0; -} - -/** - * Prompts the user for an index to select, - * which is associated with a git author. - */ -int select_author_index(int author_count, char *prompt) { - int index, item_count; - do { - printf(prompt, YELLOW, NO_FORMAT); - item_count = scanf("%d", &index); - if (item_count == EOF) { - exit(1); - } - } while (item_count == 0); - return index - 1; -} - -/** - * Displays all authors in the authors file. - */ -void display_available_authors(char **authors, int author_count) { - // Display authors on each line to select for author, then co-author. - printf("\t%s[%d]%s: %s%s%s\n", GREEN, 0, NO_FORMAT, RED, "Remove current author from role", NO_FORMAT); - for (int i = 0; i < author_count; i++) { - printf("\t%s[%d]%s: %s\n", GREEN, i + 1, NO_FORMAT, authors[i]); - } -} - -/** - * Frees the authors array. - */ -void free_authors(char **authors, int author_count) { - for (int i = 0; i < author_count; i++) { - free(authors[i]); - } - free(authors); -} - -/** - * @return All author entries in the authors file. - */ -char **read_authors(int *length) { - FILE *authors_file = fopen(authors_file_name, "r"); - // Check if authors file exists. - if (authors_file == NULL) { - printf("%sFile %s not in directory.%s\n", RED, authors_file_name, NO_FORMAT); - printf("Run with the init parameter to create the file and add code authors.\n"); - exit(1); - } - - // Store all authors in array. - char **authors = malloc(255 * sizeof(*authors)); - char buff[255]; - int i; - for (i = 0; fgets(buff, 255, authors_file); i++) { - buff[strcspn(buff, "\n")] = '\0'; - authors[i] = malloc(strlen(buff) + 1); - strcpy(authors[i], buff); - } - *length = i; - fclose(authors_file); - return authors; -} - -/** - * Sets the author via git config. - * @see set_author_name - * @see set_author_email - */ -int set_author(char *name, char *email) { - return set_author_name(name) && set_author_email(email); -} - -int set_author_name(char *name) { - char git_cmd[BUFSIZ]; - snprintf(git_cmd, BUFSIZ, "git config user.name \"%s\"", name); - return system(git_cmd); -} - -int set_author_email(char *email) { - char git_cmd[BUFSIZ]; - snprintf(git_cmd, BUFSIZ, "git config user.email \"%s\"", email); - return system(git_cmd); -} - -int set_commit_template(void) { - char git_cmd[BUFSIZ]; - snprintf(git_cmd, BUFSIZ, "git config commit.template \"%s\"", commit_template_path); - return system(git_cmd); -} - -int set_co_author(char *name, char *email) { - FILE *template = fopen(commit_template_path, "w"); - if (strlen(name) > 0 || strlen(email) > 0) { - char entry[BUFSIZ]; - snprintf(entry, BUFSIZ, "\n\nCo-authored-by: %s %s", name, email); - fputs(entry, template); - } - return fclose(template); -} - -void print_help(void) { - printf("%s%sCommands:%s\n\n", RED, BOLD, NO_FORMAT); - printf(" %s<no command>%s - Select an author and optional co-author which exists in %s\n", GREEN, NO_FORMAT, authors_file_name); - printf(" %sinit%s - Initiate the setup for git pair\n", GREEN, NO_FORMAT); - printf(" %sadd%s - Add an author to your %s file for selection\n", GREEN, NO_FORMAT, authors_file_name); - printf(" %shelp%s - Display this message\n", GREEN, NO_FORMAT); - printf("\n"); -} - -void print_title(void) { - printf("%s%s%s", YELLOW, title, NO_FORMAT); -}
\ No newline at end of file |