summaryrefslogtreecommitdiffstats
path: root/XD.c
blob: aa3d43da2901eaba8548daeade5349661227de3d (plain) (blame)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef ERR
#include <stdarg.h>
#endif

#ifdef GIT
#include <sys/stat.h>
#include <dirent.h>
#include <git2.h>
#endif

#define P(X) fwrite(X, 1, 1, stdout)

#ifdef ERR
void
l(const char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  vfprintf(stderr, fmt, ap);
  va_end(ap);

  if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
    fputc(' ', stderr);
    perror(NULL);
  } else {
    fputc('\n', stderr);
  }
}
#define L(...) l(__VA_ARGS__)
#else
#define L(...)
#endif

#ifdef GIT
/**
 * @brief search all parent directories for a git repo
 *
 * @return absolute path to git repo
 */
char
*find_git_repo()
{
  char path[PATH_MAX] = ".", *rpath;
  struct stat s;
  int i, c;


  /* find the number of jumps to the root of the fs */
  rpath = realpath(path, NULL);
  for (i = c = 0; i < strlen(rpath); i++) {
    if (rpath[i] == '/') {
      c++;
    }
  }
  free(rpath);

  /* start searching */
  for (i = c; i > 0; i--) {
    strcat(path, "/.git");

    /* if there seems to be a git directory return the directory it was found in */
    if (stat(path, &s) == 0 && S_ISDIR(s.st_mode)) {
      return realpath(path, NULL);
    }

    /* reset contents of gpath, and go up a directory */
    memset(&path[strlen(path) - 4], '.', 2);
    memset(&path[strlen(path) - 2], 0, 2);
  }

  return NULL;
}

/**
 * @brief open git repo if one is available at the current path
 *
 * @return a pointer to the git repo object
 */
git_repository
*init_git()
{
  char *buf;
  git_repository *repo;

  /* check for a repo before loading libgit2 */
  if ((buf = find_git_repo()) == NULL) {
    return NULL;
  }

  /* disable a bunch of git options to hopefully speed things up */
  git_libgit2_opts(GIT_OPT_ENABLE_CACHING, 0);
  git_libgit2_opts(GIT_OPT_SET_MWINDOW_SIZE, 0);
  git_libgit2_opts(GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, 0);

  git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, "");
  git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, "");
  git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, "");
  git_libgit2_opts(GIT_OPT_SET_TEMPLATE_PATH, "");

  git_libgit2_opts(GIT_OPT_DISABLE_PACK_KEEP_FILE_CHECKS, 1);

  /* initialize the git library and repository */
  if (git_libgit2_init() < 0) {
    L("Failed to initalize libgit2, proceeding without git functionality enabled.");
    return NULL;
  }

  if (git_repository_open(&repo, buf) < 0) {
    L("Failed to open git repo: %s", git_error_last()->message);
    free(buf);
    return NULL;
  }

  /* get rid of object containing git repo path and return the repo */
  free(buf);
  return repo;
}

/**
 * @brief check for any existing stashes in the provided git repo
 *
 * @param repo git repo to check for existing stashes
 * @return 1 if stashes found 0 otherwise
 */
int
has_stashes(git_repository *repo)
{
	git_reference *stash = NULL;
  int e;

  e = git_reference_lookup(&stash, repo, "refs/stash");
  if (e == GIT_ENOTFOUND) {
    return 0;
  } else if (e < 0) {
    L("Error looking up stash reference: %s", git_error_last()->message);
    return 0;
  } else {
    e = 1;
  }

  git_reference_free(stash);
  return e;
}

/**
 * @brief check for any untracked changes in the current git repo
 *
 * @param repo git repository object
 * @return 1 if any untracked changes have been found 0 otherwise
 */
int
has_untracked(git_repository *repo)
{
  git_status_options opts = GIT_STATUS_OPTIONS_INIT;
  git_status_list *list = NULL;
  int r = 0;

  opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
  opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
    GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;

  if (git_status_list_new(&list, repo, &opts) < 0) {
    L("Error checking for untracked changes: %s", git_error_last()->message);
    return 0;
  }

  /* if any changes are found return 1 */
  if (git_status_list_entrycount(list) > 0) {
    r = 1;
  }

  git_status_list_free(list);
  return r;
}

/**
 * @brief check for staged changes
 *
 * @param repo git repository object
 * @return 1 if any staged changes found 0 otherwise
 */
int
has_staged(git_repository *repo)
{
  git_status_entry entry;
  git_status_list *list = NULL;
  git_status_options opts = GIT_STATUS_OPTIONS_INIT;
  int i, c, r = 0;

  opts.flags = GIT_STATUS_INDEX_NEW;
  opts.show = GIT_STATUS_SHOW_INDEX_ONLY;

  if (git_status_list_new(&list, repo, &opts) < 0) {
    L("Error checking for staged changes: %s", git_error_last()->message);
    return 0;
  }

  /* if any staged changes are found return 1 */
  if ((c = git_status_list_entrycount(list)) > 0) {
    for (i = 0; i < c; i++) {
      entry = *git_status_byindex(list, i);

      if (entry.status & (GIT_STATUS_INDEX_NEW
        | GIT_STATUS_INDEX_DELETED
        | GIT_STATUS_INDEX_MODIFIED)) {
        r = 1;
        break;
      }
    }
  }

  git_status_list_free(list);
  return r;
}
#endif

int
main(int argc, char *argv[])
{
  int code = -1;

  /* print version information */
  if (strcmp(argv[1], "-v") == 0) {
    printf("XD [number] v%s\n", VERSION);
    return 0;
  }

  #ifdef GIT
  git_repository *repo;

  if ((repo = init_git())) {
    /* change the eyes depending on the current git repo's status */
    if (has_stashes(repo)) {
      P("8"); /* goggle eyes if we have some stashed changes */
    } else if (git_repository_is_empty(repo)) {
      P("B"); /* sunglasses if we're in a new repo with no HEAD */
    } else {
      P(";"); /* wink when we're in a git repo */
    }

    /* change the nose depending on the current git repo's status */
    if (has_staged(repo)) {
      P("*"); /* change to broken nose for staged changes */
    } else if (has_untracked(repo)) {
      P("^"); /* add a little nose when there are untracked changes in the repo */
    } else if (git_repository_head_detached(repo)) {
      P("-"); /* add a minus nose when the HEAD is detached */
    }
    git_repository_free(repo);
    git_libgit2_shutdown();
  } else
  #endif
  if (1) {
    P(":");
  }

  /* get exit code from user args */
  if (argv[1]) {
    code = atoi(argv[1]);
  }

  /* change mouth based on exit code */
  if (code >= 0) {
    switch (code) {
      case 0: P(")"); break; /* all good */
      case 130: P("O"); break; /* Ctrl-c pressed (SIGTERM) */
      case 126: P("P"); break; /* permission denied */
      case 127: P("/"); break; /* command not found */
      default: P("("); break; /* all other codes (usually the program saying it has failed) */
    }
  } else {
    P("|"); /* no code info */
  }
}