2 * libgit2 "status" example - shows how to use the status APIs
4 * Written by the libgit2 contributors
6 * To the extent possible under law, the author(s) have dedicated all copyright
7 * and related and neighboring rights to this software to the public domain
8 * worldwide. This software is distributed without any warranty.
10 * You should have received a copy of the CC0 Public Domain Dedication along
11 * with this software. If not, see
12 * <http://creativecommons.org/publicdomain/zero/1.0/>.
18 * This example demonstrates the use of the libgit2 status APIs,
19 * particularly the `git_status_list` object, to roughly simulate the
20 * output of running `git status`. It serves as a simple example of
21 * using those APIs to get basic status information.
25 * - Robust error handling
26 * - Colorized or paginated output formatting
30 * - Examples of translating command line arguments to the status
31 * options settings to mimic `git status` results.
32 * - A sample status formatter that matches the default "long" format
34 * - A sample status formatter that matches the "short" format
44 #define MAX_PATHSPEC 8
47 git_status_options statusopt
;
49 char *pathspec
[MAX_PATHSPEC
];
58 static void parse_opts(struct status_opts
*o
, int argc
, char *argv
[]);
59 static void show_branch(git_repository
*repo
, int format
);
60 static void print_long(git_status_list
*status
);
61 static void print_short(git_repository
*repo
, git_status_list
*status
);
62 static int print_submod(git_submodule
*sm
, const char *name
, void *payload
);
64 int lg2_status(git_repository
*repo
, int argc
, char *argv
[])
66 git_status_list
*status
;
67 struct status_opts o
= { GIT_STATUS_OPTIONS_INIT
, "." };
69 o
.statusopt
.show
= GIT_STATUS_SHOW_INDEX_AND_WORKDIR
;
70 o
.statusopt
.flags
= GIT_STATUS_OPT_INCLUDE_UNTRACKED
|
71 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX
|
72 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY
;
74 parse_opts(&o
, argc
, argv
);
76 if (git_repository_is_bare(repo
))
77 fatal("Cannot report status on bare repository",
78 git_repository_path(repo
));
82 printf("\033[H\033[2J");
85 * Run status on the repository
87 * We use `git_status_list_new()` to generate a list of status
88 * information which lets us iterate over it at our
89 * convenience and extract the data we want to show out of
92 * You can use `git_status_foreach()` or
93 * `git_status_foreach_ext()` if you'd prefer to execute a
94 * callback for each entry. The latter gives you more control
95 * about what results are presented.
97 check_lg2(git_status_list_new(&status
, repo
, &o
.statusopt
),
98 "Could not get status", NULL
);
101 show_branch(repo
, o
.format
);
104 int submod_count
= 0;
105 check_lg2(git_submodule_foreach(repo
, print_submod
, &submod_count
),
106 "Cannot iterate submodules", o
.repodir
);
109 if (o
.format
== FORMAT_LONG
)
112 print_short(repo
, status
);
114 git_status_list_free(status
);
125 * If the user asked for the branch, let's show the short name of the
128 static void show_branch(git_repository
*repo
, int format
)
131 const char *branch
= NULL
;
132 git_reference
*head
= NULL
;
134 error
= git_repository_head(&head
, repo
);
136 if (error
== GIT_EUNBORNBRANCH
|| error
== GIT_ENOTFOUND
)
139 branch
= git_reference_shorthand(head
);
141 check_lg2(error
, "failed to get current branch", NULL
);
143 if (format
== FORMAT_LONG
)
144 printf("# On branch %s\n",
145 branch
? branch
: "Not currently on any branch.");
147 printf("## %s\n", branch
? branch
: "HEAD (no branch)");
149 git_reference_free(head
);
153 * This function print out an output similar to git's status command
154 * in long form, including the command-line hints.
156 static void print_long(git_status_list
*status
)
158 size_t i
, maxi
= git_status_list_entrycount(status
);
159 const git_status_entry
*s
;
160 int header
= 0, changes_in_index
= 0;
161 int changed_in_workdir
= 0, rm_in_workdir
= 0;
162 const char *old_path
, *new_path
;
164 /** Print index changes. */
166 for (i
= 0; i
< maxi
; ++i
) {
167 char *istatus
= NULL
;
169 s
= git_status_byindex(status
, i
);
171 if (s
->status
== GIT_STATUS_CURRENT
)
174 if (s
->status
& GIT_STATUS_WT_DELETED
)
177 if (s
->status
& GIT_STATUS_INDEX_NEW
)
178 istatus
= "new file: ";
179 if (s
->status
& GIT_STATUS_INDEX_MODIFIED
)
180 istatus
= "modified: ";
181 if (s
->status
& GIT_STATUS_INDEX_DELETED
)
182 istatus
= "deleted: ";
183 if (s
->status
& GIT_STATUS_INDEX_RENAMED
)
184 istatus
= "renamed: ";
185 if (s
->status
& GIT_STATUS_INDEX_TYPECHANGE
)
186 istatus
= "typechange:";
192 printf("# Changes to be committed:\n");
193 printf("# (use \"git reset HEAD <file>...\" to unstage)\n");
198 old_path
= s
->head_to_index
->old_file
.path
;
199 new_path
= s
->head_to_index
->new_file
.path
;
201 if (old_path
&& new_path
&& strcmp(old_path
, new_path
))
202 printf("#\t%s %s -> %s\n", istatus
, old_path
, new_path
);
204 printf("#\t%s %s\n", istatus
, old_path
? old_path
: new_path
);
208 changes_in_index
= 1;
213 /** Print workdir changes to tracked files. */
215 for (i
= 0; i
< maxi
; ++i
) {
216 char *wstatus
= NULL
;
218 s
= git_status_byindex(status
, i
);
221 * With `GIT_STATUS_OPT_INCLUDE_UNMODIFIED` (not used in this example)
222 * `index_to_workdir` may not be `NULL` even if there are
223 * no differences, in which case it will be a `GIT_DELTA_UNMODIFIED`.
225 if (s
->status
== GIT_STATUS_CURRENT
|| s
->index_to_workdir
== NULL
)
228 /** Print out the output since we know the file has some changes */
229 if (s
->status
& GIT_STATUS_WT_MODIFIED
)
230 wstatus
= "modified: ";
231 if (s
->status
& GIT_STATUS_WT_DELETED
)
232 wstatus
= "deleted: ";
233 if (s
->status
& GIT_STATUS_WT_RENAMED
)
234 wstatus
= "renamed: ";
235 if (s
->status
& GIT_STATUS_WT_TYPECHANGE
)
236 wstatus
= "typechange:";
242 printf("# Changes not staged for commit:\n");
243 printf("# (use \"git add%s <file>...\" to update what will be committed)\n", rm_in_workdir
? "/rm" : "");
244 printf("# (use \"git checkout -- <file>...\" to discard changes in working directory)\n");
249 old_path
= s
->index_to_workdir
->old_file
.path
;
250 new_path
= s
->index_to_workdir
->new_file
.path
;
252 if (old_path
&& new_path
&& strcmp(old_path
, new_path
))
253 printf("#\t%s %s -> %s\n", wstatus
, old_path
, new_path
);
255 printf("#\t%s %s\n", wstatus
, old_path
? old_path
: new_path
);
259 changed_in_workdir
= 1;
263 /** Print untracked files. */
267 for (i
= 0; i
< maxi
; ++i
) {
268 s
= git_status_byindex(status
, i
);
270 if (s
->status
== GIT_STATUS_WT_NEW
) {
273 printf("# Untracked files:\n");
274 printf("# (use \"git add <file>...\" to include in what will be committed)\n");
279 printf("#\t%s\n", s
->index_to_workdir
->old_file
.path
);
285 /** Print ignored files. */
287 for (i
= 0; i
< maxi
; ++i
) {
288 s
= git_status_byindex(status
, i
);
290 if (s
->status
== GIT_STATUS_IGNORED
) {
293 printf("# Ignored files:\n");
294 printf("# (use \"git add -f <file>...\" to include in what will be committed)\n");
299 printf("#\t%s\n", s
->index_to_workdir
->old_file
.path
);
303 if (!changes_in_index
&& changed_in_workdir
)
304 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
308 * This version of the output prefixes each path with two status
309 * columns and shows submodule status information.
311 static void print_short(git_repository
*repo
, git_status_list
*status
)
313 size_t i
, maxi
= git_status_list_entrycount(status
);
314 const git_status_entry
*s
;
315 char istatus
, wstatus
;
316 const char *extra
, *a
, *b
, *c
;
318 for (i
= 0; i
< maxi
; ++i
) {
319 s
= git_status_byindex(status
, i
);
321 if (s
->status
== GIT_STATUS_CURRENT
)
325 istatus
= wstatus
= ' ';
328 if (s
->status
& GIT_STATUS_INDEX_NEW
)
330 if (s
->status
& GIT_STATUS_INDEX_MODIFIED
)
332 if (s
->status
& GIT_STATUS_INDEX_DELETED
)
334 if (s
->status
& GIT_STATUS_INDEX_RENAMED
)
336 if (s
->status
& GIT_STATUS_INDEX_TYPECHANGE
)
339 if (s
->status
& GIT_STATUS_WT_NEW
) {
344 if (s
->status
& GIT_STATUS_WT_MODIFIED
)
346 if (s
->status
& GIT_STATUS_WT_DELETED
)
348 if (s
->status
& GIT_STATUS_WT_RENAMED
)
350 if (s
->status
& GIT_STATUS_WT_TYPECHANGE
)
353 if (s
->status
& GIT_STATUS_IGNORED
) {
358 if (istatus
== '?' && wstatus
== '?')
362 * A commit in a tree is how submodules are stored, so
363 * let's go take a look at its status.
365 if (s
->index_to_workdir
&&
366 s
->index_to_workdir
->new_file
.mode
== GIT_FILEMODE_COMMIT
)
368 unsigned int smstatus
= 0;
370 if (!git_submodule_status(&smstatus
, repo
, s
->index_to_workdir
->new_file
.path
,
371 GIT_SUBMODULE_IGNORE_UNSPECIFIED
)) {
372 if (smstatus
& GIT_SUBMODULE_STATUS_WD_MODIFIED
)
373 extra
= " (new commits)";
374 else if (smstatus
& GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED
)
375 extra
= " (modified content)";
376 else if (smstatus
& GIT_SUBMODULE_STATUS_WD_WD_MODIFIED
)
377 extra
= " (modified content)";
378 else if (smstatus
& GIT_SUBMODULE_STATUS_WD_UNTRACKED
)
379 extra
= " (untracked content)";
384 * Now that we have all the information, format the output.
387 if (s
->head_to_index
) {
388 a
= s
->head_to_index
->old_file
.path
;
389 b
= s
->head_to_index
->new_file
.path
;
391 if (s
->index_to_workdir
) {
393 a
= s
->index_to_workdir
->old_file
.path
;
395 b
= s
->index_to_workdir
->old_file
.path
;
396 c
= s
->index_to_workdir
->new_file
.path
;
399 if (istatus
== 'R') {
401 printf("%c%c %s %s %s%s\n", istatus
, wstatus
, a
, b
, c
, extra
);
403 printf("%c%c %s %s%s\n", istatus
, wstatus
, a
, b
, extra
);
406 printf("%c%c %s %s%s\n", istatus
, wstatus
, a
, c
, extra
);
408 printf("%c%c %s%s\n", istatus
, wstatus
, a
, extra
);
412 for (i
= 0; i
< maxi
; ++i
) {
413 s
= git_status_byindex(status
, i
);
415 if (s
->status
== GIT_STATUS_WT_NEW
)
416 printf("?? %s\n", s
->index_to_workdir
->old_file
.path
);
420 static int print_submod(git_submodule
*sm
, const char *name
, void *payload
)
422 int *count
= payload
;
426 printf("# Submodules\n");
429 printf("# - submodule '%s' at %s\n",
430 git_submodule_name(sm
), git_submodule_path(sm
));
436 * Parse options that git's status command supports.
438 static void parse_opts(struct status_opts
*o
, int argc
, char *argv
[])
440 struct args_info args
= ARGS_INFO_INIT
;
442 for (args
.pos
= 1; args
.pos
< argc
; ++args
.pos
) {
443 char *a
= argv
[args
.pos
];
446 if (o
->npaths
< MAX_PATHSPEC
)
447 o
->pathspec
[o
->npaths
++] = a
;
449 fatal("Example only supports a limited pathspec", NULL
);
451 else if (!strcmp(a
, "-s") || !strcmp(a
, "--short"))
452 o
->format
= FORMAT_SHORT
;
453 else if (!strcmp(a
, "--long"))
454 o
->format
= FORMAT_LONG
;
455 else if (!strcmp(a
, "--porcelain"))
456 o
->format
= FORMAT_PORCELAIN
;
457 else if (!strcmp(a
, "-b") || !strcmp(a
, "--branch"))
459 else if (!strcmp(a
, "-z")) {
461 if (o
->format
== FORMAT_DEFAULT
)
462 o
->format
= FORMAT_PORCELAIN
;
464 else if (!strcmp(a
, "--ignored"))
465 o
->statusopt
.flags
|= GIT_STATUS_OPT_INCLUDE_IGNORED
;
466 else if (!strcmp(a
, "-uno") ||
467 !strcmp(a
, "--untracked-files=no"))
468 o
->statusopt
.flags
&= ~GIT_STATUS_OPT_INCLUDE_UNTRACKED
;
469 else if (!strcmp(a
, "-unormal") ||
470 !strcmp(a
, "--untracked-files=normal"))
471 o
->statusopt
.flags
|= GIT_STATUS_OPT_INCLUDE_UNTRACKED
;
472 else if (!strcmp(a
, "-uall") ||
473 !strcmp(a
, "--untracked-files=all"))
474 o
->statusopt
.flags
|= GIT_STATUS_OPT_INCLUDE_UNTRACKED
|
475 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS
;
476 else if (!strcmp(a
, "--ignore-submodules=all"))
477 o
->statusopt
.flags
|= GIT_STATUS_OPT_EXCLUDE_SUBMODULES
;
478 else if (!strncmp(a
, "--git-dir=", strlen("--git-dir=")))
479 o
->repodir
= a
+ strlen("--git-dir=");
480 else if (!strcmp(a
, "--repeat"))
482 else if (match_int_arg(&o
->repeat
, &args
, "--repeat", 0))
484 else if (!strcmp(a
, "--list-submodules"))
487 check_lg2(-1, "Unsupported option", a
);
490 if (o
->format
== FORMAT_DEFAULT
)
491 o
->format
= FORMAT_LONG
;
492 if (o
->format
== FORMAT_LONG
)
495 o
->statusopt
.pathspec
.strings
= o
->pathspec
;
496 o
->statusopt
.pathspec
.count
= o
->npaths
;