@matheusbsbs / matheus-bagliano / commits / 8f7e3de097
Fork 0

wt-status: print to s->fp, not stdout

We pass around a `FILE *` in the `struct wt_status` and almost always print to it. But in a few places, we write to `stdout` instead, either explicitly through `fprintf(stdout, ...)` or implicitly with `printf(...)` (and a few `putchar(...)`). Always be explicit about writing to `s->fp`. To the best of my understanding, this never mattered in practice because these spots are involved in various forms of `git status` which always end up at standard output anyway. When we do write to another file, it's because we're creating a commit message template, and these code paths aren't involved. But let's be consistent to help future readers and avoid future bugs. Signed-off-by: Martin Ågren <martin.agren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Martin Ågren committed Sep 27, 2020 at 15:15 UTC 8f7e3de0970c419688f23f505b5cb7a9690d9b09
1 file changed +32 -25
wt-status.c
+32 -25
@@ -1801,29 +1801,36 @@ static void wt_longstatus_print(struct wt_status *s)
1801 ; /* nothing */
1802 else if (s->workdir_dirty) {
1803 if (s->hints)
1804 - printf(_("no changes added to commit "
1805 - "(use \"git add\" and/or \"git commit -a\")\n"));
1804 + fprintf(s->fp, _("no changes added to commit "
1805 + "(use \"git add\" and/or "
1806 + "\"git commit -a\")\n"));
1807 else
1807 - printf(_("no changes added to commit\n"));
1808 + fprintf(s->fp, _("no changes added to "
1809 + "commit\n"));
1810 } else if (s->untracked.nr) {
1811 if (s->hints)
1810 - printf(_("nothing added to commit but untracked files "
1811 - "present (use \"git add\" to track)\n"));
1812 + fprintf(s->fp, _("nothing added to commit but "
1813 + "untracked files present (use "
1814 + "\"git add\" to track)\n"));
1815 else
1813 - printf(_("nothing added to commit but untracked files present\n"));
1816 + fprintf(s->fp, _("nothing added to commit but "
1817 + "untracked files present\n"));
1818 } else if (s->is_initial) {
1819 if (s->hints)
1816 - printf(_("nothing to commit (create/copy files "
1817 - "and use \"git add\" to track)\n"));
1820 + fprintf(s->fp, _("nothing to commit (create/"
1821 + "copy files and use \"git "
1822 + "add\" to track)\n"));
1823 else
1819 - printf(_("nothing to commit\n"));
1824 + fprintf(s->fp, _("nothing to commit\n"));
1825 } else if (!s->show_untracked_files) {
1826 if (s->hints)
1822 - printf(_("nothing to commit (use -u to show untracked files)\n"));
1827 + fprintf(s->fp, _("nothing to commit (use -u to "
1828 + "show untracked files)\n"));
1829 else
1824 - printf(_("nothing to commit\n"));
1830 + fprintf(s->fp, _("nothing to commit\n"));
1831 } else
1826 - printf(_("nothing to commit, working tree clean\n"));
1832 + fprintf(s->fp, _("nothing to commit, working tree "
1833 + "clean\n"));
1834 }
1835 if(s->show_stash)
1836 wt_longstatus_print_stash_summary(s);
@@ -1846,12 +1853,12 @@ static void wt_shortstatus_unmerged(struct string_list_item *it,
1853 }
1854 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1855 if (s->null_termination) {
1849 - fprintf(stdout, " %s%c", it->string, 0);
1856 + fprintf(s->fp, " %s%c", it->string, 0);
1857 } else {
1858 struct strbuf onebuf = STRBUF_INIT;
1859 const char *one;
1860 one = quote_path(it->string, s->prefix, &onebuf);
1854 - printf(" %s\n", one);
1861 + fprintf(s->fp, " %s\n", one);
1862 strbuf_release(&onebuf);
1863 }
1864 }
@@ -1864,16 +1871,16 @@ static void wt_shortstatus_status(struct string_list_item *it,
1871 if (d->index_status)
1872 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1873 else
1867 - putchar(' ');
1874 + fputc(' ', s->fp);
1875 if (d->worktree_status)
1876 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1877 else
1871 - putchar(' ');
1872 - putchar(' ');
1878 + fputc(' ', s->fp);
1879 + fputc(' ', s->fp);
1880 if (s->null_termination) {
1874 - fprintf(stdout, "%s%c", it->string, 0);
1881 + fprintf(s->fp, "%s%c", it->string, 0);
1882 if (d->rename_source)
1876 - fprintf(stdout, "%s%c", d->rename_source, 0);
1883 + fprintf(s->fp, "%s%c", d->rename_source, 0);
1884 } else {
1885 struct strbuf onebuf = STRBUF_INIT;
1886 const char *one;
@@ -1881,20 +1888,20 @@ static void wt_shortstatus_status(struct string_list_item *it,
1888 if (d->rename_source) {
1889 one = quote_path(d->rename_source, s->prefix, &onebuf);
1890 if (*one != '"' && strchr(one, ' ') != NULL) {
1884 - putchar('"');
1891 + fputc('"', s->fp);
1892 strbuf_addch(&onebuf, '"');
1893 one = onebuf.buf;
1894 }
1888 - printf("%s -> ", one);
1895 + fprintf(s->fp, "%s -> ", one);
1896 strbuf_release(&onebuf);
1897 }
1898 one = quote_path(it->string, s->prefix, &onebuf);
1899 if (*one != '"' && strchr(one, ' ') != NULL) {
1893 - putchar('"');
1900 + fputc('"', s->fp);
1901 strbuf_addch(&onebuf, '"');
1902 one = onebuf.buf;
1903 }
1897 - printf("%s\n", one);
1904 + fprintf(s->fp, "%s\n", one);
1905 strbuf_release(&onebuf);
1906 }
1907 }
@@ -1903,13 +1910,13 @@ static void wt_shortstatus_other(struct string_list_item *it,
1910 struct wt_status *s, const char *sign)
1911 {
1912 if (s->null_termination) {
1906 - fprintf(stdout, "%s %s%c", sign, it->string, 0);
1913 + fprintf(s->fp, "%s %s%c", sign, it->string, 0);
1914 } else {
1915 struct strbuf onebuf = STRBUF_INIT;
1916 const char *one;
1917 one = quote_path(it->string, s->prefix, &onebuf);
1918 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
1912 - printf(" %s\n", one);
1919 + fprintf(s->fp, " %s\n", one);
1920 strbuf_release(&onebuf);
1921 }
1922 }