]> git.proxmox.com Git - libgit2.git/blame - src/stash.c
Deploy GIT_REPOSITORY_INIT_OPTIONS_INIT
[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;
a8122b5d 59 size_t pos = 0, len;
590fb68b 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
2508cc66 101 if (git_commit_lookup(b_commit, repo, git_reference_target(head)) < 0)
590fb68b 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
43eeca04 118 if (git_index_write_tree(&i_tree_oid, index) < 0)
590fb68b 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(
590fb68b 172 const git_diff_delta *delta,
793c4385
RB
173 float progress,
174 void *payload)
590fb68b 175{
176 int pos;
793c4385 177 struct cb_data *data = (struct cb_data *)payload;
590fb68b 178
179 GIT_UNUSED(progress);
180
181 switch (delta->status) {
182 case GIT_DELTA_IGNORED:
183 if (!data->include_ignored)
184 break;
185
f45ec1a0 186 return git_index_add_from_workdir(data->index, delta->new_file.path);
590fb68b 187
188 case GIT_DELTA_UNTRACKED:
189 if (!data->include_untracked)
190 break;
191
f45ec1a0 192 return git_index_add_from_workdir(data->index, delta->new_file.path);
590fb68b 193
194 case GIT_DELTA_ADDED:
195 /* Fall through */
196 case GIT_DELTA_MODIFIED:
197 if (!data->include_changed)
198 break;
199
f45ec1a0 200 return git_index_add_from_workdir(data->index, delta->new_file.path);
590fb68b 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
f45ec1a0 209 if (git_index_remove(data->index, delta->new_file.path, 0) < 0)
590fb68b 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;
2f8d30be 232 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
590fb68b 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
5735bf5e 253 if (git_diff_workdir_to_tree(&diff, git_index_owner(index), i_tree, &opts) < 0)
590fb68b 254 goto cleanup;
255
793c4385 256 if (git_diff_foreach(diff, update_index_cb, NULL, NULL, &data) < 0)
590fb68b 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{
5735bf5e 315 git_repository *repo = git_index_owner(index);
590fb68b 316 git_tree *b_tree = NULL;
317 git_diff_list *diff = NULL, *diff2 = NULL;
2f8d30be 318 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
590fb68b 319 struct cb_data data = {0};
320 int error = -1;
321
322 if (git_commit_tree(&b_tree, b_commit) < 0)
323 goto cleanup;
324
bbe6dbec 325 if (git_diff_index_to_tree(&diff, repo, b_tree, NULL, &opts) < 0)
590fb68b 326 goto cleanup;
327
bbe6dbec 328 if (git_diff_workdir_to_index(&diff2, repo, NULL, &opts) < 0)
590fb68b 329 goto cleanup;
330
331 if (git_diff_merge(diff, diff2) < 0)
332 goto cleanup;
333
334 data.index = index;
335 data.include_changed = true;
336
793c4385 337 if (git_diff_foreach(diff, update_index_cb, NULL, NULL, &data) < 0)
590fb68b 338 goto cleanup;
339
340 if (build_tree_from_index(tree_out, index) < 0)
341 goto cleanup;
342
343 error = 0;
344
345cleanup:
346 git_diff_list_free(diff);
347 git_diff_list_free(diff2);
348 git_tree_free(b_tree);
349 return error;
350}
351
352static int commit_worktree(
353 git_oid *w_commit_oid,
354 git_index *index,
355 git_signature *stasher,
356 const char *message,
357 git_commit *i_commit,
358 git_commit *b_commit,
359 git_commit *u_commit)
360{
361 git_tree *w_tree = NULL, *i_tree = NULL;
362 int error = -1;
363
364 const git_commit *parents[] = { NULL, NULL, NULL };
365
366 parents[0] = b_commit;
367 parents[1] = i_commit;
368 parents[2] = u_commit;
369
370 if (git_commit_tree(&i_tree, i_commit) < 0)
371 return -1;
372
373 if (git_index_read_tree(index, i_tree) < 0)
374 goto cleanup;
375
376 if (build_workdir_tree(&w_tree, index, b_commit) < 0)
377 goto cleanup;
378
379 if (git_commit_create(
380 w_commit_oid,
381 git_index_owner(index),
382 NULL,
383 stasher,
384 stasher,
385 NULL,
386 message,
387 w_tree,
388 u_commit ? 3 : 2, parents) < 0)
389 goto cleanup;
390
391 error = 0;
392
393cleanup:
394 git_tree_free(i_tree);
395 git_tree_free(w_tree);
396 return error;
397}
398
399static int prepare_worktree_commit_message(
400 git_buf* msg,
401 const char *user_message)
402{
403 git_buf buf = GIT_BUF_INIT;
404 int error = -1;
405
406 git_buf_set(&buf, git_buf_cstr(msg), git_buf_len(msg));
407 git_buf_clear(msg);
408
409 if (!user_message)
410 git_buf_printf(msg, "WIP on %s", git_buf_cstr(&buf));
411 else {
412 const char *colon;
413
414 if ((colon = strchr(git_buf_cstr(&buf), ':')) == NULL)
415 goto cleanup;
416
417 git_buf_puts(msg, "On ");
418 git_buf_put(msg, git_buf_cstr(&buf), colon - buf.ptr);
419 git_buf_printf(msg, ": %s\n", user_message);
420 }
421
422 error = git_buf_oom(msg) || git_buf_oom(&buf) ? -1 : 0;
423
424cleanup:
425 git_buf_free(&buf);
426 return error;
427}
428
429static int update_reflog(
430 git_oid *w_commit_oid,
431 git_repository *repo,
432 git_signature *stasher,
433 const char *message)
434{
435 git_reference *stash = NULL;
436 git_reflog *reflog = NULL;
437 int error;
438
2508cc66 439 if ((error = git_reference_create(&stash, repo, GIT_REFS_STASH_FILE, w_commit_oid, 1)) < 0)
590fb68b 440 goto cleanup;
441
442 if ((error = git_reflog_read(&reflog, stash)) < 0)
443 goto cleanup;
444
445 if ((error = git_reflog_append(reflog, w_commit_oid, stasher, message)) < 0)
446 goto cleanup;
447
448 if ((error = git_reflog_write(reflog)) < 0)
449 goto cleanup;
450
451 error = 0;
452
453cleanup:
454 git_reference_free(stash);
455 git_reflog_free(reflog);
456 return error;
457}
458
459static int is_dirty_cb(const char *path, unsigned int status, void *payload)
460{
461 GIT_UNUSED(path);
462 GIT_UNUSED(status);
463 GIT_UNUSED(payload);
464
465 return 1;
466}
467
468static int ensure_there_are_changes_to_stash(
469 git_repository *repo,
470 bool include_untracked_files,
471 bool include_ignored_files)
472{
473 int error;
474 git_status_options opts;
475
476 memset(&opts, 0, sizeof(opts));
477 opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
478 if (include_untracked_files)
479 opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
480 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
481
482 if (include_ignored_files)
483 opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED;
484
485 error = git_status_foreach_ext(repo, &opts, is_dirty_cb, NULL);
486
487 if (error == GIT_EUSER)
488 return 0;
489
490 if (!error)
491 return create_error(GIT_ENOTFOUND, "There is nothing to stash.");
492
493 return error;
494}
495
496static int reset_index_and_workdir(
497 git_repository *repo,
498 git_commit *commit,
499 bool remove_untracked)
500{
b81aa2f1 501 git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
590fb68b 502
ad9a921b
RB
503 opts.checkout_strategy =
504 GIT_CHECKOUT_UPDATE_MODIFIED | GIT_CHECKOUT_UPDATE_UNTRACKED;
590fb68b 505
506 if (remove_untracked)
507 opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_UNTRACKED;
508
509 return git_checkout_tree(repo, (git_object *)commit, &opts);
510}
511
512int git_stash_save(
513 git_oid *out,
514 git_repository *repo,
515 git_signature *stasher,
516 const char *message,
517 uint32_t flags)
518{
519 git_index *index = NULL;
520 git_commit *b_commit = NULL, *i_commit = NULL, *u_commit = NULL;
521 git_buf msg = GIT_BUF_INIT;
522 int error;
523
524 assert(out && repo && stasher);
525
526 if ((error = ensure_non_bare_repository(repo)) < 0)
527 return error;
528
529 if ((error = retrieve_base_commit_and_message(&b_commit, &msg, repo)) < 0)
530 goto cleanup;
531
532 if ((error = ensure_there_are_changes_to_stash(
533 repo,
534 (flags & GIT_STASH_INCLUDE_UNTRACKED) == GIT_STASH_INCLUDE_UNTRACKED,
535 (flags & GIT_STASH_INCLUDE_IGNORED) == GIT_STASH_INCLUDE_IGNORED)) < 0)
536 goto cleanup;
537
538 error = -1;
539
540 if (git_repository_index(&index, repo) < 0)
541 goto cleanup;
542
543 if (commit_index(&i_commit, index, stasher, git_buf_cstr(&msg), b_commit) < 0)
544 goto cleanup;
545
546 if ((flags & GIT_STASH_INCLUDE_UNTRACKED || flags & GIT_STASH_INCLUDE_IGNORED)
547 && commit_untracked(&u_commit, index, stasher, git_buf_cstr(&msg), i_commit, flags) < 0)
548 goto cleanup;
549
550 if (prepare_worktree_commit_message(&msg, message) < 0)
551 goto cleanup;
552
553 if (commit_worktree(out, index, stasher, git_buf_cstr(&msg), i_commit, b_commit, u_commit) < 0)
554 goto cleanup;
555
556 git_buf_rtrim(&msg);
557 if (update_reflog(out, repo, stasher, git_buf_cstr(&msg)) < 0)
558 goto cleanup;
559
560 if (reset_index_and_workdir(
561 repo,
562 ((flags & GIT_STASH_KEEP_INDEX) == GIT_STASH_KEEP_INDEX) ?
563 i_commit : b_commit,
564 (flags & GIT_STASH_INCLUDE_UNTRACKED) == GIT_STASH_INCLUDE_UNTRACKED) < 0)
565 goto cleanup;
566
567 error = 0;
568
569cleanup:
570 git_buf_free(&msg);
571 git_commit_free(i_commit);
572 git_commit_free(b_commit);
573 git_commit_free(u_commit);
574 git_index_free(index);
575 return error;
576}
23388413 577
578int git_stash_foreach(
579 git_repository *repo,
1d8ec670 580 git_stash_cb callback,
23388413 581 void *payload)
582{
583 git_reference *stash;
584 git_reflog *reflog = NULL;
585 int error;
586 size_t i, max;
587 const git_reflog_entry *entry;
588
589 error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE);
590 if (error == GIT_ENOTFOUND)
591 return 0;
592
593 if (error < 0)
594 goto cleanup;
595
596 if ((error = git_reflog_read(&reflog, stash)) < 0)
597 goto cleanup;
598
599 max = git_reflog_entrycount(reflog);
600 for (i = 0; i < max; i++) {
b15df1d9 601 entry = git_reflog_entry_byindex(reflog, i);
23388413 602
603 if (callback(i,
2508cc66
BS
604 git_reflog_entry_message(entry),
605 git_reflog_entry_id_new(entry),
23388413 606 payload)) {
607 error = GIT_EUSER;
608 goto cleanup;
609 }
610 }
611
612 error = 0;
613
614cleanup:
615 git_reference_free(stash);
616 git_reflog_free(reflog);
617 return error;
618}
e4c64cf2 619
620int git_stash_drop(
621 git_repository *repo,
622 size_t index)
623{
624 git_reference *stash;
625 git_reflog *reflog = NULL;
626 size_t max;
627 int error;
628
629 if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0)
630 return error;
631
632 if ((error = git_reflog_read(&reflog, stash)) < 0)
633 goto cleanup;
634
635 max = git_reflog_entrycount(reflog);
636
637 if (index > max - 1) {
638 error = GIT_ENOTFOUND;
639 giterr_set(GITERR_STASH, "No stashed state at position %" PRIuZ, index);
640 goto cleanup;
641 }
642
b15df1d9 643 if ((error = git_reflog_drop(reflog, index, true)) < 0)
e4c64cf2 644 goto cleanup;
645
646 if ((error = git_reflog_write(reflog)) < 0)
647 goto cleanup;
648
649 if (max == 1) {
650 error = git_reference_delete(stash);
651 stash = NULL;
652 }
653
654cleanup:
655 git_reference_free(stash);
656 git_reflog_free(reflog);
657 return error;
658}