Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f18f772

Browse files
committed
Add example implementation of long format status
1 parent 22b6b82 commit f18f772

File tree

1 file changed

+145
-2
lines changed

1 file changed

+145
-2
lines changed

examples/status.c

Lines changed: 145 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ static void show_branch(git_repository *repo, int format)
7676
check(error, "failed to get current branch", NULL);
7777

7878
if (format == FORMAT_LONG)
79-
printf("# %s\n", branch ? branch : "Not currently on any branch.");
79+
printf("# On branch %s\n",
80+
branch ? branch : "Not currently on any branch.");
8081
else
8182
printf("## %s\n", branch ? branch : "HEAD (no branch)");
8283

@@ -85,8 +86,150 @@ static void show_branch(git_repository *repo, int format)
8586

8687
static void print_long(git_repository *repo, git_status_list *status)
8788
{
89+
size_t i, maxi = git_status_list_entrycount(status);
90+
const git_status_entry *s;
91+
int header = 0, changes_in_index = 0;
92+
int changed_in_workdir = 0, rm_in_workdir = 0;
93+
const char *old_path, *new_path;
94+
8895
(void)repo;
89-
(void)status;
96+
97+
/* print index changes */
98+
99+
for (i = 0; i < maxi; ++i) {
100+
char *istatus = NULL;
101+
102+
s = git_status_byindex(status, i);
103+
104+
if (s->status == GIT_STATUS_CURRENT)
105+
continue;
106+
107+
if (s->status & GIT_STATUS_WT_DELETED)
108+
rm_in_workdir = 1;
109+
110+
if (s->status & GIT_STATUS_INDEX_NEW)
111+
istatus = "new file: ";
112+
if (s->status & GIT_STATUS_INDEX_MODIFIED)
113+
istatus = "modified: ";
114+
if (s->status & GIT_STATUS_INDEX_DELETED)
115+
istatus = "deleted: ";
116+
if (s->status & GIT_STATUS_INDEX_RENAMED)
117+
istatus = "renamed: ";
118+
if (s->status & GIT_STATUS_INDEX_TYPECHANGE)
119+
istatus = "typechange:";
120+
121+
if (istatus == NULL)
122+
continue;
123+
124+
if (!header) {
125+
printf("# Changes to be committed:\n");
126+
printf("# (use \"git reset HEAD <file>...\" to unstage)\n");
127+
printf("#\n");
128+
header = 1;
129+
}
130+
131+
old_path = s->head_to_index->old_file.path;
132+
new_path = s->head_to_index->new_file.path;
133+
134+
if (old_path && new_path && strcmp(old_path, new_path))
135+
printf("#\t%s %s -> %s\n", istatus, old_path, new_path);
136+
else
137+
printf("#\t%s %s\n", istatus, old_path ? old_path : new_path);
138+
}
139+
140+
if (header) {
141+
changes_in_index = 1;
142+
printf("#\n");
143+
}
144+
header = 0;
145+
146+
/* print workdir changes to tracked files */
147+
148+
for (i = 0; i < maxi; ++i) {
149+
char *wstatus = NULL;
150+
151+
s = git_status_byindex(status, i);
152+
153+
if (s->status == GIT_STATUS_CURRENT || s->index_to_workdir == NULL)
154+
continue;
155+
156+
if (s->status & GIT_STATUS_WT_MODIFIED)
157+
wstatus = "modified: ";
158+
if (s->status & GIT_STATUS_WT_DELETED)
159+
wstatus = "deleted: ";
160+
if (s->status & GIT_STATUS_WT_RENAMED)
161+
wstatus = "renamed: ";
162+
if (s->status & GIT_STATUS_WT_TYPECHANGE)
163+
wstatus = "typechange:";
164+
165+
if (wstatus == NULL)
166+
continue;
167+
168+
if (!header) {
169+
printf("# Changes not staged for commit:\n");
170+
printf("# (use \"git add%s <file>...\" to update what will be committed)\n", rm_in_workdir ? "/rm" : "");
171+
printf("# (use \"git checkout -- <file>...\" to discard changes in working directory)\n");
172+
printf("#\n");
173+
header = 1;
174+
}
175+
176+
old_path = s->index_to_workdir->old_file.path;
177+
new_path = s->index_to_workdir->new_file.path;
178+
179+
if (old_path && new_path && strcmp(old_path, new_path))
180+
printf("#\t%s %s -> %s\n", wstatus, old_path, new_path);
181+
else
182+
printf("#\t%s %s\n", wstatus, old_path ? old_path : new_path);
183+
}
184+
185+
if (header) {
186+
changed_in_workdir = 1;
187+
printf("#\n");
188+
}
189+
header = 0;
190+
191+
/* print untracked files */
192+
193+
header = 0;
194+
195+
for (i = 0; i < maxi; ++i) {
196+
s = git_status_byindex(status, i);
197+
198+
if (s->status == GIT_STATUS_WT_NEW) {
199+
200+
if (!header) {
201+
printf("# Untracked files:\n");
202+
printf("# (use \"git add <file>...\" to include in what will be committed)\n");
203+
printf("#\n");
204+
header = 1;
205+
}
206+
207+
printf("#\t%s\n", s->index_to_workdir->old_file.path);
208+
}
209+
}
210+
211+
header = 0;
212+
213+
/* print ignored files */
214+
215+
for (i = 0; i < maxi; ++i) {
216+
s = git_status_byindex(status, i);
217+
218+
if (s->status == GIT_STATUS_IGNORED) {
219+
220+
if (!header) {
221+
printf("# Ignored files:\n");
222+
printf("# (use \"git add -f <file>...\" to include in what will be committed)\n");
223+
printf("#\n");
224+
header = 1;
225+
}
226+
227+
printf("#\t%s\n", s->index_to_workdir->old_file.path);
228+
}
229+
}
230+
231+
if (!changes_in_index && changed_in_workdir)
232+
printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
90233
}
91234

92235
static void print_short(git_repository *repo, git_status_list *status)

0 commit comments

Comments
 (0)