Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
git-mirror
/
git
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
0
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security
Insights
Files
e23d0b4
Documentation
arm
compat
contrib
mozilla-sha1
ppc
t
templates
xdiff
.gitignore
COPYING
GIT-VERSION-GEN
INSTALL
Makefile
README
apply.c
blame.c
blob.c
blob.h
cache.h
cat-file.c
check-ref-format.c
checkout-index.c
clone-pack.c
combine-diff.c
commit-tree.c
commit.c
commit.h
config.c
connect.c
convert-objects.c
copy.c
csum-file.c
csum-file.h
ctype.c
daemon.c
date.c
delta.h
describe.c
diff-delta.c
diff-files.c
diff-index.c
diff-stages.c
diff-tree.c
diff.c
diff.h
diffcore-break.c
diffcore-delta.c
diffcore-order.c
diffcore-pickaxe.c
diffcore-rename.c
diffcore.h
entry.c
environment.c
exec_cmd.c
exec_cmd.h
fetch-clone.c
fetch-pack.c
fetch.c
fetch.h
fsck-objects.c
generate-cmdlist.sh
get-tar-commit-id.c
git-add.sh
git-am.sh
git-annotate.perl
git-applymbox.sh
git-applypatch.sh
git-archimport.perl
git-bisect.sh
git-branch.sh
git-checkout.sh
git-cherry.sh
git-clean.sh
git-clone.sh
git-commit.sh
git-compat-util.h
git-count-objects.sh
git-cvsexportcommit.perl
git-cvsimport.perl
git-cvsserver.perl
git-diff.sh
git-fetch.sh
git-fmt-merge-msg.perl
git-format-patch.sh
git-grep.sh
git-lost-found.sh
git-ls-remote.sh
git-merge-octopus.sh
git-merge-one-file.sh
git-merge-ours.sh
git-merge-recursive.py
git-merge-resolve.sh
git-merge-stupid.sh
git-merge.sh
git-mv.perl
git-parse-remote.sh
git-prune.sh
git-pull.sh
git-push.sh
git-rebase.sh
git-relink.perl
git-repack.sh
git-request-pull.sh
git-rerere.perl
git-reset.sh
git-resolve.sh
git-revert.sh
git-rm.sh
git-send-email.perl
git-sh-setup.sh
git-shortlog.perl
git-svnimport.perl
git-tag.sh
git-verify-tag.sh
git-whatchanged.sh
git.c
git.spec.in
gitMergeCommon.py
gitk
hash-object.c
http-fetch.c
http-push.c
http.c
http.h
ident.c
imap-send.c
index-pack.c
index.c
init-db.c
local-fetch.c
log-tree.c
log-tree.h
ls-files.c
ls-tree.c
mailinfo.c
mailsplit.c
merge-base.c
merge-index.c
merge-tree.c
mktag.c
mktree.c
name-rev.c
object.c
object.h
pack-check.c
pack-objects.c
pack-redundant.c
pack.h
pager.c
patch-delta.c
patch-id.c
path.c
peek-remote.c
pkt-line.c
pkt-line.h
prune-packed.c
quote.c
quote.h
read-cache.c
read-tree.c
receive-pack.c
refs.c
refs.h
repo-config.c
rev-list.c
rev-parse.c
revision.c
revision.h
rsh.c
rsh.h
run-command.c
run-command.h
send-pack.c
server-info.c
setup.c
sha1_file.c
sha1_name.c
shell.c
show-branch.c
show-index.c
ssh-fetch.c
ssh-pull.c
ssh-push.c
ssh-upload.c
strbuf.c
strbuf.h
stripspace.c
symbolic-ref.c
tag.c
tag.h
tar-tree.c
tar.h
test-date.c
test-delta.c
tree-diff.c
tree-walk.c
tree-walk.h
tree.c
tree.h
unpack-file.c
unpack-objects.c
update-index.c
update-ref.c
update-server-info.c
upload-pack.c
usage.c
var.c
verify-pack.c
write-tree.c
xdiff-interface.c
xdiff-interface.h
Breadcrumbs
git
/
rev-parse.c
Blame
Blame
Latest commit
History
History
349 lines (324 loc) · 6.74 KB
Breadcrumbs
git
/
rev-parse.c
Top
File metadata and controls
Code
Blame
349 lines (324 loc) · 6.74 KB
Raw
/* * rev-parse.c * * Copyright (C) Linus Torvalds, 2005 */ #include "cache.h" #include "commit.h" #include "refs.h" #include "quote.h" #define DO_REVS 1 #define DO_NOREV 2 #define DO_FLAGS 4 #define DO_NONFLAGS 8 static int filter = ~0; static char *def = NULL; #define NORMAL 0 #define REVERSED 1 static int show_type = NORMAL; static int symbolic = 0; static int abbrev = 0; static int output_sq = 0; static int revs_count = 0; /* * Some arguments are relevant "revision" arguments, * others are about output format or other details. * This sorts it all out. */ static int is_rev_argument(const char *arg) { static const char *rev_args[] = { "--all", "--bisect", "--dense", "--header", "--max-age=", "--max-count=", "--min-age=", "--no-merges", "--objects", "--objects-edge", "--parents", "--pretty", "--sparse", "--topo-order", "--date-order", "--unpacked", NULL }; const char **p = rev_args; /* accept -<digit>, like traditional "head" */ if ((*arg == '-') && isdigit(arg[1])) return 1; for (;;) { const char *str = *p++; int len; if (!str) return 0; len = strlen(str); if (!strcmp(arg, str) || (str[len-1] == '=' && !strncmp(arg, str, len))) return 1; } } /* Output argument as a string, either SQ or normal */ static void show(const char *arg) { if (output_sq) { int sq = '\'', ch; putchar(sq); while ((ch = *arg++)) { if (ch == sq) fputs("'\\'", stdout); putchar(ch); } putchar(sq); putchar(' '); } else puts(arg); } /* Output a revision, only if filter allows it */ static void show_rev(int type, const unsigned char *sha1, const char *name) { if (!(filter & DO_REVS)) return; def = NULL; revs_count++; if (type != show_type) putchar('^'); if (symbolic && name) show(name); else if (abbrev) show(find_unique_abbrev(sha1, abbrev)); else show(sha1_to_hex(sha1)); } /* Output a flag, only if filter allows it. */ static int show_flag(char *arg) { if (!(filter & DO_FLAGS)) return 0; if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) { show(arg); return 1; } return 0; } static void show_default(void) { char *s = def; if (s) { unsigned char sha1[20]; def = NULL; if (!get_sha1(s, sha1)) { show_rev(NORMAL, sha1, s); return; } } } static int show_reference(const char *refname, const unsigned char *sha1) { show_rev(NORMAL, sha1, refname); return 0; } static void show_datestring(const char *flag, const char *datestr) { static char buffer[100]; /* date handling requires both flags and revs */ if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS)) return; snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr)); show(buffer); } static int show_file(const char *arg) { show_default(); if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) { show(arg); return 1; } return 0; } int main(int argc, char **argv) { int i, as_is = 0, verify = 0; unsigned char sha1[20]; const char *prefix = setup_git_directory(); git_config(git_default_config); for (i = 1; i < argc; i++) { char *arg = argv[i]; char *dotdot; if (as_is) { if (show_file(arg) && as_is < 2) verify_filename(prefix, arg); continue; } if (!strcmp(arg,"-n")) { if (++i >= argc) die("-n requires an argument"); if ((filter & DO_FLAGS) && (filter & DO_REVS)) { show(arg); show(argv[i]); } continue; } if (!strncmp(arg,"-n",2)) { if ((filter & DO_FLAGS) && (filter & DO_REVS)) show(arg); continue; } if (*arg == '-') { if (!strcmp(arg, "--")) { as_is = 2; /* Pass on the "--" if we show anything but files.. */ if (filter & (DO_FLAGS | DO_REVS)) show_file(arg); continue; } if (!strcmp(arg, "--default")) { def = argv[i+1]; i++; continue; } if (!strcmp(arg, "--revs-only")) { filter &= ~DO_NOREV; continue; } if (!strcmp(arg, "--no-revs")) { filter &= ~DO_REVS; continue; } if (!strcmp(arg, "--flags")) { filter &= ~DO_NONFLAGS; continue; } if (!strcmp(arg, "--no-flags")) { filter &= ~DO_FLAGS; continue; } if (!strcmp(arg, "--verify")) { filter &= ~(DO_FLAGS|DO_NOREV); verify = 1; continue; } if (!strcmp(arg, "--short") || !strncmp(arg, "--short=", 8)) { filter &= ~(DO_FLAGS|DO_NOREV); verify = 1; abbrev = DEFAULT_ABBREV; if (arg[7] == '=') abbrev = strtoul(arg + 8, NULL, 10); if (abbrev < MINIMUM_ABBREV) abbrev = MINIMUM_ABBREV; else if (40 <= abbrev) abbrev = 40; continue; } if (!strcmp(arg, "--sq")) { output_sq = 1; continue; } if (!strcmp(arg, "--not")) { show_type ^= REVERSED; continue; } if (!strcmp(arg, "--symbolic")) { symbolic = 1; continue; } if (!strcmp(arg, "--all")) { for_each_ref(show_reference); continue; } if (!strcmp(arg, "--show-prefix")) { if (prefix) puts(prefix); continue; } if (!strcmp(arg, "--show-cdup")) { const char *pfx = prefix; while (pfx) { pfx = strchr(pfx, '/'); if (pfx) { pfx++; printf("../"); } } putchar('\n'); continue; } if (!strcmp(arg, "--git-dir")) { const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); static char cwd[PATH_MAX]; if (gitdir) { puts(gitdir); continue; } if (!prefix) { puts(".git"); continue; } if (!getcwd(cwd, PATH_MAX)) die("unable to get current working directory"); printf("%s/.git\n", cwd); continue; } if (!strncmp(arg, "--since=", 8)) { show_datestring("--max-age=", arg+8); continue; } if (!strncmp(arg, "--after=", 8)) { show_datestring("--max-age=", arg+8); continue; } if (!strncmp(arg, "--before=", 9)) { show_datestring("--min-age=", arg+9); continue; } if (!strncmp(arg, "--until=", 8)) { show_datestring("--min-age=", arg+8); continue; } if (show_flag(arg) && verify) die("Needed a single revision"); continue; } /* Not a flag argument */ dotdot = strstr(arg, ".."); if (dotdot) { unsigned char end[20]; char *next = dotdot + 2; char *this = arg; *dotdot = 0; if (!*next) next = "HEAD"; if (dotdot == arg) this = "HEAD"; if (!get_sha1(this, sha1) && !get_sha1(next, end)) { show_rev(NORMAL, end, next); show_rev(REVERSED, sha1, this); continue; } *dotdot = '.'; } if (!get_sha1(arg, sha1)) { show_rev(NORMAL, sha1, arg); continue; } if (*arg == '^' && !get_sha1(arg+1, sha1)) { show_rev(REVERSED, sha1, arg+1); continue; } as_is = 1; if (!show_file(arg)) continue; if (verify) die("Needed a single revision"); verify_filename(prefix, arg); } show_default(); if (verify && revs_count != 1) die("Needed a single revision"); return 0; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
You can’t perform that action at this time.