summaryrefslogtreecommitdiff
path: root/g.c
blob: 4d609ad17be35e68394bede1d7da2c995fd1b383 (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
 *  g - the *wrapper* git wrapper
 *      $version_number
 */


//                             mm-dd.vvv
//                             with:
//                             mm  -> month
//                             dd  -> day
//                             vvv -> continous version number
const char version_number[] = "11-20.001";

const char red[] = "\033[31m";
const char def[] = "\033[0m";

char strn1[2048]; // arg buffer 1
char strn2[2048]; // arg buffer 2
char strn3[2048]; // arg buffer 3
char strn4[2048]; // arg buffer 4

const char *init_vars[] = {"i", "I", "in", "ini", "init", NULL};
const char *status_vars[] = {"s", "S", "st", "sta", "stat", "statu", "status", NULL};
const char *diff_vars[] = {"d", "D", "di", "dif", "diff", NULL};
const char *commit_vars[] = {"c", "C", "co", "com", "comm", "commi", "commit", NULL};
const char *fetch_vars[] = {"f", "F", "fe", "fet", "fetc", "fetch", NULL};
const char *merge_vars[] = {"m", "M", "me", "mer", "merg", "merge", NULL};
const char *push_vars[] = {"p", "P", "pu", "pus", "push", NULL};
const char *pull_vars[] = {"pl", "Pl", "pL", "PL", "pul", "pull", NULL};
const char *help_vars[] = {"h", "H", "he", "hel", "help", "-h", "--help", NULL};
const char *log_vars[] = {"l", "L", "lo", "log", NULL};
const char *stash_vars[] = {"st", "St", "sT", "ST", "sta", "stas", "stash", NULL};

const char *diff_staged_vars[] = {"s", "S", "st", "sta", "stag", "stage", "staged", "--staged", NULL};
const char *commit_add_vars[] = {"a", "A", "ad", "add", NULL};
const char *commit_message_vars[] = {"m", "M", "ms", "msg", NULL};
const char *push_upstream_vars[] = {"u", "U", "up", "ups", "upst", "upstr", "upstre", "upstrea", "upstream", "--set-upstream", NULL};

int matches(const char *cmd, const char *list[]);
void usage();

int main(int argc, char **argv) {
    /*
     *  the structure is:
     *  if (n. of argc)
     *  — options with that n. of argc
     *  else if (another n. of argc)
     *  — options with that other n. of argc
     *  and so on and so forth.
     *  sorry but this is the best implementation
     *  you'll get out of an eepy ass at almost 2AM
     */
    if (argc == 2) {                                       // g x
        strcpy(strn1, argv[1]);

        if (matches(strn1, init_vars) == 0) {
            system("git init");
            return 0;
        }
        else if (matches(strn1, status_vars) == 0) {
            system("git status");
            return 0;
        }
        else if (matches(strn1, diff_vars) == 0) {
            system("git diff");
            return 0;
        }
        else if (matches(strn1, commit_vars) == 0) {
            system("git commit");
            return 0;
        }
        else if (matches(strn1, fetch_vars) == 0) {
            system("git fetch");
            return 0;
        }
        else if (matches(strn1, merge_vars) == 0) {
            system("git merge");
            return 0;
        }
        else if (matches(strn1, push_vars) == 0) {
            system("git push");
            return 0;
        }
        else if (matches(strn1, pull_vars) == 0) {
            system("git pull");
            return 0;
        }
        else if (matches(strn1, log_vars) == 0) {
            system("git log");
            return 0;
        }
        else if (matches(strn1, stash_vars) == 0) {
            system("git stash");
        }
        else if (matches(strn1, help_vars) == 0) {
            usage();
            return 0;
        }
    } else if (argc == 3) {                                // g x y
        strcpy(strn1, argv[1]);
        strcpy(strn2, argv[2]);

        if (matches(strn1, init_vars) == 0) {
            if (strlen(strn2) == 0) {
                system("git init");
                return 0;
            } else if (strlen(strn2) > 0) {
                char command_buffer[4096] = "git init ";
                strcat(command_buffer, strn2); // TODO: use snprintf or anything safer than strcat()
                system(command_buffer);
            }
        } else if (matches(strn1, diff_vars) == 0) {
            if (strlen(strn2) == 0) {
                system("git diff");
                return 0;
            } else if (strlen(strn2) > 0) {
                if (matches(strn2, diff_staged_vars) == 0) {
                    system("git diff --staged");
                    return 0;
                } else {
                    printf(":: %sunknown%s option for '%sd%s': %s%s%s\n", red, def, red, def, red, strn2, def);
                    printf(":: running '%sgit diff --staged%s'\n", red, def);
                    system("git diff --staged");
                    return 0;
                }
            }
        }
    }

    usage();
    return 0;
}

int matches(const char *cmd, const char *list[]) {
    for (int i = 0; list[i] != NULL; i++) {
        if (strcmp(cmd, list[i]) == 0) {
            return 0;
        }
    }
    return 1;
}


void usage() {
    printf(
        "%sg%s - the %spermissive%s git wrapper\n"
        "spinning version %s%s%s\n"
        "==============================================================\n"
        "generally: 'g [%sstuff%s]', got it?\n"
        "[] -> %soptional%s\n"
        "{} -> %srequired%s\n"
        "==============================================================\n"
        "stuff;\n"
       "g %si%s [path]                     -> git %si%snit [path]\n"

       "g %ss%s                            -> git %ss%status\n"
       "g %sd%s [s] [commit1 [commit2]]    -> git %sd%siff [--staged] [commit1 [commit2]]\n"
       "g %sc%s [a] [m {\"msg\"}]            -> git %sc%sommit [-a] [-m {\"msg\"}]\n"
       "g %sf%s [remote [branch]]          -> git %sf%setch [remote [branch]]\n"
       "g %sm%s [branch]                   -> git %sm%serge [branch]\n"
       "g %sp%s [u] [remote [branch|tag]]  -> git %sp%sush [-u] [remote [branch|tag]]\n"
       "g %spl%s [r]                       -> git %sp%sul%sl%s [--rebase]\n"
       "g %sl%s [o] [number]               -> git %sl%sog [--oneline] [-n {number}]\n"
       "g %sst%s [p|l]                     -> git %sst%sash [pop|list]\n"
       "g %sh%s                            -> prints this usage screen\n"
       "==============================================================\n"
       "g %sg%s {url} [folder]             -> git %sc%slone {url} [folder]\n"
       "g %sa%s {file | a}                 -> git %sa%sdd {file | -A}\n"
       "g %sb%s [d] {name}                 -> git %sb%sranch [-d] {name}\n"
       "g %ssw%s [c] {branch}              -> git %ssw%sitch [-c] {branch}\n"
       "g %sco%s {commit}                  -> git %sc%sheck%so%sut {commit}\n"
       "g %sre%s [s] {file}                -> git %sre%sstore [--staged] {file}\n"
       "g %srt%s [s|m|h] {commit}          -> git %sr%sese%st%s [--soft|--mixed|--hard] {commit}\n"
       "g %srv%s {commit}                  -> git %sr%se%sv%sert {commit}\n"
       "g %sr%s {a{name{url}}|g{name}}     -> git %sr%semote {add{name{url}}|get-url{name}}\n"
       "g %ssh%s {commit}                  -> git %ssh%sow {commit}\n"
       "g %st%s [a] {name} [m {\"msg\"}]     -> git %st%sag [-a] {name} [-m {\"msg\"}]\n"
        , red, def, red, def
        , red, version_number, def
        , red, def
        , red, def
        , red, def
        //
        , red, def, red, def
        , red, def, red, def
        , red, def, red, def
        , red, def, red, def
        , red, def, red, def
        , red, def, red, def
        , red, def, red, def, red, def 
        , red, def, red, def, red, def 
        , red, def, red, def
        , red, def, red, def
        , red, def, red, def
        , red, def, red, def
        , red, def, red, def
        , red, def, red, def
        , red, def, red, def
        , red, def, red, def
        , red, def, red, def, red, def 
        , red, def, red, def
        , red, def, red, def, red, def 
        , red, def, red, def, red, def 
        , red, def, red, def
    );
}