]>
git.proxmox.com Git - libgit2.git/blob - examples/stash.c
2 * libgit2 "stash" example - shows how to use the stash API
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/>.
32 static void usage(const char *fmt
, ...)
36 fputs("usage: git stash list\n", stderr
);
37 fputs(" or: git stash ( pop | apply )\n", stderr
);
38 fputs(" or: git stash [push]\n", stderr
);
42 vfprintf(stderr
, fmt
, ap
);
48 static void parse_subcommand(struct opts
*opts
, int argc
, char *argv
[])
50 char *arg
= (argc
< 2) ? "push" : argv
[1];
53 if (!strcmp(arg
, "apply")) {
55 } else if (!strcmp(arg
, "list")) {
57 } else if (!strcmp(arg
, "pop")) {
59 } else if (!strcmp(arg
, "push")) {
62 usage("invalid command %s", arg
);
67 opts
->argc
= (argc
< 2) ? argc
- 1 : argc
- 2;
71 static int cmd_apply(git_repository
*repo
, struct opts
*opts
)
74 usage("apply does not accept any parameters");
76 check_lg2(git_stash_apply(repo
, 0, NULL
),
77 "Unable to apply stash", NULL
);
82 static int list_stash_cb(size_t index
, const char *message
,
83 const git_oid
*stash_id
, void *payload
)
87 printf("stash@{%"PRIuZ
"}: %s\n", index
, message
);
91 static int cmd_list(git_repository
*repo
, struct opts
*opts
)
94 usage("list does not accept any parameters");
96 check_lg2(git_stash_foreach(repo
, list_stash_cb
, NULL
),
97 "Unable to list stashes", NULL
);
102 static int cmd_push(git_repository
*repo
, struct opts
*opts
)
104 git_signature
*signature
;
109 usage("push does not accept any parameters");
111 check_lg2(git_signature_default(&signature
, repo
),
112 "Unable to get signature", NULL
);
113 check_lg2(git_stash_save(&stashid
, repo
, signature
, NULL
, GIT_STASH_DEFAULT
),
114 "Unable to save stash", NULL
);
115 check_lg2(git_commit_lookup(&stash
, repo
, &stashid
),
116 "Unable to lookup stash commit", NULL
);
118 printf("Saved working directory %s\n", git_commit_summary(stash
));
120 git_signature_free(signature
);
121 git_commit_free(stash
);
126 static int cmd_pop(git_repository
*repo
, struct opts
*opts
)
129 usage("pop does not accept any parameters");
131 check_lg2(git_stash_pop(repo
, 0, NULL
),
132 "Unable to pop stash", NULL
);
134 printf("Dropped refs/stash@{0}\n");
139 int lg2_stash(git_repository
*repo
, int argc
, char *argv
[])
141 struct opts opts
= { 0 };
143 parse_subcommand(&opts
, argc
, argv
);
147 return cmd_apply(repo
, &opts
);
149 return cmd_list(repo
, &opts
);
151 return cmd_push(repo
, &opts
);
153 return cmd_pop(repo
, &opts
);