@matheusbsbs / matheus-bagliano / commits / 2b713c272c
Fork 0

fetch: allow refspecs specified through stdin

In a subsequent patch, partial clones will be taught to fetch missing objects using a "git fetch" subprocess. Because the number of objects fetched may be too numerous to fit on the command line, teach "fetch" to accept refspecs passed through stdin. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Aug 17, 2020 at 21:01 UTC 2b713c272cd8f9dc70ad746e8ba01a93d8baa023
2 files changed +21 -2
Documentation/git-fetch.txt
+4
@@ -48,6 +48,10 @@ include::fetch-options.txt[]
48
49 include::pull-fetch-param.txt[]
50
51 +--stdin::
52 + Read refspecs, one per line, from stdin in addition to those provided
53 + as arguments. The "tag <name>" format is not supported.
54 +
55 include::urls-remotes.txt[]
56
57
builtin/fetch.c
+17 -2
@@ -80,6 +80,7 @@ static struct list_objects_filter_options filter_options;
80 static struct string_list server_options = STRING_LIST_INIT_DUP;
81 static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP;
82 static int fetch_write_commit_graph = -1;
83 +static int stdin_refspecs = 0;
84
85 static int git_fetch_config(const char *k, const char *v, void *cb)
86 {
@@ -205,6 +206,8 @@ static struct option builtin_fetch_options[] = {
206 N_("check for forced-updates on all updated branches")),
207 OPT_BOOL(0, "write-commit-graph", &fetch_write_commit_graph,
208 N_("write the commit-graph after fetching")),
209 + OPT_BOOL(0, "stdin", &stdin_refspecs,
210 + N_("accept refspecs from stdin")),
211 OPT_END()
212 };
213
@@ -1680,7 +1683,8 @@ static inline void fetch_one_setup_partial(struct remote *remote)
1683 return;
1684 }
1685
1683 -static int fetch_one(struct remote *remote, int argc, const char **argv, int prune_tags_ok)
1686 +static int fetch_one(struct remote *remote, int argc, const char **argv,
1687 + int prune_tags_ok, int use_stdin_refspecs)
1688 {
1689 struct refspec rs = REFSPEC_INIT_FETCH;
1690 int i;
@@ -1737,6 +1741,13 @@ static int fetch_one(struct remote *remote, int argc, const char **argv, int pru
1741 }
1742 }
1743
1744 + if (use_stdin_refspecs) {
1745 + struct strbuf line = STRBUF_INIT;
1746 + while (strbuf_getline_lf(&line, stdin) != EOF)
1747 + refspec_append(&rs, line.buf);
1748 + strbuf_release(&line);
1749 + }
1750 +
1751 if (server_options.nr)
1752 gtransport->server_options = &server_options;
1753
@@ -1837,7 +1848,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
1848 if (remote) {
1849 if (filter_options.choice || has_promisor_remote())
1850 fetch_one_setup_partial(remote);
1840 - result = fetch_one(remote, argc, argv, prune_tags_ok);
1851 + result = fetch_one(remote, argc, argv, prune_tags_ok, stdin_refspecs);
1852 } else {
1853 int max_children = max_jobs;
1854
@@ -1845,6 +1856,10 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
1856 die(_("--filter can only be used with the remote "
1857 "configured in extensions.partialclone"));
1858
1859 + if (stdin_refspecs)
1860 + die(_("--stdin can only be used when fetching "
1861 + "from one remote"));
1862 +
1863 if (max_children < 0)
1864 max_children = fetch_parallel_config;
1865