diff options
Diffstat (limited to 'org.eclipse.jgit.pgm.test')
48 files changed, 4478 insertions, 1421 deletions
diff --git a/org.eclipse.jgit.pgm.test/.classpath b/org.eclipse.jgit.pgm.test/.classpath index 30d83d8e9c..02bc21c1a7 100644 --- a/org.eclipse.jgit.pgm.test/.classpath +++ b/org.eclipse.jgit.pgm.test/.classpath @@ -1,8 +1,20 @@ <?xml version="1.0" encoding="UTF-8"?> <classpath> - <classpathentry kind="src" path="tst"/> - <classpathentry kind="src" path="src"/> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> + <classpathentry kind="src" path="tst"> + <attributes> + <attribute name="test" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="src" path="src"> + <attributes> + <attribute name="test" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"> + <attributes> + <attribute name="module" value="true"/> + </attributes> + </classpathentry> <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> <classpathentry kind="output" path="bin"/> </classpath> diff --git a/org.eclipse.jgit.pgm.test/.settings/org.eclipse.jdt.core.prefs b/org.eclipse.jgit.pgm.test/.settings/org.eclipse.jdt.core.prefs index dcc0d3a0fe..362915de03 100644 --- a/org.eclipse.jgit.pgm.test/.settings/org.eclipse.jdt.core.prefs +++ b/org.eclipse.jgit.pgm.test/.settings/org.eclipse.jdt.core.prefs @@ -7,9 +7,9 @@ org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nul org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.compliance=17 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate @@ -24,8 +24,9 @@ org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled org.eclipse.jdt.core.compiler.problem.discouragedReference=warning org.eclipse.jdt.core.compiler.problem.emptyStatement=warning +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore +org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning org.eclipse.jdt.core.compiler.problem.fallthroughCase=warning org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled org.eclipse.jdt.core.compiler.problem.fieldHiding=warning @@ -51,12 +52,12 @@ org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=error org.eclipse.jdt.core.compiler.problem.missingJavadocComments=ignore org.eclipse.jdt.core.compiler.problem.missingJavadocCommentsOverriding=disabled org.eclipse.jdt.core.compiler.problem.missingJavadocCommentsVisibility=protected -org.eclipse.jdt.core.compiler.problem.missingJavadocTagDescription=return_tag -org.eclipse.jdt.core.compiler.problem.missingJavadocTags=error +org.eclipse.jdt.core.compiler.problem.missingJavadocTagDescription=no_tag +org.eclipse.jdt.core.compiler.problem.missingJavadocTags=ignore org.eclipse.jdt.core.compiler.problem.missingJavadocTagsMethodTypeParameters=disabled org.eclipse.jdt.core.compiler.problem.missingJavadocTagsOverriding=disabled org.eclipse.jdt.core.compiler.problem.missingJavadocTagsVisibility=private -org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore +org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=warning org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore @@ -76,10 +77,11 @@ org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore org.eclipse.jdt.core.compiler.problem.rawTypeReference=ignore org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning org.eclipse.jdt.core.compiler.problem.redundantNullCheck=warning -org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=ignore +org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=warning org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=error org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=error org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled @@ -91,14 +93,15 @@ org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=warning -org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning -org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore +org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=ignore +org.eclipse.jdt.core.compiler.problem.unnecessaryElse=warning org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=error org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=error org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled +org.eclipse.jdt.core.compiler.problem.unusedExceptionParameter=ignore org.eclipse.jdt.core.compiler.problem.unusedImport=error org.eclipse.jdt.core.compiler.problem.unusedLabel=error org.eclipse.jdt.core.compiler.problem.unusedLocal=error @@ -111,34 +114,66 @@ org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=error org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=ignore org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=error -org.eclipse.jdt.core.compiler.source=1.7 +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 +org.eclipse.jdt.core.formatter.align_assignment_statements_on_columns=false +org.eclipse.jdt.core.formatter.align_fields_grouping_blank_lines=2147483647 org.eclipse.jdt.core.formatter.align_type_members_on_columns=false +org.eclipse.jdt.core.formatter.align_variable_declarations_on_columns=false +org.eclipse.jdt.core.formatter.align_with_spaces=false +org.eclipse.jdt.core.formatter.alignment_for_additive_operator=16 +org.eclipse.jdt.core.formatter.alignment_for_annotations_on_enum_constant=0 +org.eclipse.jdt.core.formatter.alignment_for_annotations_on_field=49 +org.eclipse.jdt.core.formatter.alignment_for_annotations_on_local_variable=49 +org.eclipse.jdt.core.formatter.alignment_for_annotations_on_method=49 +org.eclipse.jdt.core.formatter.alignment_for_annotations_on_package=49 +org.eclipse.jdt.core.formatter.alignment_for_annotations_on_parameter=0 +org.eclipse.jdt.core.formatter.alignment_for_annotations_on_type=49 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_qualified_allocation_expression=16 +org.eclipse.jdt.core.formatter.alignment_for_assertion_message=0 org.eclipse.jdt.core.formatter.alignment_for_assignment=0 org.eclipse.jdt.core.formatter.alignment_for_binary_expression=16 +org.eclipse.jdt.core.formatter.alignment_for_bitwise_operator=16 org.eclipse.jdt.core.formatter.alignment_for_compact_if=16 +org.eclipse.jdt.core.formatter.alignment_for_compact_loops=16 org.eclipse.jdt.core.formatter.alignment_for_conditional_expression=80 +org.eclipse.jdt.core.formatter.alignment_for_conditional_expression_chain=0 org.eclipse.jdt.core.formatter.alignment_for_enum_constants=0 org.eclipse.jdt.core.formatter.alignment_for_expressions_in_array_initializer=16 +org.eclipse.jdt.core.formatter.alignment_for_expressions_in_for_loop_header=0 +org.eclipse.jdt.core.formatter.alignment_for_logical_operator=16 org.eclipse.jdt.core.formatter.alignment_for_method_declaration=0 +org.eclipse.jdt.core.formatter.alignment_for_module_statements=16 org.eclipse.jdt.core.formatter.alignment_for_multiple_fields=16 +org.eclipse.jdt.core.formatter.alignment_for_multiplicative_operator=16 +org.eclipse.jdt.core.formatter.alignment_for_parameterized_type_references=0 org.eclipse.jdt.core.formatter.alignment_for_parameters_in_constructor_declaration=16 org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration=16 +org.eclipse.jdt.core.formatter.alignment_for_record_components=16 +org.eclipse.jdt.core.formatter.alignment_for_relational_operator=0 org.eclipse.jdt.core.formatter.alignment_for_resources_in_try=80 org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation=16 +org.eclipse.jdt.core.formatter.alignment_for_shift_operator=0 +org.eclipse.jdt.core.formatter.alignment_for_string_concatenation=16 org.eclipse.jdt.core.formatter.alignment_for_superclass_in_type_declaration=16 org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_enum_declaration=16 +org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_record_declaration=16 org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_type_declaration=16 org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_constructor_declaration=16 org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_method_declaration=16 +org.eclipse.jdt.core.formatter.alignment_for_type_annotations=0 +org.eclipse.jdt.core.formatter.alignment_for_type_arguments=0 +org.eclipse.jdt.core.formatter.alignment_for_type_parameters=0 org.eclipse.jdt.core.formatter.alignment_for_union_type_in_multicatch=16 org.eclipse.jdt.core.formatter.blank_lines_after_imports=1 +org.eclipse.jdt.core.formatter.blank_lines_after_last_class_body_declaration=0 org.eclipse.jdt.core.formatter.blank_lines_after_package=1 +org.eclipse.jdt.core.formatter.blank_lines_before_abstract_method=1 org.eclipse.jdt.core.formatter.blank_lines_before_field=1 org.eclipse.jdt.core.formatter.blank_lines_before_first_class_body_declaration=0 org.eclipse.jdt.core.formatter.blank_lines_before_imports=1 @@ -147,6 +182,7 @@ org.eclipse.jdt.core.formatter.blank_lines_before_method=1 org.eclipse.jdt.core.formatter.blank_lines_before_new_chunk=1 org.eclipse.jdt.core.formatter.blank_lines_before_package=0 org.eclipse.jdt.core.formatter.blank_lines_between_import_groups=1 +org.eclipse.jdt.core.formatter.blank_lines_between_statement_group_in_switch=0 org.eclipse.jdt.core.formatter.blank_lines_between_type_declarations=1 org.eclipse.jdt.core.formatter.brace_position_for_annotation_type_declaration=end_of_line org.eclipse.jdt.core.formatter.brace_position_for_anonymous_type_declaration=end_of_line @@ -156,12 +192,18 @@ org.eclipse.jdt.core.formatter.brace_position_for_block_in_case=end_of_line org.eclipse.jdt.core.formatter.brace_position_for_constructor_declaration=end_of_line org.eclipse.jdt.core.formatter.brace_position_for_enum_constant=end_of_line org.eclipse.jdt.core.formatter.brace_position_for_enum_declaration=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_lambda_body=end_of_line org.eclipse.jdt.core.formatter.brace_position_for_method_declaration=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_record_constructor=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_record_declaration=end_of_line org.eclipse.jdt.core.formatter.brace_position_for_switch=end_of_line org.eclipse.jdt.core.formatter.brace_position_for_type_declaration=end_of_line +org.eclipse.jdt.core.formatter.comment.align_tags_descriptions_grouped=false +org.eclipse.jdt.core.formatter.comment.align_tags_names_descriptions=false org.eclipse.jdt.core.formatter.comment.clear_blank_lines=false org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_block_comment=false org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_javadoc_comment=false +org.eclipse.jdt.core.formatter.comment.count_line_length_from_starting_position=false org.eclipse.jdt.core.formatter.comment.format_block_comments=true org.eclipse.jdt.core.formatter.comment.format_comments=true org.eclipse.jdt.core.formatter.comment.format_header=false @@ -171,7 +213,9 @@ org.eclipse.jdt.core.formatter.comment.format_line_comments=true org.eclipse.jdt.core.formatter.comment.format_source_code=true org.eclipse.jdt.core.formatter.comment.indent_parameter_description=true org.eclipse.jdt.core.formatter.comment.indent_root_tags=true +org.eclipse.jdt.core.formatter.comment.indent_tag_description=false org.eclipse.jdt.core.formatter.comment.insert_new_line_before_root_tags=insert +org.eclipse.jdt.core.formatter.comment.insert_new_line_between_different_tags=do not insert org.eclipse.jdt.core.formatter.comment.insert_new_line_for_parameter=insert org.eclipse.jdt.core.formatter.comment.line_length=80 org.eclipse.jdt.core.formatter.comment.new_lines_at_block_boundaries=true @@ -183,10 +227,11 @@ org.eclipse.jdt.core.formatter.continuation_indentation_for_array_initializer=2 org.eclipse.jdt.core.formatter.disabling_tag=@formatter\:off org.eclipse.jdt.core.formatter.enabling_tag=@formatter\:on org.eclipse.jdt.core.formatter.format_guardian_clause_on_one_line=false -org.eclipse.jdt.core.formatter.format_line_comment_starting_on_first_column=true +org.eclipse.jdt.core.formatter.format_line_comment_starting_on_first_column=false org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_annotation_declaration_header=true org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_constant_header=true org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_declaration_header=true +org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_record_header=true org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_type_header=true org.eclipse.jdt.core.formatter.indent_breaks_compare_to_cases=true org.eclipse.jdt.core.formatter.indent_empty_lines=false @@ -196,15 +241,17 @@ org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_cases=true org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch=false org.eclipse.jdt.core.formatter.indentation.size=4 org.eclipse.jdt.core.formatter.insert_new_line_after_annotation=insert +org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_enum_constant=insert org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_field=insert org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_local_variable=insert org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_member=insert org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_method=insert org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_package=insert -org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_parameter=insert +org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_parameter=do not insert org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_type=insert org.eclipse.jdt.core.formatter.insert_new_line_after_label=do not insert org.eclipse.jdt.core.formatter.insert_new_line_after_opening_brace_in_array_initializer=do not insert +org.eclipse.jdt.core.formatter.insert_new_line_after_type_annotation=do not insert org.eclipse.jdt.core.formatter.insert_new_line_at_end_of_file_if_missing=do not insert org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement=do not insert org.eclipse.jdt.core.formatter.insert_new_line_before_closing_brace_in_array_initializer=do not insert @@ -218,11 +265,15 @@ org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_constant=insert org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_declaration=insert org.eclipse.jdt.core.formatter.insert_new_line_in_empty_method_body=insert org.eclipse.jdt.core.formatter.insert_new_line_in_empty_type_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_after_additive_operator=insert org.eclipse.jdt.core.formatter.insert_space_after_and_in_type_parameter=insert +org.eclipse.jdt.core.formatter.insert_space_after_arrow_in_switch_case=insert +org.eclipse.jdt.core.formatter.insert_space_after_arrow_in_switch_default=insert org.eclipse.jdt.core.formatter.insert_space_after_assignment_operator=insert org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation=do not insert org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation_type_declaration=do not insert org.eclipse.jdt.core.formatter.insert_space_after_binary_operator=insert +org.eclipse.jdt.core.formatter.insert_space_after_bitwise_operator=insert org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_arguments=insert org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_parameters=insert org.eclipse.jdt.core.formatter.insert_space_after_closing_brace_in_block=insert @@ -248,10 +299,16 @@ org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_invocation_arg org.eclipse.jdt.core.formatter.insert_space_after_comma_in_multiple_field_declarations=insert org.eclipse.jdt.core.formatter.insert_space_after_comma_in_multiple_local_declarations=insert org.eclipse.jdt.core.formatter.insert_space_after_comma_in_parameterized_type_reference=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_record_components=insert org.eclipse.jdt.core.formatter.insert_space_after_comma_in_superinterfaces=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_switch_case_expressions=insert org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_arguments=insert org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_parameters=insert org.eclipse.jdt.core.formatter.insert_space_after_ellipsis=insert +org.eclipse.jdt.core.formatter.insert_space_after_lambda_arrow=insert +org.eclipse.jdt.core.formatter.insert_space_after_logical_operator=insert +org.eclipse.jdt.core.formatter.insert_space_after_multiplicative_operator=insert +org.eclipse.jdt.core.formatter.insert_space_after_not_operator=do not insert org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_parameterized_type_reference=do not insert org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_arguments=do not insert org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_parameters=do not insert @@ -268,6 +325,7 @@ org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_if=do not ins org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_declaration=do not insert org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_invocation=do not insert org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_parenthesized_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_record_declaration=do not insert org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_switch=do not insert org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_synchronized=do not insert org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_try=do not insert @@ -276,13 +334,20 @@ org.eclipse.jdt.core.formatter.insert_space_after_postfix_operator=do not insert org.eclipse.jdt.core.formatter.insert_space_after_prefix_operator=do not insert org.eclipse.jdt.core.formatter.insert_space_after_question_in_conditional=insert org.eclipse.jdt.core.formatter.insert_space_after_question_in_wildcard=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_relational_operator=insert org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_for=insert org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_try_resources=insert +org.eclipse.jdt.core.formatter.insert_space_after_shift_operator=insert +org.eclipse.jdt.core.formatter.insert_space_after_string_concatenation=insert org.eclipse.jdt.core.formatter.insert_space_after_unary_operator=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_additive_operator=insert org.eclipse.jdt.core.formatter.insert_space_before_and_in_type_parameter=insert +org.eclipse.jdt.core.formatter.insert_space_before_arrow_in_switch_case=insert +org.eclipse.jdt.core.formatter.insert_space_before_arrow_in_switch_default=insert org.eclipse.jdt.core.formatter.insert_space_before_assignment_operator=insert org.eclipse.jdt.core.formatter.insert_space_before_at_in_annotation_type_declaration=insert org.eclipse.jdt.core.formatter.insert_space_before_binary_operator=insert +org.eclipse.jdt.core.formatter.insert_space_before_bitwise_operator=insert org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_parameterized_type_reference=do not insert org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_arguments=do not insert org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_parameters=do not insert @@ -299,6 +364,7 @@ org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_if=do not in org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_method_declaration=do not insert org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_method_invocation=do not insert org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_parenthesized_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_record_declaration=do not insert org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_switch=do not insert org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_synchronized=do not insert org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_try=do not insert @@ -325,10 +391,15 @@ org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_invocation_ar org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_field_declarations=do not insert org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_local_declarations=do not insert org.eclipse.jdt.core.formatter.insert_space_before_comma_in_parameterized_type_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_record_components=do not insert org.eclipse.jdt.core.formatter.insert_space_before_comma_in_superinterfaces=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_switch_case_expressions=do not insert org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_arguments=do not insert org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_parameters=do not insert org.eclipse.jdt.core.formatter.insert_space_before_ellipsis=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_lambda_arrow=insert +org.eclipse.jdt.core.formatter.insert_space_before_logical_operator=insert +org.eclipse.jdt.core.formatter.insert_space_before_multiplicative_operator=insert org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_parameterized_type_reference=do not insert org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_arguments=do not insert org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_parameters=do not insert @@ -340,6 +411,8 @@ org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_constructor_ org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_constant=insert org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_declaration=insert org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_method_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_record_constructor=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_record_declaration=insert org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_switch=insert org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_type_declaration=insert org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_allocation_expression=do not insert @@ -355,6 +428,7 @@ org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_if=insert org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_declaration=do not insert org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_invocation=do not insert org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_parenthesized_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_record_declaration=do not insert org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_switch=insert org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_synchronized=insert org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_try=insert @@ -365,9 +439,12 @@ org.eclipse.jdt.core.formatter.insert_space_before_postfix_operator=do not inser org.eclipse.jdt.core.formatter.insert_space_before_prefix_operator=do not insert org.eclipse.jdt.core.formatter.insert_space_before_question_in_conditional=insert org.eclipse.jdt.core.formatter.insert_space_before_question_in_wildcard=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_relational_operator=insert org.eclipse.jdt.core.formatter.insert_space_before_semicolon=do not insert org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_for=do not insert org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_try_resources=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_shift_operator=insert +org.eclipse.jdt.core.formatter.insert_space_before_string_concatenation=insert org.eclipse.jdt.core.formatter.insert_space_before_unary_operator=do not insert org.eclipse.jdt.core.formatter.insert_space_between_brackets_in_array_type_reference=do not insert org.eclipse.jdt.core.formatter.insert_space_between_empty_braces_in_array_initializer=do not insert @@ -379,20 +456,63 @@ org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_decla org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invocation=do not insert org.eclipse.jdt.core.formatter.join_lines_in_comments=true org.eclipse.jdt.core.formatter.join_wrapped_lines=true +org.eclipse.jdt.core.formatter.keep_annotation_declaration_on_one_line=one_line_never +org.eclipse.jdt.core.formatter.keep_anonymous_type_declaration_on_one_line=one_line_never +org.eclipse.jdt.core.formatter.keep_code_block_on_one_line=one_line_never org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line=false org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line=false +org.eclipse.jdt.core.formatter.keep_enum_constant_declaration_on_one_line=one_line_never +org.eclipse.jdt.core.formatter.keep_enum_declaration_on_one_line=one_line_never +org.eclipse.jdt.core.formatter.keep_if_then_body_block_on_one_line=one_line_never org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line=false +org.eclipse.jdt.core.formatter.keep_lambda_body_block_on_one_line=one_line_never +org.eclipse.jdt.core.formatter.keep_loop_body_block_on_one_line=one_line_never +org.eclipse.jdt.core.formatter.keep_method_body_on_one_line=one_line_never +org.eclipse.jdt.core.formatter.keep_record_constructor_on_one_line=one_line_never +org.eclipse.jdt.core.formatter.keep_record_declaration_on_one_line=one_line_never +org.eclipse.jdt.core.formatter.keep_simple_do_while_body_on_same_line=false +org.eclipse.jdt.core.formatter.keep_simple_for_body_on_same_line=false +org.eclipse.jdt.core.formatter.keep_simple_getter_setter_on_one_line=false +org.eclipse.jdt.core.formatter.keep_simple_while_body_on_same_line=false org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line=false +org.eclipse.jdt.core.formatter.keep_type_declaration_on_one_line=one_line_never org.eclipse.jdt.core.formatter.lineSplit=80 org.eclipse.jdt.core.formatter.never_indent_block_comments_on_first_column=false org.eclipse.jdt.core.formatter.never_indent_line_comments_on_first_column=false +org.eclipse.jdt.core.formatter.number_of_blank_lines_after_code_block=0 +org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_code_block=0 org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body=0 +org.eclipse.jdt.core.formatter.number_of_blank_lines_at_end_of_code_block=0 +org.eclipse.jdt.core.formatter.number_of_blank_lines_at_end_of_method_body=0 +org.eclipse.jdt.core.formatter.number_of_blank_lines_before_code_block=0 org.eclipse.jdt.core.formatter.number_of_empty_lines_to_preserve=1 +org.eclipse.jdt.core.formatter.parentheses_positions_in_annotation=common_lines +org.eclipse.jdt.core.formatter.parentheses_positions_in_catch_clause=common_lines +org.eclipse.jdt.core.formatter.parentheses_positions_in_enum_constant_declaration=common_lines +org.eclipse.jdt.core.formatter.parentheses_positions_in_for_statment=common_lines +org.eclipse.jdt.core.formatter.parentheses_positions_in_if_while_statement=common_lines +org.eclipse.jdt.core.formatter.parentheses_positions_in_lambda_declaration=common_lines +org.eclipse.jdt.core.formatter.parentheses_positions_in_method_delcaration=common_lines +org.eclipse.jdt.core.formatter.parentheses_positions_in_method_invocation=common_lines +org.eclipse.jdt.core.formatter.parentheses_positions_in_record_declaration=common_lines +org.eclipse.jdt.core.formatter.parentheses_positions_in_switch_statement=common_lines +org.eclipse.jdt.core.formatter.parentheses_positions_in_try_clause=common_lines org.eclipse.jdt.core.formatter.put_empty_statement_on_new_line=true org.eclipse.jdt.core.formatter.tabulation.char=tab org.eclipse.jdt.core.formatter.tabulation.size=4 +org.eclipse.jdt.core.formatter.text_block_indentation=0 org.eclipse.jdt.core.formatter.use_on_off_tags=true org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations=false +org.eclipse.jdt.core.formatter.wrap_before_additive_operator=true +org.eclipse.jdt.core.formatter.wrap_before_assertion_message_operator=true +org.eclipse.jdt.core.formatter.wrap_before_assignment_operator=false org.eclipse.jdt.core.formatter.wrap_before_binary_operator=true +org.eclipse.jdt.core.formatter.wrap_before_bitwise_operator=true +org.eclipse.jdt.core.formatter.wrap_before_conditional_operator=true +org.eclipse.jdt.core.formatter.wrap_before_logical_operator=true +org.eclipse.jdt.core.formatter.wrap_before_multiplicative_operator=true org.eclipse.jdt.core.formatter.wrap_before_or_operator_multicatch=true +org.eclipse.jdt.core.formatter.wrap_before_relational_operator=true +org.eclipse.jdt.core.formatter.wrap_before_shift_operator=true +org.eclipse.jdt.core.formatter.wrap_before_string_concatenation=true org.eclipse.jdt.core.formatter.wrap_outer_expressions_when_nested=true diff --git a/org.eclipse.jgit.pgm.test/.settings/org.eclipse.jdt.ui.prefs b/org.eclipse.jgit.pgm.test/.settings/org.eclipse.jdt.ui.prefs index c336cce6ed..5cfb8b6ac6 100644 --- a/org.eclipse.jgit.pgm.test/.settings/org.eclipse.jdt.ui.prefs +++ b/org.eclipse.jgit.pgm.test/.settings/org.eclipse.jdt.ui.prefs @@ -1,7 +1,7 @@ eclipse.preferences.version=1 editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true formatter_profile=_JGit Format -formatter_settings_version=12 +formatter_settings_version=21 org.eclipse.jdt.ui.ignorelowercasenames=true org.eclipse.jdt.ui.importorder=java;javax;org;com; org.eclipse.jdt.ui.ondemandthreshold=99 @@ -9,21 +9,23 @@ org.eclipse.jdt.ui.staticondemandthreshold=99 org.eclipse.jdt.ui.text.custom_code_templates=<?xml version\="1.0" encoding\="UTF-8"?><templates/> sp_cleanup.add_default_serial_version_id=true sp_cleanup.add_generated_serial_version_id=false -sp_cleanup.add_missing_annotations=false +sp_cleanup.add_missing_annotations=true sp_cleanup.add_missing_deprecated_annotations=true sp_cleanup.add_missing_methods=false sp_cleanup.add_missing_nls_tags=false sp_cleanup.add_missing_override_annotations=true -sp_cleanup.add_missing_override_annotations_interface_methods=false +sp_cleanup.add_missing_override_annotations_interface_methods=true sp_cleanup.add_serial_version_id=false sp_cleanup.always_use_blocks=true sp_cleanup.always_use_parentheses_in_expressions=false sp_cleanup.always_use_this_for_non_static_field_access=false sp_cleanup.always_use_this_for_non_static_method_access=false +sp_cleanup.convert_functional_interfaces=false sp_cleanup.convert_to_enhanced_for_loop=false sp_cleanup.correct_indentation=false sp_cleanup.format_source_code=true sp_cleanup.format_source_code_changes_only=true +sp_cleanup.insert_inferred_type_arguments=false sp_cleanup.make_local_variable_final=false sp_cleanup.make_parameters_final=false sp_cleanup.make_private_fields_final=true @@ -39,11 +41,12 @@ sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class= sp_cleanup.qualify_static_member_accesses_with_declaring_class=false sp_cleanup.qualify_static_method_accesses_with_declaring_class=false sp_cleanup.remove_private_constructors=true +sp_cleanup.remove_redundant_type_arguments=true sp_cleanup.remove_trailing_whitespaces=true sp_cleanup.remove_trailing_whitespaces_all=true sp_cleanup.remove_trailing_whitespaces_ignore_empty=false -sp_cleanup.remove_unnecessary_casts=false -sp_cleanup.remove_unnecessary_nls_tags=false +sp_cleanup.remove_unnecessary_casts=true +sp_cleanup.remove_unnecessary_nls_tags=true sp_cleanup.remove_unused_imports=false sp_cleanup.remove_unused_local_variables=false sp_cleanup.remove_unused_private_fields=true @@ -52,8 +55,10 @@ sp_cleanup.remove_unused_private_methods=true sp_cleanup.remove_unused_private_types=true sp_cleanup.sort_members=false sp_cleanup.sort_members_all=false +sp_cleanup.use_anonymous_class_creation=false sp_cleanup.use_blocks=false sp_cleanup.use_blocks_only_for_return_and_throw=false +sp_cleanup.use_lambda=false sp_cleanup.use_parentheses_in_expressions=false sp_cleanup.use_this_for_non_static_field_access=false sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true diff --git a/org.eclipse.jgit.pgm.test/.settings/org.eclipse.mylyn.team.ui.prefs b/org.eclipse.jgit.pgm.test/.settings/org.eclipse.mylyn.team.ui.prefs index 0cba949fb7..2fca432276 100644 --- a/org.eclipse.jgit.pgm.test/.settings/org.eclipse.mylyn.team.ui.prefs +++ b/org.eclipse.jgit.pgm.test/.settings/org.eclipse.mylyn.team.ui.prefs @@ -1,3 +1,3 @@ #Tue Jul 19 20:11:28 CEST 2011 -commit.comment.template=${task.description} \n\nBug\: ${task.key} +commit.comment.template=${task.description}\n\nBug\: ${task.key} eclipse.preferences.version=1 diff --git a/org.eclipse.jgit.pgm.test/.settings/org.eclipse.pde.api.tools.prefs b/org.eclipse.jgit.pgm.test/.settings/org.eclipse.pde.api.tools.prefs index cd148d9049..c0030ded71 100644 --- a/org.eclipse.jgit.pgm.test/.settings/org.eclipse.pde.api.tools.prefs +++ b/org.eclipse.jgit.pgm.test/.settings/org.eclipse.pde.api.tools.prefs @@ -1,4 +1,4 @@ -#Tue Oct 18 00:52:01 CEST 2011 +ANNOTATION_ELEMENT_TYPE_ADDED_FIELD=Error ANNOTATION_ELEMENT_TYPE_ADDED_METHOD_WITHOUT_DEFAULT_VALUE=Error ANNOTATION_ELEMENT_TYPE_CHANGED_TYPE_CONVERSION=Error ANNOTATION_ELEMENT_TYPE_REMOVED_FIELD=Error @@ -8,6 +8,10 @@ API_COMPONENT_ELEMENT_TYPE_REMOVED_API_TYPE=Error API_COMPONENT_ELEMENT_TYPE_REMOVED_REEXPORTED_API_TYPE=Error API_COMPONENT_ELEMENT_TYPE_REMOVED_REEXPORTED_TYPE=Error API_COMPONENT_ELEMENT_TYPE_REMOVED_TYPE=Error +API_USE_SCAN_FIELD_SEVERITY=Error +API_USE_SCAN_METHOD_SEVERITY=Error +API_USE_SCAN_TYPE_SEVERITY=Error +CLASS_ELEMENT_TYPE_ADDED_FIELD=Error CLASS_ELEMENT_TYPE_ADDED_METHOD=Error CLASS_ELEMENT_TYPE_ADDED_RESTRICTIONS=Error CLASS_ELEMENT_TYPE_ADDED_TYPE_PARAMETER=Error @@ -47,6 +51,7 @@ ILLEGAL_IMPLEMENT=Warning ILLEGAL_INSTANTIATE=Warning ILLEGAL_OVERRIDE=Warning ILLEGAL_REFERENCE=Warning +INTERFACE_ELEMENT_TYPE_ADDED_DEFAULT_METHOD=Error INTERFACE_ELEMENT_TYPE_ADDED_FIELD=Error INTERFACE_ELEMENT_TYPE_ADDED_METHOD=Error INTERFACE_ELEMENT_TYPE_ADDED_RESTRICTIONS=Error @@ -58,6 +63,7 @@ INTERFACE_ELEMENT_TYPE_REMOVED_FIELD=Error INTERFACE_ELEMENT_TYPE_REMOVED_METHOD=Error INTERFACE_ELEMENT_TYPE_REMOVED_TYPE_MEMBER=Error INTERFACE_ELEMENT_TYPE_REMOVED_TYPE_PARAMETER=Error +INVALID_ANNOTATION=Ignore INVALID_JAVADOC_TAG=Ignore INVALID_REFERENCE_IN_SYSTEM_LIBRARIES=Error LEAK_EXTEND=Warning @@ -75,6 +81,7 @@ METHOD_ELEMENT_TYPE_CHANGED_STATIC_TO_NON_STATIC=Error METHOD_ELEMENT_TYPE_CHANGED_VARARGS_TO_ARRAY=Error METHOD_ELEMENT_TYPE_REMOVED_ANNOTATION_DEFAULT_VALUE=Error METHOD_ELEMENT_TYPE_REMOVED_TYPE_PARAMETER=Error +MISSING_EE_DESCRIPTIONS=Warning TYPE_PARAMETER_ELEMENT_TYPE_ADDED_CLASS_BOUND=Error TYPE_PARAMETER_ELEMENT_TYPE_ADDED_INTERFACE_BOUND=Error TYPE_PARAMETER_ELEMENT_TYPE_CHANGED_CLASS_BOUND=Error @@ -83,10 +90,13 @@ TYPE_PARAMETER_ELEMENT_TYPE_REMOVED_CLASS_BOUND=Error TYPE_PARAMETER_ELEMENT_TYPE_REMOVED_INTERFACE_BOUND=Error UNUSED_PROBLEM_FILTERS=Warning automatically_removed_unused_problem_filters=false +changed_execution_env=Error eclipse.preferences.version=1 incompatible_api_component_version=Error incompatible_api_component_version_include_major_without_breaking_change=Disabled incompatible_api_component_version_include_minor_without_api_change=Disabled +incompatible_api_component_version_report_major_without_breaking_change=Warning +incompatible_api_component_version_report_minor_without_api_change=Ignore invalid_since_tag_version=Error malformed_since_tag=Error missing_since_tag=Error diff --git a/org.eclipse.jgit.pgm.test/.settings/org.eclipse.pde.prefs b/org.eclipse.jgit.pgm.test/.settings/org.eclipse.pde.prefs new file mode 100644 index 0000000000..2174e4fd5b --- /dev/null +++ b/org.eclipse.jgit.pgm.test/.settings/org.eclipse.pde.prefs @@ -0,0 +1,34 @@ +compilers.f.unresolved-features=1 +compilers.f.unresolved-plugins=1 +compilers.incompatible-environment=1 +compilers.p.build=1 +compilers.p.build.bin.includes=1 +compilers.p.build.encodings=2 +compilers.p.build.java.compiler=2 +compilers.p.build.java.compliance=1 +compilers.p.build.missing.output=2 +compilers.p.build.output.library=1 +compilers.p.build.source.library=1 +compilers.p.build.src.includes=1 +compilers.p.deprecated=1 +compilers.p.discouraged-class=1 +compilers.p.internal=1 +compilers.p.missing-packages=2 +compilers.p.missing-version-export-package=2 +compilers.p.missing-version-import-package=2 +compilers.p.missing-version-require-bundle=2 +compilers.p.no-required-att=0 +compilers.p.no.automatic.module=1 +compilers.p.not-externalized-att=2 +compilers.p.service.component.without.lazyactivation=1 +compilers.p.unknown-attribute=1 +compilers.p.unknown-class=1 +compilers.p.unknown-element=1 +compilers.p.unknown-identifier=1 +compilers.p.unknown-resource=1 +compilers.p.unresolved-ex-points=0 +compilers.p.unresolved-import=0 +compilers.s.create-docs=false +compilers.s.doc-folder=doc +compilers.s.open-tags=1 +eclipse.preferences.version=1 diff --git a/org.eclipse.jgit.pgm.test/BUILD b/org.eclipse.jgit.pgm.test/BUILD new file mode 100644 index 0000000000..b8ffa69594 --- /dev/null +++ b/org.eclipse.jgit.pgm.test/BUILD @@ -0,0 +1,42 @@ +load( + "@com_googlesource_gerrit_bazlets//tools:junit.bzl", + "junit_tests", +) +load("@rules_java//java:defs.bzl", "java_library") + +junit_tests( + name = "pgm", + srcs = glob(["tst/**/*.java"]), + jvm_flags = [ + "-Xmx512m", + "-Dfile.encoding=UTF-8", + ], + tags = ["pgm"], + deps = [ + ":helpers", + "//lib:args4j", + "//lib:commons-io", + "//lib:commons-compress", + "//lib:javaewah", + "//lib:junit", + "//lib:slf4j-api", + "//lib:slf4j-simple", + "//lib:xz", + "//org.eclipse.jgit:jgit", + "//org.eclipse.jgit.junit:junit", + "//org.eclipse.jgit.pgm:pgm", + ], +) + +java_library( + name = "helpers", + testonly = 1, + srcs = glob(["src/**/*.java"]), + deps = [ + "//lib:args4j", + "//lib:junit", + "//org.eclipse.jgit:jgit", + "//org.eclipse.jgit.junit:junit", + "//org.eclipse.jgit.pgm:pgm", + ], +) diff --git a/org.eclipse.jgit.pgm.test/META-INF/MANIFEST.MF b/org.eclipse.jgit.pgm.test/META-INF/MANIFEST.MF index 47863420ef..b2401f39d8 100644 --- a/org.eclipse.jgit.pgm.test/META-INF/MANIFEST.MF +++ b/org.eclipse.jgit.pgm.test/META-INF/MANIFEST.MF @@ -1,29 +1,35 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 -Bundle-Name: %plugin_name +Bundle-Name: %Bundle-Name +Automatic-Module-Name: org.eclipse.jgit.pgm.test Bundle-SymbolicName: org.eclipse.jgit.pgm.test -Bundle-Version: 4.0.0.qualifier -Bundle-Vendor: %provider_name +Bundle-Version: 7.4.0.qualifier +Bundle-Vendor: %Bundle-Vendor Bundle-Localization: plugin Bundle-ActivationPolicy: lazy -Bundle-RequiredExecutionEnvironment: JavaSE-1.7 -Import-Package: org.eclipse.jgit.api;version="[4.0.0,4.1.0)", - org.eclipse.jgit.api.errors;version="[4.0.0,4.1.0)", - org.eclipse.jgit.diff;version="[4.0.0,4.1.0)", - org.eclipse.jgit.dircache;version="[4.0.0,4.1.0)", - org.eclipse.jgit.junit;version="[4.0.0,4.1.0)", - org.eclipse.jgit.lib;version="[4.0.0,4.1.0)", - org.eclipse.jgit.merge;version="[4.0.0,4.1.0)", - org.eclipse.jgit.pgm;version="[4.0.0,4.1.0)", - org.eclipse.jgit.pgm.internal;version="[4.0.0,4.1.0)", - org.eclipse.jgit.pgm.opt;version="[4.0.0,4.1.0)", - org.eclipse.jgit.revwalk;version="[4.0.0,4.1.0)", - org.eclipse.jgit.storage.file;version="[4.0.0,4.1.0)", - org.eclipse.jgit.transport;version="[4.0.0,4.1.0)", - org.eclipse.jgit.treewalk;version="[4.0.0,4.1.0)", - org.eclipse.jgit.util;version="[4.0.0,4.1.0)", - org.eclipse.jgit.util.io;version="[4.0.0,4.1.0)", - org.hamcrest.core;bundle-version="[1.1.0,2.0.0)", - org.junit;version="[4.4.0,5.0.0)", - org.kohsuke.args4j;version="[2.0.12,2.1.0)" +Bundle-RequiredExecutionEnvironment: JavaSE-17 +Bundle-SCM: url=https://github.com/eclipse-jgit/jgit, connection=scm:git:https://eclipse.gerrithub.io/eclipse-jgit/jgit.git, developerConnection=scm:git:https://eclipse.gerrithub.io/a/eclipse-jgit/jgit.git +Import-Package: org.eclipse.jgit.api;version="[7.4.0,7.5.0)", + org.eclipse.jgit.api.errors;version="[7.4.0,7.5.0)", + org.eclipse.jgit.diff;version="[7.4.0,7.5.0)", + org.eclipse.jgit.dircache;version="[7.4.0,7.5.0)", + org.eclipse.jgit.internal.diffmergetool;version="[7.4.0,7.5.0)", + org.eclipse.jgit.internal.storage.file;version="[7.4.0,7.5.0)", + org.eclipse.jgit.junit;version="[7.4.0,7.5.0)", + org.eclipse.jgit.lib;version="[7.4.0,7.5.0)", + org.eclipse.jgit.lib.internal;version="[7.4.0,7.5.0)", + org.eclipse.jgit.merge;version="[7.4.0,7.5.0)", + org.eclipse.jgit.pgm;version="[7.4.0,7.5.0)", + org.eclipse.jgit.pgm.internal;version="[7.4.0,7.5.0)", + org.eclipse.jgit.pgm.opt;version="[7.4.0,7.5.0)", + org.eclipse.jgit.revwalk;version="[7.4.0,7.5.0)", + org.eclipse.jgit.storage.file;version="[7.4.0,7.5.0)", + org.eclipse.jgit.transport;version="[7.4.0,7.5.0)", + org.eclipse.jgit.treewalk;version="[7.4.0,7.5.0)", + org.eclipse.jgit.util;version="[7.4.0,7.5.0)", + org.eclipse.jgit.util.io;version="[7.4.0,7.5.0)", + org.hamcrest.core;bundle-version="[1.1.0,3.0.0)", + org.junit;version="[4.13,5.0.0)", + org.junit.rules;version="[4.13,5.0.0)", + org.kohsuke.args4j;version="[2.33.0,3.0.0)" Require-Bundle: org.tukaani.xz;bundle-version="[1.3.0,2.0.0)" diff --git a/org.eclipse.jgit.pgm.test/org.eclipse.jgit.pgm--All-Tests (Java8).launch b/org.eclipse.jgit.pgm.test/org.eclipse.jgit.pgm--All-Tests (Java17) (de).launch index 1b61d3d3f1..c64458cd19 100644 --- a/org.eclipse.jgit.pgm.test/org.eclipse.jgit.pgm--All-Tests (Java8).launch +++ b/org.eclipse.jgit.pgm.test/org.eclipse.jgit.pgm--All-Tests (Java17) (de).launch @@ -7,6 +7,9 @@ <listEntry value="2"/> </listAttribute> <booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/> +<mapAttribute key="org.eclipse.debug.core.environmentVariables"> +<mapEntry key="LANG" value="de_DE.UTF-8"/> +</mapAttribute> <listAttribute key="org.eclipse.debug.ui.favoriteGroups"> <listEntry value="org.eclipse.debug.ui.launchGroup.debug"/> <listEntry value="org.eclipse.debug.ui.launchGroup.run"/> @@ -17,12 +20,11 @@ <stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/> <booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/> <listAttribute key="org.eclipse.jdt.launching.CLASSPATH"> -<listEntry value="<?xml version="1.0" encoding="UTF-8" standalone="no"?> <runtimeClasspathEntry containerPath="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7" path="1" type="4"/> "/> +<listEntry value="<?xml version="1.0" encoding="UTF-8" standalone="no"?> <runtimeClasspathEntry containerPath="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8" path="1" type="4"/> "/> <listEntry value="<?xml version="1.0" encoding="UTF-8" standalone="no"?> <runtimeClasspathEntry id="org.eclipse.jdt.launching.classpathentry.defaultClasspath"> <memento exportedEntriesOnly="false" project="org.eclipse.jgit.pgm.test"/> </runtimeClasspathEntry> "/> -<listEntry value="<?xml version="1.0" encoding="UTF-8" standalone="no"?> <runtimeClasspathEntry path="3" projectName="org.eclipse.jgit.java7" type="1"/> "/> </listAttribute> <booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="false"/> -<stringAttribute key="org.eclipse.jdt.launching.JRE_CONTAINER" value="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> +<stringAttribute key="org.eclipse.jdt.launching.JRE_CONTAINER" value="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"/> <stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value=""/> <stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.eclipse.jgit.pgm.test"/> </launchConfiguration> diff --git a/org.eclipse.jgit.pgm.test/org.eclipse.jgit.pgm--All-Tests (Java7).launch b/org.eclipse.jgit.pgm.test/org.eclipse.jgit.pgm--All-Tests (Java17).launch index 600ce7b729..48040cc450 100644 --- a/org.eclipse.jgit.pgm.test/org.eclipse.jgit.pgm--All-Tests (Java7).launch +++ b/org.eclipse.jgit.pgm.test/org.eclipse.jgit.pgm--All-Tests (Java17).launch @@ -15,13 +15,13 @@ <booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/> <stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/> <stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/> +<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/> <listAttribute key="org.eclipse.jdt.launching.CLASSPATH"> -<listEntry value="<?xml version="1.0" encoding="UTF-8" standalone="no"?> <runtimeClasspathEntry containerPath="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7" path="1" type="4"/> "/> +<listEntry value="<?xml version="1.0" encoding="UTF-8" standalone="no"?> <runtimeClasspathEntry containerPath="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8" path="1" type="4"/> "/> <listEntry value="<?xml version="1.0" encoding="UTF-8" standalone="no"?> <runtimeClasspathEntry id="org.eclipse.jdt.launching.classpathentry.defaultClasspath"> <memento exportedEntriesOnly="false" project="org.eclipse.jgit.pgm.test"/> </runtimeClasspathEntry> "/> -<listEntry value="<?xml version="1.0" encoding="UTF-8" standalone="no"?> <runtimeClasspathEntry path="3" projectName="org.eclipse.jgit.java7" type="1"/> "/> </listAttribute> <booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="false"/> -<stringAttribute key="org.eclipse.jdt.launching.JRE_CONTAINER" value="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> +<stringAttribute key="org.eclipse.jdt.launching.JRE_CONTAINER" value="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"/> <stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value=""/> <stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.eclipse.jgit.pgm.test"/> </launchConfiguration> diff --git a/org.eclipse.jgit.pgm.test/plugin.properties b/org.eclipse.jgit.pgm.test/plugin.properties index e131c6970f..61cae8e35f 100644 --- a/org.eclipse.jgit.pgm.test/plugin.properties +++ b/org.eclipse.jgit.pgm.test/plugin.properties @@ -1,2 +1,2 @@ -plugin_name=JGit Command Line Interface Tests -provider_name=Eclipse JGit +Bundle-Name=JGit Command Line Interface Tests +Bundle-Vendor=Eclipse JGit diff --git a/org.eclipse.jgit.pgm.test/pom.xml b/org.eclipse.jgit.pgm.test/pom.xml index 979081d904..d242156ccb 100644 --- a/org.eclipse.jgit.pgm.test/pom.xml +++ b/org.eclipse.jgit.pgm.test/pom.xml @@ -1,45 +1,12 @@ <?xml version="1.0" encoding="UTF-8"?> <!-- - Copyright (C) 2012, IBM Corporation - and other copyright owners as documented in the project's IP log. + Copyright (C) 2012, IBM Corporation and others - This program and the accompanying materials are made available - under the terms of the Eclipse Distribution License v1.0 which - accompanies this distribution, is reproduced below, and is - available at http://www.eclipse.org/org/documents/edl-v10.php + This program and the accompanying materials are made available under the + terms of the Eclipse Distribution License v. 1.0 which is available at + http://www.eclipse.org/org/documents/edl-v10.php. - All rights reserved. - - Redistribution and use in source and binary forms, with or - without modification, are permitted provided that the following - conditions are met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following - disclaimer in the documentation and/or other materials provided - with the distribution. - - - Neither the name of the Eclipse Foundation, Inc. nor the - names of its contributors may be used to endorse or promote - products derived from this software without specific prior - written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + SPDX-License-Identifier: BSD-3-Clause --> <project xmlns="http://maven.apache.org/POM/4.0.0" @@ -50,7 +17,7 @@ <parent> <groupId>org.eclipse.jgit</groupId> <artifactId>org.eclipse.jgit-parent</artifactId> - <version>4.0.0-SNAPSHOT</version> + <version>7.4.0-SNAPSHOT</version> </parent> <artifactId>org.eclipse.jgit.pgm.test</artifactId> @@ -60,6 +27,10 @@ Tests for command line client tools built on top of JGit. </description> + <properties> + <maven.javadoc.skip>true</maven.javadoc.skip> + </properties> + <dependencies> <dependency> <groupId>junit</groupId> @@ -84,6 +55,17 @@ <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.eclipse.jgit</groupId> + <artifactId>org.eclipse.jgit.ssh.jsch</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.tukaani</groupId> + <artifactId>xz</artifactId> + <optional>true</optional> + </dependency> </dependencies> <build> @@ -104,7 +86,7 @@ <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> - <argLine>-Djava.io.tmpdir=${project.build.directory}</argLine> + <argLine>@{argLine} -Xmx512m -Djava.io.tmpdir=${project.build.directory}</argLine> </configuration> </plugin> </plugins> diff --git a/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java b/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java index 50ddfe04d8..aab3f141fd 100644 --- a/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java +++ b/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2012, IBM Corporation and others. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2012, IBM Corporation and others. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.lib; @@ -46,46 +13,93 @@ import static org.junit.Assert.assertEquals; import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.eclipse.jgit.junit.JGitTestUtil; import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase; import org.eclipse.jgit.pgm.CLIGitCommand; +import org.eclipse.jgit.pgm.CLIGitCommand.Result; +import org.eclipse.jgit.pgm.TextBuiltin.TerminatedByHelpException; import org.junit.Before; public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase { /** Test repository, initialized for this test case. */ protected Repository db; - /** Working directory of {@link #db}. */ - protected File trash; - @Override @Before public void setUp() throws Exception { super.setUp(); db = createWorkRepository(); - trash = db.getWorkTree(); } + /** + * Executes specified git commands (with arguments) + * + * @param cmds + * each string argument must be a valid git command line, e.g. + * "git branch -h" + * @return command output + * @throws Exception + */ + protected String[] executeUnchecked(String... cmds) throws Exception { + List<String> result = new ArrayList<>(cmds.length); + for (String cmd : cmds) { + result.addAll(CLIGitCommand.executeUnchecked(cmd, db)); + } + return result.toArray(new String[0]); + } + + /** + * Executes specified git commands (with arguments), throws exception and + * stops execution on first command which output contains a 'fatal:' error + * + * @param cmds + * each string argument must be a valid git command line, e.g. + * "git branch -h" + * @return command output + * @throws Exception + */ protected String[] execute(String... cmds) throws Exception { - List<String> result = new ArrayList<String>(cmds.length); - for (String cmd : cmds) - result.addAll(CLIGitCommand.execute(cmd, db)); + List<String> result = new ArrayList<>(cmds.length); + for (String cmd : cmds) { + Result r = CLIGitCommand.executeRaw(cmd, db); + if (r.ex instanceof TerminatedByHelpException) { + result.addAll(r.errLines()); + } else if (r.ex != null) { + throw r.ex; + } + result.addAll(r.outLines()); + } return result.toArray(new String[0]); } - protected File writeTrashFile(final String name, final String data) + /** + * @param link + * the path of the symbolic link to create + * @param target + * the target of the symbolic link + * @return the path to the symbolic link + * @throws Exception + */ + protected Path writeLink(String link, String target) throws Exception { + return JGitTestUtil.writeLink(db, link, target); + } + + protected File writeTrashFile(String name, String data) throws IOException { return JGitTestUtil.writeTrashFile(db, name, data); } - protected String read(final File file) throws IOException { + @Override + protected String read(File file) throws IOException { return JGitTestUtil.read(file); } - protected void deleteTrashFile(final String name) throws IOException { + protected void deleteTrashFile(String name) throws IOException { JGitTestUtil.deleteTrashFile(db, name); } @@ -143,11 +157,12 @@ public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase { } protected String cmdString(String... cmds) { - if (cmds.length == 0) + switch (cmds.length) { + case 0: return ""; - else if (cmds.length == 1) + case 1: return "\"" + escapeJava(cmds[0]) + "\""; - else { + default: StringBuilder sb = new StringBuilder(cmdString(cmds[0])); for (int i=1; i<cmds.length; i++) { sb.append(", "); @@ -164,16 +179,53 @@ public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase { .replaceAll("\t", "\\\\t"); } + protected String shellQuote(String s) { + return "'" + s.replace("'", "'\\''") + "'"; + } + + protected String shellQuote(File f) { + return "'" + f.getPath().replace("'", "'\\''") + "'"; + } + + protected void assertStringArrayEquals(String expected, String[] actual) { + // if there is more than one line, ignore last one if empty + assertEquals(1, + actual.length > 1 && actual[actual.length - 1].isEmpty() + ? actual.length - 1 : actual.length); + assertEquals(expected, actual[0]); + } + protected void assertArrayOfLinesEquals(String[] expected, String[] actual) { - assertEquals(toText(expected), toText(actual)); + assertEquals(toString(expected), toString(actual)); + } + + public static String toString(String... lines) { + return toString(Arrays.asList(lines)); } - private static String toText(String[] lines) { + public static String toString(List<String> lines) { StringBuilder b = new StringBuilder(); for (String s : lines) { - b.append(s); - b.append('\n'); + // trim indentation, to simplify tests + s = s.trim(); + if (s != null && !s.isEmpty()) { + b.append(s); + b.append('\n'); + } + } + // delete last line break to allow simpler tests with one line compare + if (b.length() > 0 && b.charAt(b.length() - 1) == '\n') { + b.deleteCharAt(b.length() - 1); } return b.toString(); } + + public static boolean contains(List<String> lines, String str) { + for (String s : lines) { + if (s.contains(str)) { + return true; + } + } + return false; + } } diff --git a/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java b/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java index d77b1505ae..ee98fc645b 100644 --- a/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java +++ b/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java @@ -1,112 +1,151 @@ /* - * Copyright (C) 2011-2012, IBM Corporation and others. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2011-2012, IBM Corporation and others. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.Assert.assertNull; + import java.io.ByteArrayOutputStream; -import java.text.MessageFormat; +import java.io.File; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; +import org.eclipse.jgit.internal.storage.file.FileRepository; import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.pgm.internal.CLIText; -import org.eclipse.jgit.pgm.opt.CmdLineParser; -import org.eclipse.jgit.pgm.opt.SubcommandHandler; +import org.eclipse.jgit.pgm.TextBuiltin.TerminatedByHelpException; import org.eclipse.jgit.util.IO; -import org.kohsuke.args4j.Argument; -public class CLIGitCommand { - @Argument(index = 0, metaVar = "metaVar_command", required = true, handler = SubcommandHandler.class) - private TextBuiltin subcommand; +public class CLIGitCommand extends Main { + + private final Result result; - @Argument(index = 1, metaVar = "metaVar_arg") - private List<String> arguments = new ArrayList<String>(); + private final Repository db; - public TextBuiltin getSubcommand() { - return subcommand; + public CLIGitCommand(Repository db) { + super(); + this.db = db; + result = new Result(); } - public List<String> getArguments() { - return arguments; + /** + * Executes git commands (with arguments) specified on the command line. The + * git repository (same for all commands) can be specified via system + * property "-Dgit_work_tree=path_to_work_tree". If the property is not set, + * current directory is used. + * + * @param args + * each element in the array must be a valid git command line, + * e.g. "git branch -h" + * @throws Exception + */ + public static void main(String[] args) throws Exception { + String workDir = System.getProperty("git_work_tree"); + if (workDir == null) { + workDir = "."; + System.out.println( + "System property 'git_work_tree' not specified, using current directory: " + + new File(workDir).getAbsolutePath()); + } + try (Repository db = new FileRepository(workDir + "/.git")) { + for (String cmd : args) { + List<String> result = execute(cmd, db); + for (String line : result) { + System.out.println(line); + } + } + } } public static List<String> execute(String str, Repository db) throws Exception { + Result result = executeRaw(str, db); + return getOutput(result); + } + + public static Result executeRaw(String str, Repository db) + throws Exception { + CLIGitCommand cmd = new CLIGitCommand(db); + cmd.run(str); + return cmd.result; + } + + public static List<String> executeUnchecked(String str, Repository db) + throws Exception { + CLIGitCommand cmd = new CLIGitCommand(db); + try { + cmd.run(str); + return getOutput(cmd.result); + } catch (Throwable e) { + return cmd.result.errLines(); + } + } + + private static List<String> getOutput(Result result) { + if (result.ex instanceof TerminatedByHelpException) { + return result.errLines(); + } + return result.outLines(); + } + + private void run(String commandLine) throws Exception { + String[] argv = convertToMainArgs(commandLine); try { - return IO.readLines(new String(rawExecute(str, db))); - } catch (Die e) { - return IO.readLines(MessageFormat.format(CLIText.get().fatalError, - e.getMessage())); + super.run(argv); + } catch (TerminatedByHelpException e) { + // this is not a failure, super called exit() on help + } finally { + writer.flush(); } } - public static byte[] rawExecute(String str, Repository db) + private static String[] convertToMainArgs(String str) throws Exception { String[] args = split(str); - if (!args[0].equalsIgnoreCase("git") || args.length < 2) + if (!args[0].equalsIgnoreCase("git") || args.length < 2) { throw new IllegalArgumentException( "Expected 'git <command> [<args>]', was:" + str); + } String[] argv = new String[args.length - 1]; System.arraycopy(args, 1, argv, 0, args.length - 1); + return argv; + } - CLIGitCommand bean = new CLIGitCommand(); - final CmdLineParser clp = new CmdLineParser(bean); - clp.parseArgument(argv); - - final TextBuiltin cmd = bean.getSubcommand(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - cmd.outs = baos; - if (cmd.requiresRepository()) - cmd.init(db, null); - else - cmd.init(null, null); - try { - cmd.execute(bean.getArguments().toArray( - new String[bean.getArguments().size()])); - } finally { - if (cmd.outw != null) - cmd.outw.flush(); + @Override + PrintWriter createErrorWriter() { + return new PrintWriter(new OutputStreamWriter( + result.err, UTF_8)); + } + + @Override + void init(TextBuiltin cmd) throws IOException { + cmd.outs = result.out; + cmd.errs = result.err; + super.init(cmd); + } + + @Override + protected Repository openGitDir(String aGitdir) throws IOException { + assertNull(aGitdir); + return db; + } + + @Override + void exit(int status, Exception t) throws Exception { + if (t == null) { + t = new IllegalStateException(Integer.toString(status)); } - return baos.toByteArray(); + result.ex = t; + throw t; } /** @@ -120,7 +159,7 @@ public class CLIGitCommand { * @return the array */ static String[] split(String commandLine) { - final List<String> list = new ArrayList<String>(); + final List<String> list = new ArrayList<>(); boolean inquote = false; boolean inDblQuote = false; StringBuilder r = new StringBuilder(); @@ -149,7 +188,7 @@ public class CLIGitCommand { inquote = !inquote; continue; case '\\': - if (inquote || ip == commandLine.length()) + if (inDblQuote || inquote || ip == commandLine.length()) r.append(b); // literal within a quote else r.append(commandLine.charAt(ip++)); @@ -161,7 +200,39 @@ public class CLIGitCommand { } if (r.length() > 0) list.add(r.toString()); - return list.toArray(new String[list.size()]); + return list.toArray(new String[0]); + } + + public static class Result { + public final ByteArrayOutputStream out = new ByteArrayOutputStream(); + + public final ByteArrayOutputStream err = new ByteArrayOutputStream(); + + public Exception ex; + + public byte[] outBytes() { + return out.toByteArray(); + } + + public byte[] errBytes() { + return err.toByteArray(); + } + + public String outString() { + return new String(out.toByteArray(), UTF_8); + } + + public List<String> outLines() { + return IO.readLines(outString()); + } + + public String errString() { + return new String(err.toByteArray(), UTF_8); + } + + public List<String> errLines() { + return IO.readLines(errString()); + } } } diff --git a/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/ToolTestCase.java b/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/ToolTestCase.java new file mode 100644 index 0000000000..55db1a12d9 --- /dev/null +++ b/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/ToolTestCase.java @@ -0,0 +1,251 @@ +/* + * Copyright (C) 2022, Simeon Andreev <simeon.danailov.andreev@gmail.com> and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.pgm; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.diff.DiffEntry; +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.eclipse.jgit.pgm.opt.CmdLineParser; +import org.eclipse.jgit.pgm.opt.SubcommandHandler; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.treewalk.FileTreeIterator; +import org.eclipse.jgit.treewalk.TreeWalk; +import org.eclipse.jgit.util.SystemReader; +import org.junit.Assume; +import org.junit.Before; +import org.kohsuke.args4j.Argument; +import org.kohsuke.args4j.CmdLineException; + +/** + * Base test case for the {@code difftool} and {@code mergetool} commands. + */ +public abstract class ToolTestCase extends CLIRepositoryTestCase { + + public static class GitCliJGitWrapperParser { + @Argument(index = 0, metaVar = "metaVar_command", required = true, handler = SubcommandHandler.class) + TextBuiltin subcommand; + + @Argument(index = 1, metaVar = "metaVar_arg") + List<String> arguments = new ArrayList<>(); + } + + protected static final String TOOL_NAME = "some_tool"; + + private static final String TEST_BRANCH_NAME = "test_branch"; + + private Git git; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + git = new Git(db); + git.commit().setMessage("initial commit").call(); + git.branchCreate().setName(TEST_BRANCH_NAME).call(); + } + + protected String[] runAndCaptureUsingInitRaw(String... args) + throws Exception { + InputStream inputStream = null; // no input stream + return runAndCaptureUsingInitRaw(inputStream, args); + } + + protected String[] runAndCaptureUsingInitRaw( + List<String> expectedErrorOutput, String... args) throws Exception { + InputStream inputStream = null; // no input stream + return runAndCaptureUsingInitRaw(inputStream, expectedErrorOutput, + args); + } + + protected String[] runAndCaptureUsingInitRaw(InputStream inputStream, + String... args) throws Exception { + List<String> expectedErrorOutput = Collections.emptyList(); + return runAndCaptureUsingInitRaw(inputStream, expectedErrorOutput, + args); + } + + protected String[] runAndCaptureUsingInitRaw(InputStream inputStream, + List<String> expectedErrorOutput, String... args) + throws CmdLineException, Exception, IOException { + CLIGitCommand.Result result = new CLIGitCommand.Result(); + + GitCliJGitWrapperParser bean = new GitCliJGitWrapperParser(); + CmdLineParser clp = new CmdLineParser(bean); + clp.parseArgument(args); + + TextBuiltin cmd = bean.subcommand; + cmd.initRaw(db, null, inputStream, result.out, result.err); + cmd.execute(bean.arguments.toArray(new String[bean.arguments.size()])); + if (cmd.getOutputWriter() != null) { + cmd.getOutputWriter().flush(); + } + if (cmd.getErrorWriter() != null) { + cmd.getErrorWriter().flush(); + } + + List<String> errLines = result.errLines().stream() + .filter(l -> !l.isBlank()) // we care only about error messages + .collect(Collectors.toList()); + assertEquals("Expected no standard error output from tool", + expectedErrorOutput.toString(), errLines.toString()); + + return result.outLines().toArray(new String[0]); + } + + protected String[] createMergeConflict() throws Exception { + // create files on initial branch + git.checkout().setName(TEST_BRANCH_NAME).call(); + writeTrashFile("dir1/a", "Hello world a"); + writeTrashFile("dir2/b", "Hello world b"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("files a & b added").call(); + // create another branch and change files + git.branchCreate().setName("branch_1").call(); + git.checkout().setName("branch_1").call(); + writeTrashFile("dir1/a", "Hello world a 1"); + writeTrashFile("dir2/b", "Hello world b 1"); + git.add().addFilepattern(".").call(); + RevCommit commit1 = git.commit() + .setMessage("files a & b modified commit 1").call(); + // checkout initial branch + git.checkout().setName(TEST_BRANCH_NAME).call(); + // create another branch and change files + git.branchCreate().setName("branch_2").call(); + git.checkout().setName("branch_2").call(); + writeTrashFile("dir1/a", "Hello world a 2"); + writeTrashFile("dir2/b", "Hello world b 2"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("files a & b modified commit 2").call(); + // cherry-pick conflicting changes + git.cherryPick().include(commit1).call(); + String[] conflictingFilenames = { "dir1/a", "dir2/b" }; + return conflictingFilenames; + } + + protected String[] createDeletedConflict() throws Exception { + // create files on initial branch + git.checkout().setName(TEST_BRANCH_NAME).call(); + writeTrashFile("dir1/a", "Hello world a"); + writeTrashFile("dir2/b", "Hello world b"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("files a & b added").call(); + // create another branch and change files + git.branchCreate().setName("branch_1").call(); + git.checkout().setName("branch_1").call(); + writeTrashFile("dir1/a", "Hello world a 1"); + writeTrashFile("dir2/b", "Hello world b 1"); + git.add().addFilepattern(".").call(); + RevCommit commit1 = git.commit() + .setMessage("files a & b modified commit 1").call(); + // checkout initial branch + git.checkout().setName(TEST_BRANCH_NAME).call(); + // create another branch and change files + git.branchCreate().setName("branch_2").call(); + git.checkout().setName("branch_2").call(); + git.rm().addFilepattern("dir1/a").call(); + git.rm().addFilepattern("dir2/b").call(); + git.commit().setMessage("files a & b deleted commit 2").call(); + // cherry-pick conflicting changes + git.cherryPick().include(commit1).call(); + String[] conflictingFilenames = { "dir1/a", "dir2/b" }; + return conflictingFilenames; + } + + protected String[] createUnstagedChanges() throws Exception { + writeTrashFile("dir1/a", "Hello world a"); + writeTrashFile("dir2/b", "Hello world b"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("files a & b").call(); + writeTrashFile("dir1/a", "New Hello world a"); + writeTrashFile("dir2/b", "New Hello world b"); + String[] conflictingFilenames = { "dir1/a", "dir2/b" }; + return conflictingFilenames; + } + + protected String[] createStagedChanges() throws Exception { + String[] conflictingFilenames = createUnstagedChanges(); + git.add().addFilepattern(".").call(); + return conflictingFilenames; + } + + protected List<DiffEntry> getRepositoryChanges(RevCommit commit) + throws Exception { + TreeWalk tw = new TreeWalk(db); + tw.addTree(commit.getTree()); + FileTreeIterator modifiedTree = new FileTreeIterator(db); + tw.addTree(modifiedTree); + List<DiffEntry> changes = DiffEntry.scan(tw); + return changes; + } + + protected Path getFullPath(String repositoryFilename) { + Path dotGitPath = db.getDirectory().toPath(); + Path repositoryRoot = dotGitPath.getParent(); + Path repositoryFilePath = repositoryRoot.resolve(repositoryFilename); + return repositoryFilePath; + } + + protected static InputStream createInputStream(String[] inputLines) { + return createInputStream(Arrays.asList(inputLines)); + } + + protected static InputStream createInputStream(List<String> inputLines) { + String input = String.join(System.lineSeparator(), inputLines); + InputStream inputStream = new ByteArrayInputStream(input.getBytes(UTF_8)); + return inputStream; + } + + protected static void assertArrayOfLinesEquals(String failMessage, + String[] expected, String[] actual) { + assertEquals(failMessage, toString(expected), toString(actual)); + } + + protected static void assertArrayOfMatchingLines(String failMessage, + Pattern[] expected, String[] actual) { + assertEquals(failMessage + System.lineSeparator() + + "Expected and actual lines count don't match. Expected: " + + Arrays.asList(expected) + ", actual: " + + Arrays.asList(actual), expected.length, actual.length); + int n = expected.length; + for (int i = 0; i < n; ++i) { + Pattern expectedPattern = expected[i]; + String actualLine = actual[i]; + Matcher matcher = expectedPattern.matcher(actualLine); + boolean matches = matcher.matches(); + assertTrue(failMessage + System.lineSeparator() + "Line " + i + " '" + + actualLine + "' doesn't match expected pattern: " + + expectedPattern + System.lineSeparator() + "Expected: " + + Arrays.asList(expected) + ", actual: " + + Arrays.asList(actual), + matches); + } + } + + protected static void assumeLinuxPlatform() { + Assume.assumeTrue("This test can run only in Linux tests", + SystemReader.getInstance().isLinux()); + } +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/AddTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/AddTest.java index 4253080a66..a48fcbcd5a 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/AddTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/AddTest.java @@ -1,59 +1,26 @@ /* - * Copyright (C) 2012 Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2012, 2025 Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; -import java.lang.Exception; -import java.lang.String; +import java.io.File; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.lib.CLIRepositoryTestCase; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; public class AddTest extends CLIRepositoryTestCase { @@ -66,20 +33,28 @@ public class AddTest extends CLIRepositoryTestCase { git = new Git(db); } - @Ignore("args4j exit()s on error instead of throwing, JVM goes down") @Test public void testAddNothing() throws Exception { - assertEquals("fatal: Argument \"filepattern\" is required", // - execute("git add")[0]); + assertThrows(Die.class, () -> execute("git add")); } - @Ignore("args4j exit()s for --help, too") @Test public void testAddUsage() throws Exception { execute("git add --help"); } @Test + public void testAddInvalidOptionCombinations() throws Exception { + writeTrashFile("greeting", "Hello, world!"); + assertThrows(Die.class, () -> execute("git add -u -A greeting")); + assertThrows(Die.class, + () -> execute("git add -u --ignore-removed greeting")); + // --renormalize implies -u + assertThrows(Die.class, + () -> execute("git add --renormalize --all greeting")); + } + + @Test public void testAddAFile() throws Exception { writeTrashFile("greeting", "Hello, world!"); assertArrayEquals(new String[] { "" }, // @@ -112,4 +87,34 @@ public class AddTest extends CLIRepositoryTestCase { assertNotNull(cache.getEntry("greeting")); assertEquals(1, cache.getEntryCount()); } + + @Test + public void testAddDeleted() throws Exception { + File greeting = writeTrashFile("greeting", "Hello, world!"); + git.add().addFilepattern("greeting").call(); + DirCache cache = db.readDirCache(); + assertNotNull(cache.getEntry("greeting")); + assertEquals(1, cache.getEntryCount()); + assertTrue(greeting.delete()); + assertArrayEquals(new String[] { "" }, // + execute("git add greeting")); + + cache = db.readDirCache(); + assertEquals(0, cache.getEntryCount()); + } + + @Test + public void testAddDeleted2() throws Exception { + File greeting = writeTrashFile("greeting", "Hello, world!"); + git.add().addFilepattern("greeting").call(); + DirCache cache = db.readDirCache(); + assertNotNull(cache.getEntry("greeting")); + assertEquals(1, cache.getEntryCount()); + assertTrue(greeting.delete()); + assertArrayEquals(new String[] { "" }, // + execute("git add -A")); + + cache = db.readDirCache(); + assertEquals(0, cache.getEntryCount()); + } } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java index 6ce092d3c1..1fc898d546 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java @@ -1,48 +1,17 @@ /* - * Copyright (C) 2012 Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2012 Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import static org.junit.Assume.assumeNoException; @@ -52,17 +21,14 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.InputStreamReader; import java.io.IOException; +import java.io.InputStreamReader; import java.io.OutputStream; -import java.lang.Object; -import java.lang.String; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -71,9 +37,7 @@ import org.eclipse.jgit.api.Git; import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.lib.CLIRepositoryTestCase; import org.eclipse.jgit.lib.FileMode; -import org.eclipse.jgit.pgm.CLIGitCommand; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; public class ArchiveTest extends CLIRepositoryTestCase { @@ -89,25 +53,26 @@ public class ArchiveTest extends CLIRepositoryTestCase { emptyTree = db.resolve("HEAD^{tree}").abbreviate(12).name(); } - @Ignore("Some versions of java.util.zip refuse to write an empty ZIP") @Test public void testEmptyArchive() throws Exception { - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=zip " + emptyTree, db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=zip " + emptyTree, db).outBytes(); assertArrayEquals(new String[0], listZipEntries(result)); } @Test public void testEmptyTar() throws Exception { - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=tar " + emptyTree, db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=tar " + emptyTree, db).outBytes(); assertArrayEquals(new String[0], listTarEntries(result)); } @Test public void testUnrecognizedFormat() throws Exception { - final String[] expect = new String[] { "fatal: Unknown archive format 'nonsense'" }; - final String[] actual = execute("git archive --format=nonsense " + emptyTree); + String[] expect = new String[] { + "fatal: Unknown archive format 'nonsense'", "" }; + String[] actual = executeUnchecked( + "git archive --format=nonsense " + emptyTree); assertArrayEquals(expect, actual); } @@ -120,9 +85,9 @@ public class ArchiveTest extends CLIRepositoryTestCase { git.add().addFilepattern("c").call(); git.commit().setMessage("populate toplevel").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=zip HEAD", db); - assertArrayEquals(new String[] { "a", "c" }, // + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=zip HEAD", db).outBytes(); + assertArrayEquals(new String[] { "a", "c" }, listZipEntries(result)); } @@ -135,20 +100,16 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testDefaultFormatIsTar() throws Exception { commitGreeting(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive HEAD", db); - assertArrayEquals(new String[] { "greeting" }, // + byte[] result = CLIGitCommand.executeRaw( + "git archive HEAD", db).outBytes(); + assertArrayEquals(new String[] { "greeting" }, listTarEntries(result)); } - private static String shellQuote(String s) { - return "'" + s.replace("'", "'\\''") + "'"; - } - @Test public void testFormatOverridesFilename() throws Exception { - final File archive = new File(db.getWorkTree(), "format-overrides-name.tar"); - final String path = archive.getAbsolutePath(); + File archive = new File(db.getWorkTree(), "format-overrides-name.tar"); + String path = archive.getAbsolutePath(); commitGreeting(); assertArrayEquals(new String[] { "" }, @@ -162,8 +123,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testUnrecognizedExtensionMeansTar() throws Exception { - final File archive = new File(db.getWorkTree(), "example.txt"); - final String path = archive.getAbsolutePath(); + File archive = new File(db.getWorkTree(), "example.txt"); + String path = archive.getAbsolutePath(); commitGreeting(); assertArrayEquals(new String[] { "" }, @@ -176,8 +137,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testNoExtensionMeansTar() throws Exception { - final File archive = new File(db.getWorkTree(), "example"); - final String path = archive.getAbsolutePath(); + File archive = new File(db.getWorkTree(), "example"); + String path = archive.getAbsolutePath(); commitGreeting(); assertArrayEquals(new String[] { "" }, @@ -189,8 +150,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testExtensionMatchIsAnchored() throws Exception { - final File archive = new File(db.getWorkTree(), "two-extensions.zip.bak"); - final String path = archive.getAbsolutePath(); + File archive = new File(db.getWorkTree(), "two-extensions.zip.bak"); + String path = archive.getAbsolutePath(); commitGreeting(); assertArrayEquals(new String[] { "" }, @@ -202,8 +163,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testZipExtension() throws Exception { - final File archiveWithDot = new File(db.getWorkTree(), "greeting.zip"); - final File archiveNoDot = new File(db.getWorkTree(), "greetingzip"); + File archiveWithDot = new File(db.getWorkTree(), "greeting.zip"); + File archiveNoDot = new File(db.getWorkTree(), "greetingzip"); commitGreeting(); execute("git archive " + @@ -218,8 +179,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testTarExtension() throws Exception { - final File archive = new File(db.getWorkTree(), "tarball.tar"); - final String path = archive.getAbsolutePath(); + File archive = new File(db.getWorkTree(), "tarball.tar"); + String path = archive.getAbsolutePath(); commitGreeting(); assertArrayEquals(new String[] { "" }, @@ -234,8 +195,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { commitGreeting(); for (String ext : Arrays.asList("tar.gz", "tgz")) { - final File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext); - final File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext); + File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext); + File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext); execute("git archive " + shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " + @@ -253,8 +214,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { commitGreeting(); for (String ext : Arrays.asList("tar.bz2", "tbz", "tbz2")) { - final File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext); - final File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext); + File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext); + File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext); execute("git archive " + shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " + @@ -272,8 +233,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { commitGreeting(); for (String ext : Arrays.asList("tar.xz", "txz")) { - final File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext); - final File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext); + File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext); + File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext); execute("git archive " + shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " + @@ -302,8 +263,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { git.add().addFilepattern("b").call(); git.commit().setMessage("add subdir").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=zip master", db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=zip master", db).outBytes(); String[] expect = { "a", "b.c", "b0c", "b/", "b/a", "b/b", "c" }; String[] actual = listZipEntries(result); @@ -328,8 +289,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { git.add().addFilepattern("b").call(); git.commit().setMessage("add subdir").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=tar master", db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=tar master", db).outBytes(); String[] expect = { "a", "b.c", "b0c", "b/", "b/a", "b/b", "c" }; String[] actual = listTarEntries(result); @@ -349,9 +310,9 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testArchivePrefixOption() throws Exception { commitBazAndFooSlashBar(); - byte[] result = CLIGitCommand.rawExecute( - "git archive --prefix=x/ --format=zip master", db); - String[] expect = { "x/baz", "x/foo/", "x/foo/bar" }; + byte[] result = CLIGitCommand.executeRaw( + "git archive --prefix=x/ --format=zip master", db).outBytes(); + String[] expect = { "x/", "x/baz", "x/foo/", "x/foo/bar" }; String[] actual = listZipEntries(result); Arrays.sort(expect); @@ -362,9 +323,9 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testTarPrefixOption() throws Exception { commitBazAndFooSlashBar(); - byte[] result = CLIGitCommand.rawExecute( - "git archive --prefix=x/ --format=tar master", db); - String[] expect = { "x/baz", "x/foo/", "x/foo/bar" }; + byte[] result = CLIGitCommand.executeRaw( + "git archive --prefix=x/ --format=tar master", db).outBytes(); + String[] expect = { "x/", "x/baz", "x/foo/", "x/foo/bar" }; String[] actual = listTarEntries(result); Arrays.sort(expect); @@ -381,18 +342,18 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testPrefixDoesNotNormalizeDoubleSlash() throws Exception { commitFoo(); - byte[] result = CLIGitCommand.rawExecute( - "git archive --prefix=x// --format=zip master", db); - String[] expect = { "x//foo" }; + byte[] result = CLIGitCommand.executeRaw( + "git archive --prefix=x// --format=zip master", db).outBytes(); + String[] expect = { "x/", "x//foo" }; assertArrayEquals(expect, listZipEntries(result)); } @Test public void testPrefixDoesNotNormalizeDoubleSlashInTar() throws Exception { commitFoo(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --prefix=x// --format=tar master", db); - String[] expect = { "x//foo" }; + byte[] result = CLIGitCommand.executeRaw( + "git archive --prefix=x// --format=tar master", db).outBytes(); + String[] expect = { "x/", "x//foo" }; assertArrayEquals(expect, listTarEntries(result)); } @@ -408,8 +369,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testPrefixWithoutTrailingSlash() throws Exception { commitBazAndFooSlashBar(); - byte[] result = CLIGitCommand.rawExecute( - "git archive --prefix=my- --format=zip master", db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --prefix=my- --format=zip master", db).outBytes(); String[] expect = { "my-baz", "my-foo/", "my-foo/bar" }; String[] actual = listZipEntries(result); @@ -421,8 +382,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testTarPrefixWithoutTrailingSlash() throws Exception { commitBazAndFooSlashBar(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --prefix=my- --format=tar master", db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --prefix=my- --format=tar master", db).outBytes(); String[] expect = { "my-baz", "my-foo/", "my-foo/bar" }; String[] actual = listTarEntries(result); @@ -441,8 +402,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { git.submoduleAdd().setURI("./.").setPath("b").call().close(); git.commit().setMessage("add submodule").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=zip master", db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=zip master", db).outBytes(); String[] expect = { ".gitmodules", "a", "b/", "c" }; String[] actual = listZipEntries(result); @@ -461,8 +422,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { git.submoduleAdd().setURI("./.").setPath("b").call().close(); git.commit().setMessage("add submodule").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=tar master", db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=tar master", db).outBytes(); String[] expect = { ".gitmodules", "a", "b/", "c" }; String[] actual = listTarEntries(result); @@ -491,8 +452,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { git.commit().setMessage("three files with different modes").call(); - final byte[] zipData = CLIGitCommand.rawExecute( // - "git archive --format=zip master", db); + byte[] zipData = CLIGitCommand.executeRaw( + "git archive --format=zip master", db).outBytes(); writeRaw("zip-with-modes.zip", zipData); assertContainsEntryWithMode("zip-with-modes.zip", "-rw-", "plain"); assertContainsEntryWithMode("zip-with-modes.zip", "-rwx", "executable"); @@ -520,8 +481,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { git.commit().setMessage("three files with different modes").call(); - final byte[] archive = CLIGitCommand.rawExecute( // - "git archive --format=tar master", db); + byte[] archive = CLIGitCommand.executeRaw( + "git archive --format=tar master", db).outBytes(); writeRaw("with-modes.tar", archive); assertTarContainsEntry("with-modes.tar", "-rw-r--r--", "plain"); assertTarContainsEntry("with-modes.tar", "-rwxr-xr-x", "executable"); @@ -531,74 +492,74 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testArchiveWithLongFilename() throws Exception { - String filename = ""; - final List<String> l = new ArrayList<String>(); + StringBuilder filename = new StringBuilder(); + List<String> l = new ArrayList<>(); for (int i = 0; i < 20; i++) { - filename = filename + "1234567890/"; - l.add(filename); + filename.append("1234567890/"); + l.add(filename.toString()); } - filename = filename + "1234567890"; - l.add(filename); - writeTrashFile(filename, "file with long path"); + filename.append("1234567890"); + l.add(filename.toString()); + writeTrashFile(filename.toString(), "file with long path"); git.add().addFilepattern("1234567890").call(); git.commit().setMessage("file with long name").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=zip HEAD", db); - assertArrayEquals(l.toArray(new String[l.size()]), + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=zip HEAD", db).outBytes(); + assertArrayEquals(l.toArray(new String[0]), listZipEntries(result)); } @Test public void testTarWithLongFilename() throws Exception { - String filename = ""; - final List<String> l = new ArrayList<String>(); + StringBuilder filename = new StringBuilder(); + List<String> l = new ArrayList<>(); for (int i = 0; i < 20; i++) { - filename = filename + "1234567890/"; - l.add(filename); + filename.append("1234567890/"); + l.add(filename.toString()); } - filename = filename + "1234567890"; - l.add(filename); - writeTrashFile(filename, "file with long path"); + filename.append("1234567890"); + l.add(filename.toString()); + writeTrashFile(filename.toString(), "file with long path"); git.add().addFilepattern("1234567890").call(); git.commit().setMessage("file with long name").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=tar HEAD", db); - assertArrayEquals(l.toArray(new String[l.size()]), + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=tar HEAD", db).outBytes(); + assertArrayEquals(l.toArray(new String[0]), listTarEntries(result)); } @Test public void testArchivePreservesContent() throws Exception { - final String payload = "“The quick brown fox jumps over the lazy dog!”"; + String payload = "“The quick brown fox jumps over the lazy dog!”"; writeTrashFile("xyzzy", payload); git.add().addFilepattern("xyzzy").call(); git.commit().setMessage("add file with content").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=zip HEAD", db); - assertArrayEquals(new String[] { payload }, // + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=zip HEAD", db).outBytes(); + assertArrayEquals(new String[] { payload }, zipEntryContent(result, "xyzzy")); } @Test public void testTarPreservesContent() throws Exception { - final String payload = "“The quick brown fox jumps over the lazy dog!”"; + String payload = "“The quick brown fox jumps over the lazy dog!”"; writeTrashFile("xyzzy", payload); git.add().addFilepattern("xyzzy").call(); git.commit().setMessage("add file with content").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=tar HEAD", db); - assertArrayEquals(new String[] { payload }, // + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=tar HEAD", db).outBytes(); + assertArrayEquals(new String[] { payload }, tarEntryContent(result, "xyzzy")); } private Process spawnAssumingCommandPresent(String... cmdline) { - final File cwd = db.getWorkTree(); - final ProcessBuilder procBuilder = new ProcessBuilder(cmdline) // - .directory(cwd) // + File cwd = db.getWorkTree(); + ProcessBuilder procBuilder = new ProcessBuilder(cmdline) + .directory(cwd) .redirectErrorStream(true); Process proc = null; try { @@ -612,15 +573,15 @@ public class ArchiveTest extends CLIRepositoryTestCase { } private BufferedReader readFromProcess(Process proc) throws Exception { - return new BufferedReader( // - new InputStreamReader(proc.getInputStream(), "UTF-8")); + return new BufferedReader( + new InputStreamReader(proc.getInputStream(), UTF_8)); } - private void grepForEntry(String name, String mode, String... cmdline) // + private void grepForEntry(String name, String mode, String... cmdline) throws Exception { - final Process proc = spawnAssumingCommandPresent(cmdline); + Process proc = spawnAssumingCommandPresent(cmdline); proc.getOutputStream().close(); - final BufferedReader reader = readFromProcess(proc); + BufferedReader reader = readFromProcess(proc); try { String line; while ((line = reader.readLine()) != null) @@ -635,16 +596,16 @@ public class ArchiveTest extends CLIRepositoryTestCase { } private void assertMagic(long offset, byte[] magicBytes, File file) throws Exception { - BufferedInputStream in = new BufferedInputStream( - new FileInputStream(file)); - try { - in.skip(offset); + try (BufferedInputStream in = new BufferedInputStream( + new FileInputStream(file))) { + if (offset > 0) { + long skipped = in.skip(offset); + assertEquals(offset, skipped); + } byte[] actual = new byte[magicBytes.length]; in.read(actual); assertArrayEquals(magicBytes, actual); - } finally { - in.close(); } } @@ -672,79 +633,73 @@ public class ArchiveTest extends CLIRepositoryTestCase { assertMagic(new byte[] { (byte) 0xfd, '7', 'z', 'X', 'Z', 0 }, file); } - private void assertContainsEntryWithMode(String zipFilename, String mode, String name) // + private void assertContainsEntryWithMode(String zipFilename, String mode, String name) throws Exception { grepForEntry(name, mode, "zipinfo", zipFilename); } - private void assertTarContainsEntry(String tarfile, String mode, String name) // + private void assertTarContainsEntry(String tarfile, String mode, String name) throws Exception { grepForEntry(name, mode, "tar", "tvf", tarfile); } - private void writeRaw(String filename, byte[] data) // + private void writeRaw(String filename, byte[] data) throws IOException { - final File path = new File(db.getWorkTree(), filename); - final OutputStream out = new FileOutputStream(path); - try { + File path = new File(db.getWorkTree(), filename); + try (OutputStream out = new FileOutputStream(path)) { out.write(data); - } finally { - out.close(); } } private static String[] listZipEntries(byte[] zipData) throws IOException { - final List<String> l = new ArrayList<String>(); - final ZipInputStream in = new ZipInputStream( // - new ByteArrayInputStream(zipData)); + List<String> l = new ArrayList<>(); + try (ZipInputStream in = new ZipInputStream( + new ByteArrayInputStream(zipData))) { + ZipEntry e; + while ((e = in.getNextEntry()) != null) + l.add(e.getName()); + } + return l.toArray(new String[0]); + } - ZipEntry e; - while ((e = in.getNextEntry()) != null) - l.add(e.getName()); - in.close(); - return l.toArray(new String[l.size()]); - } - - private static Future<Object> writeAsync(final OutputStream stream, final byte[] data) { - final ExecutorService executor = Executors.newSingleThreadExecutor(); - - return executor.submit(new Callable<Object>() { // - public Object call() throws IOException { - try { - stream.write(data); - return null; - } finally { - stream.close(); - } + private static Future<Object> writeAsync(OutputStream stream, byte[] data) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + + return executor.submit(() -> { + try { + stream.write(data); + return null; + } finally { + stream.close(); } }); } private String[] listTarEntries(byte[] tarData) throws Exception { - final List<String> l = new ArrayList<String>(); - final Process proc = spawnAssumingCommandPresent("tar", "tf", "-"); - final BufferedReader reader = readFromProcess(proc); - final OutputStream out = proc.getOutputStream(); - - // Dump tarball to tar stdin in background - final Future<?> writing = writeAsync(out, tarData); - - try { - String line; - while ((line = reader.readLine()) != null) - l.add(line); - - return l.toArray(new String[l.size()]); - } finally { - writing.get(); - reader.close(); - proc.destroy(); + List<String> l = new ArrayList<>(); + Process proc = spawnAssumingCommandPresent("tar", "tf", "-"); + try (BufferedReader reader = readFromProcess(proc)) { + OutputStream out = proc.getOutputStream(); + + // Dump tarball to tar stdin in background + Future<?> writing = writeAsync(out, tarData); + + try { + String line; + while ((line = reader.readLine()) != null) + l.add(line); + + return l.toArray(new String[0]); + } finally { + writing.get(); + proc.destroy(); + } } } - private static String[] zipEntryContent(byte[] zipData, String path) // + private static String[] zipEntryContent(byte[] zipData, String path) throws IOException { - final ZipInputStream in = new ZipInputStream( // + ZipInputStream in = new ZipInputStream( new ByteArrayInputStream(zipData)); ZipEntry e; while ((e = in.getNextEntry()) != null) { @@ -752,37 +707,37 @@ public class ArchiveTest extends CLIRepositoryTestCase { continue; // found! - final List<String> l = new ArrayList<String>(); - final BufferedReader reader = new BufferedReader( // - new InputStreamReader(in, "UTF-8")); + List<String> l = new ArrayList<>(); + BufferedReader reader = new BufferedReader( + new InputStreamReader(in, UTF_8)); String line; while ((line = reader.readLine()) != null) l.add(line); - return l.toArray(new String[l.size()]); + return l.toArray(new String[0]); } // not found return null; } - private String[] tarEntryContent(byte[] tarData, String path) // + private String[] tarEntryContent(byte[] tarData, String path) throws Exception { - final List<String> l = new ArrayList<String>(); - final Process proc = spawnAssumingCommandPresent("tar", "Oxf", "-", path); - final BufferedReader reader = readFromProcess(proc); - final OutputStream out = proc.getOutputStream(); - final Future<?> writing = writeAsync(out, tarData); - - try { - String line; - while ((line = reader.readLine()) != null) - l.add(line); - - return l.toArray(new String[l.size()]); - } finally { - writing.get(); - reader.close(); - proc.destroy(); + List<String> l = new ArrayList<>(); + Process proc = spawnAssumingCommandPresent("tar", "Oxf", "-", path); + try (BufferedReader reader = readFromProcess(proc)) { + OutputStream out = proc.getOutputStream(); + Future<?> writing = writeAsync(out, tarData); + + try { + String line; + while ((line = reader.readLine()) != null) + l.add(line); + + return l.toArray(new String[0]); + } finally { + writing.get(); + proc.destroy(); + } } } } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BlameTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BlameTest.java new file mode 100644 index 0000000000..ea30433583 --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BlameTest.java @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2019 Thomas Wolf <thomas.wolf@paranor.ch> and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.pgm; + +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.MergeResult; +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.PersonIdent; +import org.eclipse.jgit.revwalk.RevCommit; +import org.junit.Test; + +public class BlameTest extends CLIRepositoryTestCase { + + @Test + public void testBlameNoHead() throws Exception { + try (Git git = new Git(db)) { + writeTrashFile("inIndex.txt", "index"); + git.add().addFilepattern("inIndex.txt").call(); + } + assertThrows("no such ref: HEAD", Die.class, + () -> execute("git blame inIndex.txt")); + } + + @Test + public void testBlameCommitted() throws Exception { + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); + writeTrashFile("committed.txt", "committed"); + git.add().addFilepattern("committed.txt").call(); + git.commit().setMessage("commit").call(); + } + assertStringArrayEquals( + "1ad3399c (GIT_COMMITTER_NAME 2009-08-15 20:12:58 -0330 1) committed", + execute("git blame committed.txt")); + } + + @Test + public void testBlameStaged() throws Exception { + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); + writeTrashFile("inIndex.txt", "index"); + git.add().addFilepattern("inIndex.txt").call(); + } + assertStringArrayEquals( + "00000000 (Not Committed Yet 2009-08-15 20:12:58 -0330 1) index", + execute("git blame inIndex.txt")); + } + + @Test + public void testBlameUnstaged() throws Exception { + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); + } + writeTrashFile("onlyInWorkingTree.txt", "not in repo"); + assertThrows("no such path 'onlyInWorkingTree.txt' in HEAD", Die.class, + () -> execute("git blame onlyInWorkingTree.txt")); + } + + @Test + public void testBlameNonExisting() throws Exception { + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); + } + assertThrows("no such path 'does_not_exist.txt' in HEAD", Die.class, + () -> execute("git blame does_not_exist.txt")); + } + + @Test + public void testBlameNonExistingInSubdir() throws Exception { + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); + } + assertThrows("no such path 'sub/does_not_exist.txt' in HEAD", Die.class, + () -> execute("git blame sub/does_not_exist.txt")); + } + + @Test + public void testBlameMergeConflict() throws Exception { + try (Git git = new Git(db)) { + writeTrashFile("file", "Origin\n"); + git.add().addFilepattern("file").call(); + git.commit().setMessage("initial commit").call(); + git.checkout().setCreateBranch(true) + .setName("side").call(); + writeTrashFile("file", + "Conflicting change from side branch\n"); + git.add().addFilepattern("file").call(); + RevCommit side = git.commit().setMessage("side commit") + .setCommitter(new PersonIdent("gitter", "")).call(); + git.checkout().setName(Constants.MASTER).call(); + writeTrashFile("file", "Change on master branch\n"); + git.add().addFilepattern("file").call(); + git.commit().setMessage("Commit conflict on master") + .setCommitter(new PersonIdent("gitter", "")).call(); + MergeResult result = git.merge() + .include("side", side).call(); + assertTrue("Expected conflict on 'file'", + result.getConflicts().containsKey("file")); + } + String[] expected = { + "00000000 (Not Committed Yet 2009-08-15 20:12:58 -0330 1) <<<<<<< HEAD", + "0f5b671c (gitter 2009-08-15 20:12:58 -0330 2) Change on master branch", + "00000000 (Not Committed Yet 2009-08-15 20:12:58 -0330 3) =======", + "ae78cff6 (gitter 2009-08-15 20:12:58 -0330 4) Conflicting change from side branch", + "00000000 (Not Committed Yet 2009-08-15 20:12:58 -0330 5) >>>>>>> side" }; + assertArrayOfLinesEquals(expected, execute("git blame file")); + } +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BranchTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BranchTest.java index 4200cd05cf..cc3a8a0ffe 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BranchTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BranchTest.java @@ -1,53 +1,26 @@ /* - * Copyright (C) 2012, 2014 IBM Corporation and others. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2012, 2014 IBM Corporation and others. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.lib.CLIRepositoryTestCase; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.RefUpdate; +import org.eclipse.jgit.pgm.internal.CLIText; import org.eclipse.jgit.revwalk.RevCommit; import org.junit.Before; import org.junit.Test; @@ -57,13 +30,25 @@ public class BranchTest extends CLIRepositoryTestCase { @Before public void setUp() throws Exception { super.setUp(); - new Git(db).commit().setMessage("initial commit").call(); + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); + } + } + + @Test + public void testHelpAfterDelete() throws Exception { + String err = toString(executeUnchecked("git branch -d")); + String help = toString(executeUnchecked("git branch -h")); + String errAndHelp = toString(executeUnchecked("git branch -d -h")); + assertEquals(CLIText.fatalError(CLIText.get().branchNameRequired), err); + assertEquals(toString(err, help), errAndHelp); } @Test public void testList() throws Exception { + assertEquals("* master", toString(execute("git branch"))); assertEquals("* master 6fd41be initial commit", - execute("git branch -v")[0]); + toString(execute("git branch -v"))); } @Test @@ -71,24 +56,188 @@ public class BranchTest extends CLIRepositoryTestCase { RefUpdate updateRef = db.updateRef(Constants.HEAD, true); updateRef.setNewObjectId(db.resolve("6fd41be")); updateRef.update(); - assertEquals("* (no branch) 6fd41be initial commit", - execute("git branch -v")[0]); + assertEquals( + toString("* (no branch) 6fd41be initial commit", + "master 6fd41be initial commit"), + toString(execute("git branch -v"))); } @Test public void testListContains() throws Exception { - new Git(db).branchCreate().setName("initial").call(); - RevCommit second = new Git(db).commit().setMessage("second commit") - .call(); - assertArrayOfLinesEquals(new String[] { " initial", "* master", "" }, - execute("git branch --contains 6fd41be")); - assertArrayOfLinesEquals(new String[] { "* master", "" }, - execute("git branch --contains " + second.name())); + try (Git git = new Git(db)) { + git.branchCreate().setName("initial").call(); + RevCommit second = git.commit().setMessage("second commit") + .call(); + assertEquals(toString(" initial", "* master"), + toString(execute("git branch --contains 6fd41be"))); + assertEquals("* master", + toString(execute("git branch --contains " + second.name()))); + } } @Test public void testExistingBranch() throws Exception { assertEquals("fatal: A branch named 'master' already exists.", - execute("git branch master")[0]); + toString(executeUnchecked("git branch master"))); + } + + @Test + public void testRenameSingleArg() throws Exception { + try { + toString(execute("git branch -m")); + fail("Must die"); + } catch (Die e) { + // expected, requires argument + } + String result = toString(execute("git branch -m slave")); + assertEquals("", result); + result = toString(execute("git branch -a")); + assertEquals("* slave", result); + } + + @Test + public void testRenameTwoArgs() throws Exception { + String result = toString(execute("git branch -m master slave")); + assertEquals("", result); + result = toString(execute("git branch -a")); + assertEquals("* slave", result); + } + + @Test + public void testCreate() throws Exception { + try { + toString(execute("git branch a b")); + fail("Must die"); + } catch (Die e) { + // expected, too many arguments + } + String result = toString(execute("git branch second")); + assertEquals("", result); + result = toString(execute("git branch")); + assertEquals(toString("* master", "second"), result); + result = toString(execute("git branch -v")); + assertEquals(toString("* master 6fd41be initial commit", + "second 6fd41be initial commit"), result); + } + + @Test + public void testDelete() throws Exception { + try { + toString(execute("git branch -d")); + fail("Must die"); + } catch (Die e) { + // expected, requires argument + } + String result = toString(execute("git branch second")); + assertEquals("", result); + result = toString(execute("git branch -d second")); + assertEquals("", result); + result = toString(execute("git branch")); + assertEquals("* master", result); + } + + @Test + public void testDeleteMultiple() throws Exception { + String result = toString(execute("git branch second", + "git branch third", "git branch fourth")); + assertEquals("", result); + result = toString(execute("git branch -d second third fourth")); + assertEquals("", result); + result = toString(execute("git branch")); + assertEquals("* master", result); + } + + @Test + public void testDeleteForce() throws Exception { + try { + toString(execute("git branch -D")); + fail("Must die"); + } catch (Die e) { + // expected, requires argument + } + String result = toString(execute("git branch second")); + assertEquals("", result); + result = toString(execute("git checkout second")); + assertEquals("Switched to branch 'second'", result); + + File a = writeTrashFile("a", "a"); + assertTrue(a.exists()); + execute("git add a", "git commit -m 'added a'"); + + result = toString(execute("git checkout master")); + assertEquals("Switched to branch 'master'", result); + + result = toString(execute("git branch")); + assertEquals(toString("* master", "second"), result); + + try { + toString(execute("git branch -d second")); + fail("Must die"); + } catch (Die e) { + // expected, the current HEAD is on second and not merged to master + } + result = toString(execute("git branch -D second")); + assertEquals("", result); + + result = toString(execute("git branch")); + assertEquals("* master", result); + } + + @Test + public void testDeleteForceMultiple() throws Exception { + String result = toString(execute("git branch second", + "git branch third", "git branch fourth")); + + assertEquals("", result); + result = toString(execute("git checkout second")); + assertEquals("Switched to branch 'second'", result); + + File a = writeTrashFile("a", "a"); + assertTrue(a.exists()); + execute("git add a", "git commit -m 'added a'"); + + result = toString(execute("git checkout master")); + assertEquals("Switched to branch 'master'", result); + + result = toString(execute("git branch")); + assertEquals(toString("fourth", "* master", "second", "third"), result); + + try { + toString(execute("git branch -d second third fourth")); + fail("Must die"); + } catch (Die e) { + // expected, the current HEAD is on second and not merged to master + } + result = toString(execute("git branch")); + assertEquals(toString("fourth", "* master", "second", "third"), result); + + result = toString(execute("git branch -D second third fourth")); + assertEquals("", result); + + result = toString(execute("git branch")); + assertEquals("* master", result); + } + + @Test + public void testCreateFromOldCommit() throws Exception { + File a = writeTrashFile("a", "a"); + assertTrue(a.exists()); + execute("git add a", "git commit -m 'added a'"); + File b = writeTrashFile("b", "b"); + assertTrue(b.exists()); + execute("git add b", "git commit -m 'added b'"); + String result = toString(execute("git log -n 1 --reverse")); + String firstCommitId = result.substring("commit ".length(), + result.indexOf('\n')); + + result = toString(execute("git branch -f second " + firstCommitId)); + assertEquals("", result); + + result = toString(execute("git branch")); + assertEquals(toString("* master", "second"), result); + + result = toString(execute("git checkout second")); + assertEquals("Switched to branch 'second'", result); + assertFalse(b.exists()); } } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CLIGitCommandTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CLIGitCommandTest.java index 24788a47bd..b499576dca 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CLIGitCommandTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CLIGitCommandTest.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2011-2012, IBM Corporation and others. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2011-2012, IBM Corporation and others. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; @@ -51,9 +18,11 @@ public class CLIGitCommandTest { @Test public void testSplit() throws Exception { + assertArrayEquals(new String[0], split("")); assertArrayEquals(new String[] { "a" }, split("a")); assertArrayEquals(new String[] { "a", "b" }, split("a b")); assertArrayEquals(new String[] { "a", "b c" }, split("a 'b c'")); assertArrayEquals(new String[] { "a", "b c" }, split("a \"b c\"")); + assertArrayEquals(new String[] { "a", "b\\c" }, split("a \"b\\c\"")); } } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java index cef9b9e1a6..d533829d52 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java @@ -1,56 +1,31 @@ /* - * Copyright (C) 2012, IBM Corporation - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2012, IBM Corporation and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; import java.util.List; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.CheckoutConflictException; import org.eclipse.jgit.diff.DiffEntry; +import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase; import org.eclipse.jgit.lib.CLIRepositoryTestCase; import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.Ref; @@ -58,91 +33,138 @@ import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.treewalk.FileTreeIterator; import org.eclipse.jgit.treewalk.FileTreeIterator.FileEntry; import org.eclipse.jgit.treewalk.TreeWalk; +import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.FileUtils; -import org.junit.Assert; +import org.junit.Assume; import org.junit.Test; public class CheckoutTest extends CLIRepositoryTestCase { + /** + * Executes specified git command (with arguments), captures exception and + * returns its message as an array of lines. Throws an AssertionError if no + * exception is thrown. + * + * @param command + * a valid git command line, e.g. "git branch -h" + * @return message contained within the exception + */ + private String[] executeExpectingException(String command) { + try { + execute(command); + throw new AssertionError("Expected Die"); + } catch (Exception e) { + return e.getMessage().split(System.lineSeparator()); + } + } @Test public void testCheckoutSelf() throws Exception { - new Git(db).commit().setMessage("initial commit").call(); + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); - assertEquals("Already on 'master'", execute("git checkout master")); + assertStringArrayEquals("Already on 'master'", + execute("git checkout master")); + } } @Test public void testCheckoutBranch() throws Exception { - new Git(db).commit().setMessage("initial commit").call(); - new Git(db).branchCreate().setName("side").call(); + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); + git.branchCreate().setName("side").call(); - assertEquals("Switched to branch 'side'", execute("git checkout side")); + assertStringArrayEquals("Switched to branch 'side'", + execute("git checkout side")); + } } @Test public void testCheckoutNewBranch() throws Exception { - new Git(db).commit().setMessage("initial commit").call(); + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); - assertEquals("Switched to a new branch 'side'", - execute("git checkout -b side")); + assertStringArrayEquals("Switched to a new branch 'side'", + execute("git checkout -b side")); + } } @Test public void testCheckoutNonExistingBranch() throws Exception { - assertEquals( + assertStringArrayEquals( "error: pathspec 'side' did not match any file(s) known to git.", - execute("git checkout side")); + executeExpectingException("git checkout side")); + } + + @Test + public void testCheckoutWithNoRef() throws Exception { + assertStringArrayEquals( + "a valid ref is expected", + executeExpectingException("git checkout")); + } + + @Test + public void testCheckoutWithInvalidRef() throws Exception { + assertStringArrayEquals( + ".feature is not a valid ref name", + executeExpectingException("git checkout .feature")); } @Test public void testCheckoutNewBranchThatAlreadyExists() throws Exception { - new Git(db).commit().setMessage("initial commit").call(); + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); - assertEquals("fatal: A branch named 'master' already exists.", - execute("git checkout -b master")); + assertStringArrayEquals( + "fatal: A branch named 'master' already exists.", + executeUnchecked("git checkout -b master")); + } } @Test public void testCheckoutNewBranchOnBranchToBeBorn() throws Exception { - assertEquals("fatal: You are on a branch yet to be born", - execute("git checkout -b side")); + assertStringArrayEquals("fatal: You are on a branch yet to be born", + executeUnchecked("git checkout -b side")); } @Test public void testCheckoutUnresolvedHead() throws Exception { - assertEquals( + assertStringArrayEquals( "error: pathspec 'HEAD' did not match any file(s) known to git.", - execute("git checkout HEAD")); + executeExpectingException("git checkout HEAD")); } @Test public void testCheckoutHead() throws Exception { - new Git(db).commit().setMessage("initial commit").call(); + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); - assertEquals("", execute("git checkout HEAD")); + assertStringArrayEquals("", execute("git checkout HEAD")); + } } @Test public void testCheckoutExistingBranchWithConflict() throws Exception { - Git git = new Git(db); - writeTrashFile("a", "Hello world a"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("commit file a").call(); - git.branchCreate().setName("branch_1").call(); - git.rm().addFilepattern("a").call(); - FileUtils.mkdirs(new File(db.getWorkTree(), "a")); - writeTrashFile("a/b", "Hello world b"); - git.add().addFilepattern("a/b").call(); - git.commit().setMessage("commit folder a").call(); - git.rm().addFilepattern("a").call(); - writeTrashFile("a", "New Hello world a"); - git.add().addFilepattern(".").call(); - - String[] execute = execute("git checkout branch_1"); - Assert.assertEquals( - "error: Your local changes to the following files would be overwritten by checkout:", - execute[0]); - Assert.assertEquals("\ta", execute[1]); + try (Git git = new Git(db)) { + writeTrashFile("a", "Hello world a"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("commit file a").call(); + git.branchCreate().setName("branch_1").call(); + git.rm().addFilepattern("a").call(); + FileUtils.mkdirs(new File(db.getWorkTree(), "a")); + writeTrashFile("a/b", "Hello world b"); + git.add().addFilepattern("a/b").call(); + git.commit().setMessage("commit folder a").call(); + git.rm().addFilepattern("a").call(); + writeTrashFile("a", "New Hello world a"); + git.add().addFilepattern(".").call(); + + String[] execute = executeExpectingException( + "git checkout branch_1"); + assertEquals( + "error: Your local changes to the following files would be overwritten by checkout:", + execute[0]); + assertEquals("\ta", execute[1]); + } } /** @@ -164,40 +186,43 @@ public class CheckoutTest extends CLIRepositoryTestCase { */ @Test public void testCheckoutWithMissingWorkingTreeFile() throws Exception { - Git git = new Git(db); - File fileA = writeTrashFile("a", "Hello world a"); - writeTrashFile("b", "Hello world b"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("add files a & b").call(); - Ref branch_1 = git.branchCreate().setName("branch_1").call(); - writeTrashFile("a", "b"); - git.add().addFilepattern("a").call(); - git.commit().setMessage("modify file a").call(); - - FileEntry entry = new FileTreeIterator.FileEntry(new File( - db.getWorkTree(), "a"), db.getFS()); - assertEquals(FileMode.REGULAR_FILE, entry.getMode()); - - FileUtils.delete(fileA); - - git.checkout().setName(branch_1.getName()).call(); - - entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), - db.getFS()); - assertEquals(FileMode.REGULAR_FILE, entry.getMode()); - assertEquals("Hello world a", read(fileA)); + try (Git git = new Git(db)) { + File fileA = writeTrashFile("a", "Hello world a"); + writeTrashFile("b", "Hello world b"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("add files a & b").call(); + Ref branch_1 = git.branchCreate().setName("branch_1").call(); + writeTrashFile("a", "b"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("modify file a").call(); + + FileEntry entry = new FileTreeIterator.FileEntry(new File( + db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + + FileUtils.delete(fileA); + + git.checkout().setName(branch_1.getName()).call(); + + entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), + db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + assertEquals("Hello world a", read(fileA)); + } } @Test public void testCheckoutOrphan() throws Exception { - Git git = new Git(db); - git.commit().setMessage("initial commit").call(); - - assertEquals("Switched to a new branch 'new_branch'", - execute("git checkout --orphan new_branch")); - assertEquals("refs/heads/new_branch", db.getRef("HEAD").getTarget().getName()); - RevCommit commit = git.commit().setMessage("orphan commit").call(); - assertEquals(0, commit.getParentCount()); + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); + + assertStringArrayEquals("Switched to a new branch 'new_branch'", + execute("git checkout --orphan new_branch")); + assertEquals("refs/heads/new_branch", + db.exactRef("HEAD").getTarget().getName()); + RevCommit commit = git.commit().setMessage("orphan commit").call(); + assertEquals(0, commit.getParentCount()); + } } /** @@ -212,41 +237,46 @@ public class CheckoutTest extends CLIRepositoryTestCase { * <li>Checkout branch '1' * </ol> * <p> - * The working tree should contain 'a' with FileMode.REGULAR_FILE after the - * checkout. + * The checkout has to delete folder but the workingtree contains a dirty + * file at this path. The checkout should fail like in native git. * * @throws Exception */ @Test public void fileModeTestMissingThenFolderWithFileInWorkingTree() throws Exception { - Git git = new Git(db); - writeTrashFile("b", "Hello world b"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("add file b").call(); - Ref branch_1 = git.branchCreate().setName("branch_1").call(); - File folderA = new File(db.getWorkTree(), "a"); - FileUtils.mkdirs(folderA); - writeTrashFile("a/c", "Hello world c"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("add folder a").call(); - - FileEntry entry = new FileTreeIterator.FileEntry(new File( - db.getWorkTree(), "a"), db.getFS()); - assertEquals(FileMode.TREE, entry.getMode()); - - FileUtils.delete(folderA, FileUtils.RECURSIVE); - writeTrashFile("a", "b"); - - entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), - db.getFS()); - assertEquals(FileMode.REGULAR_FILE, entry.getMode()); - - git.checkout().setName(branch_1.getName()).call(); - - entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), - db.getFS()); - assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + try (Git git = new Git(db)) { + writeTrashFile("b", "Hello world b"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("add file b").call(); + Ref branch_1 = git.branchCreate().setName("branch_1").call(); + File folderA = new File(db.getWorkTree(), "a"); + FileUtils.mkdirs(folderA); + writeTrashFile("a/c", "Hello world c"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("add folder a").call(); + + FileEntry entry = new FileTreeIterator.FileEntry(new File( + db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.TREE, entry.getMode()); + + FileUtils.delete(folderA, FileUtils.RECURSIVE); + writeTrashFile("a", "b"); + + entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), + db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + + try { + git.checkout().setName(branch_1.getName()).call(); + fail("Don't get the expected conflict"); + } catch (CheckoutConflictException e) { + assertEquals("[a]", e.getConflictingPaths().toString()); + entry = new FileTreeIterator.FileEntry( + new File(db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + } + } } /** @@ -268,30 +298,31 @@ public class CheckoutTest extends CLIRepositoryTestCase { */ @Test public void fileModeTestFolderWithMissingInWorkingTree() throws Exception { - Git git = new Git(db); - writeTrashFile("b", "Hello world b"); - writeTrashFile("a", "b"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("add file b & file a").call(); - Ref branch_1 = git.branchCreate().setName("branch_1").call(); - git.rm().addFilepattern("a").call(); - File folderA = new File(db.getWorkTree(), "a"); - FileUtils.mkdirs(folderA); - writeTrashFile("a/c", "Hello world c"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("add folder a").call(); - - FileEntry entry = new FileTreeIterator.FileEntry(new File( - db.getWorkTree(), "a"), db.getFS()); - assertEquals(FileMode.TREE, entry.getMode()); - - FileUtils.delete(folderA, FileUtils.RECURSIVE); - - git.checkout().setName(branch_1.getName()).call(); - - entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), - db.getFS()); - assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + try (Git git = new Git(db)) { + writeTrashFile("b", "Hello world b"); + writeTrashFile("a", "b"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("add file b & file a").call(); + Ref branch_1 = git.branchCreate().setName("branch_1").call(); + git.rm().addFilepattern("a").call(); + File folderA = new File(db.getWorkTree(), "a"); + FileUtils.mkdirs(folderA); + writeTrashFile("a/c", "Hello world c"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("add folder a").call(); + + FileEntry entry = new FileTreeIterator.FileEntry(new File( + db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.TREE, entry.getMode()); + + FileUtils.delete(folderA, FileUtils.RECURSIVE); + + git.checkout().setName(branch_1.getName()).call(); + + entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), + db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + } } /** @@ -313,32 +344,33 @@ public class CheckoutTest extends CLIRepositoryTestCase { */ @Test public void fileModeTestMissingWithFolderInWorkingTree() throws Exception { - Git git = new Git(db); - writeTrashFile("b", "Hello world b"); - writeTrashFile("a", "b"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("add file b & file a").call(); - Ref branch_1 = git.branchCreate().setName("branch_1").call(); - git.rm().addFilepattern("a").call(); - git.commit().setMessage("delete file a").call(); - - FileUtils.mkdirs(new File(db.getWorkTree(), "a")); - writeTrashFile("a/c", "Hello world c"); - - FileEntry entry = new FileTreeIterator.FileEntry(new File( - db.getWorkTree(), "a"), db.getFS()); - assertEquals(FileMode.TREE, entry.getMode()); - - CheckoutConflictException exception = null; - try { - git.checkout().setName(branch_1.getName()).call(); - } catch (CheckoutConflictException e) { - exception = e; + try (Git git = new Git(db)) { + writeTrashFile("b", "Hello world b"); + writeTrashFile("a", "b"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("add file b & file a").call(); + Ref branch_1 = git.branchCreate().setName("branch_1").call(); + git.rm().addFilepattern("a").call(); + git.commit().setMessage("delete file a").call(); + + FileUtils.mkdirs(new File(db.getWorkTree(), "a")); + writeTrashFile("a/c", "Hello world c"); + + FileEntry entry = new FileTreeIterator.FileEntry(new File( + db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.TREE, entry.getMode()); + + CheckoutConflictException exception = null; + try { + git.checkout().setName(branch_1.getName()).call(); + } catch (CheckoutConflictException e) { + exception = e; + } + assertNotNull(exception); + assertEquals(2, exception.getConflictingPaths().size()); + assertEquals("a", exception.getConflictingPaths().get(0)); + assertEquals("a/c", exception.getConflictingPaths().get(1)); } - assertNotNull(exception); - assertEquals(2, exception.getConflictingPaths().size()); - assertEquals("a", exception.getConflictingPaths().get(0)); - assertEquals("a/c", exception.getConflictingPaths().get(1)); } /** @@ -360,40 +392,41 @@ public class CheckoutTest extends CLIRepositoryTestCase { @Test public void fileModeTestFolderThenMissingWithFileInWorkingTree() throws Exception { - Git git = new Git(db); - FileUtils.mkdirs(new File(db.getWorkTree(), "a")); - writeTrashFile("a/c", "Hello world c"); - writeTrashFile("b", "Hello world b"); - git.add().addFilepattern(".").call(); - RevCommit commit1 = git.commit().setMessage("add folder a & file b") - .call(); - Ref branch_1 = git.branchCreate().setName("branch_1").call(); - git.rm().addFilepattern("a").call(); - RevCommit commit2 = git.commit().setMessage("delete folder a").call(); - - TreeWalk tw = new TreeWalk(db); - tw.addTree(commit1.getTree()); - tw.addTree(commit2.getTree()); - List<DiffEntry> scan = DiffEntry.scan(tw); - assertEquals(1, scan.size()); - assertEquals(FileMode.MISSING, scan.get(0).getNewMode()); - assertEquals(FileMode.TREE, scan.get(0).getOldMode()); - - writeTrashFile("a", "b"); - - FileEntry entry = new FileTreeIterator.FileEntry(new File( - db.getWorkTree(), "a"), db.getFS()); - assertEquals(FileMode.REGULAR_FILE, entry.getMode()); - - CheckoutConflictException exception = null; - try { - git.checkout().setName(branch_1.getName()).call(); - } catch (CheckoutConflictException e) { - exception = e; + try (Git git = new Git(db)) { + FileUtils.mkdirs(new File(db.getWorkTree(), "a")); + writeTrashFile("a/c", "Hello world c"); + writeTrashFile("b", "Hello world b"); + git.add().addFilepattern(".").call(); + RevCommit commit1 = git.commit().setMessage("add folder a & file b") + .call(); + Ref branch_1 = git.branchCreate().setName("branch_1").call(); + git.rm().addFilepattern("a").call(); + RevCommit commit2 = git.commit().setMessage("delete folder a").call(); + + TreeWalk tw = new TreeWalk(db); + tw.addTree(commit1.getTree()); + tw.addTree(commit2.getTree()); + List<DiffEntry> scan = DiffEntry.scan(tw); + assertEquals(1, scan.size()); + assertEquals(FileMode.MISSING, scan.get(0).getNewMode()); + assertEquals(FileMode.TREE, scan.get(0).getOldMode()); + + writeTrashFile("a", "b"); + + FileEntry entry = new FileTreeIterator.FileEntry(new File( + db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + + CheckoutConflictException exception = null; + try { + git.checkout().setName(branch_1.getName()).call(); + } catch (CheckoutConflictException e) { + exception = e; + } + assertNotNull(exception); + assertEquals(1, exception.getConflictingPaths().size()); + assertEquals("a", exception.getConflictingPaths().get(0)); } - assertNotNull(exception); - assertEquals(1, exception.getConflictingPaths().size()); - assertEquals("a", exception.getConflictingPaths().get(0)); } /** @@ -416,30 +449,31 @@ public class CheckoutTest extends CLIRepositoryTestCase { @Test public void fileModeTestFolderThenFileWithMissingInWorkingTree() throws Exception { - Git git = new Git(db); - FileUtils.mkdirs(new File(db.getWorkTree(), "a")); - writeTrashFile("a/c", "Hello world c"); - writeTrashFile("b", "Hello world b"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("add folder a & file b").call(); - Ref branch_1 = git.branchCreate().setName("branch_1").call(); - git.rm().addFilepattern("a").call(); - File fileA = new File(db.getWorkTree(), "a"); - writeTrashFile("a", "b"); - git.add().addFilepattern("a").call(); - git.commit().setMessage("add file a").call(); - - FileEntry entry = new FileTreeIterator.FileEntry(new File( - db.getWorkTree(), "a"), db.getFS()); - assertEquals(FileMode.REGULAR_FILE, entry.getMode()); - - FileUtils.delete(fileA); - - git.checkout().setName(branch_1.getName()).call(); - - entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), - db.getFS()); - assertEquals(FileMode.TREE, entry.getMode()); + try (Git git = new Git(db)) { + FileUtils.mkdirs(new File(db.getWorkTree(), "a")); + writeTrashFile("a/c", "Hello world c"); + writeTrashFile("b", "Hello world b"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("add folder a & file b").call(); + Ref branch_1 = git.branchCreate().setName("branch_1").call(); + git.rm().addFilepattern("a").call(); + File fileA = new File(db.getWorkTree(), "a"); + writeTrashFile("a", "b"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("add file a").call(); + + FileEntry entry = new FileTreeIterator.FileEntry(new File( + db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + + FileUtils.delete(fileA); + + git.checkout().setName(branch_1.getName()).call(); + + entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), + db.getFS()); + assertEquals(FileMode.TREE, entry.getMode()); + } } /** @@ -450,7 +484,8 @@ public class CheckoutTest extends CLIRepositoryTestCase { * <li>Create branch '1' * <li>Modify file 'a' * <li>Commit - * <li>Delete file 'a' & replace by folder 'a' in the working tree & index + * <li>Delete file 'a' and replace by folder 'a' in the working tree and + * index * <li>Checkout branch '1' * </ol> * <p> @@ -460,38 +495,39 @@ public class CheckoutTest extends CLIRepositoryTestCase { */ @Test public void fileModeTestFileThenFileWithFolderInIndex() throws Exception { - Git git = new Git(db); - writeTrashFile("a", "Hello world a"); - writeTrashFile("b", "Hello world b"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("add files a & b").call(); - Ref branch_1 = git.branchCreate().setName("branch_1").call(); - writeTrashFile("a", "b"); - git.add().addFilepattern("a").call(); - git.commit().setMessage("add file a").call(); - - FileEntry entry = new FileTreeIterator.FileEntry(new File( - db.getWorkTree(), "a"), db.getFS()); - assertEquals(FileMode.REGULAR_FILE, entry.getMode()); - - git.rm().addFilepattern("a").call(); - FileUtils.mkdirs(new File(db.getWorkTree(), "a")); - writeTrashFile("a/c", "Hello world c"); - git.add().addFilepattern(".").call(); - - entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), - db.getFS()); - assertEquals(FileMode.TREE, entry.getMode()); - - CheckoutConflictException exception = null; - try { - git.checkout().setName(branch_1.getName()).call(); - } catch (CheckoutConflictException e) { - exception = e; + try (Git git = new Git(db)) { + writeTrashFile("a", "Hello world a"); + writeTrashFile("b", "Hello world b"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("add files a & b").call(); + Ref branch_1 = git.branchCreate().setName("branch_1").call(); + writeTrashFile("a", "b"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("add file a").call(); + + FileEntry entry = new FileTreeIterator.FileEntry(new File( + db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + + git.rm().addFilepattern("a").call(); + FileUtils.mkdirs(new File(db.getWorkTree(), "a")); + writeTrashFile("a/c", "Hello world c"); + git.add().addFilepattern(".").call(); + + entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), + db.getFS()); + assertEquals(FileMode.TREE, entry.getMode()); + + CheckoutConflictException exception = null; + try { + git.checkout().setName(branch_1.getName()).call(); + } catch (CheckoutConflictException e) { + exception = e; + } + assertNotNull(exception); + assertEquals(1, exception.getConflictingPaths().size()); + assertEquals("a", exception.getConflictingPaths().get(0)); } - assertNotNull(exception); - assertEquals(1, exception.getConflictingPaths().size()); - assertEquals("a", exception.getConflictingPaths().get(0)); } /** @@ -502,7 +538,8 @@ public class CheckoutTest extends CLIRepositoryTestCase { * <li>Create branch '1' * <li>Modify file 'a' * <li>Commit - * <li>Delete file 'a' & replace by folder 'a' in the working tree & index + * <li>Delete file 'a' and replace by folder 'a' in the working tree and + * index * <li>Checkout branch '1' * </ol> * <p> @@ -513,78 +550,135 @@ public class CheckoutTest extends CLIRepositoryTestCase { */ @Test public void fileModeTestFileWithFolderInIndex() throws Exception { - Git git = new Git(db); - writeTrashFile("b", "Hello world b"); - writeTrashFile("a", "b"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("add file b & file a").call(); - Ref branch_1 = git.branchCreate().setName("branch_1").call(); - git.rm().addFilepattern("a").call(); - writeTrashFile("a", "Hello world a"); - git.add().addFilepattern("a").call(); - git.commit().setMessage("add file a").call(); - - FileEntry entry = new FileTreeIterator.FileEntry(new File( - db.getWorkTree(), "a"), db.getFS()); - assertEquals(FileMode.REGULAR_FILE, entry.getMode()); - - git.rm().addFilepattern("a").call(); - FileUtils.mkdirs(new File(db.getWorkTree(), "a")); - writeTrashFile("a/c", "Hello world c"); - git.add().addFilepattern(".").call(); - - entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), - db.getFS()); - assertEquals(FileMode.TREE, entry.getMode()); - - CheckoutConflictException exception = null; - try { - git.checkout().setName(branch_1.getName()).call(); - } catch (CheckoutConflictException e) { - exception = e; + try (Git git = new Git(db)) { + writeTrashFile("b", "Hello world b"); + writeTrashFile("a", "b"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("add file b & file a").call(); + Ref branch_1 = git.branchCreate().setName("branch_1").call(); + git.rm().addFilepattern("a").call(); + writeTrashFile("a", "Hello world a"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("add file a").call(); + + FileEntry entry = new FileTreeIterator.FileEntry(new File( + db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + + git.rm().addFilepattern("a").call(); + FileUtils.mkdirs(new File(db.getWorkTree(), "a")); + writeTrashFile("a/c", "Hello world c"); + git.add().addFilepattern(".").call(); + + entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), + db.getFS()); + assertEquals(FileMode.TREE, entry.getMode()); + + CheckoutConflictException exception = null; + try { + git.checkout().setName(branch_1.getName()).call(); + } catch (CheckoutConflictException e) { + exception = e; + } + assertNotNull(exception); + assertEquals(1, exception.getConflictingPaths().size()); + assertEquals("a", exception.getConflictingPaths().get(0)); + + // TODO: ideally we'd like to get two paths from this exception + // assertEquals(2, exception.getConflictingPaths().size()); + // assertEquals("a", exception.getConflictingPaths().get(0)); + // assertEquals("a/c", exception.getConflictingPaths().get(1)); } - assertNotNull(exception); - assertEquals(1, exception.getConflictingPaths().size()); - assertEquals("a", exception.getConflictingPaths().get(0)); - - // TODO: ideally we'd like to get two paths from this exception - // assertEquals(2, exception.getConflictingPaths().size()); - // assertEquals("a", exception.getConflictingPaths().get(0)); - // assertEquals("a/c", exception.getConflictingPaths().get(1)); } - static private void assertEquals(Object expected, Object actual) { - Assert.assertEquals(expected, actual); + @Test + public void testCheckoutPath() throws Exception { + try (Git git = new Git(db)) { + writeTrashFile("a", "Hello world a"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("commit file a").call(); + git.branchCreate().setName("branch_1").call(); + git.checkout().setName("branch_1").call(); + File b = writeTrashFile("b", "Hello world b"); + git.add().addFilepattern("b").call(); + git.commit().setMessage("commit file b").call(); + File a = writeTrashFile("a", "New Hello world a"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("modified a").call(); + assertArrayEquals(new String[] { "" }, + execute("git checkout HEAD~2 -- a")); + assertEquals("Hello world a", read(a)); + assertArrayEquals(new String[] { "* branch_1", " master", "" }, + execute("git branch")); + assertEquals("Hello world b", read(b)); + } } - static private void assertEquals(String expected, String[] actual) { - // if there is more than one line, ignore last one if empty - Assert.assertEquals( - 1, - actual.length > 1 && actual[actual.length - 1].equals("") ? actual.length - 1 - : actual.length); - Assert.assertEquals(expected, actual[0]); + @Test + public void testCheckoutAllPaths() throws Exception { + try (Git git = new Git(db)) { + writeTrashFile("a", "Hello world a"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("commit file a").call(); + git.branchCreate().setName("branch_1").call(); + git.checkout().setName("branch_1").call(); + File b = writeTrashFile("b", "Hello world b"); + git.add().addFilepattern("b").call(); + git.commit().setMessage("commit file b").call(); + File a = writeTrashFile("a", "New Hello world a"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("modified a").call(); + assertArrayEquals(new String[] { "" }, + execute("git checkout HEAD~2 -- .")); + assertEquals("Hello world a", read(a)); + assertArrayEquals(new String[] { "* branch_1", " master", "" }, + execute("git branch")); + assertEquals("Hello world b", read(b)); + } } @Test - public void testCheckoutPath() throws Exception { - Git git = new Git(db); - writeTrashFile("a", "Hello world a"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("commit file a").call(); - git.branchCreate().setName("branch_1").call(); - git.checkout().setName("branch_1").call(); - File b = writeTrashFile("b", "Hello world b"); - git.add().addFilepattern("b").call(); - git.commit().setMessage("commit file b").call(); - File a = writeTrashFile("a", "New Hello world a"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("modified a").call(); - assertArrayEquals(new String[] { "" }, - execute("git checkout HEAD~2 -- a")); - assertEquals("Hello world a", read(a)); - assertArrayEquals(new String[] { "* branch_1", " master", "" }, - execute("git branch")); - assertEquals("Hello world b", read(b)); + public void testCheckoutSingleFile() throws Exception { + try (Git git = new Git(db)) { + File a = writeTrashFile("a", "file a"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("commit file a").call(); + writeTrashFile("a", "b"); + assertEquals("b", read(a)); + assertEquals("[]", Arrays.toString(execute("git checkout -- a"))); + assertEquals("file a", read(a)); + } + } + + @Test + public void testCheckoutLink() throws Exception { + Assume.assumeTrue(FS.DETECTED.supportsSymlinks()); + try (Git git = new Git(db)) { + Path path = writeLink("a", "link_a"); + assertTrue(Files.isSymbolicLink(path)); + git.add().addFilepattern(".").call(); + git.commit().setMessage("commit link a").call(); + deleteTrashFile("a"); + writeTrashFile("a", "Hello world a"); + assertFalse(Files.isSymbolicLink(path)); + assertEquals("[]", Arrays.toString(execute("git checkout -- a"))); + assertEquals("link_a", FileUtils.readSymLink(path.toFile())); + assertTrue(Files.isSymbolicLink(path)); + } + } + + @Test + public void testCheckoutForce_Bug530771() throws Exception { + try (Git git = new Git(db)) { + File f = writeTrashFile("a", "Hello world"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("create a").call(); + writeTrashFile("a", "Goodbye world"); + assertEquals("[]", + Arrays.toString(execute("git checkout -f HEAD"))); + assertEquals("Hello world", read(f)); + assertEquals("[a, mode:100644, content:Hello world]", + indexState(db, LocalDiskRepositoryTestCase.CONTENT)); + } } } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CleanTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CleanTest.java new file mode 100644 index 0000000000..dbdcc5605c --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CleanTest.java @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2016, Ned Twigg <ned.twigg@diffplug.com> and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.pgm; + +import static org.eclipse.jgit.junit.JGitTestUtil.check; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.junit.Test; + +public class CleanTest extends CLIRepositoryTestCase { + @Test + public void testCleanRequiresForce() throws Exception { + try (Git git = new Git(db)) { + assertArrayOfLinesEquals( + new String[] { "Removing a", "Removing b" }, + execute("git clean")); + } catch (Die e) { + // TODO: should be "fatal: clean.requireForce defaults to true and + // neither -i, -n, nor -f given; refusing to clean" but we don't + // support -i yet. Fix this when/if we add support for -i. + assertEquals( + "fatal: clean.requireForce defaults to true and neither -n nor -f given; refusing to clean", + e.getMessage()); + } + } + + @Test + public void testCleanRequiresForceConfig() throws Exception { + try (Git git = new Git(db)) { + git.getRepository().getConfig().setBoolean("clean", null, + "requireForce", false); + assertArrayOfLinesEquals( + new String[] { "" }, + execute("git clean")); + } + } + + @Test + public void testCleanLeaveDirs() throws Exception { + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); + + writeTrashFile("dir/file", "someData"); + writeTrashFile("a", "someData"); + writeTrashFile("b", "someData"); + + // all these files should be there + assertTrue(check(db, "a")); + assertTrue(check(db, "b")); + assertTrue(check(db, "dir/file")); + + // dry run should make no change + assertArrayOfLinesEquals( + new String[] { "Removing a", "Removing b" }, + execute("git clean -n")); + assertTrue(check(db, "a")); + assertTrue(check(db, "b")); + assertTrue(check(db, "dir/file")); + + // force should make a change + assertArrayOfLinesEquals( + new String[] { "Removing a", "Removing b" }, + execute("git clean -f")); + assertFalse(check(db, "a")); + assertFalse(check(db, "b")); + assertTrue(check(db, "dir/file")); + } + } + + @Test + public void testCleanDeleteDirs() throws Exception { + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); + + writeTrashFile("dir/file", "someData"); + writeTrashFile("a", "someData"); + writeTrashFile("b", "someData"); + + // all these files should be there + assertTrue(check(db, "a")); + assertTrue(check(db, "b")); + assertTrue(check(db, "dir/file")); + + assertArrayOfLinesEquals( + new String[] { "Removing a", "Removing b", + "Removing dir/" }, + execute("git clean -d -f")); + assertFalse(check(db, "a")); + assertFalse(check(db, "b")); + assertFalse(check(db, "dir/file")); + } + } +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CloneTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CloneTest.java index fe80388c0c..c56cc6bf3c 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CloneTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CloneTest.java @@ -1,60 +1,43 @@ /* - * Copyright (C) 2014 Matthias Sohn <matthias.sohn@sap.com> - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2014 Matthias Sohn <matthias.sohn@sap.com> and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import java.io.File; +import java.time.Instant; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.junit.JGitTestUtil; import org.eclipse.jgit.junit.MockSystemReader; +import org.eclipse.jgit.junit.TestRepository; import org.eclipse.jgit.lib.CLIRepositoryTestCase; import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.RefUpdate; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.StoredConfig; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.transport.RefSpec; +import org.eclipse.jgit.transport.RemoteConfig; import org.eclipse.jgit.transport.URIish; import org.eclipse.jgit.util.SystemReader; import org.junit.Before; @@ -64,10 +47,14 @@ public class CloneTest extends CLIRepositoryTestCase { private Git git; + private TestRepository<Repository> tr; + @Override @Before public void setUp() throws Exception { super.setUp(); + + tr = new TestRepository<>(db); git = new Git(db); } @@ -78,9 +65,9 @@ public class CloneTest extends CLIRepositoryTestCase { File gitDir = db.getDirectory(); String sourceURI = gitDir.toURI().toString(); File target = createTempDirectory("target"); - StringBuilder cmd = new StringBuilder("git clone ").append(sourceURI - + " " + target.getPath()); - String[] result = execute(cmd.toString()); + String cmd = "git clone " + sourceURI + " " + + shellQuote(target.getPath()); + String[] result = execute(cmd); assertArrayEquals(new String[] { "Cloning into '" + target.getPath() + "'...", "", "" }, result); @@ -90,10 +77,65 @@ public class CloneTest extends CLIRepositoryTestCase { assertEquals("expected 1 branch", 1, branches.size()); } - private void createInitialCommit() throws Exception { + @Test + public void testCloneInitialBranch() throws Exception { + createInitialCommit(); + + File gitDir = db.getDirectory(); + String sourceURI = gitDir.toURI().toString(); + File target = createTempDirectory("target"); + String cmd = "git clone --branch master " + sourceURI + " " + + shellQuote(target.getPath()); + String[] result = execute(cmd); + assertArrayEquals(new String[] { + "Cloning into '" + target.getPath() + "'...", "", "" }, result); + + Git git2 = Git.open(target); + List<Ref> branches = git2.branchList().call(); + assertEquals("expected 1 branch", 1, branches.size()); + + Repository db2 = git2.getRepository(); + ObjectId head = db2.resolve("HEAD"); + assertNotNull(head); + assertNotEquals(ObjectId.zeroId(), head); + ObjectId master = db2.resolve("master"); + assertEquals(head, master); + } + + @Test + public void testCloneInitialBranchMissing() throws Exception { + createInitialCommit(); + + File gitDir = db.getDirectory(); + String sourceURI = gitDir.toURI().toString(); + File target = createTempDirectory("target"); + String cmd = "git clone --branch foo " + sourceURI + " " + + shellQuote(target.getPath()); + Die e = assertThrows(Die.class, () -> execute(cmd)); + assertEquals("Remote branch 'foo' not found in upstream origin", + e.getMessage()); + } + + private RevCommit createInitialCommit() throws Exception { JGitTestUtil.writeTrashFile(db, "hello.txt", "world"); git.add().addFilepattern("hello.txt").call(); - git.commit().setMessage("Initial commit").call(); + return git.commit().setMessage("Initial commit").call(); + } + + private RevCommit createSecondCommit() throws Exception { + JGitTestUtil.writeTrashFile(db, "Test.txt", "Some change"); + git.add().addFilepattern("Test.txt").call(); + return git.commit() + .setCommitter(new PersonIdent(this.committer, tr.getInstant())) + .setMessage("Second commit").call(); + } + + private RevCommit createThirdCommit() throws Exception { + JGitTestUtil.writeTrashFile(db, "change.txt", "another change"); + git.add().addFilepattern("change.txt").call(); + return git.commit() + .setCommitter(new PersonIdent(this.committer, tr.getInstant())) + .setMessage("Third commit").call(); } @Test @@ -101,9 +143,9 @@ public class CloneTest extends CLIRepositoryTestCase { File gitDir = db.getDirectory(); String sourceURI = gitDir.toURI().toString(); File target = createTempDirectory("target"); - StringBuilder cmd = new StringBuilder("git clone ").append(sourceURI - + " " + target.getPath()); - String[] result = execute(cmd.toString()); + String cmd = "git clone " + sourceURI + " " + + shellQuote(target.getPath()); + String[] result = execute(cmd); assertArrayEquals(new String[] { "Cloning into '" + target.getPath() + "'...", "warning: You appear to have cloned an empty repository.", "", @@ -125,8 +167,8 @@ public class CloneTest extends CLIRepositoryTestCase { File gitDir = db.getDirectory(); String sourceURI = gitDir.toURI().toString(); String name = new URIish(sourceURI).getHumanishName(); - StringBuilder cmd = new StringBuilder("git clone ").append(sourceURI); - String[] result = execute(cmd.toString()); + String cmd = "git clone " + sourceURI; + String[] result = execute(cmd); assertArrayEquals(new String[] { "Cloning into '" + new File(target, name).getName() + "'...", "", "" }, result); @@ -135,17 +177,18 @@ public class CloneTest extends CLIRepositoryTestCase { assertEquals("expected 1 branch", 1, branches.size()); } + @Test public void testCloneBare() throws Exception { createInitialCommit(); File gitDir = db.getDirectory(); String sourcePath = gitDir.getAbsolutePath(); - String targetPath = (new File(sourcePath)).getParentFile() + String targetPath = new File(sourcePath).getParentFile() .getParentFile().getAbsolutePath() - + "/target.git"; - StringBuilder cmd = new StringBuilder("git clone --bare ") - .append(sourcePath + " " + targetPath); - String[] result = execute(cmd.toString()); + + File.separator + "target.git"; + String cmd = "git clone --bare " + shellQuote(sourcePath) + " " + + shellQuote(targetPath); + String[] result = execute(cmd); assertArrayEquals(new String[] { "Cloning into '" + targetPath + "'...", "", "" }, result); Git git2 = Git.open(new File(targetPath)); @@ -153,4 +196,150 @@ public class CloneTest extends CLIRepositoryTestCase { assertEquals("expected 1 branch", 1, branches.size()); assertTrue("expected bare repository", git2.getRepository().isBare()); } + + @Test + public void testCloneMirror() throws Exception { + ObjectId head = createInitialCommit(); + // create a non-standard ref + RefUpdate ru = db.updateRef("refs/meta/foo/bar"); + ru.setNewObjectId(head); + ru.update(); + + File gitDir = db.getDirectory(); + String sourcePath = gitDir.getAbsolutePath(); + String targetPath = new File(sourcePath).getParentFile() + .getParentFile().getAbsolutePath() + File.separator + + "target.git"; + String cmd = "git clone --mirror " + shellQuote(sourcePath) + " " + + shellQuote(targetPath); + String[] result = execute(cmd); + assertArrayEquals( + new String[] { "Cloning into '" + targetPath + "'...", "", "" }, + result); + Git git2 = Git.open(new File(targetPath)); + List<Ref> branches = git2.branchList().call(); + assertEquals("expected 1 branch", 1, branches.size()); + assertTrue("expected bare repository", git2.getRepository().isBare()); + StoredConfig config = git2.getRepository().getConfig(); + RemoteConfig rc = new RemoteConfig(config, "origin"); + assertTrue("expected mirror configuration", rc.isMirror()); + RefSpec fetchRefSpec = rc.getFetchRefSpecs().get(0); + assertTrue("exected force udpate", fetchRefSpec.isForceUpdate()); + assertEquals("refs/*", fetchRefSpec.getSource()); + assertEquals("refs/*", fetchRefSpec.getDestination()); + assertNotNull(git2.getRepository().exactRef("refs/meta/foo/bar")); + } + + @Test + public void testDepth() throws Exception { + createInitialCommit(); + createSecondCommit(); + createThirdCommit(); + + File gitDir = db.getDirectory(); + String sourceURI = gitDir.toURI().toString(); + File target = createTempDirectory("target"); + String cmd = "git clone --depth 1 " + sourceURI + " " + + shellQuote(target.getPath()); + String[] result = execute(cmd); + assertArrayEquals(new String[] { + "Cloning into '" + target.getPath() + "'...", "", "" }, result); + + Git git2 = Git.open(target); + addRepoToClose(git2.getRepository()); + + List<RevCommit> log = StreamSupport + .stream(git2.log().all().call().spliterator(), false) + .collect(Collectors.toList()); + assertEquals(1, log.size()); + RevCommit commit = log.get(0); + assertEquals(Set.of(commit.getId()), + git2.getRepository().getObjectDatabase().getShallowCommits()); + assertEquals("Third commit", commit.getFullMessage()); + assertEquals(0, commit.getParentCount()); + } + + @Test + public void testDepth2() throws Exception { + createInitialCommit(); + createSecondCommit(); + createThirdCommit(); + + File gitDir = db.getDirectory(); + String sourceURI = gitDir.toURI().toString(); + File target = createTempDirectory("target"); + String cmd = "git clone --depth 2 " + sourceURI + " " + + shellQuote(target.getPath()); + String[] result = execute(cmd); + assertArrayEquals(new String[] { + "Cloning into '" + target.getPath() + "'...", "", "" }, result); + + Git git2 = Git.open(target); + addRepoToClose(git2.getRepository()); + + List<RevCommit> log = StreamSupport + .stream(git2.log().all().call().spliterator(), false) + .collect(Collectors.toList()); + assertEquals(2, log.size()); + assertEquals(List.of("Third commit", "Second commit"), log.stream() + .map(RevCommit::getFullMessage).collect(Collectors.toList())); + } + + @Test + public void testCloneRepositoryWithShallowSince() throws Exception { + createInitialCommit(); + tr.tick(30); + RevCommit secondCommit = createSecondCommit(); + tr.tick(45); + createThirdCommit(); + + File gitDir = db.getDirectory(); + String sourceURI = gitDir.toURI().toString(); + File target = createTempDirectory("target"); + String cmd = "git clone --shallow-since=" + + Instant.ofEpochSecond(secondCommit.getCommitTime()).toString() + + " " + sourceURI + " " + shellQuote(target.getPath()); + String[] result = execute(cmd); + assertArrayEquals(new String[] { + "Cloning into '" + target.getPath() + "'...", "", "" }, result); + + Git git2 = Git.open(target); + addRepoToClose(git2.getRepository()); + + List<RevCommit> log = StreamSupport + .stream(git2.log().all().call().spliterator(), false) + .collect(Collectors.toList()); + assertEquals(2, log.size()); + assertEquals(List.of("Third commit", "Second commit"), log.stream() + .map(RevCommit::getFullMessage).collect(Collectors.toList())); + } + + @Test + public void testCloneRepositoryWithShallowExclude() throws Exception { + final RevCommit firstCommit = createInitialCommit(); + final RevCommit secondCommit = createSecondCommit(); + createThirdCommit(); + + File gitDir = db.getDirectory(); + String sourceURI = gitDir.toURI().toString(); + File target = createTempDirectory("target"); + String cmd = "git clone --shallow-exclude=" + + firstCommit.getId().getName() + " --shallow-exclude=" + + secondCommit.getId().getName() + " " + sourceURI + " " + + shellQuote(target.getPath()); + String[] result = execute(cmd); + assertArrayEquals(new String[] { + "Cloning into '" + target.getPath() + "'...", "", "" }, result); + + Git git2 = Git.open(target); + addRepoToClose(git2.getRepository()); + + List<RevCommit> log = StreamSupport + .stream(git2.log().all().call().spliterator(), false) + .collect(Collectors.toList()); + assertEquals(1, log.size()); + assertEquals(List.of("Third commit"), log.stream() + .map(RevCommit::getFullMessage).collect(Collectors.toList())); + } + } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CommitAndLogTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CommitAndLogTest.java index ffb66ce755..da145b25c8 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CommitAndLogTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CommitAndLogTest.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2012, IBM Corporation and others. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2012, IBM Corporation and others. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CommitTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CommitTest.java new file mode 100644 index 0000000000..fc0f9387af --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CommitTest.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2015, Andrey Loskutov <loskutov@gmx.de> and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.pgm; + +import static org.junit.Assert.assertEquals; + +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.junit.Test; + +public class CommitTest extends CLIRepositoryTestCase { + + @Test + public void testCommitPath() throws Exception { + writeTrashFile("a", "a"); + writeTrashFile("b", "a"); + String result = toString(execute("git add a")); + assertEquals("", result); + + result = toString(execute("git status -- a")); + assertEquals(toString("On branch master", "Changes to be committed:", + "new file: a"), result); + + result = toString(execute("git status -- b")); + assertEquals(toString("On branch master", "Untracked files:", "b"), + result); + + result = toString(execute("git commit a -m 'added a'")); + assertEquals( + "[master 8cb3ef7e5171aaee1792df6302a5a0cd30425f7a] added a", + result); + + result = toString(execute("git status -- a")); + assertEquals("On branch master", result); + + result = toString(execute("git status -- b")); + assertEquals(toString("On branch master", "Untracked files:", "b"), + result); + } + + @Test + public void testCommitAll() throws Exception { + writeTrashFile("a", "a"); + writeTrashFile("b", "a"); + String result = toString(execute("git add a b")); + assertEquals("", result); + + result = toString(execute("git status -- a b")); + assertEquals(toString("On branch master", "Changes to be committed:", + "new file: a", "new file: b"), result); + + result = toString(execute("git commit -m 'added a b'")); + assertEquals( + "[master 3c93fa8e3a28ee26690498be78016edcb3a38c73] added a b", + result); + + result = toString(execute("git status -- a b")); + assertEquals("On branch master", result); + } + +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ConfigTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ConfigTest.java index aefdff185c..0af310c497 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ConfigTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ConfigTest.java @@ -1,56 +1,23 @@ /* - * Copyright (C) 2012, Matthias Sohn <matthias.sohn@sap.com> - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2012, Matthias Sohn <matthias.sohn@sap.com> and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; +import java.util.HashMap; +import java.util.Map; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.lib.CLIRepositoryTestCase; -import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.SystemReader; import org.junit.Before; import org.junit.Test; @@ -60,9 +27,12 @@ public class ConfigTest extends CLIRepositoryTestCase { @Before public void setUp() throws Exception { super.setUp(); - new Git(db).commit().setMessage("initial commit").call(); + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); + } } + @SuppressWarnings("boxing") @Test public void testListConfig() throws Exception { boolean isWindows = SystemReader.getInstance().getProperty("os.name") @@ -71,18 +41,30 @@ public class ConfigTest extends CLIRepositoryTestCase { .equals("Mac OS X"); String[] output = execute("git config --list"); - List<String> expect = new ArrayList<String>(); - expect.add("core.filemode=" + !isWindows); - expect.add("core.logallrefupdates=true"); - if (isMac) - expect.add("core.precomposeunicode=true"); - expect.add("core.repositoryformatversion=0"); - if (!FS.DETECTED.supportsSymlinks()) - expect.add("core.symlinks=false"); - expect.add(""); // ends with LF (last line empty) - assertEquals("expected default configuration", - Arrays.asList(expect.toArray()).toString(), - Arrays.asList(output).toString()); + + Map<String, String> options = parseOptions(output); + + assertEquals(!isWindows, Boolean.valueOf(options.get("core.filemode"))); + assertTrue(Boolean.valueOf(options.get("core.logallrefupdates"))); + if (isMac) { + assertTrue(Boolean.valueOf(options.get("core.precomposeunicode"))); + } + assertEquals(Integer.valueOf(0), + Integer.valueOf(options.get("core.repositoryformatversion"))); + } + + private Map<String, String> parseOptions(String[] output) { + Map<String, String> options = new HashMap<>(); + Arrays.stream(output).forEachOrdered(s -> { + int p = s.indexOf('='); + if (p == -1) { + return; + } + String key = s.substring(0, p); + String value = s.substring(p + 1); + options.put(key, value); + }); + return options; } } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DescribeTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DescribeTest.java index 6352a26524..595767d3a0 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DescribeTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DescribeTest.java @@ -1,51 +1,25 @@ /* - * Copyright (C) 2013, Matthias Sohn <matthias.sohn@sap.com> - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2013, Matthias Sohn <matthias.sohn@sap.com> and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Arrays; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.eclipse.jgit.pgm.internal.CLIText; import org.junit.Before; import org.junit.Test; @@ -65,19 +39,23 @@ public class DescribeTest extends CLIRepositoryTestCase { git.tag().setName("v1.0").call(); } + private void secondCommit() throws Exception { + writeTrashFile("greeting", "Hello, world!"); + git.add().addFilepattern("greeting").call(); + git.commit().setMessage("2nd commit").call(); + } + @Test public void testNoHead() throws Exception { - assertArrayEquals( - new String[] { "fatal: No names found, cannot describe anything." }, - execute("git describe")); + assertEquals(CLIText.fatalError(CLIText.get().noNamesFound), + toString(executeUnchecked("git describe"))); } @Test public void testHeadNoTag() throws Exception { git.commit().setMessage("initial commit").call(); - assertArrayEquals( - new String[] { "fatal: No names found, cannot describe anything." }, - execute("git describe")); + assertEquals(CLIText.fatalError(CLIText.get().noNamesFound), + toString(executeUnchecked("git describe"))); } @Test @@ -90,9 +68,7 @@ public class DescribeTest extends CLIRepositoryTestCase { @Test public void testDescribeCommit() throws Exception { initialCommitAndTag(); - writeTrashFile("greeting", "Hello, world!"); - git.add().addFilepattern("greeting").call(); - git.commit().setMessage("2nd commit").call(); + secondCommit(); assertArrayEquals(new String[] { "v1.0-1-g56f6ceb", "" }, execute("git describe")); } @@ -103,4 +79,108 @@ public class DescribeTest extends CLIRepositoryTestCase { assertArrayEquals(new String[] { "v1.0-0-g6fd41be", "" }, execute("git describe --long HEAD")); } + + @Test + public void testDescribeCommitMatch() throws Exception { + initialCommitAndTag(); + secondCommit(); + assertArrayEquals(new String[] { "v1.0-1-g56f6ceb", "" }, + execute("git describe --match v1.*")); + } + + @Test + public void testDescribeCommitMatchAbbrev() throws Exception { + initialCommitAndTag(); + secondCommit(); + assertArrayEquals(new String[] { "v1.0-1-g56f6cebdf3f5", "" }, + execute("git describe --abbrev 12 --match v1.*")); + } + + @Test + public void testDescribeCommitMatchAbbrevMin() throws Exception { + initialCommitAndTag(); + secondCommit(); + assertArrayEquals(new String[] { "v1.0-1-g56f6", "" }, + execute("git describe --abbrev -5 --match v1.*")); + } + + @Test + public void testDescribeCommitMatchAbbrevMax() throws Exception { + initialCommitAndTag(); + secondCommit(); + assertArrayEquals(new String[] { + "v1.0-1-g56f6cebdf3f5ceeecd803365abf0996fb1fa006d", "" }, + execute("git describe --abbrev 50 --match v1.*")); + } + + @Test + public void testDescribeCommitMatch2() throws Exception { + initialCommitAndTag(); + secondCommit(); + git.tag().setName("v2.0").call(); + assertArrayEquals(new String[] { "v1.0-1-g56f6ceb", "" }, + execute("git describe --match v1.*")); + } + + @Test + public void testDescribeExclude() throws Exception { + initialCommitAndTag(); + secondCommit(); + git.tag().setName("v2.0").call(); + assertArrayEquals(new String[] { "v1.0-1-g56f6ceb", "" }, + execute("git describe --exclude v2.*")); + } + + @Test + public void testDescribeCommitMultiMatch() throws Exception { + initialCommitAndTag(); + secondCommit(); + git.tag().setName("v2.0.0").call(); + git.tag().setName("v2.1.1").call(); + assertArrayEquals("git yields v2.0.0", new String[] { "v2.0.0", "" }, + execute("git describe --match v2.0* --match v2.1.*")); + } + + @Test + public void testDescribeCommitMultiExclude() throws Exception { + initialCommitAndTag(); + secondCommit(); + git.tag().setName("v2.0.0").call(); + git.tag().setName("v2.1.1").call(); + git.tag().setName("v2.2").call(); + assertArrayEquals("git yields v2.2", new String[] { "v2.2", "" }, + execute("git describe --exclude v2.0* --exclude v2.1.*")); + } + + @Test + public void testDescribeCommitNoMatch() throws Exception { + initialCommitAndTag(); + writeTrashFile("greeting", "Hello, world!"); + secondCommit(); + try { + execute("git describe --match 1.*"); + fail("git describe should not find any tag matching 1.*"); + } catch (Die e) { + assertEquals("No names found, cannot describe anything.", + e.getMessage()); + } + } + + @Test + public void testHelpArgumentBeforeUnknown() throws Exception { + String[] output = execute("git describe -h -XYZ"); + String all = Arrays.toString(output); + assertTrue("Unexpected help output: " + all, + all.contains("jgit describe")); + assertFalse("Unexpected help output: " + all, all.contains("fatal")); + } + + @Test + public void testHelpArgumentAfterUnknown() throws Exception { + String[] output = executeUnchecked("git describe -XYZ -h"); + String all = Arrays.toString(output); + assertTrue("Unexpected help output: " + all, + all.contains("jgit describe")); + assertTrue("Unexpected help output: " + all, all.contains("fatal")); + } } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DiffTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DiffTest.java new file mode 100644 index 0000000000..859b54de4d --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DiffTest.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2022, Matthias Sohn <matthias.sohn@sap.com> and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.eclipse.jgit.pgm; + +import static org.junit.Assert.assertEquals; + +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.junit.Before; +import org.junit.Test; + +public class DiffTest extends CLIRepositoryTestCase { + + private static final String NO_NEWLINE = "\\ No newline at end of file"; + + @Before + public void setup() throws Exception { + writeTrashFile("a", "a"); + execute("git add a"); + execute("git commit -m added"); + } + + @Test + public void testDiffCommitNewFile() throws Exception { + writeTrashFile("a1", "a"); + String result = toString(execute("git diff")); + assertEquals( + toString("diff --git a/a1 b/a1", "new file mode 100644", + "index 0000000..2e65efe", "--- /dev/null", "+++ b/a1", + "@@ -0,0 +1 @@", "+a", NO_NEWLINE), + result); + } + + @Test + public void testDiffCommitModifiedFile() throws Exception { + writeTrashFile("a", "a1"); + String result = toString(execute("git diff")); + assertEquals( + toString("diff --git a/a b/a", "index 2e65efe..59ef8d1 100644", + "--- a/a", "+++ b/a", "@@ -1 +1 @@", + "-a", NO_NEWLINE, "+a1", NO_NEWLINE), + result); + } + + @Test + public void testDiffCommitModifiedFileNameOnly() throws Exception { + writeTrashFile("a", "a1"); + writeTrashFile("b", "b"); + String result = toString(execute("git diff --name-only")); + assertEquals(toString("a", "b"), result); + } + + @Test + public void testDiffCommitModifiedFileNameStatus() throws Exception { + writeTrashFile("a", "a1"); + writeTrashFile("b", "b"); + String result = toString(execute("git diff --name-status")); + assertEquals(toString("M\ta", "A\tb"), result); + } +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DiffToolTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DiffToolTest.java new file mode 100644 index 0000000000..f0908cebc4 --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DiffToolTest.java @@ -0,0 +1,346 @@ +/* + * Copyright (C) 2021-2022, Simeon Andreev <simeon.danailov.andreev@gmail.com> and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.pgm; + +import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DIFFTOOL_SECTION; +import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DIFF_SECTION; +import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_CMD; +import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PROMPT; +import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_TOOL; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.InputStream; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; + +import org.eclipse.jgit.internal.diffmergetool.DiffTools; +import org.eclipse.jgit.internal.diffmergetool.ExternalDiffTool; +import org.eclipse.jgit.lib.StoredConfig; +import org.junit.Before; +import org.junit.Test; + +/** + * Testing the {@code difftool} command. + */ +public class DiffToolTest extends ToolTestCase { + + private static final String DIFF_TOOL = CONFIG_DIFFTOOL_SECTION; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + configureEchoTool(TOOL_NAME); + } + + @Test(expected = Die.class) + public void testUndefinedTool() throws Exception { + String toolName = "undefined"; + String[] conflictingFilenames = createUnstagedChanges(); + + List<String> expectedErrors = new ArrayList<>(); + for (String changedFilename : conflictingFilenames) { + expectedErrors.add("External diff tool is not defined: " + toolName); + expectedErrors.add("compare of " + changedFilename + " failed"); + } + + runAndCaptureUsingInitRaw(expectedErrors, DIFF_TOOL, "--no-prompt", + "--tool", toolName); + fail("Expected exception to be thrown due to undefined external tool"); + } + + @Test(expected = Die.class) + public void testUserToolWithCommandNotFoundError() throws Exception { + String toolName = "customTool"; + + int errorReturnCode = 127; // command not found + String command = "exit " + errorReturnCode; + + StoredConfig config = db.getConfig(); + config.setString(CONFIG_DIFFTOOL_SECTION, toolName, CONFIG_KEY_CMD, + command); + + createMergeConflict(); + runAndCaptureUsingInitRaw(DIFF_TOOL, "--no-prompt", "--tool", toolName); + + fail("Expected exception to be thrown due to external tool exiting with error code: " + + errorReturnCode); + } + + @Test(expected = Die.class) + public void testEmptyToolName() throws Exception { + assumeLinuxPlatform(); + + String emptyToolName = ""; + + StoredConfig config = db.getConfig(); + // the default diff tool is configured without a subsection + String subsection = null; + config.setString(CONFIG_DIFF_SECTION, subsection, CONFIG_KEY_TOOL, + emptyToolName); + + createUnstagedChanges(); + + String araxisErrorLine = "compare: unrecognized option `-wait' @ error/compare.c/CompareImageCommand/1123."; + String[] expectedErrorOutput = { araxisErrorLine, araxisErrorLine, }; + runAndCaptureUsingInitRaw(Arrays.asList(expectedErrorOutput), DIFF_TOOL, + "--no-prompt"); + fail("Expected exception to be thrown due to external tool exiting with an error"); + } + + @Test + public void testToolWithPrompt() throws Exception { + String[] inputLines = { + "y", // accept launching diff tool + "y", // accept launching diff tool + }; + + String[] conflictingFilenames = createUnstagedChanges(); + String[] expectedOutput = getExpectedCompareOutput(conflictingFilenames); + + String option = "--tool"; + + InputStream inputStream = createInputStream(inputLines); + assertArrayOfLinesEquals("Incorrect output for option: " + option, + expectedOutput, runAndCaptureUsingInitRaw(inputStream, + DIFF_TOOL, "--prompt", option, TOOL_NAME)); + } + + @Test + public void testToolAbortLaunch() throws Exception { + String[] inputLines = { + "y", // accept launching diff tool + "n", // don't launch diff tool + }; + + String[] conflictingFilenames = createUnstagedChanges(); + int abortIndex = 1; + String[] expectedOutput = getExpectedAbortOutput(conflictingFilenames, abortIndex); + + String option = "--tool"; + + InputStream inputStream = createInputStream(inputLines); + assertArrayOfLinesEquals("Incorrect output for option: " + option, + expectedOutput, + runAndCaptureUsingInitRaw(inputStream, DIFF_TOOL, "--prompt", option, + TOOL_NAME)); + } + + @Test(expected = Die.class) + public void testNotDefinedTool() throws Exception { + createUnstagedChanges(); + + runAndCaptureUsingInitRaw(DIFF_TOOL, "--tool", "undefined"); + fail("Expected exception when trying to run undefined tool"); + } + + @Test + public void testTool() throws Exception { + String[] conflictFilenames = createUnstagedChanges(); + String[] expectedOutput = getExpectedToolOutputNoPrompt(conflictFilenames); + + String[] options = { + "--tool", + "-t", + }; + + for (String option : options) { + assertArrayOfLinesEquals("Incorrect output for option: " + option, + expectedOutput, + runAndCaptureUsingInitRaw(DIFF_TOOL, option, + TOOL_NAME)); + } + } + + @Test + public void testToolTrustExitCode() throws Exception { + String[] conflictingFilenames = createUnstagedChanges(); + String[] expectedOutput = getExpectedToolOutputNoPrompt(conflictingFilenames); + + String[] options = { "--tool", "-t", }; + + for (String option : options) { + assertArrayOfLinesEquals("Incorrect output for option: " + option, + expectedOutput, runAndCaptureUsingInitRaw(DIFF_TOOL, + "--trust-exit-code", option, TOOL_NAME)); + } + } + + @Test + public void testToolNoGuiNoPromptNoTrustExitcode() throws Exception { + String[] conflictingFilenames = createUnstagedChanges(); + String[] expectedOutput = getExpectedToolOutputNoPrompt(conflictingFilenames); + + String[] options = { "--tool", "-t", }; + + for (String option : options) { + assertArrayOfLinesEquals("Incorrect output for option: " + option, + expectedOutput, runAndCaptureUsingInitRaw(DIFF_TOOL, + "--no-gui", "--no-prompt", "--no-trust-exit-code", + option, TOOL_NAME)); + } + } + + @Test + public void testToolCached() throws Exception { + String[] conflictingFilenames = createStagedChanges(); + Pattern[] expectedOutput = getExpectedCachedToolOutputNoPrompt(conflictingFilenames); + + String[] options = { "--cached", "--staged", }; + + for (String option : options) { + assertArrayOfMatchingLines("Incorrect output for option: " + option, + expectedOutput, runAndCaptureUsingInitRaw(DIFF_TOOL, + option, "--tool", TOOL_NAME)); + } + } + + @Test + public void testToolHelp() throws Exception { + List<String> expectedOutput = new ArrayList<>(); + + DiffTools diffTools = new DiffTools(db); + Map<String, ExternalDiffTool> predefinedTools = diffTools + .getPredefinedTools(true); + List<ExternalDiffTool> availableTools = new ArrayList<>(); + List<ExternalDiffTool> notAvailableTools = new ArrayList<>(); + for (ExternalDiffTool tool : predefinedTools.values()) { + if (tool.isAvailable()) { + availableTools.add(tool); + } else { + notAvailableTools.add(tool); + } + } + + expectedOutput.add( + "'git difftool --tool=<tool>' may be set to one of the following:"); + for (ExternalDiffTool tool : availableTools) { + String toolName = tool.getName(); + expectedOutput.add(toolName); + } + String customToolHelpLine = TOOL_NAME + "." + CONFIG_KEY_CMD + " " + + getEchoCommand(); + expectedOutput.add("user-defined:"); + expectedOutput.add(customToolHelpLine); + expectedOutput.add( + "The following tools are valid, but not currently available:"); + for (ExternalDiffTool tool : notAvailableTools) { + String toolName = tool.getName(); + expectedOutput.add(toolName); + } + String[] userDefinedToolsHelp = { + "Some of the tools listed above only work in a windowed", + "environment. If run in a terminal-only session, they will fail.", + }; + expectedOutput.addAll(Arrays.asList(userDefinedToolsHelp)); + + String option = "--tool-help"; + assertArrayOfLinesEquals("Incorrect output for option: " + option, + expectedOutput.toArray(new String[0]), + runAndCaptureUsingInitRaw(DIFF_TOOL, option)); + } + + private void configureEchoTool(String toolName) { + StoredConfig config = db.getConfig(); + // the default diff tool is configured without a subsection + String subsection = null; + config.setString(CONFIG_DIFF_SECTION, subsection, CONFIG_KEY_TOOL, + toolName); + + String command = getEchoCommand(); + + config.setString(CONFIG_DIFFTOOL_SECTION, toolName, CONFIG_KEY_CMD, + command); + /* + * prevent prompts as we are running in tests and there is no user to + * interact with on the command line + */ + config.setString(CONFIG_DIFFTOOL_SECTION, toolName, CONFIG_KEY_PROMPT, + String.valueOf(false)); + } + + private String[] getExpectedToolOutputNoPrompt(String[] conflictingFilenames) { + String[] expectedToolOutput = new String[conflictingFilenames.length]; + for (int i = 0; i < conflictingFilenames.length; ++i) { + String newPath = conflictingFilenames[i]; + Path fullPath = getFullPath(newPath); + expectedToolOutput[i] = fullPath.toString(); + } + return expectedToolOutput; + } + + private Pattern[] getExpectedCachedToolOutputNoPrompt(String[] conflictingFilenames) { + String tmpDir = System.getProperty("java.io.tmpdir"); + if (tmpDir.endsWith(File.separator)) { + tmpDir = tmpDir.substring(0, tmpDir.length() - 1); + } + Pattern emptyPattern = Pattern.compile(""); + List<Pattern> expectedToolOutput = new ArrayList<>(); + for (int i = 0; i < conflictingFilenames.length; ++i) { + String changedFilename = conflictingFilenames[i]; + Path fullPath = getFullPath(changedFilename); + String filename = fullPath.getFileName().toString(); + String regexp = tmpDir + File.separatorChar + filename + + "_REMOTE_.*"; + Pattern pattern = Pattern.compile(regexp); + expectedToolOutput.add(pattern); + expectedToolOutput.add(emptyPattern); + } + expectedToolOutput.add(emptyPattern); + return expectedToolOutput.toArray(new Pattern[0]); + } + + private String[] getExpectedCompareOutput(String[] conflictingFilenames) { + List<String> expected = new ArrayList<>(); + int n = conflictingFilenames.length; + for (int i = 0; i < n; ++i) { + String changedFilename = conflictingFilenames[i]; + expected.add( + "Viewing (" + (i + 1) + "/" + n + "): '" + changedFilename + + "'"); + expected.add("Launch '" + TOOL_NAME + "' [Y/n]?"); + Path fullPath = getFullPath(changedFilename); + expected.add(fullPath.toString()); + } + return expected.toArray(new String[0]); + } + + private String[] getExpectedAbortOutput(String[] conflictingFilenames, + int abortIndex) { + List<String> expected = new ArrayList<>(); + int n = conflictingFilenames.length; + for (int i = 0; i < n; ++i) { + String changedFilename = conflictingFilenames[i]; + expected.add( + "Viewing (" + (i + 1) + "/" + n + "): '" + changedFilename + + "'"); + expected.add("Launch '" + TOOL_NAME + "' [Y/n]?"); + if (i == abortIndex) { + break; + } + Path fullPath = getFullPath(changedFilename); + expected.add(fullPath.toString()); + } + return expected.toArray(new String[0]); + } + + private static String getEchoCommand() { + /* + * use 'REMOTE' placeholder, as it will be replaced by a file path + * within the repository. + */ + return "(echo \"$REMOTE\")"; + } +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/FetchTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/FetchTest.java index 9685d4517a..1c6b7839d3 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/FetchTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/FetchTest.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2013, Chris Aniszczyk <zx@twitter.com> and others. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2013, Chris Aniszczyk <zx@twitter.com> and others. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; @@ -66,6 +33,7 @@ public class FetchTest extends CLIRepositoryTestCase { git.commit().setMessage("initial commit").call(); Repository remoteRepository = createWorkRepository(); + addRepoToClose(remoteRepository); remoteGit = new Git(remoteRepository); // setup the first repository to fetch from the second repository @@ -92,6 +60,19 @@ public class FetchTest extends CLIRepositoryTestCase { } @Test + public void testFetchForceUpdate() throws Exception { + String[] result = execute( + "git fetch test refs/heads/master:refs/remotes/origin/master"); + assertEquals(" * [new branch] master -> origin/master", + result[1]); + assertEquals(" * [new tag] tag -> tag", result[2]); + remoteGit.commit().setAmend(true).setMessage("amended").call(); + result = execute( + "git fetch -f test refs/heads/master:refs/remotes/origin/master"); + assertEquals("", result[0]); + } + + @Test public void testFetchNoTags() throws Exception { String[] result = execute("git fetch --no-tags test refs/heads/master:refs/remotes/origin/master"); assertEquals(" * [new branch] master -> origin/master", diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/InitTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/InitTest.java new file mode 100644 index 0000000000..88789d3383 --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/InitTest.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2016, Rüdiger Herrmann <ruediger.herrmann@gmx.de> and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.eclipse.jgit.pgm; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import java.io.File; + +import org.eclipse.jgit.internal.storage.file.FileRepository; +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.Repository; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +public class InitTest extends CLIRepositoryTestCase { + + @Rule + public final TemporaryFolder tempFolder = new TemporaryFolder(); + + @Test + public void testInitBare() throws Exception { + File directory = tempFolder.getRoot(); + + String[] result = execute( + "git init '" + directory.getCanonicalPath() + "' --bare"); + + String[] expecteds = new String[] { + "Initialized empty Git repository in " + + directory.getCanonicalPath(), + "" }; + assertArrayEquals(expecteds, result); + } + + @Test + public void testInitDirectory() throws Exception { + File workDirectory = tempFolder.getRoot(); + File gitDirectory = new File(workDirectory, Constants.DOT_GIT); + + String[] result = execute( + "git init '" + workDirectory.getCanonicalPath() + "'"); + + String[] expecteds = new String[] { + "Initialized empty Git repository in " + + gitDirectory.getCanonicalPath(), + "" }; + assertArrayEquals(expecteds, result); + } + + @Test + public void testInitDirectoryInitialBranch() throws Exception { + File workDirectory = tempFolder.getRoot(); + File gitDirectory = new File(workDirectory, Constants.DOT_GIT); + + String[] result = execute( + "git init -b main '" + workDirectory.getCanonicalPath() + "'"); + + String[] expecteds = new String[] { + "Initialized empty Git repository in " + + gitDirectory.getCanonicalPath(), + "" }; + assertArrayEquals(expecteds, result); + + try (Repository repo = new FileRepository(gitDirectory)) { + assertEquals("refs/heads/main", repo.getFullBranch()); + } + } +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/LogTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/LogTest.java new file mode 100644 index 0000000000..1cc52a44ac --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/LogTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2022, Matthias Sohn <matthias.sohn@sap.com> and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.eclipse.jgit.pgm; + +import static org.junit.Assert.assertEquals; + +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.junit.Before; +import org.junit.Test; + +public class LogTest extends CLIRepositoryTestCase { + + @Before + public void setup() throws Exception { + writeTrashFile("a", "a"); + writeTrashFile("b", "a"); + execute("git add a b"); + execute("git commit -m added"); + } + + @Test + public void testLogCommitNewFile() throws Exception { + String result = toString(execute("git log")); + assertEquals( + toString("commit b4680f542095a8b41ea4258a5c03b548543a817c", + "Author: GIT_COMMITTER_NAME <GIT_COMMITTER_EMAIL>", + "Date: Sat Aug 15 20:12:58 2009 -0330", "added"), + result); + } + + @Test + public void testLogNameOnly() throws Exception { + String result = toString(execute("git log --name-only")); + assertEquals( + toString("commit b4680f542095a8b41ea4258a5c03b548543a817c", + "Author: GIT_COMMITTER_NAME <GIT_COMMITTER_EMAIL>", + "Date: Sat Aug 15 20:12:58 2009 -0330", "added", "a", + "b"), + result); + } + + @Test + public void testDiffCommitModifiedFileNameStatus() throws Exception { + String result = toString(execute("git log --name-status")); + assertEquals(toString("commit b4680f542095a8b41ea4258a5c03b548543a817c", + "Author: GIT_COMMITTER_NAME <GIT_COMMITTER_EMAIL>", + "Date: Sat Aug 15 20:12:58 2009 -0330", "added", "A\ta", + "A\tb"), + result); + } +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/LsFilesTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/LsFilesTest.java new file mode 100644 index 0000000000..4259cc1215 --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/LsFilesTest.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2017, Matthias Sohn <matthias.sohn@sap.com> and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.pgm; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.junit.JGitTestUtil; +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.eclipse.jgit.util.FileUtils; +import org.junit.Before; +import org.junit.Test; + +public class LsFilesTest extends CLIRepositoryTestCase { + + @Before + @Override + public void setUp() throws Exception { + super.setUp(); + try (Git git = new Git(db)) { + JGitTestUtil.writeTrashFile(db, "hello", "hello"); + JGitTestUtil.writeTrashFile(db, "dir", "world", "world"); + git.add().addFilepattern("dir").call(); + git.commit().setMessage("Initial commit").call(); + + JGitTestUtil.writeTrashFile(db, "hello2", "hello"); + git.add().addFilepattern("hello2").call(); + FileUtils.createSymLink(new File(db.getWorkTree(), "link"), + "target"); + FileUtils.mkdir(new File(db.getWorkTree(), "target")); + writeTrashFile("target/file", "someData"); + git.add().addFilepattern("target").addFilepattern("link").call(); + git.commit().setMessage("hello2").call(); + + JGitTestUtil.writeTrashFile(db, "staged", "x"); + JGitTestUtil.deleteTrashFile(db, "hello2"); + git.add().addFilepattern("staged").call(); + JGitTestUtil.writeTrashFile(db, "untracked", "untracked"); + } + } + + @Test + public void testHelp() throws Exception { + String[] result = execute("git ls-files -h"); + assertTrue(result[1].startsWith("jgit ls-files")); + } + + @Test + public void testLsFiles() throws Exception { + assertArrayEquals(new String[] { "dir/world", "hello2", "link", + "staged", "target/file", "" }, execute("git ls-files")); + } +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/LsRemoteTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/LsRemoteTest.java index ba6b771de5..46eec7436e 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/LsRemoteTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/LsRemoteTest.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2014, Matthias Sohn <matthias.sohn@sap.com> - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2014, Matthias Sohn <matthias.sohn@sap.com> and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; @@ -66,7 +33,7 @@ public class LsRemoteTest extends CLIRepositoryTestCase { git.add().addFilepattern("Test.txt").call(); git.commit().setMessage("Initial commit").call(); - // create a master branch and switch to it + // create a test branch and switch to it git.branchCreate().setName("test").call(); RefUpdate rup = db.updateRef(Constants.HEAD); rup.link("refs/heads/test"); @@ -80,7 +47,7 @@ public class LsRemoteTest extends CLIRepositoryTestCase { @Test public void testLsRemote() throws Exception { final List<String> result = CLIGitCommand.execute( - "git ls-remote " + db.getDirectory(), db); + "git ls-remote " + shellQuote(db.getDirectory()), db); assertArrayEquals(new String[] { "d0b1ef2b3dea02bb2ca824445c04e6def012c32c HEAD", "d0b1ef2b3dea02bb2ca824445c04e6def012c32c refs/heads/master", @@ -98,7 +65,8 @@ public class LsRemoteTest extends CLIRepositoryTestCase { public void testLsRemoteHeads() throws Exception { final List<String> result = CLIGitCommand.execute( "git ls-remote --heads " - + db.getDirectory(), db); + + shellQuote(db.getDirectory()), + db); assertArrayEquals(new String[] { "d0b1ef2b3dea02bb2ca824445c04e6def012c32c refs/heads/master", "d0b1ef2b3dea02bb2ca824445c04e6def012c32c refs/heads/test", @@ -108,7 +76,7 @@ public class LsRemoteTest extends CLIRepositoryTestCase { @Test public void testLsRemoteTags() throws Exception { final List<String> result = CLIGitCommand.execute( - "git ls-remote --tags " + db.getDirectory(), db); + "git ls-remote --tags " + shellQuote(db.getDirectory()), db); assertArrayEquals(new String[] { "efc02078d83a5226986ae917323acec7e1e8b7cb refs/tags/tag1", "d0b1ef2b3dea02bb2ca824445c04e6def012c32c refs/tags/tag1^{}", @@ -122,8 +90,27 @@ public class LsRemoteTest extends CLIRepositoryTestCase { @Test public void testLsRemoteHeadsTags() throws Exception { final List<String> result = CLIGitCommand.execute( - "git ls-remote --heads --tags " + db.getDirectory(), db); + "git ls-remote --heads --tags " + shellQuote(db.getDirectory()), + db); + assertArrayEquals(new String[] { + "d0b1ef2b3dea02bb2ca824445c04e6def012c32c refs/heads/master", + "d0b1ef2b3dea02bb2ca824445c04e6def012c32c refs/heads/test", + "efc02078d83a5226986ae917323acec7e1e8b7cb refs/tags/tag1", + "d0b1ef2b3dea02bb2ca824445c04e6def012c32c refs/tags/tag1^{}", + "4e4b837e0fd4ba83c003678b03592dc1509a4115 refs/tags/tag2", + "d0b1ef2b3dea02bb2ca824445c04e6def012c32c refs/tags/tag2^{}", + "489384bf8ace47522fe32093d2ceb85b65a6cbb1 refs/tags/tag3", + "d0b1ef2b3dea02bb2ca824445c04e6def012c32c refs/tags/tag3^{}", + "" }, result.toArray()); + } + + @Test + public void testLsRemoteSymRefs() throws Exception { + final List<String> result = CLIGitCommand.execute( + "git ls-remote --symref " + shellQuote(db.getDirectory()), db); assertArrayEquals(new String[] { + "ref: refs/heads/test HEAD", + "d0b1ef2b3dea02bb2ca824445c04e6def012c32c HEAD", "d0b1ef2b3dea02bb2ca824445c04e6def012c32c refs/heads/master", "d0b1ef2b3dea02bb2ca824445c04e6def012c32c refs/heads/test", "efc02078d83a5226986ae917323acec7e1e8b7cb refs/tags/tag1", diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/MergeTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/MergeTest.java index 975e8c4f76..905235d98a 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/MergeTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/MergeTest.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2012, 2014 IBM Corporation and others. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2012, 2014 IBM Corporation and others. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; @@ -50,6 +17,7 @@ import java.util.Iterator; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.lib.CLIRepositoryTestCase; import org.eclipse.jgit.merge.MergeStrategy; +import org.eclipse.jgit.pgm.internal.CLIText; import org.eclipse.jgit.revwalk.RevCommit; import org.junit.Before; import org.junit.Test; @@ -194,8 +162,9 @@ public class MergeTest extends CLIRepositoryTestCase { @Test public void testNoFastForwardAndSquash() throws Exception { - assertEquals("fatal: You cannot combine --squash with --no-ff.", - execute("git merge master --no-ff --squash")[0]); + assertEquals( + CLIText.fatalError(CLIText.get().cannotCombineSquashWithNoff), + executeUnchecked("git merge master --no-ff --squash")[0]); } @Test @@ -209,8 +178,8 @@ public class MergeTest extends CLIRepositoryTestCase { git.add().addFilepattern("file").call(); git.commit().setMessage("commit#2").call(); - assertEquals("fatal: Not possible to fast-forward, aborting.", - execute("git merge master --ff-only")[0]); + assertEquals(CLIText.fatalError(CLIText.get().ffNotPossibleAborting), + executeUnchecked("git merge master --ff-only")[0]); } @Test diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/MergeToolTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/MergeToolTest.java new file mode 100644 index 0000000000..6339831a40 --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/MergeToolTest.java @@ -0,0 +1,399 @@ +/* + * Copyright (C) 2022, Simeon Andreev <simeon.danailov.andreev@gmail.com> and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.pgm; + +import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_CMD; +import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PROMPT; +import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_TOOL; +import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_MERGETOOL_SECTION; +import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_MERGE_SECTION; +import static org.junit.Assert.fail; + +import java.io.InputStream; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import org.eclipse.jgit.internal.diffmergetool.ExternalMergeTool; +import org.eclipse.jgit.internal.diffmergetool.MergeTools; +import org.eclipse.jgit.lib.StoredConfig; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +/** + * Testing the {@code mergetool} command. + */ +public class MergeToolTest extends ToolTestCase { + + private static final String MERGE_TOOL = CONFIG_MERGETOOL_SECTION; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + configureEchoTool(TOOL_NAME); + } + + @Test + public void testUndefinedTool() throws Exception { + String toolName = "undefined"; + String[] conflictingFilenames = createMergeConflict(); + + List<String> expectedErrors = new ArrayList<>(); + for (String conflictingFilename : conflictingFilenames) { + expectedErrors.add("External merge tool is not defined: " + toolName); + expectedErrors.add("merge of " + conflictingFilename + " failed"); + } + + runAndCaptureUsingInitRaw(expectedErrors, MERGE_TOOL, + "--no-prompt", "--tool", toolName); + } + + @Test(expected = Die.class) + public void testUserToolWithCommandNotFoundError() throws Exception { + String toolName = "customTool"; + + int errorReturnCode = 127; // command not found + String command = "exit " + errorReturnCode; + + StoredConfig config = db.getConfig(); + config.setString(CONFIG_MERGETOOL_SECTION, toolName, CONFIG_KEY_CMD, + command); + + createMergeConflict(); + runAndCaptureUsingInitRaw(MERGE_TOOL, "--no-prompt", "--tool", + toolName); + + fail("Expected exception to be thrown due to external tool exiting with error code: " + + errorReturnCode); + } + + @Ignore + @Test + public void testEmptyToolName() throws Exception { + assumeLinuxPlatform(); + + String emptyToolName = ""; + + StoredConfig config = db.getConfig(); + // the default merge tool is configured without a subsection + String subsection = null; + config.setString(CONFIG_MERGE_SECTION, subsection, CONFIG_KEY_TOOL, + emptyToolName); + + createMergeConflict(); + + String araxisErrorLine = "compare-im6.q16: unrecognized option `-wait' @ error/compare.c/CompareImageCommand/1131."; + String[] expectedErrorOutput = { araxisErrorLine, araxisErrorLine, }; + runAndCaptureUsingInitRaw(Arrays.asList(expectedErrorOutput), + MERGE_TOOL, "--no-prompt"); + } + + @Test + public void testAbortMerge() throws Exception { + String[] inputLines = { + "y", // start tool for merge resolution + "n", // don't accept merge tool result + "n", // don't continue resolution + }; + String[] conflictingFilenames = createMergeConflict(); + int abortIndex = 1; + String[] expectedOutput = getExpectedAbortMergeOutput( + conflictingFilenames, + abortIndex); + + String option = "--tool"; + + InputStream inputStream = createInputStream(inputLines); + assertArrayOfLinesEquals("Incorrect output for option: " + option, + expectedOutput, runAndCaptureUsingInitRaw(inputStream, + MERGE_TOOL, "--prompt", option, TOOL_NAME)); + } + + @Test + public void testAbortLaunch() throws Exception { + String[] inputLines = { + "n", // abort merge tool launch + }; + String[] conflictingFilenames = createMergeConflict(); + String[] expectedOutput = getExpectedAbortLaunchOutput( + conflictingFilenames); + + String option = "--tool"; + + InputStream inputStream = createInputStream(inputLines); + assertArrayOfLinesEquals("Incorrect output for option: " + option, + expectedOutput, runAndCaptureUsingInitRaw(inputStream, + MERGE_TOOL, "--prompt", option, TOOL_NAME)); + } + + @Test + public void testMergeConflict() throws Exception { + String[] inputLines = { + "y", // start tool for merge resolution + "y", // accept merge result as successful + "y", // start tool for merge resolution + "y", // accept merge result as successful + }; + String[] conflictingFilenames = createMergeConflict(); + String[] expectedOutput = getExpectedMergeConflictOutput( + conflictingFilenames); + + String option = "--tool"; + + InputStream inputStream = createInputStream(inputLines); + assertArrayOfLinesEquals("Incorrect output for option: " + option, + expectedOutput, runAndCaptureUsingInitRaw(inputStream, + MERGE_TOOL, "--prompt", option, TOOL_NAME)); + } + + @Test + public void testDeletedConflict() throws Exception { + String[] inputLines = { + "d", // choose delete option to resolve conflict + "m", // choose merge option to resolve conflict + }; + String[] conflictingFilenames = createDeletedConflict(); + String[] expectedOutput = getExpectedDeletedConflictOutput( + conflictingFilenames); + + String option = "--tool"; + + InputStream inputStream = createInputStream(inputLines); + assertArrayOfLinesEquals("Incorrect output for option: " + option, + expectedOutput, runAndCaptureUsingInitRaw(inputStream, + MERGE_TOOL, "--prompt", option, TOOL_NAME)); + } + + @Test + public void testNoConflict() throws Exception { + createStagedChanges(); + String[] expectedOutput = { "No files need merging" }; + + String[] options = { "--tool", "-t", }; + + for (String option : options) { + assertArrayOfLinesEquals("Incorrect output for option: " + option, + expectedOutput, + runAndCaptureUsingInitRaw(MERGE_TOOL, option, TOOL_NAME)); + } + } + + @Test + public void testMergeConflictNoPrompt() throws Exception { + String[] conflictingFilenames = createMergeConflict(); + String[] expectedOutput = getExpectedMergeConflictOutputNoPrompt( + conflictingFilenames); + + String option = "--tool"; + + assertArrayOfLinesEquals("Incorrect output for option: " + option, + expectedOutput, + runAndCaptureUsingInitRaw(MERGE_TOOL, option, TOOL_NAME)); + } + + @Test + public void testMergeConflictNoGuiNoPrompt() throws Exception { + String[] conflictingFilenames = createMergeConflict(); + String[] expectedOutput = getExpectedMergeConflictOutputNoPrompt( + conflictingFilenames); + + String option = "--tool"; + + assertArrayOfLinesEquals("Incorrect output for option: " + option, + expectedOutput, runAndCaptureUsingInitRaw(MERGE_TOOL, + "--no-gui", "--no-prompt", option, TOOL_NAME)); + } + + @Test + public void testToolHelp() throws Exception { + List<String> expectedOutput = new ArrayList<>(); + + MergeTools diffTools = new MergeTools(db); + Map<String, ExternalMergeTool> predefinedTools = diffTools + .getPredefinedTools(true); + List<ExternalMergeTool> availableTools = new ArrayList<>(); + List<ExternalMergeTool> notAvailableTools = new ArrayList<>(); + for (ExternalMergeTool tool : predefinedTools.values()) { + if (tool.isAvailable()) { + availableTools.add(tool); + } else { + notAvailableTools.add(tool); + } + } + + expectedOutput.add( + "'git mergetool --tool=<tool>' may be set to one of the following:"); + for (ExternalMergeTool tool : availableTools) { + String toolName = tool.getName(); + expectedOutput.add(toolName); + } + String customToolHelpLine = TOOL_NAME + "." + CONFIG_KEY_CMD + " " + + getEchoCommand(); + expectedOutput.add("user-defined:"); + expectedOutput.add(customToolHelpLine); + expectedOutput.add( + "The following tools are valid, but not currently available:"); + for (ExternalMergeTool tool : notAvailableTools) { + String toolName = tool.getName(); + expectedOutput.add(toolName); + } + String[] userDefinedToolsHelp = { + "Some of the tools listed above only work in a windowed", + "environment. If run in a terminal-only session, they will fail.", }; + expectedOutput.addAll(Arrays.asList(userDefinedToolsHelp)); + + String option = "--tool-help"; + assertArrayOfLinesEquals("Incorrect output for option: " + option, + expectedOutput.toArray(new String[0]), + runAndCaptureUsingInitRaw(MERGE_TOOL, option)); + } + + private void configureEchoTool(String toolName) { + StoredConfig config = db.getConfig(); + // the default merge tool is configured without a subsection + String subsection = null; + config.setString(CONFIG_MERGE_SECTION, subsection, CONFIG_KEY_TOOL, + toolName); + + String command = getEchoCommand(); + + config.setString(CONFIG_MERGETOOL_SECTION, toolName, CONFIG_KEY_CMD, + command); + /* + * prevent prompts as we are running in tests and there is no user to + * interact with on the command line + */ + config.setString(CONFIG_MERGETOOL_SECTION, toolName, CONFIG_KEY_PROMPT, + String.valueOf(false)); + } + + private String[] getExpectedMergeConflictOutputNoPrompt( + String[] conflictFilenames) { + List<String> expected = new ArrayList<>(); + expected.add("Merging:"); + for (String conflictFilename : conflictFilenames) { + expected.add(conflictFilename); + } + for (String conflictFilename : conflictFilenames) { + expected.add("Normal merge conflict for '" + conflictFilename + + "':"); + expected.add("{local}: modified file"); + expected.add("{remote}: modified file"); + Path filePath = getFullPath(conflictFilename); + expected.add(filePath.toString()); + expected.add(conflictFilename + " seems unchanged."); + } + return expected.toArray(new String[0]); + } + + private static String[] getExpectedAbortLaunchOutput( + String[] conflictFilenames) { + List<String> expected = new ArrayList<>(); + expected.add("Merging:"); + for (String conflictFilename : conflictFilenames) { + expected.add(conflictFilename); + } + if (conflictFilenames.length > 1) { + String conflictFilename = conflictFilenames[0]; + expected.add( + "Normal merge conflict for '" + conflictFilename + "':"); + expected.add("{local}: modified file"); + expected.add("{remote}: modified file"); + expected.add("Hit return to start merge resolution tool (" + + TOOL_NAME + "):"); + } + return expected.toArray(new String[0]); + } + + private String[] getExpectedAbortMergeOutput( + String[] conflictFilenames, int abortIndex) { + List<String> expected = new ArrayList<>(); + expected.add("Merging:"); + for (String conflictFilename : conflictFilenames) { + expected.add(conflictFilename); + } + for (int i = 0; i < conflictFilenames.length; ++i) { + if (i == abortIndex) { + break; + } + + String conflictFilename = conflictFilenames[i]; + expected.add( + "Normal merge conflict for '" + conflictFilename + "':"); + expected.add("{local}: modified file"); + expected.add("{remote}: modified file"); + Path fullPath = getFullPath(conflictFilename); + expected.add("Hit return to start merge resolution tool (" + + TOOL_NAME + "): " + fullPath); + expected.add(conflictFilename + " seems unchanged."); + expected.add("Was the merge successful [y/n]?"); + if (i < conflictFilenames.length - 1) { + expected.add( + "\tContinue merging other unresolved paths [y/n]?"); + } + } + return expected.toArray(new String[0]); + } + + private String[] getExpectedMergeConflictOutput( + String[] conflictFilenames) { + List<String> expected = new ArrayList<>(); + expected.add("Merging:"); + for (String conflictFilename : conflictFilenames) { + expected.add(conflictFilename); + } + for (int i = 0; i < conflictFilenames.length; ++i) { + String conflictFilename = conflictFilenames[i]; + expected.add("Normal merge conflict for '" + conflictFilename + + "':"); + expected.add("{local}: modified file"); + expected.add("{remote}: modified file"); + Path filePath = getFullPath(conflictFilename); + expected.add("Hit return to start merge resolution tool (" + + TOOL_NAME + "): " + filePath); + expected.add(conflictFilename + " seems unchanged."); + expected.add("Was the merge successful [y/n]?"); + if (i < conflictFilenames.length - 1) { + // expected.add( + // "\tContinue merging other unresolved paths [y/n]?"); + } + } + return expected.toArray(new String[0]); + } + + private static String[] getExpectedDeletedConflictOutput( + String[] conflictFilenames) { + List<String> expected = new ArrayList<>(); + expected.add("Merging:"); + for (String mergeConflictFilename : conflictFilenames) { + expected.add(mergeConflictFilename); + } + for (int i = 0; i < conflictFilenames.length; ++i) { + String conflictFilename = conflictFilenames[i]; + expected.add(conflictFilename + " seems unchanged."); + expected.add("{local}: deleted"); + expected.add("{remote}: modified file"); + expected.add("Use (m)odified or (d)eleted file, or (a)bort?"); + } + return expected.toArray(new String[0]); + } + + private static String getEchoCommand() { + /* + * use 'MERGED' placeholder, as both 'LOCAL' and 'REMOTE' will be + * replaced with full paths to a temporary file during some of the tests + */ + return "(echo \"$MERGED\")"; + } +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/PackRefsTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/PackRefsTest.java new file mode 100644 index 0000000000..b4d4ea9e56 --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/PackRefsTest.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.eclipse.jgit.pgm; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.internal.storage.file.FileRepository; +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.eclipse.jgit.lib.ConfigConstants; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.Ref; +import org.junit.Before; +import org.junit.Test; + +public class PackRefsTest extends CLIRepositoryTestCase { + private Git git; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + git = new Git(db); + git.commit().setMessage("initial commit").call(); + } + + @Test + public void tagPacked() throws Exception { + git.tag().setName("test").call(); + git.packRefs().call(); + assertEquals(Ref.Storage.PACKED, + git.getRepository().exactRef("refs/tags/test").getStorage()); + } + + @Test + public void nonTagRefNotPackedWithoutAll() throws Exception { + git.branchCreate().setName("test").call(); + git.packRefs().call(); + assertEquals(Ref.Storage.LOOSE, + git.getRepository().exactRef("refs/heads/test").getStorage()); + } + + @Test + public void nonTagRefPackedWithAll() throws Exception { + git.branchCreate().setName("test").call(); + git.packRefs().setAll(true).call(); + assertEquals(Ref.Storage.PACKED, + git.getRepository().exactRef("refs/heads/test").getStorage()); + } + + @Test + public void refTableCompacted() throws Exception { + ((FileRepository) git.getRepository()).convertRefStorage( + ConfigConstants.CONFIG_REF_STORAGE_REFTABLE, false, false); + + git.commit().setMessage("test commit").call(); + File tableDir = new File(db.getDirectory(), Constants.REFTABLE); + File[] reftables = tableDir.listFiles(); + assertNotNull(reftables); + assertTrue(reftables.length > 2); + + git.packRefs().call(); + + reftables = tableDir.listFiles(); + assertNotNull(reftables); + assertEquals(2, reftables.length); + } +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ProxyConfigTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ProxyConfigTest.java new file mode 100644 index 0000000000..42530f3a34 --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ProxyConfigTest.java @@ -0,0 +1,233 @@ +/* + * Copyright (C) 2016, Chrisian Halstrick <christian.halstrick@sap.com> and + * other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v1.0 which accompanies this + * distribution, is reproduced below, and is available at + * http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.jgit.pgm; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.util.List; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +/** + * Test how the content of the environment variables http[s]_proxy (upper- and + * lowercase) influence the setting of the system properties + * http[s].proxy[Host|Port] + */ +public class ProxyConfigTest { + private ProcessBuilder processBuilder; + + private Map<String, String> environment; + + @Before + public void setUp() { + String separator = System.getProperty("file.separator"); + String classpath = System.getProperty("java.class.path"); + String path = System.getProperty("java.home") + separator + "bin" + + separator + "java"; + processBuilder = new ProcessBuilder(path, "-cp", classpath, + ProxyPropertiesDumper.class.getName()); + environment = processBuilder.environment(); + environment.remove("http_proxy"); + environment.remove("https_proxy"); + environment.remove("HTTP_PROXY"); + environment.remove("HTTPS_PROXY"); + } + + @Test + public void testNoSetting() throws Exception { + Process start = processBuilder.start(); + start.waitFor(); + assertEquals( + "http.proxyHost: null, http.proxyPort: null, https.proxyHost: null, https.proxyPort: null", + getOutput(start)); + } + + @Test + public void testHttpProxy_lowerCase() throws Exception { + environment.put("http_proxy", "http://xx:1234"); + Process start = processBuilder.start(); + start.waitFor(); + assertEquals( + "http.proxyHost: xx, http.proxyPort: 1234, https.proxyHost: null, https.proxyPort: null", + getOutput(start)); + } + + @Test + public void testHttpProxy_upperCase() throws Exception { + environment.put("HTTP_PROXY", "http://XX:1234"); + Process start = processBuilder.start(); + start.waitFor(); + assertEquals( + "http.proxyHost: null, http.proxyPort: null, https.proxyHost: null, https.proxyPort: null", + getOutput(start)); + } + + @Test + public void testHttpProxy_bothCases() throws Exception { + environment.put("http_proxy", "http://xx:1234"); + environment.put("HTTP_PROXY", "http://XX:1234"); + Process start = processBuilder.start(); + start.waitFor(); + assertEquals( + "http.proxyHost: xx, http.proxyPort: 1234, https.proxyHost: null, https.proxyPort: null", + getOutput(start)); + } + + @Test + public void testHttpsProxy_lowerCase() throws Exception { + environment.put("https_proxy", "http://xx:1234"); + Process start = processBuilder.start(); + start.waitFor(); + assertEquals( + "http.proxyHost: null, http.proxyPort: null, https.proxyHost: xx, https.proxyPort: 1234", + getOutput(start)); + } + + @Test + public void testHttpsProxy_upperCase() throws Exception { + environment.put("HTTPS_PROXY", "http://XX:1234"); + Process start = processBuilder.start(); + start.waitFor(); + assertEquals( + "http.proxyHost: null, http.proxyPort: null, https.proxyHost: XX, https.proxyPort: 1234", + getOutput(start)); + } + + @Test + public void testHttpsProxy_bothCases() throws Exception { + environment.put("https_proxy", "http://xx:1234"); + environment.put("HTTPS_PROXY", "http://XX:1234"); + Process start = processBuilder.start(); + start.waitFor(); + assertEquals( + "http.proxyHost: null, http.proxyPort: null, https.proxyHost: xx, https.proxyPort: 1234", + getOutput(start)); + } + + @Test + public void testAll() throws Exception { + environment.put("http_proxy", "http://xx:1234"); + environment.put("HTTP_PROXY", "http://XX:1234"); + environment.put("https_proxy", "http://yy:1234"); + environment.put("HTTPS_PROXY", "http://YY:1234"); + Process start = processBuilder.start(); + start.waitFor(); + assertEquals( + "http.proxyHost: xx, http.proxyPort: 1234, https.proxyHost: yy, https.proxyPort: 1234", + getOutput(start)); + } + + @Test + public void testDontOverwriteHttp() + throws IOException, InterruptedException { + environment.put("http_proxy", "http://xx:1234"); + environment.put("HTTP_PROXY", "http://XX:1234"); + environment.put("https_proxy", "http://yy:1234"); + environment.put("HTTPS_PROXY", "http://YY:1234"); + List<String> command = processBuilder.command(); + command.add(1, "-Dhttp.proxyHost=gondola"); + command.add(2, "-Dhttp.proxyPort=5678"); + command.add("dontClearProperties"); + Process start = processBuilder.start(); + start.waitFor(); + assertEquals( + "http.proxyHost: gondola, http.proxyPort: 5678, https.proxyHost: yy, https.proxyPort: 1234", + getOutput(start)); + } + + @Test + public void testOverwriteHttpPort() + throws IOException, InterruptedException { + environment.put("http_proxy", "http://xx:1234"); + environment.put("HTTP_PROXY", "http://XX:1234"); + environment.put("https_proxy", "http://yy:1234"); + environment.put("HTTPS_PROXY", "http://YY:1234"); + List<String> command = processBuilder.command(); + command.add(1, "-Dhttp.proxyPort=5678"); + command.add("dontClearProperties"); + Process start = processBuilder.start(); + start.waitFor(); + assertEquals( + "http.proxyHost: xx, http.proxyPort: 1234, https.proxyHost: yy, https.proxyPort: 1234", + getOutput(start)); + } + + private static String getOutput(Process p) + throws IOException, UnsupportedEncodingException { + try (InputStream inputStream = p.getInputStream()) { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int length; + while ((length = inputStream.read(buffer)) != -1) { + result.write(buffer, 0, length); + } + return result.toString(UTF_8.name()); + } + } +} + +class ProxyPropertiesDumper { + public static void main(String args[]) { + try { + if (args.length == 0 || !args[0].equals("dontClearProperties")) { + System.clearProperty("http.proxyHost"); + System.clearProperty("http.proxyPort"); + System.clearProperty("https.proxyHost"); + System.clearProperty("https.proxyPort"); + } + Main.configureHttpProxy(); + System.out.printf( + "http.proxyHost: %s, http.proxyPort: %s, https.proxyHost: %s, https.proxyPort: %s", + System.getProperty("http.proxyHost"), + System.getProperty("http.proxyPort"), + System.getProperty("https.proxyHost"), + System.getProperty("https.proxyPort")); + System.out.flush(); + } catch (MalformedURLException e) { + System.out.println("exception: " + e); + } + } +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ReflogTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ReflogTest.java index ce808326b4..1423887de9 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ReflogTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ReflogTest.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2012, Tomasz Zarna <tomasz.zarna@tasktop.com> - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2012, Tomasz Zarna <tomasz.zarna@tasktop.com> and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; @@ -57,24 +24,27 @@ public class ReflogTest extends CLIRepositoryTestCase { @Test public void testSingleCommit() throws Exception { - new Git(db).commit().setMessage("initial commit").call(); + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); - assertEquals("6fd41be HEAD@{0}: commit (initial): initial commit", - execute("git reflog")[0]); + assertEquals("6fd41be HEAD@{0}: commit (initial): initial commit", + execute("git reflog")[0]); + } } @Test public void testBranch() throws Exception { - Git git = new Git(db); - git.commit().setMessage("first commit").call(); - git.checkout().setCreateBranch(true).setName("side").call(); - writeTrashFile("file", "side content"); - git.add().addFilepattern("file").call(); - git.commit().setMessage("side commit").call(); + try (Git git = new Git(db)) { + git.commit().setMessage("first commit").call(); + git.checkout().setCreateBranch(true).setName("side").call(); + writeTrashFile("file", "side content"); + git.add().addFilepattern("file").call(); + git.commit().setMessage("side commit").call(); - assertArrayEquals(new String[] { - "38890c7 side@{0}: commit: side commit", - "d216986 side@{1}: branch: Created from commit first commit", - "" }, execute("git reflog refs/heads/side")); + assertArrayEquals(new String[] { + "38890c7 side@{0}: commit: side commit", + "d216986 side@{1}: branch: Created from commit first commit", + "" }, execute("git reflog refs/heads/side")); + } } -}
\ No newline at end of file +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RemoteTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RemoteTest.java new file mode 100644 index 0000000000..6936a249f7 --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RemoteTest.java @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com> and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.pgm; + + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.StoredConfig; +import org.eclipse.jgit.transport.RefSpec; +import org.eclipse.jgit.transport.RemoteConfig; +import org.eclipse.jgit.transport.URIish; +import org.junit.Before; +import org.junit.Test; + +public class RemoteTest extends CLIRepositoryTestCase { + + private StoredConfig config; + + private RemoteConfig remote; + + @Before + @Override + public void setUp() throws Exception { + super.setUp(); + + // create another repository + Repository remoteRepository = createWorkRepository(); + + // set it up as a remote to this repository + config = db.getConfig(); + remote = new RemoteConfig(config, "test"); + remote.addFetchRefSpec( + new RefSpec("+refs/heads/*:refs/remotes/test/*")); + URIish uri = new URIish( + remoteRepository.getDirectory().toURI().toURL()); + remote.addURI(uri); + remote.update(config); + config.save(); + + Git.wrap(remoteRepository).commit().setMessage("initial commit").call(); + } + + @Test + public void testList() throws Exception { + assertArrayEquals(new String[] { remote.getName(), "" }, + execute("git remote")); + } + + @Test + public void testVerboseList() throws Exception { + assertArrayEquals( + new String[] { + String.format("%s\t%s (fetch)", remote.getName(), + remote.getURIs().get(0)), + String.format("%s\t%s (push)", remote.getName(), + remote.getURIs().get(0)), + "" }, + execute("git remote -v")); + } + + @Test + public void testAdd() throws Exception { + assertArrayEquals(new String[] { "" }, + execute("git remote add second git://test.com/second")); + + List<RemoteConfig> remotes = RemoteConfig.getAllRemoteConfigs(config); + assertEquals(2, remotes.size()); + assertEquals("second", remotes.get(0).getName()); + assertEquals("test", remotes.get(1).getName()); + } + + @Test + public void testRemove() throws Exception { + assertArrayEquals(new String[] { "" }, + execute("git remote remove test")); + + assertTrue(RemoteConfig.getAllRemoteConfigs(config).isEmpty()); + } + + @Test + public void testSetUrl() throws Exception { + assertArrayEquals(new String[] { "" }, + execute("git remote set-url test git://test.com/test")); + + RemoteConfig result = new RemoteConfig(config, "test"); + assertEquals("test", result.getName()); + assertArrayEquals(new URIish[] { new URIish("git://test.com/test") }, + result.getURIs().toArray()); + assertTrue(result.getPushURIs().isEmpty()); + } + + @Test + public void testSetUrlPush() throws Exception { + assertArrayEquals(new String[] { "" }, + execute("git remote set-url --push test git://test.com/test")); + + RemoteConfig result = new RemoteConfig(config, "test"); + assertEquals("test", result.getName()); + assertEquals(remote.getURIs(), result.getURIs()); + assertArrayEquals(new URIish[] { new URIish("git://test.com/test") }, + result.getPushURIs().toArray()); + } + + @Test + public void testUpdate() throws Exception { + assertArrayEquals(new String[] { + "From " + remote.getURIs().get(0).toString(), + " * [new branch] master -> test/master", "", "" }, + execute("git remote update test")); + } + +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RepoTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RepoTest.java index 90efae286b..3b3a98edf1 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RepoTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RepoTest.java @@ -1,51 +1,21 @@ /* - * Copyright (C) 2014 Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2014 Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.File; +import java.util.Arrays; + import org.eclipse.jgit.api.Git; import org.eclipse.jgit.junit.JGitTestUtil; import org.eclipse.jgit.lib.CLIRepositoryTestCase; @@ -71,33 +41,62 @@ public class RepoTest extends CLIRepositoryTestCase { super.setUp(); defaultDb = createWorkRepository(); - Git git = new Git(defaultDb); - JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "world"); - git.add().addFilepattern("hello.txt").call(); - git.commit().setMessage("Initial commit").call(); + try (Git git = new Git(defaultDb)) { + JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "world"); + git.add().addFilepattern("hello.txt").call(); + git.commit().setMessage("Initial commit").call(); + } notDefaultDb = createWorkRepository(); - git = new Git(notDefaultDb); - JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello"); - git.add().addFilepattern("world.txt").call(); - git.commit().setMessage("Initial commit").call(); + try (Git git = new Git(notDefaultDb)) { + JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello"); + git.add().addFilepattern("world.txt").call(); + git.commit().setMessage("Initial commit").call(); + } groupADb = createWorkRepository(); - git = new Git(groupADb); - JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world"); - git.add().addFilepattern("a.txt").call(); - git.commit().setMessage("Initial commit").call(); + try (Git git = new Git(groupADb)) { + JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world"); + git.add().addFilepattern("a.txt").call(); + git.commit().setMessage("Initial commit").call(); + } groupBDb = createWorkRepository(); - git = new Git(groupBDb); - JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world"); - git.add().addFilepattern("b.txt").call(); - git.commit().setMessage("Initial commit").call(); + try (Git git = new Git(groupBDb)) { + JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world"); + git.add().addFilepattern("b.txt").call(); + git.commit().setMessage("Initial commit").call(); + } resolveRelativeUris(); } @Test + public void testMissingPath() throws Exception { + try { + execute("git repo"); + fail("Must die"); + } catch (Die e) { + // expected, requires argument + } + } + + /** + * See bug 484951: "git repo -h" should not print unexpected values + * + * @throws Exception + */ + @Test + public void testZombieHelpArgument() throws Exception { + String[] output = execute("git repo -h"); + String all = Arrays.toString(output); + assertTrue("Unexpected help output: " + all, + all.contains("jgit repo")); + assertFalse("Unexpected help output: " + all, + all.contains("jgit repo VAL")); + } + + @Test public void testAddRepoManifest() throws Exception { StringBuilder xmlContent = new StringBuilder(); xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ResetTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ResetTest.java new file mode 100644 index 0000000000..d878232a37 --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ResetTest.java @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com> and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.pgm; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.eclipse.jgit.revwalk.RevCommit; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +public class ResetTest extends CLIRepositoryTestCase { + + private Git git; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + git = new Git(db); + } + + @Test + public void testPathOptionHelp() throws Exception { + String[] result = execute("git reset -h"); + assertTrue("Unexpected argument: " + result[1], + result[1].endsWith("[-- path ...]")); + } + + @Test + public void testZombieArgument_Bug484951() throws Exception { + String[] result = execute("git reset -h"); + assertFalse("Unexpected argument: " + result[0], + result[0].contains("[VAL ...]")); + } + + @Test + public void testResetSelf() throws Exception { + RevCommit commit = git.commit().setMessage("initial commit").call(); + assertStringArrayEquals("", + execute("git reset --hard " + commit.getId().name())); + assertEquals(commit.getId(), + git.getRepository().exactRef("HEAD").getObjectId()); + } + + @Test + public void testResetPrevious() throws Exception { + RevCommit commit = git.commit().setMessage("initial commit").call(); + git.commit().setMessage("second commit").call(); + assertStringArrayEquals("", + execute("git reset --hard " + commit.getId().name())); + assertEquals(commit.getId(), + git.getRepository().exactRef("HEAD").getObjectId()); + } + + @Test + public void testResetEmptyPath() throws Exception { + RevCommit commit = git.commit().setMessage("initial commit").call(); + assertStringArrayEquals("", + execute("git reset --hard " + commit.getId().name() + " --")); + assertEquals(commit.getId(), + git.getRepository().exactRef("HEAD").getObjectId()); + } + + @Test + public void testResetPathDoubleDash() throws Exception { + resetPath(true, true); + } + + @Test + public void testResetPathNoDoubleDash() throws Exception { + resetPath(false, true); + } + + @Test + public void testResetPathDoubleDashNoRef() throws Exception { + resetPath(true, false); + } + + @Ignore("Currently we cannote recognize if a name is a commit-ish or a path, " + + "so 'git reset a' will not work if 'a' is not a branch name but a file path") + @Test + public void testResetPathNoDoubleDashNoRef() throws Exception { + resetPath(false, false); + } + + private void resetPath(boolean useDoubleDash, boolean supplyCommit) + throws Exception { + // create files a and b + writeTrashFile("a", "Hello world a"); + writeTrashFile("b", "Hello world b"); + // stage the files + git.add().addFilepattern(".").call(); + // commit the files + RevCommit commit = git.commit().setMessage("files a & b").call(); + + // change both files a and b + writeTrashFile("a", "New Hello world a"); + writeTrashFile("b", "New Hello world b"); + // stage the files + git.add().addFilepattern(".").call(); + + // reset only file a + String cmd = String.format("git reset %s%s a", + supplyCommit ? commit.getId().name() : "", + useDoubleDash ? " --" : ""); + assertStringArrayEquals("", execute(cmd)); + assertEquals(commit.getId(), + git.getRepository().exactRef("HEAD").getObjectId()); + + org.eclipse.jgit.api.Status status = git.status().call(); + // assert that file a is unstaged + assertArrayEquals(new String[] { "a" }, + status.getModified().toArray()); + // assert that file b is still staged + assertArrayEquals(new String[] { "b" }, + status.getChanged().toArray()); + } + +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RevListTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RevListTest.java new file mode 100644 index 0000000000..7c6cf4290e --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RevListTest.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2021, kylezhao <kylezhao@tencent.com> and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.eclipse.jgit.pgm; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.eclipse.jgit.revwalk.RevCommit; +import org.junit.Before; +import org.junit.Test; + +public class RevListTest extends CLIRepositoryTestCase { + + private Git git; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + git = new Git(db); + } + + @Test + public void testWithParentsFlag() throws Exception { + List<RevCommit> commits = createCommitsForParentsFlag(git); + String result = toString( + execute("git rev-list HEAD --parents -- Test.txt")); + + String expect = toString( + commits.get(3).name() + ' ' + commits.get(1).name(), + commits.get(1).name()); + + assertEquals(expect, result); + } + + @Test + public void testWithoutParentsFlag() throws Exception { + List<RevCommit> commits = createCommitsForParentsFlag(git); + String result = toString(execute("git rev-list HEAD -- Test.txt")); + + String expect = toString(commits.get(3).name(), commits.get(1).name()); + + assertEquals(expect, result); + } + + private List<RevCommit> createCommitsForParentsFlag(Git repo) + throws Exception { + List<RevCommit> commits = new ArrayList<>(); + writeTrashFile("Test1.txt", "Hello world"); + repo.add().addFilepattern("Test1.txt").call(); + commits.add(repo.commit().setMessage("commit#0").call()); + writeTrashFile("Test.txt", "Hello world!"); + repo.add().addFilepattern("Test.txt").call(); + commits.add(repo.commit().setMessage("commit#1").call()); + writeTrashFile("Test1.txt", "Hello world!!"); + repo.add().addFilepattern("Test1.txt").call(); + commits.add(repo.commit().setMessage("commit#2").call()); + writeTrashFile("Test.txt", "Hello world!!!"); + repo.add().addFilepattern("Test.txt").call(); + commits.add(repo.commit().setMessage("commit#3").call()); + return commits; + } +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RmTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RmTest.java new file mode 100644 index 0000000000..00a1a9a735 --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RmTest.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2013 Robin Stocker <robin@nibor.org> and others. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.jgit.pgm; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; + +import java.io.File; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.dircache.DirCache; +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.junit.Before; +import org.junit.Test; + +public class RmTest extends CLIRepositoryTestCase { + private Git git; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + git = new Git(db); + } + + @Test + public void multiplePathsShouldBeRemoved() throws Exception { + File a = writeTrashFile("a", "Hello"); + File b = writeTrashFile("b", "world!"); + git.add().addFilepattern("a").addFilepattern("b").call(); + + String[] result = execute("git rm a b"); + assertArrayEquals(new String[] { "" }, result); + DirCache cache = db.readDirCache(); + assertNull(cache.getEntry("a")); + assertNull(cache.getEntry("b")); + assertFalse(a.exists()); + assertFalse(b.exists()); + } +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ShowTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ShowTest.java new file mode 100644 index 0000000000..47d5d3433a --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ShowTest.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2022, Matthias Sohn <matthias.sohn@sap.com> and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.eclipse.jgit.pgm; + +import static org.junit.Assert.assertEquals; + +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.junit.Before; +import org.junit.Test; + +public class ShowTest extends CLIRepositoryTestCase { + + private static final String NO_NEWLINE = "\\ No newline at end of file"; + + @Before + public void setup() throws Exception { + writeTrashFile("a", "a"); + writeTrashFile("b", "b"); + execute("git add a b"); + execute("git commit -m added"); + writeTrashFile("a", "a1"); + execute("git add a"); + execute("git commit -m modified"); + } + + @Test + public void testShow() throws Exception { + String result = toString(execute("git show")); + assertEquals( + toString("commit ecdf62e777b7413fc463c20e935403d424410ab2", + "Author: GIT_COMMITTER_NAME <GIT_COMMITTER_EMAIL>", + "Date: Sat Aug 15 20:12:58 2009 -0330", "", + " modified", "", "diff --git a/a b/a", + "index 2e65efe..59ef8d1 100644", "--- a/a", "+++ b/a", + "@@ -1 +1 @@", "-a", NO_NEWLINE, "+a1", NO_NEWLINE), + result); + } + + @Test + public void testShowNameOnly() throws Exception { + String result = toString(execute("git show --name-only")); + assertEquals(toString("commit ecdf62e777b7413fc463c20e935403d424410ab2", + "Author: GIT_COMMITTER_NAME <GIT_COMMITTER_EMAIL>", + "Date: Sat Aug 15 20:12:58 2009 -0330", "", " modified", + "a"), result); + } + + @Test + public void testShowNameStatus() throws Exception { + String result = toString(execute("git show --name-status")); + assertEquals(toString("commit ecdf62e777b7413fc463c20e935403d424410ab2", + "Author: GIT_COMMITTER_NAME <GIT_COMMITTER_EMAIL>", + "Date: Sat Aug 15 20:12:58 2009 -0330", "", " modified", + "M\ta"), result); + } +} diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/StatusTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/StatusTest.java index 793fc7daf6..380b6e2f56 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/StatusTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/StatusTest.java @@ -1,60 +1,36 @@ /* - * Copyright (C) 2012, 2015 François Rey <eclipse.org_@_francois_._rey_._name> - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2012, 2015 François Rey <eclipse.org_@_francois_._rey_._name> and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; +import static org.eclipse.jgit.lib.Constants.MASTER; +import static org.eclipse.jgit.lib.Constants.R_HEADS; +import static org.junit.Assert.assertTrue; import java.io.IOException; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.lib.CLIRepositoryTestCase; -import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.revwalk.RevCommit; import org.junit.Test; public class StatusTest extends CLIRepositoryTestCase { @Test + public void testPathOptionHelp() throws Exception { + String[] result = execute("git status -h"); + assertTrue("Unexpected argument: " + result[1], + result[1].endsWith("[-- path ...]")); + } + + @Test public void testStatusDefault() throws Exception { executeTest("git status", false, true); } @@ -254,7 +230,7 @@ public class StatusTest extends CLIRepositoryTestCase { } private void detachHead(Git git) throws IOException, GitAPIException { - String commitId = db.getRef(Constants.MASTER).getObjectId().name(); + String commitId = db.exactRef(R_HEADS + MASTER).getObjectId().name(); git.checkout().setName(commitId).call(); } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/TagTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/TagTest.java index ab09db5a56..26d617dd81 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/TagTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/TagTest.java @@ -1,63 +1,38 @@ /* - * Copyright (C) 2012, Tomasz Zarna <tomasz.zarna@tasktop.com> and others. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2012, Tomasz Zarna <tomasz.zarna@tasktop.com> and others. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.junit.TestRepository; import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; import org.junit.Before; import org.junit.Test; public class TagTest extends CLIRepositoryTestCase { private Git git; + private RevCommit initialCommit; + @Override @Before public void setUp() throws Exception { super.setUp(); git = new Git(db); - git.commit().setMessage("initial commit").call(); + initialCommit = git.commit().setMessage("initial commit").call(); } @Test @@ -68,6 +43,59 @@ public class TagTest extends CLIRepositoryTestCase { git.commit().setMessage("commit").call(); assertEquals("fatal: tag 'test' already exists", - execute("git tag test")[0]); + executeUnchecked("git tag test")[0]); + } + + @Test + public void testTagDelete() throws Exception { + git.tag().setName("test").call(); + assertNotNull(git.getRepository().exactRef("refs/tags/test")); + assertEquals("", executeUnchecked("git tag -d test")[0]); + assertNull(git.getRepository().exactRef("refs/tags/test")); + } + + @Test + public void testTagDeleteFail() throws Exception { + try { + assertEquals("fatal: error: tag 'test' not found.", + executeUnchecked("git tag -d test")[0]); + } catch (Die e) { + assertEquals("fatal: error: tag 'test' not found", e.getMessage()); + } + } + + @Test + public void testContains() throws Exception { + /* c3 + * | + * v2 - c2 b2 - v1 + * | | + * c1 b1 + * \ / + * a + */ + try (TestRepository<Repository> r = new TestRepository<>( + db)) { + RevCommit b1 = r.commit(initialCommit); + RevCommit b2 = r.commit(b1); + RevCommit c1 = r.commit(initialCommit); + RevCommit c2 = r.commit(c1); + RevCommit c3 = r.commit(c2); + r.update("refs/tags/v1", r.tag("v1", b2)); + r.update("refs/tags/v2", r.tag("v1.1", c2)); + + assertArrayEquals( + new String[] { "v1", "v2", "" }, + execute("git tag --contains " + initialCommit.name())); + + assertArrayEquals(new String[] { "v1", "" }, + execute("git tag --contains " + b1.name())); + + assertArrayEquals(new String[] { "v2", "" }, + execute("git tag --contains " + c1.name())); + + assertArrayEquals(new String[] { "" }, + execute("git tag --contains " + c3.name())); + } } } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/TextBuiltinTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/TextBuiltinTest.java new file mode 100644 index 0000000000..2731f2ef21 --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/TextBuiltinTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2017, Ned Twigg <ned.twigg@diffplug.com> and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.pgm; + +import static org.eclipse.jgit.junit.JGitTestUtil.check; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.eclipse.jgit.pgm.opt.CmdLineParser; +import org.eclipse.jgit.pgm.opt.SubcommandHandler; +import org.junit.Test; +import org.kohsuke.args4j.Argument; + +public class TextBuiltinTest extends CLIRepositoryTestCase { + public static class GitCliJGitWrapperParser { + @Argument(index = 0, metaVar = "metaVar_command", required = true, handler = SubcommandHandler.class) + TextBuiltin subcommand; + + @Argument(index = 1, metaVar = "metaVar_arg") + List<String> arguments = new ArrayList<>(); + } + + private String[] runAndCaptureUsingInitRaw(String... args) + throws Exception { + CLIGitCommand.Result result = new CLIGitCommand.Result(); + + GitCliJGitWrapperParser bean = new GitCliJGitWrapperParser(); + final CmdLineParser clp = new CmdLineParser(bean); + clp.parseArgument(args); + + final TextBuiltin cmd = bean.subcommand; + cmd.initRaw(db, null, null, result.out, result.err); + cmd.execute(bean.arguments.toArray(new String[bean.arguments.size()])); + if (cmd.getOutputWriter() != null) { + cmd.getOutputWriter().flush(); + } + if (cmd.getErrorWriter() != null) { + cmd.getErrorWriter().flush(); + } + return result.outLines().toArray(new String[0]); + } + + @Test + public void testCleanDeleteDirs() throws Exception { + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); + + writeTrashFile("dir/file", "someData"); + writeTrashFile("a", "someData"); + writeTrashFile("b", "someData"); + + // all these files should be there + assertTrue(check(db, "a")); + assertTrue(check(db, "b")); + assertTrue(check(db, "dir/file")); + + assertArrayOfLinesEquals(new String[] { "Removing a", "Removing b", + "Removing dir/" }, + runAndCaptureUsingInitRaw("clean", "-d", "-f")); + assertFalse(check(db, "a")); + assertFalse(check(db, "b")); + assertFalse(check(db, "dir/file")); + } + } +} |