]> git.proxmox.com Git - libgit2.git/blame - src/stash.c
stash: add git_stash_foreach()
[libgit2.git] / src / stash.c
CommitLineData
590fb68b 1/*
2 * Copyright (C) 2009-2012 the libgit2 contributors
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8#include "common.h"
9#include "repository.h"
10#include "commit.h"
11#include "tree.h"
12#include "reflog.h"
13#include "git2/diff.h"
14#include "git2/stash.h"
15#include "git2/status.h"
16#include "git2/checkout.h"
17
18static int create_error(int error, const char *msg)
19{
20 giterr_set(GITERR_STASH, "Cannot stash changes - %s", msg);
21 return error;
22}
23
24static int ensure_non_bare_repository(git_repository *repo)
25{
26 if (!git_repository_is_bare(repo))
27 return 0;
28
29 return create_error(GIT_EBAREREPO,
30 "Stash related operations require a working directory.");
31}
32
33static int retrieve_head(git_reference **out, git_repository *repo)
34{
35 int error = git_repository_head(out, repo);
36
37 if (error == GIT_EORPHANEDHEAD)
38 return create_error(error, "You do not have the initial commit yet.");
39
40 return error;
41}
42
43static int append_abbreviated_oid(git_buf *out, const git_oid *b_commit)
44{
45 char *formatted_oid;
46
47 formatted_oid = git_oid_allocfmt(b_commit);
48 GITERR_CHECK_ALLOC(formatted_oid);
49
50 git_buf_put(out, formatted_oid, 7);
51 git__free(formatted_oid);
52
53 return git_buf_oom(out) ? -1 : 0;
54}
55
56static int append_commit_description(git_buf *out, git_commit* commit)
57{
58 const char *message;
59 int pos = 0, len;
60
61 if (append_abbreviated_oid(out, git_commit_id(commit)) < 0)
62 return -1;
63
64 message = git_commit_message(commit);
65 len = strlen(message);
66
67 /* TODO: Replace with proper commit short message
68 * when git_commit_message_short() is implemented.
69 */
70 while (pos < len && message[pos] != '\n')
71 pos++;
72
73 git_buf_putc(out, ' ');
74 git_buf_put(out, message, pos);
75 git_buf_putc(out, '\n');
76
77 return git_buf_oom(out) ? -1 : 0;
78}
79
80static int retrieve_base_commit_and_message(
81 git_commit **b_commit,
82 git_buf *stash_message,
83 git_repository *repo)
84{
85 git_reference *head = NULL;
86 int error;
87
88 if ((error = retrieve_head(&head, repo)) < 0)
89 return error;
90
91 error = -1;
92
93 if (strcmp("HEAD", git_reference_name(head)) == 0)
94 git_buf_puts(stash_message, "(no branch): ");
95 else
96 git_buf_printf(
97 stash_message,
98 "%s: ",
99 git_reference_name(head) + strlen(GIT_REFS_HEADS_DIR));
100
101 if (git_commit_lookup(b_commit, repo, git_reference_oid(head)) < 0)
102 goto cleanup;
103
104 if (append_commit_description(stash_message, *b_commit) < 0)
105 goto cleanup;
106
107 error = 0;
108
109cleanup:
110 git_reference_free(head);
111 return error;
112}
113
114static int build_tree_from_index(git_tree **out, git_index *index)
115{
116 git_oid i_tree_oid;
117
118 if (git_tree_create_fromindex(&i_tree_oid, index) < 0)
119 return -1;
120
121 return git_tree_lookup(out, git_index_owner(index), &i_tree_oid);
122}
123
124static int commit_index(
125 git_commit **i_commit,
126 git_index *index,
127 git_signature *stasher,
128 const char *message,
129 const git_commit *parent)
130{
131 git_tree *i_tree = NULL;
132 git_oid i_commit_oid;
133 git_buf msg = GIT_BUF_INIT;
134 int error = -1;
135
136 if (build_tree_from_index(&i_tree, index) < 0)
137 goto cleanup;
138
139 if (git_buf_printf(&msg, "index on %s\n", message) < 0)
140 goto cleanup;
141
142 if (git_commit_create(
143 &i_commit_oid,
144 git_index_owner(index),
145 NULL,
146 stasher,
147 stasher,
148 NULL,
149 git_buf_cstr(&msg),
150 i_tree,
151 1,
152 &parent) < 0)
153 goto cleanup;
154
155 error = git_commit_lookup(i_commit, git_index_owner(index), &i_commit_oid);
156
157cleanup:
158 git_tree_free(i_tree);
159 git_buf_free(&msg);
160 return error;
161}
162
163struct cb_data {
164 git_index *index;
165
166 bool include_changed;
167 bool include_untracked;
168 bool include_ignored;
169};
170
171static int update_index_cb(
172 void *cb_data,
173 const git_diff_delta *delta,
174 float progress)
175{
176 int pos;
177 struct cb_data *data = (struct cb_data *)cb_data;
178
179 GIT_UNUSED(progress);
180
181 switch (delta->status) {
182 case GIT_DELTA_IGNORED:
183 if (!data->include_ignored)
184 break;
185
186 return git_index_add(data->index, delta->new_file.path, 0);
187
188 case GIT_DELTA_UNTRACKED:
189 if (!data->include_untracked)
190 break;
191
192 return git_index_add(data->index, delta->new_file.path, 0);
193
194 case GIT_DELTA_ADDED:
195 /* Fall through */
196 case GIT_DELTA_MODIFIED:
197 if (!data->include_changed)
198 break;
199
200 return git_index_add(data->index, delta->new_file.path, 0);
201
202 case GIT_DELTA_DELETED:
203 if (!data->include_changed)
204 break;
205
206 if ((pos = git_index_find(data->index, delta->new_file.path)) < 0)
207 return -1;
208
209 if (git_index_remove(data->index, pos) < 0)
210 return -1;
211
212 default:
213 /* Unimplemented */
214 giterr_set(
215 GITERR_INVALID,
216 "Cannot update index. Unimplemented status kind (%d)",
217 delta->status);
218 return -1;
219 }
220
221 return 0;
222}
223
224static int build_untracked_tree(
225 git_tree **tree_out,
226 git_index *index,
227 git_commit *i_commit,
228 uint32_t flags)
229{
230 git_tree *i_tree = NULL;
231 git_diff_list *diff = NULL;
232 git_diff_options opts = {0};
233 struct cb_data data = {0};
234 int error = -1;
235
236 git_index_clear(index);
237
238 data.index = index;
239
240 if (flags & GIT_STASH_INCLUDE_UNTRACKED) {
241 opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_RECURSE_UNTRACKED_DIRS;
242 data.include_untracked = true;
243 }
244
245 if (flags & GIT_STASH_INCLUDE_IGNORED) {
246 opts.flags |= GIT_DIFF_INCLUDE_IGNORED;
247 data.include_ignored = true;
248 }
249
250 if (git_commit_tree(&i_tree, i_commit) < 0)
251 goto cleanup;
252
253 if (git_diff_workdir_to_tree(git_index_owner(index), &opts, i_tree, &diff) < 0)
254 goto cleanup;
255
256 if (git_diff_foreach(diff, &data, update_index_cb, NULL, NULL) < 0)
257 goto cleanup;
258
259 if (build_tree_from_index(tree_out, index) < 0)
260 goto cleanup;
261
262 error = 0;
263
264cleanup:
265 git_diff_list_free(diff);
266 git_tree_free(i_tree);
267 return error;
268}
269
270static int commit_untracked(
271 git_commit **u_commit,
272 git_index *index,
273 git_signature *stasher,
274 const char *message,
275 git_commit *i_commit,
276 uint32_t flags)
277{
278 git_tree *u_tree = NULL;
279 git_oid u_commit_oid;
280 git_buf msg = GIT_BUF_INIT;
281 int error = -1;
282
283 if (build_untracked_tree(&u_tree, index, i_commit, flags) < 0)
284 goto cleanup;
285
286 if (git_buf_printf(&msg, "untracked files on %s\n", message) < 0)
287 goto cleanup;
288
289 if (git_commit_create(
290 &u_commit_oid,
291 git_index_owner(index),
292 NULL,
293 stasher,
294 stasher,
295 NULL,
296 git_buf_cstr(&msg),
297 u_tree,
298 0,
299 NULL) < 0)
300 goto cleanup;
301
302 error = git_commit_lookup(u_commit, git_index_owner(index), &u_commit_oid);
303
304cleanup:
305 git_tree_free(u_tree);
306 git_buf_free(&msg);
307 return error;
308}
309
310static int build_workdir_tree(
311 git_tree **tree_out,
312 git_index *index,
313 git_commit *b_commit)
314{
315 git_tree *b_tree = NULL;
316 git_diff_list *diff = NULL, *diff2 = NULL;
317 git_diff_options opts = {0};
318 struct cb_data data = {0};
319 int error = -1;
320
321 if (git_commit_tree(&b_tree, b_commit) < 0)
322 goto cleanup;
323
324 if (git_diff_index_to_tree(git_index_owner(index), &opts, b_tree, &diff) < 0)
325 goto cleanup;
326
327 if (git_diff_workdir_to_index(git_index_owner(index), &opts, &diff2) < 0)
328 goto cleanup;
329
330 if (git_diff_merge(diff, diff2) < 0)
331 goto cleanup;
332
333 data.index = index;
334 data.include_changed = true;
335
336 if (git_diff_foreach(diff, &data, update_index_cb, NULL, NULL) < 0)
337 goto cleanup;
338
339 if (build_tree_from_index(tree_out, index) < 0)
340 goto cleanup;
341
342 error = 0;
343
344cleanup:
345 git_diff_list_free(diff);
346 git_diff_list_free(diff2);
347 git_tree_free(b_tree);
348 return error;
349}
350
351static int commit_worktree(
352 git_oid *w_commit_oid,
353 git_index *index,
354 git_signature *stasher,
355 const char *message,
356 git_commit *i_commit,
357 git_commit *b_commit,
358 git_commit *u_commit)
359{
360 git_tree *w_tree = NULL, *i_tree = NULL;
361 int error = -1;
362
363 const git_commit *parents[] = { NULL, NULL, NULL };
364
365 parents[0] = b_commit;
366 parents[1] = i_commit;
367 parents[2] = u_commit;
368
369 if (git_commit_tree(&i_tree, i_commit) < 0)
370 return -1;
371
372 if (git_index_read_tree(index, i_tree) < 0)
373 goto cleanup;
374
375 if (build_workdir_tree(&w_tree, index, b_commit) < 0)
376 goto cleanup;
377
378 if (git_commit_create(
379 w_commit_oid,
380 git_index_owner(index),
381 NULL,
382 stasher,
383 stasher,
384 NULL,
385 message,
386 w_tree,
387 u_commit ? 3 : 2, parents) < 0)
388 goto cleanup;
389
390 error = 0;
391
392cleanup:
393 git_tree_free(i_tree);
394 git_tree_free(w_tree);
395 return error;
396}
397
398static int prepare_worktree_commit_message(
399 git_buf* msg,
400 const char *user_message)
401{
402 git_buf buf = GIT_BUF_INIT;
403 int error = -1;
404
405 git_buf_set(&buf, git_buf_cstr(msg), git_buf_len(msg));
406 git_buf_clear(msg);
407
408 if (!user_message)
409 git_buf_printf(msg, "WIP on %s", git_buf_cstr(&buf));
410 else {
411 const char *colon;
412
413 if ((colon = strchr(git_buf_cstr(&buf), ':')) == NULL)
414 goto cleanup;
415
416 git_buf_puts(msg, "On ");
417 git_buf_put(msg, git_buf_cstr(&buf), colon - buf.ptr);
418 git_buf_printf(msg, ": %s\n", user_message);
419 }
420
421 error = git_buf_oom(msg) || git_buf_oom(&buf) ? -1 : 0;
422
423cleanup:
424 git_buf_free(&buf);
425 return error;
426}
427
428static int update_reflog(
429 git_oid *w_commit_oid,
430 git_repository *repo,
431 git_signature *stasher,
432 const char *message)
433{
434 git_reference *stash = NULL;
435 git_reflog *reflog = NULL;
436 int error;
437
438 if ((error = git_reference_create_oid(&stash, repo, GIT_REFS_STASH_FILE, w_commit_oid, 1)) < 0)
439 goto cleanup;
440
441 if ((error = git_reflog_read(&reflog, stash)) < 0)
442 goto cleanup;
443
444 if ((error = git_reflog_append(reflog, w_commit_oid, stasher, message)) < 0)
445 goto cleanup;
446
447 if ((error = git_reflog_write(reflog)) < 0)
448 goto cleanup;
449
450 error = 0;
451
452cleanup:
453 git_reference_free(stash);
454 git_reflog_free(reflog);
455 return error;
456}
457
458static int is_dirty_cb(const char *path, unsigned int status, void *payload)
459{
460 GIT_UNUSED(path);
461 GIT_UNUSED(status);
462 GIT_UNUSED(payload);
463
464 return 1;
465}
466
467static int ensure_there_are_changes_to_stash(
468 git_repository *repo,
469 bool include_untracked_files,
470 bool include_ignored_files)
471{
472 int error;
473 git_status_options opts;
474
475 memset(&opts, 0, sizeof(opts));
476 opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
477 if (include_untracked_files)
478 opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
479 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
480
481 if (include_ignored_files)
482 opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED;
483
484 error = git_status_foreach_ext(repo, &opts, is_dirty_cb, NULL);
485
486 if (error == GIT_EUSER)
487 return 0;
488
489 if (!error)
490 return create_error(GIT_ENOTFOUND, "There is nothing to stash.");
491
492 return error;
493}
494
495static int reset_index_and_workdir(
496 git_repository *repo,
497 git_commit *commit,
498 bool remove_untracked)
499{
500 git_checkout_opts opts;
501
502 memset(&opts, 0, sizeof(git_checkout_opts));
503
504 opts.checkout_strategy =
505 GIT_CHECKOUT_CREATE_MISSING | GIT_CHECKOUT_OVERWRITE_MODIFIED;
506
507 if (remove_untracked)
508 opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_UNTRACKED;
509
510 return git_checkout_tree(repo, (git_object *)commit, &opts);
511}
512
513int git_stash_save(
514 git_oid *out,
515 git_repository *repo,
516 git_signature *stasher,
517 const char *message,
518 uint32_t flags)
519{
520 git_index *index = NULL;
521 git_commit *b_commit = NULL, *i_commit = NULL, *u_commit = NULL;
522 git_buf msg = GIT_BUF_INIT;
523 int error;
524
525 assert(out && repo && stasher);
526
527 if ((error = ensure_non_bare_repository(repo)) < 0)
528 return error;
529
530 if ((error = retrieve_base_commit_and_message(&b_commit, &msg, repo)) < 0)
531 goto cleanup;
532
533 if ((error = ensure_there_are_changes_to_stash(
534 repo,
535 (flags & GIT_STASH_INCLUDE_UNTRACKED) == GIT_STASH_INCLUDE_UNTRACKED,
536 (flags & GIT_STASH_INCLUDE_IGNORED) == GIT_STASH_INCLUDE_IGNORED)) < 0)
537 goto cleanup;
538
539 error = -1;
540
541 if (git_repository_index(&index, repo) < 0)
542 goto cleanup;
543
544 if (commit_index(&i_commit, index, stasher, git_buf_cstr(&msg), b_commit) < 0)
545 goto cleanup;
546
547 if ((flags & GIT_STASH_INCLUDE_UNTRACKED || flags & GIT_STASH_INCLUDE_IGNORED)
548 && commit_untracked(&u_commit, index, stasher, git_buf_cstr(&msg), i_commit, flags) < 0)
549 goto cleanup;
550
551 if (prepare_worktree_commit_message(&msg, message) < 0)
552 goto cleanup;
553
554 if (commit_worktree(out, index, stasher, git_buf_cstr(&msg), i_commit, b_commit, u_commit) < 0)
555 goto cleanup;
556
557 git_buf_rtrim(&msg);
558 if (update_reflog(out, repo, stasher, git_buf_cstr(&msg)) < 0)
559 goto cleanup;
560
561 if (reset_index_and_workdir(
562 repo,
563 ((flags & GIT_STASH_KEEP_INDEX) == GIT_STASH_KEEP_INDEX) ?
564 i_commit : b_commit,
565 (flags & GIT_STASH_INCLUDE_UNTRACKED) == GIT_STASH_INCLUDE_UNTRACKED) < 0)
566 goto cleanup;
567
568 error = 0;
569
570cleanup:
571 git_buf_free(&msg);
572 git_commit_free(i_commit);
573 git_commit_free(b_commit);
574 git_commit_free(u_commit);
575 git_index_free(index);
576 return error;
577}
23388413 578
579int git_stash_foreach(
580 git_repository *repo,
581 stash_cb callback,
582 void *payload)
583{
584 git_reference *stash;
585 git_reflog *reflog = NULL;
586 int error;
587 size_t i, max;
588 const git_reflog_entry *entry;
589
590 error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE);
591 if (error == GIT_ENOTFOUND)
592 return 0;
593
594 if (error < 0)
595 goto cleanup;
596
597 if ((error = git_reflog_read(&reflog, stash)) < 0)
598 goto cleanup;
599
600 max = git_reflog_entrycount(reflog);
601 for (i = 0; i < max; i++) {
602 entry = git_reflog_entry_byindex(reflog, max - i - 1);
603
604 if (callback(i,
605 git_reflog_entry_msg(entry),
606 git_reflog_entry_oidnew(entry),
607 payload)) {
608 error = GIT_EUSER;
609 goto cleanup;
610 }
611 }
612
613 error = 0;
614
615cleanup:
616 git_reference_free(stash);
617 git_reflog_free(reflog);
618 return error;
619}