@matheusbsbs / matheus-bagliano / commits / 552cecc214
Fork 0

Teach "git reflog" a subcommand to delete single entries

This commit implements the "delete" subcommand: git reflog delete master@{2} will delete the second reflog entry of the "master" branch. With this, it should be easy to implement "git stash pop" everybody seems to want these days. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Johannes Schindelin committed Oct 17, 2007 at 02:50 UTC 552cecc21447efe9d5f9a86d55b5e428d56a0c53
3 files changed +90
Documentation/git-reflog.txt
+5
@@ -19,6 +19,8 @@ depending on the subcommand:
19 git reflog expire [--dry-run] [--stale-fix] [--verbose]
20 [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...
21
22 +git reflog delete ref@\{specifier\}...
23 +
24 git reflog [show] [log-options]
25
26 Reflog is a mechanism to record when the tip of branches are
@@ -36,6 +38,9 @@ subcommands) will take all the normal log options, and show the log of
38 It is basically an alias for 'git log -g --abbrev-commit
39 --pretty=oneline', see gitlink:git-log[1].
40
41 +To delete single entries from the reflog, use the subcommand "delete"
42 +and specify the _exact_ entry (e.g. ``git reflog delete master@\{2\}'').
43 +
44
45 OPTIONS
46 -------
builtin-reflog.c
+59
@@ -25,6 +25,7 @@ struct cmd_reflog_expire_cb {
25 int verbose;
26 unsigned long expire_total;
27 unsigned long expire_unreachable;
28 + int recno;
29 };
30
31 struct expire_reflog_cb {
@@ -220,6 +221,9 @@ static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
221 goto prune;
222 }
223
224 + if (cb->cmd->recno && --(cb->cmd->recno) == 0)
225 + goto prune;
226 +
227 if (cb->newlog) {
228 char sign = (tz < 0) ? '-' : '+';
229 int zone = (tz < 0) ? (-tz) : tz;
@@ -363,6 +367,58 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
367 return status;
368 }
369
370 +static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
371 + const char *email, unsigned long timestamp, int tz,
372 + const char *message, void *cb_data)
373 +{
374 + struct cmd_reflog_expire_cb *cb = cb_data;
375 + if (!cb->expire_total || timestamp < cb->expire_total)
376 + cb->recno++;
377 + return 0;
378 +}
379 +
380 +static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
381 +{
382 + struct cmd_reflog_expire_cb cb;
383 + int i, status = 0;
384 +
385 + if (argc < 2)
386 + return error("Nothing to delete?");
387 +
388 + memset(&cb, 0, sizeof(cb));
389 +
390 + for (i = 1; i < argc; i++) {
391 + const char *spec = strstr(argv[i], "@{");
392 + unsigned char sha1[20];
393 + char *ep, *ref;
394 + int recno;
395 +
396 + if (!spec) {
397 + status |= error("Not a reflog: %s", ref);
398 + continue;
399 + }
400 +
401 + if (!dwim_ref(argv[i], spec - argv[i], sha1, &ref)) {
402 + status |= error("%s points nowhere!", argv[i]);
403 + continue;
404 + }
405 +
406 + recno = strtoul(spec + 2, &ep, 10);
407 + if (*ep == '}') {
408 + cb.recno = -recno;
409 + for_each_reflog_ent(ref, count_reflog_ent, &cb);
410 + } else {
411 + cb.expire_total = approxidate(spec + 2);
412 + for_each_reflog_ent(ref, count_reflog_ent, &cb);
413 + cb.expire_total = 0;
414 + }
415 +
416 + status |= expire_reflog(ref, sha1, 0, &cb);
417 + free(ref);
418 + }
419 + return status;
420 +}
421 +
422 /*
423 * main "reflog"
424 */
@@ -382,6 +438,9 @@ int cmd_reflog(int argc, const char **argv, const char *prefix)
438 if (!strcmp(argv[1], "expire"))
439 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
440
441 + if (!strcmp(argv[1], "delete"))
442 + return cmd_reflog_delete(argc - 1, argv + 1, prefix);
443 +
444 /* Not a recognized reflog command..*/
445 usage(reflog_usage);
446 }
t/t1410-reflog.sh
+26
@@ -175,4 +175,30 @@ test_expect_success 'recover and check' '
175
176 '
177
178 +test_expect_success 'delete' '
179 + echo 1 > C &&
180 + test_tick &&
181 + git commit -m rat C &&
182 +
183 + echo 2 > C &&
184 + test_tick &&
185 + git commit -m ox C &&
186 +
187 + echo 3 > C &&
188 + test_tick &&
189 + git commit -m tiger C &&
190 +
191 + test 5 = $(git reflog | wc -l) &&
192 +
193 + git reflog delete master@{1} &&
194 + git reflog show master > output &&
195 + test 4 = $(wc -l < output) &&
196 + ! grep ox < output &&
197 +
198 + git reflog delete master@{07.04.2005.15:15:00.-0700} &&
199 + git reflog show master > output &&
200 + test 3 = $(wc -l < output) &&
201 + ! grep dragon < output
202 +'
203 +
204 test_done