aboutsummaryrefslogtreecommitdiff
path: root/bdep/project-author.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2018-08-22 19:34:51 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2018-08-22 19:34:51 +0200
commit7e7dee76c870917866fd23f385211ee8101705b4 (patch)
treeb78508ec5316b6f50cbf4417337b96e874bdf439 /bdep/project-author.cxx
parentb2c194aa4913afee30a78fc191912b9622a4d9ae (diff)
Add support for specifying publisher's name in addition to email
Diffstat (limited to 'bdep/project-author.cxx')
-rw-r--r--bdep/project-author.cxx87
1 files changed, 87 insertions, 0 deletions
diff --git a/bdep/project-author.cxx b/bdep/project-author.cxx
new file mode 100644
index 0000000..7aa1eb9
--- /dev/null
+++ b/bdep/project-author.cxx
@@ -0,0 +1,87 @@
+// file : bdep/project-author.cxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#include <bdep/project-author.hxx>
+
+#include <bdep/git.hxx>
+#include <bdep/diagnostics.hxx>
+
+using namespace butl;
+
+namespace bdep
+{
+ project_author
+ find_project_author (const dir_path& prj)
+ {
+ project_author r;
+
+ // The search order is as follows:
+ //
+ // BDEP_AUTHOR_(NAME|EMAIL)
+ // <VCS>
+ // (USER|EMAIL)
+ //
+ r.name = getenv ("BDEP_AUTHOR_NAME");
+ r.email = getenv ("BDEP_AUTHOR_EMAIL");
+
+ if (!r.name || !r.email)
+ {
+ // See if this is a VCS repository we recognize.
+ //
+ if (git_repository (prj))
+ {
+ // In git the author name/email can be specified with the
+ // GIT_AUTHOR_NAME/EMAIL environment variables after which things
+ // fall back to the committer (GIT_COMMITTER_NAME/EMAIL and then
+ // the user.name/email git-config value). The resolved value can
+ // be queried with the GIT_AUTHOR_IDENT logical variable.
+ //
+ if (optional<string> l = git_line (semantic_version {2, 1, 0},
+ prj,
+ true /* ignore_error */,
+ "var", "GIT_AUTHOR_IDENT"))
+ {
+ // The output should be a single line in this form:
+ //
+ // NAME <EMAIL> TIME ZONE
+ //
+ // For example:
+ //
+ // John Doe <john@example.org> 1530517726 +0200
+ //
+ // The <> delimiters are there even if the email is empty so we use
+ // them as anchors.
+ //
+ size_t p1, p2;
+
+ if ((p2 = l->rfind ('>' )) == string::npos ||
+ (p1 = l->rfind ('<', p2)) == string::npos)
+ fail << "invalid GIT_AUTHOR_IDENT value '" << *l << "'" << endf;
+
+ if (!r.name)
+ {
+ string n (*l, 0, p1);
+
+ if (!trim (n).empty ())
+ r.name = move (n);
+ }
+
+ if (!r.email)
+ {
+ ++p1;
+ string e (*l, p1, p2 - p1);
+
+ if (!trim (e).empty ())
+ r.email = move (e);
+ }
+ }
+ }
+ }
+
+ if (!r.name ) r.name = getenv ("USER");
+ if (!r.email) r.email = getenv ("EMAIL");
+
+ return r;
+ }
+}